Skip to content

Commit 8f24037

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #29670: DOC: change marginal scatter plot to subplot_mosaic
1 parent 9c7b6a9 commit 8f24037

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

galleries/examples/lines_bars_and_markers/scatter_hist.py

+38-31
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
Scatter plot with histograms
44
============================
55
6-
Show the marginal distributions of a scatter plot as histograms at the sides of
7-
the plot.
6+
Add histograms to the x-axes and y-axes margins of a scatter plot.
7+
8+
This layout features a central scatter plot illustrating the relationship
9+
between x and y, a histogram at the top displaying the distribution of x, and a
10+
histogram on the right showing the distribution of y.
811
912
For a nice alignment of the main Axes with the marginals, two options are shown
1013
below:
@@ -15,14 +18,9 @@
1518
While `.Axes.inset_axes` may be a bit more complex, it allows correct handling
1619
of main Axes with a fixed aspect ratio.
1720
18-
An alternative method to produce a similar figure using the ``axes_grid1``
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).
22-
23-
Let us first define a function that takes x and y data as input, as well
24-
as three Axes, the main Axes for the scatter, and two marginal Axes. It will
25-
then create the scatter and histograms inside the provided Axes.
21+
Let us first define a function that takes x and y data as input, as well as
22+
three Axes, the main Axes for the scatter, and two marginal Axes. It will then
23+
create the scatter and histograms inside the provided Axes.
2624
"""
2725

2826
import matplotlib.pyplot as plt
@@ -55,27 +53,22 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
5553

5654

5755
# %%
56+
# Defining the Axes positions using subplot_mosaic
57+
# ------------------------------------------------
5858
#
59-
# Defining the Axes positions using a gridspec
60-
# --------------------------------------------
61-
#
62-
# We define a gridspec with unequal width- and height-ratios to achieve desired
63-
# layout. Also see the :ref:`arranging_axes` tutorial.
64-
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
68-
# the size of the marginal Axes and the main Axes in both directions.
69-
# Also adjust the subplot parameters for a square plot.
70-
gs = fig.add_gridspec(2, 2, width_ratios=(4, 1), height_ratios=(1, 4),
71-
left=0.1, right=0.9, bottom=0.1, top=0.9,
72-
wspace=0.05, hspace=0.05)
73-
# Create the Axes.
74-
ax = fig.add_subplot(gs[1, 0])
75-
ax_histx = fig.add_subplot(gs[0, 0], sharex=ax)
76-
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)
59+
# We use the `~.pyplot.subplot_mosaic` function to define the positions and
60+
# names of the three axes; the empty axes is specified by ``'.'``. We manually
61+
# specify the size of the figure, and can make the different axes have
62+
# different sizes by specifying the *width_ratios* and *height_ratios*
63+
# arguments. The *layout* argument is set to ``'constrained'`` to optimize the
64+
# spacing between the axes.
65+
66+
fig, axs = plt.subplot_mosaic([['histx', '.'],
67+
['scatter', 'histy']],
68+
figsize=(6, 6),
69+
width_ratios=(4, 1), height_ratios=(1, 4),
70+
layout='constrained')
71+
scatter_hist(x, y, axs['scatter'], axs['histx'], axs['histy'])
7972

8073

8174
# %%
@@ -109,13 +102,27 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
109102

110103
# %%
111104
#
105+
# While we recommend using one of the two methods described above, there are
106+
# number of other ways to achieve a similar layout:
107+
#
108+
# - The Axes can be positioned manually in relative coordinates using
109+
# `~matplotlib.figure.Figure.add_axes`.
110+
# - A gridspec can be used to create the layout
111+
# (`~matplotlib.figure.Figure.add_gridspec`) and adding only the three desired
112+
# axes (`~matplotlib.figure.Figure.add_subplot`).
113+
# - Four subplots can be created using `~.pyplot.subplots`, and the unused
114+
# axes in the upper right can be removed manually.
115+
# - The ``axes_grid1`` toolkit can be used, as shown in
116+
# :doc:`/gallery/axes_grid1/scatter_hist_locatable_axes`.
117+
#
112118
# .. admonition:: References
113119
#
114120
# The use of the following functions, methods, classes and modules is shown
115121
# in this example:
116122
#
123+
# - `matplotlib.figure.Figure.subplot_mosaic`
124+
# - `matplotlib.pyplot.subplot_mosaic`
117125
# - `matplotlib.figure.Figure.add_subplot`
118-
# - `matplotlib.figure.Figure.add_gridspec`
119126
# - `matplotlib.axes.Axes.inset_axes`
120127
# - `matplotlib.axes.Axes.scatter`
121128
# - `matplotlib.axes.Axes.hist`

0 commit comments

Comments
 (0)