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

Commitc98158b

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 commitc98158b

File tree

2 files changed

+49
-51
lines changed

2 files changed

+49
-51
lines changed

‎examples/axes_grid1/scatter_hist_locatable_axes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
Scatter Histogram (Locatable Axes)
44
==================================
55
6-
Show the marginal distributions of a scatter as histograms at the sides of
6+
Show the marginal distributions of a scatterplotas histograms at the sides of
77
the plot.
88
99
For a nice alignment of the main axes with the marginals, the axes positions
10-
are defined by a ``Divider``, produced via `.make_axes_locatable`.
10+
are defined by a ``Divider``, produced via `.make_axes_locatable`. Note that
11+
the ``Divider`` API allows setting axes sizes and pads in inches, which is its
12+
main feature.
1113
12-
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.
14+
If one wants to set axes sizes and pads relative to the main Figure, see the
15+
:doc:`/gallery/lines_bars_and_markers/scatter_hist` example.
1616
"""
1717

1818
importnumpyasnp

‎examples/lines_bars_and_markers/scatter_hist.py

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,23 @@
33
Scatter plot with histograms
44
============================
55
6-
Show the marginal distributions of a scatter as histograms at the sides of
6+
Show the marginal distributions of a scatterplotas histograms at the sides of
77
the plot.
88
99
For a nice alignment of the main axes with the marginals, two options are shown
10-
below.
10+
below:
1111
12-
* the axes positions are defined in terms of rectangles in figure coordinates
13-
* the axes positions are defined via a gridspec
12+
* the axes positions are defined via a gridspec;
13+
* the axes positions are defined via a inset_axes.
14+
15+
While `.Axes.inset_axes` may be a bit more complex, it allows correct handling
16+
of main axes with a fixed aspect ratio.
1417
1518
An alternative method to produce a similar figure using the ``axes_grid1``
1619
toolkit is shown in the
17-
:doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` example.
20+
:doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` example. Finally, it is
21+
also possible to simply position all axes in absolute coordinates using
22+
`.Figure.add_axes` (not shown here).
1823
1924
Let us first define a function that takes x and y data as input, as well
2025
as three axes, the main axes for the scatter, and two marginal axes. It will
@@ -50,62 +55,54 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
5055
ax_histy.hist(y,bins=bins,orientation='horizontal')
5156

5257

53-
#############################################################################
54-
#
55-
# Axes in figure coordinates
56-
# --------------------------
57-
#
58-
# To define the axes positions, `.Figure.add_axes` is provided with a rectangle
59-
# ``[left, bottom, width, height]`` in figure coordinates. The marginal axes
60-
# share one dimension with the main axes.
61-
62-
# definitions for the axes
63-
left,width=0.1,0.65
64-
bottom,height=0.1,0.65
65-
spacing=0.005
66-
67-
68-
rect_scatter= [left,bottom,width,height]
69-
rect_histx= [left,bottom+height+spacing,width,0.2]
70-
rect_histy= [left+width+spacing,bottom,0.2,height]
71-
72-
# start with a square Figure
73-
fig=plt.figure(figsize=(8,8))
74-
75-
ax=fig.add_axes(rect_scatter)
76-
ax_histx=fig.add_axes(rect_histx,sharex=ax)
77-
ax_histy=fig.add_axes(rect_histy,sharey=ax)
78-
79-
# use the previously defined function
80-
scatter_hist(x,y,ax,ax_histx,ax_histy)
81-
82-
plt.show()
83-
84-
8558
#############################################################################
8659
#
8760
# Using a gridspec
8861
# ----------------
8962
#
90-
# We may equally define a gridspec with unequal width- and height-ratios to
91-
# achieve desired layout. Also see the :doc:`/tutorials/intermediate/gridspec`
92-
# tutorial.
93-
94-
# start with a square Figure
95-
fig=plt.figure(figsize=(8,8))
63+
# We define a gridspec with unequal width- and height-ratios to achieve desired
64+
# layout. Also see the :doc:`/tutorials/intermediate/gridspec` tutorial.
9665

66+
# Start with a square Figure.
67+
fig=plt.figure(figsize=(6,6))
9768
# Add a gridspec with two rows and two columns and a ratio of 2 to 7 between
9869
# the size of the marginal axes and the main axes in both directions.
9970
# Also adjust the subplot parameters for a square plot.
10071
gs=fig.add_gridspec(2,2,width_ratios=(7,2),height_ratios=(2,7),
10172
left=0.1,right=0.9,bottom=0.1,top=0.9,
10273
wspace=0.05,hspace=0.05)
103-
74+
# Create the Axes.
10475
ax=fig.add_subplot(gs[1,0])
10576
ax_histx=fig.add_subplot(gs[0,0],sharex=ax)
10677
ax_histy=fig.add_subplot(gs[1,1],sharey=ax)
78+
# Draw the scatter plot and marginals.
79+
scatter_hist(x,y,ax,ax_histx,ax_histy)
10780

108-
# use the previously defined function
81+
82+
#############################################################################
83+
#
84+
# Using inset_axes
85+
# ----------------
86+
#
87+
# Despite its name, `~.Axes.inset_axes` can also be used to position marginals
88+
# *outside* the main axes. The advantage of doing so is that the aspect ratio
89+
# of the main axes can be fixed, regardless of the figure size.
90+
91+
# Create a Figure, which doesn't have to be square.
92+
fig=plt.figure(constrained_layout=True)
93+
# Create the main axes, leaving 25% of the figure space at the top and on the
94+
# right to position marginals.
95+
ax=fig.add_gridspec(top=0.75,right=0.75).subplots()
96+
# The main axes' aspect can be fixed.
97+
ax.set(aspect=1)
98+
# Create marginal axes, which have 25% of the size of the main axes. Note that
99+
# the inset axes are positioned *outside* (on the right and the top) of the
100+
# main axes, by specifying axes coordinates greater than 1. Axes coordinates
101+
# less than 0 would likewise specify positions on the left and the bottom of
102+
# the main axes.
103+
ax_histx=ax.inset_axes([0,1.05,1,0.25],sharex=ax)
104+
ax_histy=ax.inset_axes([1.05,0,0.25,1],sharey=ax)
105+
# Draw the scatter plot and marginals.
109106
scatter_hist(x,y,ax,ax_histx,ax_histy)
110107

111108
plt.show()
@@ -121,5 +118,6 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
121118
# - `matplotlib.figure.Figure.add_axes`
122119
# - `matplotlib.figure.Figure.add_subplot`
123120
# - `matplotlib.figure.Figure.add_gridspec`
121+
# - `matplotlib.axes.Axes.inset_axes`
124122
# - `matplotlib.axes.Axes.scatter`
125123
# - `matplotlib.axes.Axes.hist`

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp