【问题标题】:Prints filename as title for each subplot打印文件名作为每个子图的标题
【发布时间】:2019-09-10 16:16:38
【问题描述】:

我正在尝试连续绘制三个图表,但遇到了奇怪的行为。

我正在尝试在循环的每个子图之前打印文件名。

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

我也尝试查找子图的标题,显然没有这样的属性

为什么 subplot 只在所有 print 语句之后绘制?如何在打印每个文件名后立即打印图表?有没有办法可以为每个子图提供标题名称?

【问题讨论】:

  • 我怀疑你使用的是 jupyter notebook 或类似的? (因为通常图表不会“打印”。)如果是这种情况,this answer 可能就是您所追求的?

标签: python numpy matplotlib


【解决方案1】:

尝试plt.suptitle(file) 而不是print(file)

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

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多