2016-09-20 83 views
0

我正在绘制一些熊猫数据框的时间序列,周末我遇到了空白问题。我能做些什么来消除时间序列图中的空白?用熊猫绘制时间序列的格式化日期时间

date_concat = pd.to_datetime(pd.Series(df.index),infer_datetime_format=True) 
pca_factors.index = date_concat 
pca_colnames = ['Outright', 'Curve', 'Convexity'] 
pca_factors.columns = pca_colnames 

fig,axes = plt.subplots(2) 
pca_factors.Curve.plot(ax=axes[0]); axes[0].set_title('Curve') 
pca_factors.Convexity.plot(ax=axes[1]); axes[1].set_title('Convexity'); plt.axhline(linewidth=2, color = 'g') 
fig.tight_layout() 
fig.savefig('convexity.png') 

部分下面的情节: enter image description here

理想情况下,我想时间序列,只显示工作日和忽略周末。

+0

你只是想在周末切断数据/行吗? – MaxU

+0

是的,但我没有具体的周末数据...当我将日期转换为日期时间格式时,它人为地创建了周末... – A1122

+0

我认为它不会添加任何数据,它只是试图保持比率,并简单地连接从星期五到星期一的点。也许你想要一个barplot? – MaxU

回答

0

为了MaxU的建议更明确:

  • 转换为datetime,你都做了,但降周末
  • 重置索引,并通过此默认绘制数据Int64Index
  • 改变X剔标签

代码:

date_concat = data_concat[date_concat.weekday < 5] # drop weekends 
pca_factors = pca_factors.reset_index() # from MaxU's comment 
pca_factors['Convexity'].plot()   # ^^^ 
plt.xticks(pca_factors.index, date_concat) # change the x tick labels