仅针对具有数据的时段绘制pandas日内时间序列

时间:2016-08-10 11:46:44

标签: python pandas matplotlib

我有一系列日内测量。测量仅在工作日的白天进行。当我绘制数据时,大熊猫在整个时间范围内扩展x轴,因此图表显示数据空白

dfb.loc[:,("value", "exp_1")].plot()

enter image description here

我可以告诉pandas / matplotlib忽略索引,情节很好,但我想在x轴上显示日期

    dfb.loc[:,("value", "exp_1")].plot(ignore_index=True)

enter image description here

我还试图用我的索引来定义xticks,但这导致第一个图表带有杂乱的x轴描述

    dfb.loc[:,("value", "exp_1")].plot(xticks=dfb.index)

enter image description here

有没有办法在保留日期的同时获得第二张情节?

编辑: 这是数据和图表的子集

enter image description here

1 个答案:

答案 0 :(得分:1)

看起来有点像虫子。您还可以考虑在https://github.com/pydata/pandas/

上创建问题

当我使用频率设置为天或工作日的DatetimeIndex尝试时,x轴不受影响。但如果频率更精细(例如小时,营业时间),那么轴确实会延长 - 与您的情况完全一样。

解决方法(不确定是否可能)是将数据框的索引转换为字符串。直接设置:

df.index = df.index.astype('str')

或者更好的仅仅是为了打印而改变:

df.set_index(df.index.astype('str')).plot(rot=30)

参数rot=30是可选的,但没有它,标签就会变得混乱。