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

Appearance settings

Commit 481701b

Browse filesBrowse files
authored
Merge pull request #21283 from anntzer/ia
Demonstrate inset_axes in scatter_hist example.
2 parents a1f7a39 + 71de3b2 commit 481701b
Copy full SHA for 481701b

File tree

Expand file treeCollapse file tree

2 files changed

+54
-57
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+54
-57
lines changed

‎examples/axes_grid1/scatter_hist_locatable_axes.py

Copy file name to clipboardExpand all lines: examples/axes_grid1/scatter_hist_locatable_axes.py
+6-6Lines 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 scatter plot as 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
import numpy as np

‎examples/lines_bars_and_markers/scatter_hist.py

Copy file name to clipboardExpand all lines: examples/lines_bars_and_markers/scatter_hist.py
+48-51Lines changed: 48 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,22 @@
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 scatter plot as 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+
.. contents::
13+
:local:
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``
16-
toolkit is shown in the
17-
:doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` example.
19+
toolkit is shown in the :doc:`/gallery/axes_grid1/scatter_hist_locatable_axes`
20+
example. Finally, it is also possible to position all axes in absolute
21+
coordinates using `.Figure.add_axes` (not shown here).
1822
1923
Let us first define a function that takes x and y data as input, as well
2024
as three axes, the main axes for the scatter, and two marginal axes. It will
@@ -52,60 +56,53 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
5256

5357
#############################################################################
5458
#
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-
85-
#############################################################################
86-
#
87-
# Using a gridspec
88-
# ----------------
59+
# Defining the axes positions using a gridspec
60+
# --------------------------------------------
8961
#
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))
62+
# We define a gridspec with unequal width- and height-ratios to achieve desired
63+
# layout. Also see the :doc:`/tutorials/intermediate/gridspec` tutorial.
9664

97-
# Add a gridspec with two rows and two columns and a ratio of 2 to 7 between
65+
# Start with a square Figure.
66+
fig = plt.figure(figsize=(6, 6))
67+
# Add a gridspec with two rows and two columns and a ratio of 1 to 4 between
9868
# the size of the marginal axes and the main axes in both directions.
9969
# Also adjust the subplot parameters for a square plot.
100-
gs = fig.add_gridspec(2, 2, width_ratios=(7, 2), height_ratios=(2, 7),
70+
gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),
10171
left=0.1, right=0.9, bottom=0.1, top=0.9,
10272
wspace=0.05, hspace=0.05)
103-
73+
# Create the Axes.
10474
ax = fig.add_subplot(gs[1, 0])
10575
ax_histx = fig.add_subplot(gs[0, 0], sharex=ax)
10676
ax_histy = fig.add_subplot(gs[1, 1], sharey=ax)
77+
# Draw the scatter plot and marginals.
78+
scatter_hist(x, y, ax, ax_histx, ax_histy)
79+
10780

108-
# use the previously defined function
81+
#############################################################################
82+
#
83+
# Defining the axes positions using inset_axes
84+
# --------------------------------------------
85+
#
86+
# `~.Axes.inset_axes` can be used to position marginals *outside* the main
87+
# axes. The advantage of doing so is that the aspect ratio of the main axes
88+
# can be fixed, and the marginals will always be drawn relative to the position
89+
# of the axes.
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()
@@ -118,8 +115,8 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
118115
# The use of the following functions, methods, classes and modules is shown
119116
# in this example:
120117
#
121-
# - `matplotlib.figure.Figure.add_axes`
122118
# - `matplotlib.figure.Figure.add_subplot`
123119
# - `matplotlib.figure.Figure.add_gridspec`
120+
# - `matplotlib.axes.Axes.inset_axes`
124121
# - `matplotlib.axes.Axes.scatter`
125122
# - `matplotlib.axes.Axes.hist`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.