How to set β€œauto” for the upper limit, but keep the fixed lower limit using matplotlib.pyplot

I want to set the upper limit of the y axis to "auto", but I want the lower limit of the y axis to always be zero. I tried "auto" and "autorange", but they don't seem to work. Thank you in advance.

Here is my code:

import matplotlib.pyplot as plt def plot(results_plt,title,filename): ############################ # Plot results # mirror result table such that each parameter forms an own data array plt.cla() #print results_plt XY_results = [] XY_results = zip( *results_plt) plt.plot(XY_results[0], XY_results[2], marker = ".") plt.title('%s' % (title) ) plt.xlabel('Input Voltage [V]') plt.ylabel('Input Current [mA]') plt.grid(True) plt.xlim(3.0, 4.2) #***I want to keep these values fixed" plt.ylim([0, 80]) #****CHANGE**** I want to change '80' to auto, but still keep 0 as the lower limit plt.savefig(path+filename+'.png') 
+58
python matplotlib limits
Jul 31 '12 at 16:41
source share
4 answers

You can pass only left or right to set_xlim :

 plt.gca().set_xlim(left=0) 

For the y axis, use bottom or top :

 plt.gca().set_ylim(bottom=0) 
+58
Jul 31 2018-12-12T00:
source share

Just set xlim for one of the limits:

 plt.xlim(xmin=0) 
+23
Sep 17 '15 at 15:16
source share

Just add a point to @silvio: if you use the axis to build as figure, ax1 = plt.subplots(1,2,1) . Then ax1.set_xlim(xmin = 0) works!

+2
Jun 08 '16 at 7:09
source share

As indicated above and according to the matplotlib documentation, the x limits of a given axis axis can be set using the set_xlim method of the matplotlib.axes.Axes class.

For example,

 >>> ax.set_xlim(left_limit, right_limit) >>> ax.set_xlim((left_limit, right_limit)) >>> ax.set_xlim(left=left_limit, right=right_limit) 

One limit can be left unchanged (for example, the left limit):

 >>> ax.set_xlim((None, right_limit)) >>> ax.set_xlim(None, right_limit) >>> ax.set_xlim(left=None, right=right_limit) >>> ax.set_xlim(right=right_limit) 

To set the x limits of the current axis, the matplotlib.pyplot module contains a xlim function that simply wraps matplotlib.pyplot.gca and matplotlib.axes.Axes.set_xlim .

 def xlim(*args, **kwargs): ax = gca() if not args and not kwargs: return ax.get_xlim() ret = ax.set_xlim(*args, **kwargs) return ret 

Similarly, for y-limits, use matplotlib.axes.Axes.set_ylim or matplotlib.pyplot.ylim . The key arguments are top and bottom .

+1
Sep 22 '17 at 10:08 on
source share



All Articles