Skill - Setting the Figure size of Matplotlib plots
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 size of a figure in matplotlib
Please make sure that you covered thepost on basics
Sometimes we may like to control the size of matplotlib figure instead of default figure size. This requirement may arise for saving figure as image etc
Setting figure size using the ‘figsize’ parameter
Here we are providing figsize
input to subplots
function as
figsize=(width_in_inches, height_in_inches)
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
# here we are setting width = 3 inches, height = 2 inches
fig, ax = plt.subplots(figsize=(3,2))
# using color keyword itn plot function to control color
ax.plot(x,y)
# 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/
References
- plot function API - https://matplotlib.org/2.1.1/api/_as_gen/matplotlib.pyplot.plot.html
- matplotlib colors - https://matplotlib.org/2.1.1/api/colors_api.html
- Examples gallery - https://matplotlib.org/gallery/index.html
Comments
Post a Comment