Figure size in different units#
The native figure size unit in Matplotlib is inches, deriving from print industry standards. However, users may need to specify their figures in other units like centimeters or pixels. This example illustrates how to do this efficiently.
import matplotlib.pyplot as plt text_kwargs = dict(ha='center', va='center', fontsize=28, color='C1')
Figure size in inches (default)#
Figure size in centimeter#
Multiplying centimeter-based numbers with a conversion factor from cm to
inches, gives the right numbers. Naming the conversion factor
the conversion almost look like appending a unit to the number, which is
Figure size in pixel#
Similarly, one can use a conversion from pixels.
Note that you could break this if you use
savefig with a
different explicit dpi value.
Quick interactive work is usually rendered to the screen, making pixels a good size of unit. But defining the conversion factor may feel a little tedious for quick iterations.
Because of the default
rcParams['figure.dpi'] = 100, one can mentally
divide the needed pixel value by 100 :
The use of the following functions, methods, classes and modules is shown in this example: