如何将 matplotlib 网格从前景移动到背景?

How can I move a matplotlib grid from foreground to background?

谁能告诉我如何将 matplotlib 图的网格移动到背景,以便条形完全可见?

import matplotlib.pyplot as plt

data = [29, 45, 56]

plt.bar(range(len(data)), data)
plt.grid(axis='y')
plt.show()

您可以添加ax.set_axisbelow(True)

import matplotlib.pyplot as plt

x = data = [29, 45, 56]
y = range(len(data))

fig, ax = plt.subplots()

plot = ax.bar(y,x)

ax.yaxis.grid(True, color ="green", lw = 1)
ax.set_axisbelow(True)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_title('Title', fontsize = 12, fontweight ='bold')
plt.show()