0

I am trying to plot three graphs in a row, but running into a weird behavior.

I am trying to print the filename out before each subplot of the loop.

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)

The only problem for my program is that all the graphs are plotted after the print(file) statement. So it became something like following:

filename1
filename2
filename3

Graph1 Graph2 Graph3
Graph1 Graph2 Graph3
Graph1 Graph2 Graph3

What I wanted is :

filename1
Graph1 Graph2 Graph3

filename2 
Graph1 Graph2 Graph3

filename3
Graph1 Graph2 Graph3

I also tried to look up title for subplot, apparently, there isn't such attributes

Why does subplot only plotted after all the print statements? How do I print the graphs right after each filename is print? Is there a way that i can supply a title name for each subplot?

ZpfSysn
  • 807
  • 2
  • 12
  • 30
  • 1
    I suspect you use jupyter notebook or similar? (Because usually graphs don't "print".) If that is the case, [this answer](https://stackoverflow.com/questions/49365763/matplotlib-sequence-is-off-when-using-plt-imshow/49366556#49366556) might be what you're after? – ImportanceOfBeingErnest Apr 20 '19 at 22:11

1 Answers1

1

Try plt.suptitle(file) instead of print(file).

If you insist on printing, then force plt to render after each iteration by 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()
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74