Python Forum
matplotlib legend and x axis interval
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
matplotlib legend and x axis interval
#1
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.
Reply
#2
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'])
Reply
#3
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.
Reply
#4
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 ...
Reply
#5
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?
Reply
#6
got it

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  integer +-* interval mcgrim 2 2,152 May-08-2019, 11:53 AM
Last Post: mcgrim
  How to customize x axis in matplotlib.pyplot for a scatter plot? wlsa 9 8,219 Nov-10-2018, 01:32 AM
Last Post: wlsa
  draw sample from distribution in interval SchroedingersLion 2 2,481 Oct-28-2018, 10:23 PM
Last Post: SchroedingersLion
  Polynomial value between a interval RiceGum 3 3,178 Nov-17-2017, 02:59 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020