使用Pandas数据框索引作为Matplotlib图中x轴的值 [英] Using a Pandas dataframe index as values for x-axis in matplotlib plot
问题描述
我在Pandas dateframe
中有一个时间序列,其中有很多列我想绘制.有没有一种方法可以将x轴设置为始终使用dateframe
中的索引?
当我使用Pandas的.plot()
方法时,x轴的格式正确,但是当我传递日期和列时,我想直接绘制到matplotlib上,所以该图无法正确绘制.预先感谢.
I have time series in a Pandas dateframe
with a number of columns which I'd like to plot. Is there a way to set the x-axis to always use the index from a dateframe
?
When I use the .plot()
method from Pandas the x-axis is formatted correctly however I when I pass my dates and the column(s) I'd like to plot directly to matplotlib the graph doesn't plot correctly. Thanks in advance.
plt.plot(site2.index.values, site2['Cl'])
plt.show()
仅供参考:site2.index.values
生成了此文件(为简洁起见,我将中间部分切除了):
FYI: site2.index.values
produces this (I've cut out the middle part for brevity):
array([
'1987-07-25T12:30:00.000000000+0200',
'1987-07-25T16:30:00.000000000+0200',
'2010-08-13T02:00:00.000000000+0200',
'2010-08-31T02:00:00.000000000+0200',
'2010-09-15T02:00:00.000000000+0200'
],
dtype='datetime64[ns]')
推荐答案
似乎问题出在我有.values
.没有它(即site2.index
),图形将正确显示.
It seems the issue was that I had .values
. Without it (i.e. site2.index
) the graph displays correctly.
这篇关于使用Pandas数据框索引作为Matplotlib图中x轴的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!