简体   繁体   English

python调整x轴标签频率

[英]python adjust x-axis label frequency

I have some time series data from 2000 to 2050. The default plot x-labels are 10 yr intervals, but i want to change them to 5 yr intervals. 我有一些2000年到2050年的时间序列数据。默认的图x标签为10年间隔,但是我想将它们更改为5年间隔。 The time series data is in a Pandas dataframe. 时间序列数据在Pandas数据框中。 I cannot find a good solution to my problem. 我找不到解决我问题的好方法。

Here is my data: 这是我的数据:

Here is my code: 这是我的代码:

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)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM