|
| 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