Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitaf7d306

Browse files
committed
Demonstrate inset_axes in scatter_hist example.
Currently, the scatter_hist example needs to force the main axes to besquare by carefully adjusting the figure size -- shared axes and fixedaspects don't work well together (and manually resizing the figure showsthat the aspect is indeed not fixed). In fact, thescatter_hist_locatable_axes example explicitly states that the advantageof using axes_grid1 is to allow the aspect to be fixed.I realized that one can also use inset_axes to position the marginalsaxes relative to the main axes *and* support a fixed aspect ratio forthe main axes. Perhaps this can be considered as a slight API abuse,but I think this is a solution for a real limitation; the question iswhether we want to promote this use?
1 parent3e2d546 commitaf7d306

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

‎examples/axes_grid1/scatter_hist_locatable_axes.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
are defined by a ``Divider``, produced via `.make_axes_locatable`.
1111
1212
An alternative method to produce a similar figure is shown in the
13-
:doc:`/gallery/lines_bars_and_markers/scatter_hist` example. The advantage of
14-
the locatable axes method shown below is that the marginal axes follow the
15-
fixed aspect ratio of the main axes.
13+
:doc:`/gallery/lines_bars_and_markers/scatter_hist` example. The main
14+
difference is that the size of the marginals and the paddings are in inches
15+
(rather than relative to the main axes). Moreover, similarly to the
16+
`~.Axes.inset_axes` approach, the main axes can have a fixed aspect ratio.
1617
"""
1718

1819
importnumpyasnp

‎examples/lines_bars_and_markers/scatter_hist.py

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
* the axes positions are defined in terms of rectangles in figure coordinates
1313
* the axes positions are defined via a gridspec
14+
* the axes positions are defined via a inset_axes
1415
1516
An alternative method to produce a similar figure using the ``axes_grid1``
1617
toolkit is shown in the
@@ -70,7 +71,7 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
7071
rect_histy= [left+width+spacing,bottom,0.2,height]
7172

7273
# start with a square Figure
73-
fig=plt.figure(figsize=(8,8))
74+
fig=plt.figure(figsize=(6,6))
7475

7576
ax=fig.add_axes(rect_scatter)
7677
ax_histx=fig.add_axes(rect_histx,sharex=ax)
@@ -79,8 +80,6 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
7980
# use the previously defined function
8081
scatter_hist(x,y,ax,ax_histx,ax_histy)
8182

82-
plt.show()
83-
8483

8584
#############################################################################
8685
#
@@ -92,7 +91,7 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
9291
# tutorial.
9392

9493
# start with a square Figure
95-
fig=plt.figure(figsize=(8,8))
94+
fig=plt.figure(figsize=(6,6))
9695

9796
# Add a gridspec with two rows and two columns and a ratio of 2 to 7 between
9897
# the size of the marginal axes and the main axes in both directions.
@@ -108,6 +107,30 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
108107
# use the previously defined function
109108
scatter_hist(x,y,ax,ax_histx,ax_histy)
110109

110+
111+
#############################################################################
112+
#
113+
# Using inset_axes
114+
# ----------------
115+
#
116+
# Despite its name, `~.Axes.inset_axes` can also be used to position marginals
117+
# *outside* the main axes. The advantage of doing so is that the aspect ratio
118+
# of the main axes can be fixed, regardless of the figure size.
119+
120+
fig=plt.figure(constrained_layout=True)# doesn't need to be square
121+
122+
# Create the main axes, leaving 25% of the figure space at the top and on the
123+
# right to position marginals.
124+
ax=fig.add_gridspec(top=0.75,right=0.75).subplots()
125+
# The axes' aspect can be fixed.
126+
ax.set(aspect=1)
127+
# Create marginals, which have 25% of the size of the main axes.
128+
ax_histx=ax.inset_axes([0,1.05,1,0.25],sharex=ax)
129+
ax_histy=ax.inset_axes([1.05,0,0.25,1],sharey=ax)
130+
131+
# use the previously defined function
132+
scatter_hist(x,y,ax,ax_histx,ax_histy)
133+
111134
plt.show()
112135

113136

@@ -121,5 +144,6 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
121144
# - `matplotlib.figure.Figure.add_axes`
122145
# - `matplotlib.figure.Figure.add_subplot`
123146
# - `matplotlib.figure.Figure.add_gridspec`
147+
# - `matplotlib.axes.Axes.inset_axes`
124148
# - `matplotlib.axes.Axes.scatter`
125149
# - `matplotlib.axes.Axes.hist`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp