How to Change Figure Size and Its Format in Matplotlib

  1. Set figsize in Figure() Method During Initialization
  2. Set rcParams to Change the Size of the Figure in Matplotlib
  3. Set_figheight() Along With Set_figwidth() to Set Matplotlib Figure Size
  4. Set_size_inches() Method to Change the Matplotlib Figure Size
  5. Change Figure Format in Matplotlib

We could use set_figheight() along with set_figwidth() and set_size_inches() methods to change figure size in Matplotlib. We can also change figure size in Matplotlib by setting figsize in figure() method and rcParams too. Similarly, to change figure format we simply change extension of image file in savefig() method.

Set figsize in Figure() Method During Initialization

We can set the value of figsize parameter in figure() method during initialization, which specifies the width and height of the figure in inches.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 0.5)
m = 1
c = 2
y = m*x + c

plt.figure(figsize=(6,4))
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')

plt.show()

Output:

set figsize using figsize parameter

The default value of the figsize parameter is [6.4, 4.8].

Set rcParams to Change the Size of the Figure in Matplotlib

We can change default figure.figsize value stored in matplotlib.rcParams dictionary to change the figure size in Matplotlib.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10,20)
m = 1
c = 2
y = m*x + c

plt.rcParams["figure.figsize"] = (8, 6)
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')

plt.show()

Output:

set figsize using rc parameter

Set_figheight() Along With Set_figwidth() to Set Matplotlib Figure Size

If the figure is already created, we can the figure size using set_figheight() and set_figwidth()methods.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10,20)
m = 1
c = 2
y = m*x + c

fig = plt.figure()
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')
fig.set_figheight(6)
fig.set_figwidth(8)

plt.show()

Output:

set figsize using set_figheight and set_figwidth

Here, the set_figheight() method sets the height of the figure and set_figwidth() sets the width of the figure.

Set_size_inches() Method to Change the Matplotlib Figure Size

This method could also set the figure size after the figure has been created.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10,20)
m = 1
c = 2
y = m*x + c

fig = plt.figure()
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')
fig.set_size_inches(5, 5)

plt.show()

Output:

set figsize using set_size_inches

Here the arguments passed into set_size_inches() method represent the width and height of the figure in inches respectively.

Change Figure Format in Matplotlib

To change the figure format, we can change the extension of the image file in savefig() method. We can save plots in different formats like png, jpg, svg, pdf, and many more.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10,20)
m = 1
c = 2
y = m*x + c

fig = plt.figure()
plt.plot(x, y)
plt.title("y=mx+c")
plt.xlabel('x-axis')
plt.ylabel('y-axis')
fig.set_size_inches(5, 5)

plt.savefig("Figure saved in jpg format.jpg")

This saves the figure in jpg format.

Related Article - Matplotlib figure format

comments powered by Disqus