【问题标题】:Plotting Pandas Time Data绘制 Pandas 时间数据
【发布时间】:2014-10-11 06:42:11
【问题描述】:

我的数据是一个名为“T”的 pandas 数据框:

               A  B  C
Date                  
2001-11-13  30.1  2  3
2007-02-23  12.0  1  7

T.index 的结果是

<class 'pandas.tseries.index.DatetimeIndex'>
[2001-11-13, 2007-02-23]
Length: 2, Freq: None, Timezone: None

所以我知道索引是一个时间序列。但是当我使用 ax.plot(T) 绘制它时,我在 x 轴上没有时间序列!

我只会有两个数据点,那么如何在图表中获取日期(即 x 轴两端的两个日期)?

【问题讨论】:

  • 使用DataFrame的pandas plot方法:T.plot()
  • 使用ax.plot(T.index.to_pydatetime(), T)

标签: python matplotlib pandas


【解决方案1】:

使用实现的 pandas 命令:

In[211]: df2
Out[211]: 
               A  B  C
1970-01-01  30.1  2  3
1980-01-01  12.0  1  7
In[212]: df2.plot()
Out[212]: <matplotlib.axes.AxesSubplot at 0x105224e0>
In[213]: plt.show()

您可以使用访问轴

ax = df2.plot()

【讨论】:

  • 太棒了!感谢您的帮助!
  • 如何只显示第一个和最后一个日期而不破坏格式?
猜你喜欢
  • 2018-05-07
  • 2018-10-16
  • 2015-10-29
  • 2022-07-26
  • 2022-01-06
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
相关资源
最近更新 更多