是否有办法在数据点之间移动网格线,而不是在数据点上 上移动网格线?
import matplotlib.pyplot as plt
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.invert_yaxis() # labels read top-to-bottom
plt.grid(color='black')
plt.show()
答案 0 :(得分:1)
您可以对网格使用较小的刻度。以下是一些示例代码,可帮助您入门:
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import numpy as np
# Fixing random state for reproducibility
np.random.seed(19680801)
plt.rcdefaults()
fig, ax = plt.subplots()
# Example data
people = ('Tom', 'Dick', 'Harry', 'Slim', 'Jim')
y_pos = np.arange(len(people))
performance = 3 + 10 * np.random.rand(len(people))
ax.barh(y_pos, performance, align='center')
ax.set_yticks(y_pos)
ax.set_yticklabels(people)
ax.yaxis.set_minor_locator(MultipleLocator(0.5))
ax.set_ylim(-0.5, len(y_pos) - 0.5) # default there is too much padding which doesn't look nice
ax.invert_yaxis() # labels read top-to-bottom
ax.grid(color='black', axis='x', which='major')
ax.grid(color='black', axis='y', which='minor')
plt.show()