The Python matplotlib
is a popular open source library, which is used to create customized visualizations including graphs and figures. It allows for specification of the size, aspect ratio, and dpi—dots per inch or pixels per inch—for the matplotlib figure
.
The code snippet below shows us how to specify the size, aspect ratio, and dpi for our matplotlib figure
. All the specifications can be provided in one line.
plt.figure(figsize=(width,height), dpi=pixels_per_inch)
figsize
argument in the figure
function allows us to specify two things:width
and height
are the actual size of the image in inches.width
and height
— width
:height
—specifies the aspect ratio of the image.dpi
argument in the figure
function allows us to specify the pixels per inch that we want in our image.When we execute matplotlib.pyplot.show()
, or matplotlib.pyplot.savefig()
, our figure will have the specified properties.
Let's look at an example:
#importsimport matplotlib.pyplot as pltimport numpy as np#input datay = np.array([12, 13, 25, 8, 42])mylabels = ["Category A", "Category B", "Category C", "Category D", "Category E"]#figure and chartplt.figure(figsize=(5,4), dpi=25)plt.pie(y, labels = mylabels)plt.show()
numpy
and matplotlib.pyplot
libraries. figure
.figure
to have. The width of the image is set to 5
, the height is 4
, the aspect ratio is 5
:4
(width to height ratio), and the dpi
is set to 25.pie
chart using the array y
. The labels for our categories have been specified in labels
.Free Resources