Setting Figure size of matplotlib plots

matplotlib_set_figure_size

Skill - Setting the Figure size of Matplotlib plots

Table of Contents

Skills Required

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


Table of Contents

Comments