Python MatplotLib 绘制 x 轴,第一个 x 轴值标记为 1(而不是 0)

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

Python MatplotLib plot x-axis with first x-axis value labeled as 1 (instead of 0)

pythonpython-2.7matplotlibplot

提问by mad

I am trying to plot a vector using python and matplotlib.

我正在尝试使用 python 和 matplotlib 绘制一个向量。

My problem is that in matplotlib.pyplot, the x-axis of my data starts with 0 and ends on 23. And in the graph the same is considered.

我的问题是,在 matplotlib.pyplot 中,我的数据的 x 轴从 0 开始,到 23 结束。在图中也考虑了相同的情况。

What I want is that this axis starts with label 1 (it is related to the first y value, or value #0 in natural python indexing) and ends on 24 (related to the last y value, or value #23 in natural python indexing).

我想要的是这个轴以标签 1 开始(它与第一个 y 值或自然 python 索引中的值 #0 相关)并以 24 结束(与最后一个 y 值或自然 python 索引中的值 #23 相关) )。

I tried pp.xlim(xmin=1), but the problem is that, this way, the first dimension (0) disappears in the graph, and the upper bound continues to be 23. I want it to be 24 and the first y value having its x value labeled as 1 (not 0).

我试过 pp.xlim(xmin=1),但问题是,这样一来,第一个维度 (0) 在图中消失了,上限仍然是 23。我希望它是 24,第一个 y其 x 值标记为 1(不是 0)的值。

This solutionis not working for me. I am trying to have the labels [1,24] in the x-axis of the graph instead of [0,23]. As I wrote, if I start with 1 in x axis using xlim=1 or set_xlim=1, the first y value (dimension 0 of the vector) is not shown in the graph. It starts with second y value (dimension 1 of the vector) and ends with the last value. I don't want it. Here is the source code I am using.

这个解决方案对我不起作用。我试图在图表的 x 轴上使用标签 [1,24] 而不是 [0,23]。正如我所写的,如果我使用 xlim=1 或 set_xlim=1 从 x 轴上的 1 开始,则图中不会显示第一个 y 值(向量的维度 0)。它以第二个 y 值(向量的维度 1)开始,以最后一个值结束。我不要。这是我正在使用的源代码。

import matplotlib.pyplot as pp
import numpy as np

a=np.array( [0.10478151, 0.09909564, 0.01319826, 0.00743225, 0.00483721, 0.18202419, 0.01732046, 0.04153536, 0.03317991, 0.0536289, 0.00585423, 0.00929871, 0.00629363, 0.12180654, 0.00607781, 0.03752038, 0.05547452, 0.01459015, 0.00604909, 0.01132442, 0.00710363, 0.11159429, 0.0079922, 0.04198672])

pp.xlabel('Dimension') 
pp.ylabel('Importance')
ax=pp.subplot(111)
ax.set_xlim(1, 24)
dim=np.arange(1,24,1);
ax.plot(a, 'ro', color='r',linewidth=1.0, label="Graph2")
pp.xticks(dim)
pp.grid()   
pp.show()    
pp.close()

When I run the code, the resulting image is the image below:

当我运行代码时,生成的图像如下图:

enter image description here

在此处输入图片说明

It is expected that the first y value will be shown in x=1 and the last in x=24. But Python indexing starts with 0, so, looks like the code is 'shifting' the values, starting in x=2 (or x=1 in python natural indexing).

预计第一个 y 值将在 x=1 中显示,最后一个在 x=24 中显示。但是 Python 索引从 0 开始,因此,看起来代码正在“移动”值,从 x=2(或 Python 自然索引中的 x=1)开始。

The solution proposed heredoes not help me, because it will not show the first value (0). I want all the values shown, but the label MUST start with 1 and end with 24. The problem is that python indexing will start with 0 and ends in 23.

这里提出解决方案对我没有帮助,因为它不会显示第一个值 (0)。我想要显示所有值,但标签必须以 1 开头并以 24 结尾。问题是 python 索引将从 0 开始并以 23 结尾。

How to deal with this problem in python?

在python中如何处理这个问题?

采纳答案by paisanco

You can get the result you want by using numpy.rollto shift the values you want from your original array onto the indices 1 to 23, and then append the final element of your original array so it is at index 24.

您可以通过使用numpy.roll将您想要的值从原始数组移动到索引 1 到 23 上,然后附加原始数组的最后一个元素,使其位于索引 24 处,从而获得想要的结果。

The code would be:

代码将是:

import matplotlib.pyplot as pp
import numpy as np

a=np.array( [0.10478151, 0.09909564, 0.01319826, 0.00743225, 0.00483721, 0.18202419, 0.01732046, 0.04153536, 0.03317991, 0.0536289, 0.00585423, 0.00929871, 0.00629363, 0.12180654, 0.00607781, 0.03752038, 0.05547452, 0.01459015, 0.00604909, 0.01132442, 0.00710363, 0.11159429, 0.0079922, 0.04198672])

pp.xlabel('Dimension') 
pp.ylabel('Importance')
ax=pp.subplot(111)
ax.set_xlim(1, 24)
dim=np.arange(1,25,1)
ax.plot(np.append(np.roll(a,1),a[23]), 'ro', color='r',linewidth=1.0, label="Graph2")
pp.xticks(dim)
pp.grid()   
pp.show()    
pp.close()

and the resulting plot looks like:

结果图如下所示:

enter image description here

在此处输入图片说明

Note the change in the line

注意行中的变化

dim=np.arange(1,25,1)

暗淡= np.arange(1,25,1)

is necessary to plot your x-axis tick marks from 1 to 24.

需要绘制从 1 到 24 的 x 轴刻度线。

回答by tacaswell

# boiler plate imports
import numpy as np
import matplotlib.pyplot as plt

# make your axes 
fig, ax = plt.subplots(1, 1)
# set the x and y labels
ax.set_xlabel('Dimension') 
ax.set_ylabel('Importance')
# set the xlim
ax.set_xlim(1, 24)
# get your locations
dim = np.arange(1,25,1);
# plot dim vs a
ax.plot(dim, a, 'ro', color='r',linewidth=1.0, label="Graph2")
# set the locations of the xticks to be on the integers
ax.set_xticks(dim)
# turn the grid on
ax.grid()   
# call show for good measure (to make sure the graph shows up)
plt.show()

In general using set_xticksis a bad idea, it would be better to do

一般来说,使用set_xticks是一个坏主意,最好这样做

ax.xaxis.set_major_locator(matplotlib.ticker.MultipleLocater(1))

which will put ticks on the integers. This will make your code make sense if you pan/zoom out side of these limits or now want to plot over a different range.

这将在整数上打勾。如果您平移/缩小这些限制的范围或现在想要在不同的范围内绘图,这将使您的代码有意义。

enter image description here

在此处输入图片说明

回答by Christian Hudon

If you want the X-axis values for your data to be something other than the default of 0 to n-1, you should simply pass said X-axis values as the first argument to your plot function calls.

如果您希望数据的 X 轴值不是默认值 0 到 n-1,您应该简单地将所述 X 轴值作为第一个参数传递给绘图函数调用。

So your example would now look like:

所以你的例子现在看起来像:

import matplotlib.pyplot as pp
import numpy as np

a = np.array([0.10478151, 0.09909564, 0.01319826, 0.00743225, 0.00483721, 0.18202419, 0.01732046, 0.04153536, 0.03317991, 0.0536289, 0.00585423, 0.00929871, 0.00629363, 0.12180654, 0.00607781, 0.03752038, 0.05547452, 0.01459015, 0.00604909, 0.01132442, 0.00710363, 0.11159429, 0.0079922, 0.04198672])

pp.xlabel('Dimension') 
pp.ylabel('Importance')

ax = pp.subplot(111)
ax.set_xlim(1, 24)
dim = np.arange(1, 24)
# --> Note the first arguments to the plot() function below <--
ax.plot(dim, a, 'ro', color='r', linewidth=1.0, label="Graph2")
pp.xticks(dim)
pp.grid()

pp.show()
pp.close()

Here is the relevant link to the matplotlib documentation: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.plot

这是 matplotlib 文档的相关链接:http: //matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.plot

回答by Isi

I know it is a very old question, but I have found a very simple solution that also puts the first value at the position of 1 (what I like more than having it on position 0):

我知道这是一个非常古老的问题,但我找到了一个非常简单的解决方案,它也将第一个值放在 1 的位置(我更喜欢将它放在位置 0 上):

import matplotlib.pyplot as pp
import numpy as np

a=np.array( [0.10478151, 0.09909564, 0.01319826, 0.00743225, 0.00483721, 0.18202419, 0.01732046, 0.04153536, 0.03317991, 0.0536289, 0.00585423, 0.00929871, 0.00629363, 0.12180654, 0.00607781, 0.03752038, 0.05547452, 0.01459015, 0.00604909, 0.01132442, 0.00710363, 0.11159429, 0.0079922, 0.04198672])

pp.xlabel('Dimension') 
pp.ylabel('Importance')
pp.plot(a, 'ro', color='r',linewidth=1.0, label="Graph2")

# just the following line will do it
pp.xticks(np.arange(len(a)), np.arange(1, len(a)+1))

pp.grid()   
pp.show()    
pp.close()