Home > Net >  Adding vertically stacked 3 row subplots to matplotlib in for loop
Adding vertically stacked 3 row subplots to matplotlib in for loop

Time:04-13

I have a for loop that is generating from a zipped list time series line plots.

Right now I have it so it is generates a single plot in a window. When the user clicks to close, the next plot shows.

I want to make it so that it displays all these plots with their own respective axis and values in rows of three vertically stacked.

I attemped this with the below code with just single vertical stacking by trying to loop through '''ax''' but it is generating the same plot. It is iterating over the same data but I am not sure how to logically change it so it iterates over each plot.

for title,unit, y, x, in zip(NameData,UnitData,value_list,dates_list):
        if np.size(y) > 0:
            fig, ax = plt.subplots(10)
            fig.supxlabel('Time')
            try:
                fig.supylabel('Units:'   unit)
            except TypeError:
                pass
            for i in range(1):
                ax[i].plot(x,y)
                ax[i].set_title(title)
            plt.show()     
            plt.close()
        else:
            pass
NameData: [Dog,Cat,Mouse]
UnitData: [mm, cm, nm]
value_list: [[1,2,3],[4,5,6],[7,8,9]]
dates_list: datetime[[1/27/2012,1/28/2012,1/29/2012],[1/27/2012,1/28/2012,1/29/2012],[1/27/2012,1/28/2012,1/29/2012]]

CodePudding user response:

Is this what you are trying to achieve?

fig, ax = plt.subplots(nrows=3, ncols=1, sharex=True)
fig.supxlabel('Time')
for i, (title, unit, y, x) in enumerate(zip(NameData,UnitData,value_list,dates_list)):
    ax[i].plot(x,y)
    ax[i].set_title(title)
    ax[i].set_ylabel(unit)
plt.tight_layout()

enter image description here

  • Related