Python Forum

Full Version: matplotlib legend and x axis interval
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Dear Python Experts,

I got pretty far with my chart but I am stuck at showing every single day 1 to 365) on the x -axis rather than in steps of 50.
I dont know where this steps of 50 comes from, must be some default.

Here is my chart:

[Image: chart.jpg]

The code is the following:

def linegraph():
    #The following step prepares the data
    NOAA = data_summary()
    TMAX = NOAA[['Data_Value']].where(NOAA['Element'] =='TMAX')/10
    TMAXx = TMAX.dropna()
    TMAXx_=TMAXx.reset_index(drop=True)
    DAYS = TMAXx_.index.tolist()
    TMIN = NOAA[['Data_Value']].where(NOAA['Element'] =='TMIN')/10
    TMINx = TMIN.dropna()
    TMINx_= TMINx.reset_index(drop=True)
    #T... as Array
    TMAXSERIES= TMAXx_[TMAXx_.columns[0]].values
    TMINSERIES= TMINx_[TMINx_.columns[0]].values
    #--------------------------------------------

    #The following part adds the caption
    x = np.arange(0,3,.25)
    y = np.sin(x)
    txt = '''
    Lorem ipsum dolor sit amet, consectetur adipisicing elit,
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
    pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
    culpa qui officia deserunt mollit anim id est laborum.'''
    fig = plt.figure()
    ax1 = fig.add_axes((.1,.4,.8,.5))
    ax1.bar(x,y,.2)
    fig.text(.1,.1,txt)  
    #---------------------------------------------------
    
    time = [x**1 for x in range(366)]
    plt.xlim(1, 365)   
    plt.title('My Title')
    plt.ylabel('C°')
    plt.xlabel('Day of the year')
    plt.legend(loc='upper right')  #DOES NOT WORK, NO EFFECT
    #plot the TMAX and the TMIN data
    plt.plot(TMAXx_, '-', TMINx_, '-')  
    return plt.fill_between(time,TMAXSERIES, TMINSERIES, where=TMAXSERIES>=TMINSERIES, facecolor='gray')
linegraph()
Besides the x axis I am also suprised that the legend does not show.
I would be super happy for any hints.
You can set your xticks with either plt.xticks() (sets ticks on current axis) or you set it for given axis ax by ax.set_xticks().
For example 
plt.xticks(range(1,361))
will set ticks for values 1...360 (they will overlap unless you have really big figure ...). You can set labels for ticks too, by giving same length list to either ax.set_xticklabels() or using plt.xticks(xticks_list, labels_list).

I think that you dont have any legend, so nothing is shown. You can try
plt.legend(loc='upper right', labels = ['serie 1', 'serie 2'])
Hi zivoni,
thanks for your reply.

I have to call the method AFTER I call the plot:

    plt.plot(TMAXx_, '-', TMINx_, '-') 

    plt.legend(loc='upper right', labels = ['serie 1', 'serie 2'])
I think displaying 365 ticks is not looking good. I will find out how to only show 12 ticks for each month one.
Naturally you need to have your variables plotted before you can add labels to them ...

With .set_xticks() you can give exact position of your ticks, so say using [31, 59, 89] adds ticks for these days - you can put numbers corrresponding with ends or mids of months. And for corresponding labels you can use .set_xticklabels() with Jan, Feb, Mar ...
Hi zinvoni,
Thanks for your reply.
I try to get the axes object instatiated but somehow I always end up drawing a new plot rather than changing
my existing one.

I added 
    ax = plt.subplot(111)
    # Hide the right and top spines
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
At which point do I have to add this so I alter my existing plot and not create a new one?
got it

    ax=plt.gca()  
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)