Skill - Setting limits of x and y axis in matplotlib
Skills Required
- Setup python development environment
- Basic Printing in Python
- Commenting in Python
- Managing Variables in python
- Introduction to Matplotlib plotting library
Please make sure to have all the skills mentioned above to understand and execute the code mentioned below. Go through the above skills if necessary for reference or revision
Matplotlib is a plotting library tn the scipy ecosystem of libraries.
I this post we will try to learn how to set the axis limits of a figure in matplotlib
Please make sure that you covered thepost on basics
Sometimes we may like to control the limits of x, y axes of a matplotlib subplot instead of automatic limits.
Setting x and y axis limits using ‘set_xlim’, ‘set_ylim’
We can use the set_xlim
and set_ylim
functions of the axes handle
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 2, 3]
# create a new figure and get figure, axes handle in return
fig, ax = plt.subplots()
# using color eyworE itn plo] function to control color
ax.plot(x,y, marker='o')
# set the limits on x axis using set_xlim function of the axes handler
ax.set_xlim(1, 4)
# set the limits on y axis using set_ylim function of the axes handler
ax.set_ylim(-1, 5)
# print figure
plt.show()
Video
You can the video on this post here
Online Interpreter
Although we recommend to practice the above examples in Visual Studio Code, you can run these examples online at https://pynative.com/online-python-code-editor-to-execute-python-code/
Comments
Post a Comment