数据点之间的网格线
Grid lines in between data points
有没有办法在数据点之间移动网格线而不是 在 数据点上?
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()
您可以对网格使用小刻度。下面是一些帮助您入门的示例代码:
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()
有没有办法在数据点之间移动网格线而不是 在 数据点上?
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()
您可以对网格使用小刻度。下面是一些帮助您入门的示例代码:
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()