python调整x轴标签频率

时间:2016-08-08 19:11:02

标签: python pandas plot axis-labels

我有一些2000到2050年的时间序列数据。默认的情节x标签是10年的间隔,但我想将它们更改为5年的间隔。时间序列数据位于Pandas数据帧中。我无法找到解决问题的好方法。

这是我的数据:

这是我的代码:

years = dates.YearLocator()
yearsFmt = dates.DateFormatter('%Y') 
datemin = 2000
datemax = 2050

a,b = 2010, 2050
d = {} 
for j in range(35,40):       
    d[j] = pd.read_csv('Averages_SWB_Run' + str(j) + '.csv', skiprows=[0])
    print 
    #Convert data['Year'] from string to datetime
    pd.to_datetime(pd.Series(d[j]['Year']), format='%Y')
    #Set data['Year'] as the index and delete the column
    d[j].index = d[j]['Year']
    del d[j]['Year']
    d[j]

    my_labels = ('Base2000s','Base Forecast','Climate Forecast 1 (GFDL)','Climate Forecast 2 (MRI)', 'Landuse Forecast 1', 'Landuse Forecast 2')
    my_colors = ('black', 'black', 'red', 'green', 'cyan', 'goldenrod')
    my_markers = ('o', 'o', '', '', '', '')
    ax = d[j]['ET'].plot(title='Northern High Plains Aquifer: Evapotranspiration',marker=my_markers[j-34], markersize=2, color=my_colors[j-34], label=my_labels[j-34], figsize=(8, 6))
    #ax1 = d[j]['ET'].plot(title='Northern High Plains Aquifer: Evapotranspiration', label='ET, Run'+ str(j))
    ax.set_ylabel('Average annual evaptranspiration, in inches per year')
    ax.set_ylim(0,25)
    lines = ax.get_lines()
    #my_labels = ['Base2000s','Base','GFDL','MRI', 'A2', 'B2']
    ax.legend(lines, [line.get_label() for line in lines], loc='best')
    #shading
    ax.axvspan(a,b, color = 'lightgray')
    txtbox = 'Gray shading represents future forecasts'
    ax.text(0.5, 0.1, txtbox, ha='left', va='top',transform=ax.transAxes,   fontsize=10)

    ax.xaxis.set_major_locator(years)
    ax.xaxis.set_major_formatter(yearsFmt)
    ax.set_xlim(datemin, datemax)
figET = ax.get_figure()
figET.savefig('ET.jpg', dpi=300)

1 个答案:

答案 0 :(得分:0)

ax.set_xticks([2000,2005,2010,2015,2020,2025,2030,2035,2040,2045,2050])