|
| 1 | +""" |
| 2 | +=============== |
| 3 | +Axes box aspect |
| 4 | +=============== |
| 5 | +
|
| 6 | +This demo shows how to set the aspect of an axes box directly via |
| 7 | +`~.Axes.set_box_aspect`. The box aspect is the ratio between axes height |
| 8 | +and axes width in physical units, independent of the data limits. |
| 9 | +This is useful to e.g. produce a square plot, independent of the data it |
| 10 | +contains, or to have a usual plot with the same axes dimensions next to |
| 11 | +an image plot with fixed (data-)aspect. |
| 12 | +
|
| 13 | +The following lists a few use cases for `~.Axes.set_box_aspect`. |
| 14 | +""" |
| 15 | + |
| 16 | +############################################################################ |
| 17 | +# A square axes, independent of data |
| 18 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 19 | +# |
| 20 | +# Produce a square axes, no matter what the data limits are. |
| 21 | + |
| 22 | +import matplotlib |
| 23 | +import numpy as np |
| 24 | +import matplotlib.pyplot as plt |
| 25 | + |
| 26 | +fig1, ax = plt.subplots() |
| 27 | + |
| 28 | +ax.set_xlim(300, 400) |
| 29 | +ax.set_box_aspect(1) |
| 30 | + |
| 31 | +plt.show() |
| 32 | + |
| 33 | +############################################################################ |
| 34 | +# Shared square axes |
| 35 | +# ~~~~~~~~~~~~~~~~~~ |
| 36 | +# |
| 37 | +# Produce shared subplots that are squared in size. |
| 38 | +# |
| 39 | +fig2, (ax, ax2) = plt.subplots(ncols=2, sharey=True) |
| 40 | + |
| 41 | +ax.plot([1, 5], [0, 10]) |
| 42 | +ax2.plot([100, 500], [10, 15]) |
| 43 | + |
| 44 | +ax.set_box_aspect(1) |
| 45 | +ax2.set_box_aspect(1) |
| 46 | + |
| 47 | +plt.show() |
| 48 | + |
| 49 | +############################################################################ |
| 50 | +# Square twin axes |
| 51 | +# ~~~~~~~~~~~~~~~~ |
| 52 | +# |
| 53 | +# Produce a square axes, with a twin axes. The twinned axes takes over the |
| 54 | +# box aspect of the parent. |
| 55 | +# |
| 56 | + |
| 57 | +fig3, ax = plt.subplots() |
| 58 | + |
| 59 | +ax2 = ax.twinx() |
| 60 | + |
| 61 | +ax.plot([0, 10]) |
| 62 | +ax2.plot([12, 10]) |
| 63 | + |
| 64 | +ax.set_box_aspect(1) |
| 65 | + |
| 66 | +plt.show() |
| 67 | + |
| 68 | + |
| 69 | +############################################################################ |
| 70 | +# Normal plot next to image |
| 71 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 72 | +# |
| 73 | +# When creating an image plot with fixed data aspect and the default |
| 74 | +# ``adjustable="box"`` next to a normal plot, the axes would be unequal in |
| 75 | +# height. `~.Axes.set_box_aspect` provides an easy solution to that by allowing |
| 76 | +# to have the normal plot's axes use the images dimensions as box aspect. |
| 77 | +# |
| 78 | +# This example also shows that ``constrained_layout`` interplays nicely with |
| 79 | +# a fixed box aspect. |
| 80 | + |
| 81 | +fig4, (ax, ax2) = plt.subplots(ncols=2, constrained_layout=True) |
| 82 | + |
| 83 | +im = np.random.rand(16, 27) |
| 84 | +ax.imshow(im) |
| 85 | + |
| 86 | +ax2.plot([23, 45]) |
| 87 | +ax2.set_box_aspect(im.shape[0]/im.shape[1]) |
| 88 | + |
| 89 | +plt.show() |
| 90 | + |
| 91 | +############################################################################ |
| 92 | +# Square joint/marginal plot |
| 93 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | +# |
| 95 | +# It may be desireable to show marginal distributions next to a plot of joint |
| 96 | +# data. The following creates a square plot with the box aspect of the |
| 97 | +# marginal axes being equal to the width- and height-ratios of the gridspec. |
| 98 | +# This ensures that all axes align perfectly, independent on the size of the |
| 99 | +# figure. |
| 100 | + |
| 101 | +fig5, axs = plt.subplots(2, 2, sharex="col", sharey="row", |
| 102 | + gridspec_kw=dict(height_ratios=[1, 3], |
| 103 | + width_ratios=[3, 1])) |
| 104 | +axs[0, 1].set_visible(False) |
| 105 | +axs[0, 0].set_box_aspect(1/3) |
| 106 | +axs[1, 0].set_box_aspect(1) |
| 107 | +axs[1, 1].set_box_aspect(3/1) |
| 108 | + |
| 109 | +x, y = np.random.randn(2, 400) * np.array([[.5], [180]]) |
| 110 | +axs[1, 0].scatter(x, y) |
| 111 | +axs[0, 0].hist(x) |
| 112 | +axs[1, 1].hist(y, orientation="horizontal") |
| 113 | + |
| 114 | +plt.show() |
| 115 | + |
| 116 | +############################################################################ |
| 117 | +# Square joint/marginal plot |
| 118 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 119 | +# |
| 120 | +# When setting the box aspect, one may still set the data aspect as well. |
| 121 | +# Here we create an axes with a box twice as long as tall and use an "equal" |
| 122 | +# data aspect for its contents, i.e. the circle actually stays circular. |
| 123 | + |
| 124 | +fig6, ax = plt.subplots() |
| 125 | + |
| 126 | +ax.add_patch(plt.Circle((5, 3), 1)) |
| 127 | +ax.set_aspect("equal", adjustable="datalim") |
| 128 | +ax.set_box_aspect(0.5) |
| 129 | +ax.autoscale() |
| 130 | + |
| 131 | +plt.show() |
| 132 | + |
| 133 | +############################################################################ |
| 134 | +# Box aspect for many subplots |
| 135 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 136 | +# |
| 137 | +# It is possible to pass the box aspect to an axes at initialization. The |
| 138 | +# following creates a 2 by 3 subplot grid with all square axes. |
| 139 | + |
| 140 | +fig7, axs = plt.subplots(2, 3, subplot_kw=dict(box_aspect=1), |
| 141 | + sharex=True, sharey=True, constrained_layout=True) |
| 142 | + |
| 143 | +for i, ax in enumerate(axs.flat): |
| 144 | + ax.scatter(i % 3, -((i // 3) - 0.5)*200, c=[plt.cm.hsv(i / 6)], s=300) |
| 145 | +plt.show() |
| 146 | + |
| 147 | +############################################################################# |
| 148 | +# |
| 149 | +# ------------ |
| 150 | +# |
| 151 | +# References |
| 152 | +# """""""""" |
| 153 | +# |
| 154 | +# The use of the following functions, methods and classes is shown |
| 155 | +# in this example: |
| 156 | + |
| 157 | +matplotlib.axes.Axes.set_box_aspect |
0 commit comments