TourEiffel
TourEiffel

Reputation: 4434

How to loop over all subplot in a figure to add a new series in it?

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?

Upvotes: 0

Views: 31

Answers (1)

BigBen
BigBen

Reputation: 50143

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)

Upvotes: 2

Related Questions