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 solution is 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:
当我运行代码时,得到的图像如下图所示:
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 here does 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中处理这个问题?
3 个解决方案
#1
1
You can get the result you want by using numpy.roll to 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获得您想要的结果。将您想要的值从原来的数组转换到索引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:
结果是这样的:
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.
有必要将x轴的刻度标记从1到24。
#2
3
# 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_xticks
is 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.
它会在整数上加上刻度。这将使你的代码有意义,如果你平移/缩小这些限制的一边或现在想要在一个不同的范围。
#3
1
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轴值作为你的plot函数调用的第一个参数。
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。
#1
1
You can get the result you want by using numpy.roll to 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获得您想要的结果。将您想要的值从原来的数组转换到索引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:
结果是这样的:
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.
有必要将x轴的刻度标记从1到24。
#2
3
# 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_xticks
is 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.
它会在整数上加上刻度。这将使你的代码有意义,如果你平移/缩小这些限制的一边或现在想要在一个不同的范围。
#3
1
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轴值作为你的plot函数调用的第一个参数。
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。