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

Time:01-09

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()

CodePudding user response:

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()
  •  Tags:  
  • Related