Skip to main content
 首页 » 编程设计

python之Pandas :在x轴上绘制时间直方图

2025年05月04日8daizhj

我有一个数据框 df像那样:

Datetime                 Dollar 
 
2009-08-01 00:00:00        87 
 
2009-08-01 00:15:00        32  
 
2009-08-01 00:30:00        19  
 
2009-08-01 00:45:00       128 

如果我尝试 df.hist() , 我只得到 x 上的值(升序) y上的轴和具体数量轴。但我需要 x 上的时间戳用于分析值(value)随时间变化的轴。

请您参考如下方法:

也许 df.plot() 适合这里。你可以这样写:

df.plot('Datetime', 'Dollar', kind='bar', rot=45) 

给出:

详细说明上面 plot 中使用的参数: 'Datetime' 和 'Dollar' 分别表示用于 x 轴和 y 轴值的列; kind 关键字参数用于指定条形图; rot 关键字参数只是旋转 x 轴标签以提高可读性。