数据点之间的网格线

时间:2020-10-08 07:37:18

标签: matplotlib grid

是否有办法在数据点之间移动网格线,而不是在数据点上 上移动网格线?

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

enter image description here

1 个答案:

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

example plot