【问题标题】:Modifying number of ticks on Pandas hourly time axis修改 Pandas 小时时间轴上的刻度数
【发布时间】:2013-11-29 19:51:59
【问题描述】:

如果我有以下使用 Pandas 数据框的示例 Python 代码:

import pandas as pd
from datetime import datetime

ts = pd.DataFrame(randn(1000), index=pd.date_range('1/1/2000 00:00:00', freq='H', periods=1000), columns=['Data'])
ts['Time'] = ts.index.map(lambda t: t.time())
ts = ts.groupby('Time').mean()
ts.plot(x_compat=True, figsize=(20,10))

输出图是:

让 X 轴刻度每小时或每两小时自动间隔的最优雅方法是什么? x_compat=True 没有影响

【问题讨论】:

    标签: python matplotlib plot pandas time-series


    【解决方案1】:

    您可以将参数xticks 传递给ts.plot()。给出正确的间隔,您可以每小时绘制我们的双小时,例如:

    max_sec = 90000
    
    ts.plot(x_compat=True, figsize=(20,10), xticks=arange(0, max_sec, 3600))
    
    ts.plot(x_compat=True, figsize=(20,10), xticks=arange(0, max_sec, 7200))
    

    这里max_secxaxis的最大值,以秒为单位。

    【讨论】:

    • 或许可以使用max_sec = (ts.Data.keys()[-1].hour + 1)*3600自动获取xaxis的最大值,以秒为单位
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-16
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    相关资源
    最近更新 更多