Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to display the grid when multiple matplotlib charts are created at the same time?

I would like to use the grid in multiple matplotlib figures, but if I just use plt.grid() the grid would only show up in one of the charts.

How can I change the code below, so that the grid shows up in both figures, please?

import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(19680801)
N_points = 100000
dist1 = rng.standard_normal(N_points)

fig = plt.figure()
axis = fig.add_subplot(1,1,1)

fig1 = plt.figure()
ax = fig1.add_subplot(1,1,1)

axis.hist(dist1)

ax.hist(dist1)
plt.grid()
plt.show()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

import matplotlib.pyplot as plt
import numpy as np

rng = np.random.default_rng(19680801)
N_points = 100000
dist1 = rng.standard_normal(N_points)

fig = plt.figure()
axis = fig.add_subplot(1,1,1)
axis.grid()
fig1 = plt.figure()
ax = fig1.add_subplot(1,1,1)
ax.grid()

axis.hist(dist1)

ax.hist(dist1)
# plt.grid()
plt.show()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading