How to change figure size and its format in Matplotlib

Set
figsize
infigure()
method during initialization 
set
rcParams
to change the size of the figure in Matplotlib 
set_figheight()
along withset_figwidth()
to set Matplotlib figure size 
set_size_inches()
method to change the figure size  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('xaxis')
plt.ylabel('yaxis')
plt.show()
Output:
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('xaxis')
plt.ylabel('yaxis')
plt.show()
Output:
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('xaxis')
plt.ylabel('yaxis')
fig.set_figheight(6)
fig.set_figwidth(8)
plt.show()
Output:
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 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('xaxis')
plt.ylabel('yaxis')
fig.set_size_inches(5, 5)
plt.show()
Output:
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('xaxis')
plt.ylabel('yaxis')
fig.set_size_inches(5, 5)
plt.savefig("Figure saved in jpg format.jpg")
This saves the figure in pdf
format.