Search code examples
matplotlibaxes

How to show min and max values at the end of the axes


I generate plots like below:

from   pylab              import *
import numpy              as np
import matplotlib.pyplot  as plt
import matplotlib.ticker
import matplotlib.ticker as ticker

rcParams['axes.linewidth']   = 2 # set the value    globally
rcParams['font.size']        = 16# set the value globally
rcParams['font.family']      = ['DejaVu Sans']
rcParams['mathtext.fontset'] = 'stix'
rcParams['legend.fontsize']  = 24
rcParams['axes.prop_cycle']  = cycler(color=['grey','b','g','r','orange']) 
rc('lines', linewidth=2, linestyle='-',marker='o')
rcParams['axes.xmargin'] = 0
rcParams['axes.ymargin'] = 0

t = arange(0,21,1) 
v = 2.0
s = v*t

plt.figure(figsize=(12, 4))
plt.plot(t,s,label='$s=%1.1f\cdot t$'%v)
plt.title('Wykres drogi w czasie $s=v\cdot t$')
plt.xlabel('Czas $t$, s')
plt.ylabel('Droga $s$, m')

plt.autoscale(enable=True, axis='both', tight=None)
legend(loc='best')
plt.xlim(min(t),max(t))
plt.ylim(min(s),max(s))
plt.grid()

plt.show()

Generated picture

When I am changing the value t = arange(0,21,1) for example to t = arange(0,20,1) which gives me for example on the x axis max value= 19.0 my max value dispirs from the x axis. The same situation is of course with y axis.

Wrong picture

My question is how to force matplotlib to produce always plots where on the axes are max values just at the end of the axes like should be always for my purposes or should be possible to chose like an option?

Imiage from my program in Fortan I did some years ago

Matplotlib is more efficiens that I use it but there should be an opition like that (the picture above).

In this way I can always observe max min in text windows or do take addiional steps to make sure about max min values. I would like to read them from axes and the question is ...Are there such possibilites in mathplotlib ??? If not I will close the post.

Axes I am thinking about more or less


Solution

  • I see two ways to solve the problem.

    Set the axes automatic limit mode to round numbers

    In the rcParams you can do this with

    rcParams['axes.autolimit_mode'] = 'round_numbers'
    

    And turn off the manual axes limits with min and max

    plt.xlim(min(t),max(t))
    plt.ylim(min(s),max(s))
    

    This will produce the image below. Still, the extreme values of the axes are shown at the nearest "round numbers", but the user can approximately catch the data range limits. If you need the exact value to be displayed, you can see the second solution which cannot be directly used from the rcParams.

    ticking with auto limits to round numbers

    or – Manually generate axes ticks

    This solution implies explicitly asking for a given number of ticks. I guess there is a way to automatize it depending on the axes size etc. But if you are dealing with more or less every time the same graph size, you can decide a fixed number of ticks manually. This can be done with

    plt.xlim(min(t),max(t))
    plt.ylim(min(s),max(s))
    plt.xticks(np.linspace(t.min(), t.max(), 7))  # arbitrary chosen
    plt.yticks(np.linspace(s.min(), s.max(), 5))  # arbitrary chosen
    

    generated the image below, quite similar to your image example.

    ticking manually