2017-04-19 115 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