Python MatplotLib绘制x轴,第一个x轴值标记为1(而不是0) [英] Python MatplotLib plot x-axis with first x-axis value labeled as 1 (instead of 0)

查看:36
本文介绍了Python MatplotLib绘制x轴,第一个x轴值标记为1(而不是0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

如何在python中处理此问题?

解决方案

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

代码如下:

导入 matplotlib.pyplot 为 pp将numpy导入为np一个= 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('重要性')ax = pp.subplot(111)ax.set_xlim(1,24)暗淡= 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()

,结果图如下:

请注意行中的更改

<块引用>

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

是绘制1到24的x轴刻度线所必需的.

I am trying to plot a vector using python and 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.

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).

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).

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.

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).

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.

How to deal with this problem in python?

解决方案

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.

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)

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

这篇关于Python MatplotLib绘制x轴,第一个x轴值标记为1(而不是0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆