I want to plot a dataframe, which has a timedelta64 index with the index on the x-axis.
The index looks like this:
In [75]: test.index
Out[75]:
TimedeltaIndex([ '00:00:00', '00:00:00.020000', '00:00:00.040000',
'00:00:00.060000', '00:00:00.080000', '00:00:00.100000',
'00:00:00.120000', '00:00:00.140000', '00:00:00.160000',
'00:00:00.180000',
...
'07:29:31.660000', '07:29:31.680000', '07:29:31.700000',
'07:29:31.720000', '07:29:31.740000', '07:29:31.760000',
'07:29:31.780000', '07:29:31.800000', '07:29:31.820000',
'07:29:31.840000'],
dtype='timedelta64[ns]', name='master', length=923486, freq=None)
When I plot this dataframe simply with:
plt.plot(test)
I would expect to get the timedeltaindex on the x-axis. However, instead I'm just getting this on the x-axis:
调用组件方法如何在x轴上获取TimedeltaIndex呢?
答案 0 :(得分:3)
首先,您可以直接使用pandas绘制数据。即而不是plt.plot(df)
你可以使用
df.plot()
这将在轴上显示正确的时间δ。
如果出于任何原因需要使用matplotlib进行绘图,则需要将TimedeltaIndex
转换为绝对日期时间。选择一些(任意)启动你可以将增量添加到开头以获得一些绝对的日期时间。
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
index = pd.to_timedelta(np.arange(50,100), unit='s')
df = pd.DataFrame(np.arange(50), index=index)
start=pd.Timestamp('20180606')
plt.plot(start+df.index, df)
plt.show()