Home > Back-end >  How to loop over all subplot in a figure to add a new series in it?
How to loop over all subplot in a figure to add a new series in it?

Time:10-14

Doing the following allow me to create a figure with subplots :

DF.plot(subplots=True, layout=(9,3),figsize = (20,40),sharex=False)
plt.tight_layout()
plt.show()

This works very well. However, I would like to add to all these subplots a new series that would be the same for each subplot.

I think it is possible by browsing all subplot in the figure and then edit each one. I don't know how to do it and don't know the smartest way to do it, maybe avoid using loop would be faster but I don't know if it is doable.

Assuming that the common serie is DF2['Datas'], how do I add it to all subplots?

CodePudding user response:

DataFrame.plot returns a matplotlib.axes.Axes or numpy.ndarray of them.

axs = DF.plot(subplots=True, layout=(9,3),figsize = (20,40),sharex=False)

for ax in axs.ravel():
   DF2['Datas'].plot(ax=ax)
  • Related