Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50fc5dd

Browse files
authored
Merge pull request #25195 from jklymak/doc-fixed-size
DOC: explain how to make a fixed-size axes
2 parents 39070ae + 9a386a5 commit 50fc5dd

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

examples/axes_grid1/demo_fixed_size_axes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
===============================
33
Axes with a fixed physical size
44
===============================
5+
6+
Note that this can be accomplished with the main library for
7+
Axes on Figures that do not change size: :ref:`fixed_size_axes`
58
"""
69

710
import matplotlib.pyplot as plt

tutorials/intermediate/arranging_axes.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""
2+
.. redirect-from:: /tutorials/intermediate/gridspec
3+
4+
.. _arranging_axes:
5+
26
===================================
37
Arranging multiple Axes in a Figure
48
===================================
@@ -58,6 +62,8 @@
5862
`~matplotlib.gridspec.SubplotSpec`
5963
Specifies the location of the subplot in the given `.GridSpec`.
6064
65+
.. _fixed_size_axes:
66+
6167
Adding single Axes at a time
6268
----------------------------
6369
@@ -80,9 +86,26 @@
8086
Similar to `.pyplot.subplot`, but uses 0-based indexing and two-d python
8187
slicing to choose cells.
8288
83-
.. redirect-from:: /tutorials/intermediate/gridspec
84-
8589
"""
90+
91+
# %%
92+
#
93+
# As a simple example of manually adding an axes a, lets add a 3 inch x 2 inch
94+
# Axes to a 4 inch x 3 inch figure. Note that the location of the subplot is
95+
# defined as [left, bottom, width, height] in figure-normalized units:
96+
97+
# sphinx_gallery_thumbnail_number = 2
98+
99+
import matplotlib.pyplot as plt
100+
import numpy as np
101+
102+
w, h = 4, 3
103+
margin = 0.5
104+
fig = plt.figure(figsize=(w, h), facecolor='lightblue')
105+
ax = fig.add_axes([margin / w, margin / h, (w - 2 * margin) / w,
106+
(h - 2 * margin) / h])
107+
108+
86109
# %%
87110
# High-level methods for making grids
88111
# ===================================
@@ -97,9 +120,6 @@
97120
# we use `~.Axes.annotate`, but other examples could be `~.Axes.plot`,
98121
# `~.Axes.pcolormesh`, etc.
99122

100-
import matplotlib.pyplot as plt
101-
import numpy as np
102-
103123
fig, axs = plt.subplots(ncols=2, nrows=2, figsize=(5.5, 3.5),
104124
layout="constrained")
105125
# add an artist, in this case a nice label in the middle...
@@ -146,7 +166,8 @@ def annotate_axes(ax, text, fontsize=18):
146166
# have a set aspect ratio. This leads to large gaps between Axes by default:
147167
#
148168

149-
fig, axs = plt.subplots(2, 2, layout="constrained", figsize=(5.5, 3.5))
169+
fig, axs = plt.subplots(2, 2, layout="constrained",
170+
figsize=(5.5, 3.5), facecolor='lightblue')
150171
for ax in axs.flat:
151172
ax.set_aspect(1)
152173
fig.suptitle('Fixed aspect Axes')
@@ -159,7 +180,8 @@ def annotate_axes(ax, text, fontsize=18):
159180
# provides `~.mpl_toolkits.axes_grid1.axes_grid.ImageGrid` to accomplish
160181
# a similar effect, but with a non-standard Axes class).
161182

162-
fig, axs = plt.subplots(2, 2, layout="compressed", figsize=(5.5, 3.5))
183+
fig, axs = plt.subplots(2, 2, layout="compressed", figsize=(5.5, 3.5),
184+
facecolor='lightblue')
163185
for ax in axs.flat:
164186
ax.set_aspect(1)
165187
fig.suptitle('Fixed aspect Axes: compressed')
@@ -218,7 +240,7 @@ def annotate_axes(ax, text, fontsize=18):
218240
fig = plt.figure(layout="constrained")
219241
subfigs = fig.subfigures(1, 2, wspace=0.07, width_ratios=[1.5, 1.])
220242
axs0 = subfigs[0].subplots(2, 2)
221-
subfigs[0].set_facecolor('0.9')
243+
subfigs[0].set_facecolor('lightblue')
222244
subfigs[0].suptitle('subfigs[0]\nLeft side')
223245
subfigs[0].supxlabel('xlabel for subfigs[0]')
224246

@@ -315,7 +337,7 @@ def annotate_axes(ax, text, fontsize=18):
315337
# These spacing parameters can also be passed to `~.pyplot.subplots` and
316338
# `~.pyplot.subplot_mosaic` as the *gridspec_kw* argument.
317339

318-
fig = plt.figure(layout=None, facecolor='0.9')
340+
fig = plt.figure(layout=None, facecolor='lightblue')
319341
gs = fig.add_gridspec(nrows=3, ncols=3, left=0.05, right=0.75,
320342
hspace=0.1, wspace=0.05)
321343
ax0 = fig.add_subplot(gs[:-1, :])

0 commit comments

Comments
 (0)