Python 如何在matplotlib中显示所有标签值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26131822/
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 00:08:07  来源:igfitidea点击:

How to display all label values in matplotlib?

pythongraphmatplotlibaxis-labels

提问by Kevin

I have two lists, when I plot with the following code, the x axis only shows up to 12 (max is 15). May I know how can I show all of the values in x list to the x axis? Thanks in advance.

我有两个列表,当我使用以下代码绘图时,x 轴最多只显示 12(最大值为 15)。我可以知道如何将 x 列表中的所有值显示到 x 轴上吗?提前致谢。

x = [4,5,6,7,8,9,10,11,12,13,14,15,0,1,2,3]
y = [10,20,30,40,50,60,70,80,90,100,110,120,130,140,150,160]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.plot(np.arange(len(x)), y, 'o')
ax1.set_xticklabels(x)
plt.show()

If I set minor=True in the set_xticklabelsfunction, it shows me all x=2,4,6,8,..,16... but I want ALL values.

如果我在set_xticklabels函数中设置 minor=True ,它会显示所有 x=2,4,6,8,..,16... 但我想要所有值。

P.S. My x axis is not sorted, should display as it shows.

PS 我的 x 轴没有排序,应该如它所示显示。

采纳答案by farenorth

Add this:

添加这个:

ax1.set_xticks(np.arange(len(x)))

To your code before your ax1.set_xticklabels(x)call. Is that what you're looking for?

在您ax1.set_xticklabels(x)致电之前查看您的代码。这就是你要找的吗?