打印文件名作为每个子标题

2024-04-25 13:08:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图连续绘制三个图形,但却遇到了一个奇怪的行为。你知道吗

我试图在循环的每个子批之前打印出文件名。你知道吗

for file in filenames:
        data = np.loadtxt(file, delimiter = ",")
        x = np.mean(data,axis=0)
        y = np.min(data,axis=0)
        z = np.max(data,axis=0)
        f,(plot1,plot2,plot3) = plt.subplots(1,3,figsize=(20,5))
        print(file)
        plot1.plot(x)
        plot2.plot(y)
        plot3.plot(z)

我的程序唯一的问题是所有的图都是在print(file)语句之后绘制的。于是就变成了这样:

filename1
filename2
filename3

Graph1 Graph2 Graph3
Graph1 Graph2 Graph3
Graph1 Graph2 Graph3

我想要的是:

filename1
Graph1 Graph2 Graph3

filename2 
Graph1 Graph2 Graph3

filename3
Graph1 Graph2 Graph3

我还试图查找子地块的标题,显然,没有这样的属性

为什么子图只在所有打印语句之后绘制?如何在打印每个文件名之后立即打印图形?有没有办法为每个子地块提供标题名称?你知道吗


Tags: 图形dataplot文件名np绘制fileprint
1条回答
网友
1楼 · 发布于 2024-04-25 13:08:01

尝试plt.suptitle(file)而不是print(file)。你知道吗

如果坚持打印,那么在每次迭代后强制plt渲染plt.show()

for file in filenames:
    data = np.loadtxt(file, delimiter = ",")
    x = np.mean(data,axis=0)
    y = np.min(data,axis=0)
    z = np.max(data,axis=0)
    f,(plot1,plot2,plot3) = plt.subplots(1,3,figsize=(20,5))
    print(file)
    plot1.plot(x)
    plot2.plot(y)
    plot3.plot(z)
    plt.show()

相关问题 更多 >