Prerequisite: Matplotlib
Plots are an effective way of visually representing data and summarizing it in a beautiful manner. However, if not plotted efficiently it seems appears complicated. In python’s matplotlib provides several libraries for the purpose of data representation.
While making a plot it is important for us to optimize its size. Here are various ways to change the default plot size as per our required dimensions or resize a given plot.
Method 1: Using set_figheight() and set_figwidth()
For changing height and width of a plot set_figheight and set_figwidth are used
Python3
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
plt.xlabel('x - axis')
plt.ylabel('y - axis')
print("Plot in it's default size: ")
plt.plot(x, y)
plt.show()
f = plt.figure()
f.set_figwidth(4)
f.set_figheight(1)
print("Plot after re-sizing: ")
plt.plot(x, y)
plt.show()
|
Output:

Method 2: Using figsize
figsize() takes two parameters- width and height (in inches). By default the values for width and height are 6.4 and 4.8 respectively.
Syntax:
plt.figure(figsize=(x,y))
Where, x and y are width and height respectively in inches.
Python3
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [6, 7, 8, 9, 10]
display(plt.plot(x, y))
plt.figure(figsize=(2, 2))
display(plt.plot(x, y))
|
Output:

output screenshot
Method 3: Changing the default rcParams
We can permanently change the default size of a figure as per our needs by setting the figure.figsize.
Python3
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [1, 2, 3, 4, 5]
plt.xlabel('x - axis')
plt.ylabel('y - axis')
plt.plot(x, y)
plt.show()
plt.rcParams['figure.figsize'] = [2, 2]
plt.plot(x, y)
plt.show()
plt.scatter(x, y)
plt.show()
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!