我有值(np.array
)我想用DateTimeIndex
绘制索引(ax.plot_date
)。两者都有相同的长度。
>>>index
DatetimeIndex(['1997-12-30', '1997-12-31', '1998-01-01', '1998-01-02',
'1998-01-05', '1998-01-06', '1998-01-07', '1998-01-08',
'1998-01-09', '1998-01-12',
...
'2015-12-29', '2015-12-30', '2015-12-31', '2016-01-01',
'2016-01-04', '2016-01-05', '2016-01-06', '2016-01-07',
'2016-01-08', '2016-01-11'],
dtype='datetime64[ns]', length=4705, freq='B')
>>>values
array([ nan, nan, nan, ..., 1.40211106,
1.46409254, 1.36151557])
现在ax.plot_date(index, values)
工作正常,但它只会在开头(nan)的地方剪切线,而我想要有间隙。我不知道如何实现这一目标。
答案 0 :(得分:2)
您需要手动设置xtick标签,这是一个示例,索引中只有4个项目,值中有2个值:
import pandas as pd
import pylab as plt
import numpy as np
#my made up data
index = pd.DatetimeIndex(['1997-12-30', '1997-12-31', '1998-01-01',
'1998-01-02'])
values = np.array([np.nan, np.nan, 1.402, 1.464])
#we are actually using this array as the x values in our graph, then replacing the labels with dates
x = np.arange(len(index))
#create plot
fig, ax = plt.subplots()
ax.set_xlim([0,len(index)])
#create axes with dates along the x axis
ax.set(xticks=x, xticklabels = index)
#plot values with the corresponding dates
ax.plot_date(x, values)
#show plot
plt.show()