[toc]
If you want to dive deep into this mind-blowing library, please feel free to have a look at this tutorial – Matplotlib β A Simple Guide with Videos. However, in this tutorial, we will focus upon our mission-critical question, i.e., How do you change the size of figures drawn with matplotlib?
Note: Before diving into the methods to change the size of a figure drawn with Matplotlib, you must ensure that you have installed the Matplotlib module. If not, you can install the package from the Python Package Index using the following command:
python -m pip install matplotlib |
After installing the module, always import it at the start of your code. Having said that, let us have a quick look at how to create a plot using Matplotlib.
πCreating a Plot
In the following piece of code, we will learn how to create a plot using Matplotlib, and then we will resize this figure/plot using different methods.
# Importing the matplotlib module import matplotlib.pyplot as plt # Assigning the values for x-axis and y-axis x = [5, 6, 7, 8, 9, 10] y = [x * 2 for x in x] # Plotting the values plt.plot(x, y) # Displaying the figure plt.show()
Note: The plot()
method is used to specify the arguments for the x-axis
and y-axis
to be plotted.
Output:
Here you can see that the height and the width of the figure are equal, resulting in an almost square-shaped diagram. Now let’s change the size of this figure. So, without further delay, let’s look at the different methods to do that.
Method 1: Using figsize
You can change the size of figures drawn with matplotlib by using the figsize
attribute. The figsize = (a, b)
attribute is a parameter of the figure()
function and is used to specify the height and width of figures in unit inches. Here “a
” represents the width, and “b
” represents the height of the figure in unit inches.
Syntax:
x = plt.figure(figsize = (width, height)) |
Recap: matplotlib.pyplot
is a state-based interface to matplotlib. It provides an implicit, MATLAB-like, way of plotting. It also opens figures on your screen, and acts as the figure GUI manager. figure() is a function of the pyplot
module of matplotlib that is used to create a figure.
Now, let’s change the figure of the created plot above in such a way that the resulting output is a rectangle. Here, we will use the figsize
attribute to change the width of the figure to 8 inches and height to 4 inches, respectively.
# Importing the matplotlib module import matplotlib.pyplot as plt # Changing the height to 4 and width to 8 plt1 = plt.figure(figsize=(8, 4)) x = [5, 6, 7, 8, 9, 10] y = [x * 2 for x in x] plt.plot(x, y) plt.show()
Note: If you don’t use the figsize
attribute to change or set the size of the figure, by default, the dimensions will be the same for the width and height that results in a square type figure.
Output:
Here, the width is larger than the height, thereby resulting in a rectangular shape. You can also alternate the height and width size (height will be 8 inches and width will be 4 inches). The resulting output will be a rectangle with a width of Β½ of the height.
Example:
# Importing the matplotlib module import matplotlib.pyplot as plt # Changing the height to 8 and width to 4 inches plt1 = plt.figure(figsize=(4, 8)) x = [5, 6, 7, 8, 9, 10] y = [x * 2 for x in x] plt.plot(x, y) plt.show()
Output:
Method 2: Using set_figheight() and set_figwidth()
The Matplotlib library has many inbuilt functions such as: set_figheight()
and set_figwidth()
. The set_figwidth()
function is used to set the width of the figure in inches and the set_figheight()
function is used to set the height of the figure in inches.
Syntax:
set_figwidth(self, val, forward = True) set_figheight(self, val, forward = True) |
Example:
# Importing the matplotlib module import matplotlib.pyplot as plt x = [5, 6, 7, 8, 9, 10] y = [x * 2 for x in x] fig = plt.figure() # Change the height and width using functions fig.set_figheight(5) fig.set_figwidth(10) plt.plot(x, y) plt.show()
Output:
Method 3: By using set_size_inches()
Another inbuilt function that the Matplotlib library has is the set_size_inches()
function. By using the set_size_inches()
function, you don’t need to write two distinct functions for width and height. Instead, this function itself has two parameters, former for width and latter for height in inches.
Syntax:
set_size_inches(width, height) |
Example:
# Importing the matplotlib module import matplotlib.pyplot as plt x = [5, 6, 7, 8, 9, 10] y = [x * 2 for x in x] fig = plt.figure() # Change the width and height using set_size_inches() function fig.set_size_inches(3, 6) plt.plot(x, y) plt.show()
Output:
Changing Size of Subplots
You can even change the size of the subplots drawn with Matplotlib using figsize
. The subplots()
function is used to describe the layout of the figure and takes three arguments. The first and second arguments represent the number of rows and columns, respectively. The index of the plot is represented using the third argument. The subplots()
function allows us to draw multiple plots on one figure. We simply have to describe the number of rows, number of columns, and the index of the plot.
Syntax:
plt.subplots(nrows, ncols ,figsize) |
Example:
# Importing the matplotlib module import matplotlib.pyplot as plt # Change the size of the subplots fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) x = [5, 6, 7, 8, 9, 10] y = [x * 2 for x in x] axes[0].plot(x, y) axes[1].plot(x, y) plt.tight_layout() plt.show()
Output:
Explanation: In the above example, the number of rows is 1, and the number of columns is 2. Hence, there are one row and two columns. Look at line number five in the above snippet to visualize how you can play around with the figure shape using figsize
.
Bonus Example
Until now, we kept changing the size of a graphical plot diagram. Now, let us go ahead and try to apply one of the methods that we learned above upon an image/picture and resize it.
Example: In the following illustration, we will see how to use the figsize attribute to play around with the shape of the figure. Initially, we will not use the attribute and see what happens, and in the next phase, we will see how the attribute affects the image size.
Here’s the code:
import matplotlib.pyplot as plt
pic = plt.imread('pic.jpg')
# display the image in a mpl figure
fig = plt.figure(figsize=(3.2, 2.4))
ax = fig.subplots()
ax.imshow(pic)
plt.show()
Conclusion
In this article, we looked at different approaches to change the size of a figure drawn with Matplotlib in Python. Please stay tuned and subscribe for more interesting articles and discussions.
Recommended Read: How to Display, Modify and Save Images in Matplotlib
Article contributed by: Shubham Sayon and Rashi Agarwal
To become a PyCharm master, check out our full course on the Finxter Computer Science Academy available for free for all Finxter Premium Members: