Python matplotlib - 设置x轴刻度

28

我有这个图表,显示如下内容:

plt.plot(valueX, scoreList)
plt.xlabel("Score number") # Text for X-Axis
plt.ylabel("Score") # Text for Y-Axis
plt.title("Scores for the topic "+progressDisplay.topicName)
plt.show()

valueX = [1, 2, 3, 4],scoreList = [5, 0, 0, 2]

我想让比例尺按照1递增,无论'scoreList'中的值为何。目前我的x轴按照0.5递增而不是1。

我该如何设置才能使其只按照1递增?

3个回答

42

只需自己设置xticks。

plt.xticks([1,2,3,4])
plt.xticks(valueX)

由于range函数只适用于整数,因此您可以使用它:

plt.xticks(range(1, 5))

或者更加动态,从数据中计算它:

plt.xticks(range(min(valueX), max(valueX)+1))

14

以下是我设置轴比例的最喜欢方法:

plt.xlim(-0.02, 0.05)
plt.ylim(-0.04, 0.04)

8
嘿,看起来你需要设置X轴比例尺。
尝试一下:
matplotlib.axes.Axes.set_xscale(1, 'linear')

这是该函数的文档


4
在matplotlib 3中,这是不正确的。应该写成 plt.gca().set_xscale('linear')。否则,当你写成 plt.axes.Axes 时,会出现 AttributeError: 'function' object has no attribute 'Axes' 的错误;当你写成 set_xscale(1, 'linear') 时,会出现 TypeError: set_xscale() takes 2 positional arguments but 3 were given 的错误。 - coldfix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接