Cannot grid the second subfigure

Question:

Im using the matplotlib and following code grids only the second subfigure, not the first one. I cannot figure out the problem.
What do you think the problem is and how to grid both figures?

Thanks

ep = np.linspace(1, 500, 500)
fig, axs = plt.subplots(2)
plt.grid()
axs[0].set_title('me')
axs[0].set(xlabel='you', ylabel='me')
axs[0].plot(ep, me_es)
axs[1].set(xlabel='ty', ylabel='no')
axs[1].set_title('sec')
axs[1].plot(ep,me_es)
plt.tight_layout(pad=1, w_pad=1.5, h_pad=2.0)
plt.rcParams['figure.figsize'] = (12.4, 10.8)

Here is the figure Im getting.Output of the code

Asked By: victoria

||

Answers:

plt.grid() is equivalent to plt.gca().grid() (see here). gca() stands for "get current axis" (see here). So it will grid the current axis which is the last one created: here the second one. In other words, in your case plt.grid() = axs[1].grid()

Adding axs[0].grid() should work for you.

Answered By: thmslmr
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.