Skip to content

Commit c035f66

Browse files
authored
Merge pull request #18360 from timhoffm/doc-figsize
Add example for specifying figure size in different units
2 parents 421d7b3 + 4544593 commit c035f66

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ per-file-ignores =
249249
examples/subplots_axes_and_figures/custom_figure_class.py: E402
250250
examples/subplots_axes_and_figures/demo_constrained_layout.py: E402
251251
examples/subplots_axes_and_figures/demo_tight_layout.py: E402
252+
examples/subplots_axes_and_figures/figure_size_units.py: E402
252253
examples/subplots_axes_and_figures/secondary_axis.py: E402
253254
examples/subplots_axes_and_figures/two_scales.py: E402
254255
examples/subplots_axes_and_figures/zoom_inset_axes.py: E402
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
==============================
3+
Figure size in different units
4+
==============================
5+
6+
The native figure size unit in Matplotlib is inches, deriving from print
7+
industry standards. However, users may need to specify their figures in other
8+
units like centimeters or pixels. This example illustrates how to do this
9+
efficiently.
10+
"""
11+
12+
# sphinx_gallery_thumbnail_number = 2
13+
14+
import matplotlib.pyplot as plt
15+
text_kwargs = dict(ha='center', va='center', fontsize=28, color='C1')
16+
17+
##############################################################################
18+
# Figure size in inches (default)
19+
# -------------------------------
20+
#
21+
plt.subplots(figsize=(6, 2))
22+
plt.text(0.5, 0.5, '6 inches x 2 inches', **text_kwargs)
23+
plt.show()
24+
25+
26+
#############################################################################
27+
# Figure size in centimeter
28+
# -------------------------
29+
# Multiplying centimeter-based numbers with a conversion factor from cm to
30+
# inches, gives the right numbers. Naming the conversion factor ``cm`` makes
31+
# the conversion almost look like appending a unit to the number, which is
32+
# nicely readable.
33+
#
34+
cm = 1/2.54 # centimeters in inches
35+
plt.subplots(figsize=(15*cm, 5*cm))
36+
plt.text(0.5, 0.5, '15cm x 5cm', **text_kwargs)
37+
plt.show()
38+
39+
40+
#############################################################################
41+
# Figure size in pixel
42+
# --------------------
43+
# Similarly, one can use a conversion from pixels.
44+
#
45+
# Note that you could break this if you use `~.pyplot.savefig` with a
46+
# different explicit dpi value.
47+
#
48+
px = 1/plt.rcParams['figure.dpi'] # pixel in inches
49+
plt.subplots(figsize=(600*px, 200*px))
50+
plt.text(0.5, 0.5, '600px x 200px', **text_kwargs)
51+
plt.show()
52+
53+
#############################################################################
54+
# Quick interactive work is usually rendered to the screen, making pixels a
55+
# good size of unit. But defining the conversion factor may feel a little
56+
# tedious for quick iterations.
57+
#
58+
# Because of the default ``rcParams['figure.dpi'] = 100``, one can mentally
59+
# divide the needed pixel value by 100 [*]_:
60+
#
61+
plt.subplots(figsize=(6, 2))
62+
plt.text(0.5, 0.5, '600px x 200px', **text_kwargs)
63+
plt.show()
64+
65+
#############################################################################
66+
# .. [*] Unfortunately, this does not work well for the ``matplotlib inline``
67+
# backend in jupyter because that backend uses a different default of
68+
# ``rcParams['figure.dpi'] = 72``. Additionally, it saves the figure
69+
# with ``bbox_inches='tight'``, which crops the figure and makes the
70+
# actual size unpredictable.
71+
72+
#############################################################################
73+
#
74+
# ------------
75+
#
76+
# References
77+
# """"""""""
78+
#
79+
# The use of the following functions and methods is shown in this example:
80+
81+
import matplotlib
82+
83+
matplotlib.pyplot.figure
84+
matplotlib.pyplot.subplots
85+
matplotlib.pyplot.subplot_mosaic

0 commit comments

Comments
 (0)