Center x-axis labels in line plot

前端 未结 3 535
-上瘾入骨i
-上瘾入骨i 2020-12-30 16:45

I am building a line plot with two lines, one for high temperatures and the other for low temperatures. The x-axis is based on days over one complete year in datetime format

3条回答
  •  星月不相逢
    2020-12-30 17:31

    Using the minor ticks as suggested in the thread posted by DavidG should work. Below is a MWE that I've adapted for your specific problem, forcing the major ticks to appear on the first of each month and using the minor ticks to place the labels in-between the major ticks :

    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import numpy as np
    import datetime
    
    # Generate some data for example :
    
    yr = 2014
    fig, ax = plt.subplots()
    
    x0 = datetime.datetime(yr, 1, 1)
    x = np.array([x0 + datetime.timedelta(days=i) for i in range(365)])
    
    y1 = np.sin(2*np.pi*np.arange(365)/365) + np.random.rand(365)/5
    y2 = np.sin(2*np.pi*np.arange(365)/365) + np.random.rand(365)/5 - 1
    
    # Draw high and low temperatures lines :
    
    ax.plot(x, y1, color='#c83c34')
    ax.plot(x, y2, color='#28659c')
    ax.fill_between(x, y2, y1, facecolor='#daecfd', alpha=0.5)
    
    # Force the major ticks position on the first of each month and hide labels:
    
    xticks = [datetime.datetime(yr, i+1, 1) for i in range(12)]
    xticks.append(datetime.datetime(yr+1, 1, 1))
    ax.set_xticks(xticks)
    ax.tick_params(axis='both', direction='out', top=False, right=False)
    ax.axis([xticks[0], xticks[-1], -2.5, 1.5])
    ax.set_xticklabels([])
    
    # CODE GOES HERE TO CENTER X-AXIS LABELS...
    
    labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
              'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    mticks = ax.get_xticks()
    ax.set_xticks((mticks[:-1]+mticks[1:])/2, minor=True)
    ax.tick_params(axis='x', which='minor', length=0)
    ax.set_xticklabels(labels, minor=True)
    fig.tight_layout()
    
    plt.show()
    

    which results in:

提交回复
热议问题