3 回答

TA贡献1874条经验 获得超12个赞
另外,如果您想简单地设置刻度线的数量,同时允许matplotlib对其进行定位(目前仅使用MaxNLocator),则有pyplot.locator_params,
pyplot.locator_params(nbins=4)
您可以按照以下方法在此方法中指定特定的轴,默认为两者:
# To specify the number of ticks on both or any single axes
pyplot.locator_params(axis='y', nbins=6)
pyplot.locator_params(axis='x', nbins=10)

TA贡献1776条经验 获得超12个赞
如果仍然有人在搜索结果中获得此页面:
fig, ax = plt.subplots()
plt.plot(...)
every_nth = 4
for n, label in enumerate(ax.xaxis.get_ticklabels()):
if n % every_nth != 0:
label.set_visible(False)

TA贡献1824条经验 获得超8个赞
万一有人仍然需要它,并且因为这里没有任何东西真正适合我,我想出了一种非常简单的方法,可以将生成的图的外观保持“原样”,同时将刻度数固定为N:
import numpy as np
import matplotlib.pyplot as plt
f, ax = plt.subplots()
ax.plot(range(100))
ymin, ymax = ax.get_ylim()
ax.set_yticks(np.round(np.linspace(ymin, ymax, N), 2))
添加回答
举报