Matplotlib Figsize : Change Size of Plot in Matplotlib

Matplotlib Figsize featured image

Sometimes the size of the image of the plot in matplotlib is not met according to our requirements. And it leads to difficulty in analyzing the image. Matplotlib figsize allows you to change the default size of the image or figure. That’s why I came up with this tutorial. In this entire post, you will know the various method to change the size of the plot in matplotib with simple and understandable examples.

Examples to implement Matplotlib Figsize

In this entire section, you will understand how you can use various methods to change the size of the image. Please note that all the examples here are implemented on Jupyter Notebook. So it’s best that you should also try these examples on the Jupyter Notebook. It helps you to understand more.

Before going to the examples. let’s execute the lines of code and plot the default figure.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 20, 1000)
y = np.cos(x)
plt.plot(x,y);

Output

Default plotting of figure
Default plotting of figure

You can see, I have not specifically described anything to resize the figure. It is the default size of the figure.

Example 1: Using plt.figure() method

There is a method in the Matplotlib, figure() that accepts the width and height of the image in inches. For example, I am using 15 and 8 as the width and height respectively, and assigning them to the Matplotlib figsize. Execute the lines of code and see how the size of the image has been changed.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 20, 1000)
y = np.cos(x)
plt.figure(figsize=(15,8))
plt.plot(x,y);

Output

Using plt.figure() method
Using plt.figure() method

Example 2: Changing size using gcf() method

In this example, I will first create a figure using the plt.gcf() method. The gcf() method gets the current figure and returns it. After that using fig.set_size_inches() I will change the size of the plot.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 20, 1000)
y = np.cos(x)
fig = plt.gcf()
fig.set_size_inches(11,8)
plt.plot(x,y);

Output

Plotting using gcf() method
Plotting using gcf() method

You can see the width of the plot is 11 inches and the height is 8 inches.

Example 3: Changing the size using Matplotlib figsize

The third method to change the size of your plot is using the figure() method. Inside the figure method, you have to pass the fig size as width and height as a tuple. For example, If I want to change the size of the plot to a width of 8 inches and a height of 3 inches, then I will execute the following lines of code.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
%matplotlib inline
x = np.linspace(0, 20, 1000)
y = np.cos(x)
figure(num=None, figsize=(8, 3))
plt.plot(x,y);

Output

Changing the size using Matplotlib figsize
Changing the size using Matplotlib figsize

Example 4:  Using plt.rcParams

The other method to change the size of the plot is using the plt.rcParams[“figure.figsize”]. Here is the whole plt.rcParams[] acts as a variable and you have to just pass the tuple containing the width and height as the value. Just copy and paste the given lines of code and see the output.

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x = np.linspace(0, 20, 1000)
y = np.cos(x)
plt.rcParams["figure.figsize"] = (20,3)
plt.plot(x,y);

Output

Using plt.rcParams
Using plt.rcParams

You can see the plot size has been set to  20 and 3 inches for the width and height respectively.

matplotlib figsize
matplotlib figsize

Conclusion

Changing the size of the plot requires when you have different charts on the same figure. It helps you take good analytical decisions. These are the various methods to implement Matplotlib figsize. You can use any method as per convenience.

Hope you have liked this tutorial. If you have any other queries then you can contact us for more help.

Source:

Matplotlib Documentation

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner