chevron_left
Axes Cookbook
0
0
0
new
Setting only the lower or upper limit in Matplotlib
Programming
chevron_rightPython
chevron_rightMatplotlib
chevron_rightCookbooks
chevron_rightAxes Cookbook
schedule Jul 1, 2022
Last updated Python●Matplotlib
Tags tocTable of Contents
expand_more To set only the lower or upper limit for plt.xlim(~)
, set either the left
or right
parameter:
plt.xlim(left=-2)plt.xlim(right=2)
For plt.ylim(~)
, set either the bottom
or top
parameter:
plt.ylim(bottom=-2)plt.ylim(top=2)
WARNING
For the effect to apply, you must call xlim(~)
or ylim(~)
after plotting the curve (e.g. after plt.plot(~)
).
Example
Suppose we have the following parabola:
x = np.linspace(-3, 3, 1000)plt.plot(x, x**2)
The default output is as follows:

Our goal is to set the lower y-limit of graph to 0, and leave the upper y-limit intact. We can use the following snippet:
x = np.linspace(-3, 3, 1000)plt.plot(x, x**2)plt.ylim(bottom=0) # Make sure you call this after the plot(~) method
This gives us the following:

We see that our curve starts from y=0
now.
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
Ask a question or leave a feedback...
0
0
0
Enjoy our search