Python matplotlib - 设置 x 轴比例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43286541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:48:08  来源:igfitidea点击:

Python matplotlib - setting x-axis scale

pythonmatplotlibtkinter

提问by YusufChowdhury

I have this graph displaying the following:

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

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] and scoreList = [5, 0, 0, 2]

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

I want the scale to go up in 1's, no matter what values are in 'scoreList'. Currently get my x-axis going up in .5 instead of 1s.

无论“scoreList”中的值是什么,我都希望比例以 1 为单位递增。目前让我的 x 轴以 0.5 而不是 1s 上升。

How do I set it so it goes up only in 1?

我如何设置它使其仅在 1 中上升?

回答by Novel

Just set the xticks yourself.

只需自己设置 xticks。

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

or

或者

plt.xticks(valueX)

Since the range functions happens to work with integers you could use that instead:

由于范围函数恰好适用于整数,因此您可以改用它:

plt.xticks(range(1, 5))

Or be even more dynamic and calculate it from the data:

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

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

回答by dustyjuicebox

Hey it looks like you need to set the x axis scale.

嘿,看来您需要设置 x 轴比例。

Try

尝试

plt.axes.Axes.set_xscale(1, 'linear')

Here's the documentation for that function

这是该功能的文档