2017-04-19 93 views
1

我有data.frame低于xticks值数据框列值matplotlib阴谋

 values years 
0 24578.0 2007-09 
1 37491.0 2008-09 
2 42905.0 2009-09 
3 65225.0 2010-09 
4 108249.0 2011-09 
5 156508.0 2012-09 
6 170910.0 2013-09 
7 182795.0 2014-09 
8 233715.0 2015-09 
9 215639.0 2016-09 
10 215639.0  TTM 

绘制的图像连接,这个问题我想多年的价值观“2007-09”到“TTM”为xtick值要做到这一点在情节

The plotted image is ataached,the issue is i want years values '2007-09' to 'TTM' as xtick values in plot

回答

1

一种方式是访问xticks当前idices在x数据。使用该值从df.year选择值,然后将标签设置这些值:

import matplotlib.pyplot as plt 

fig, ax = plt.subplots() 
df.plot(ax=ax) 
tick_idx = plt.xticks()[0] 
year_labels = df.years[tick_idx].values 
ax.xaxis.set_ticklabels(year_labels) 

enter image description here

您还可以设置x轴,显示所有年份,像这样:

fig, ax = plt.subplots() 
df.plot(ax=ax, xticks=df.index, rot=45) 
ax.set_xticklabels(df.years) 

enter image description here