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

Skip to content

Commit 2c2741d

Browse files
committed
DOC: change marginal scatter plot to subplot_mosaic
1 parent c9402f3 commit 2c2741d

File tree

1 file changed

+22
-19
lines changed

1 file changed

+22
-19
lines changed

galleries/examples/lines_bars_and_markers/scatter_hist.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
Show the marginal distributions of a scatter plot as histograms at the sides of
77
the plot.
88
9+
This layout has a central scatter plot, with marginal histograms above and to the
10+
right of the central plot.
11+
912
For a nice alignment of the main Axes with the marginals, two options are shown
1013
below:
1114
@@ -55,27 +58,27 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
5558

5659

5760
# %%
61+
# Defining the Axes positions using subplot_mosaic
62+
# ------------------------------------------------
5863
#
59-
# Defining the Axes positions using a gridspec
60-
# --------------------------------------------
64+
# We use the `~.pyplot.subplot_mosaic` function to define the positions and
65+
# names of the three axes; the empty axes is specified by `'.'`. We manually
66+
# specify the size of the figure, and can make the different axes have
67+
# different sizes by specifying the *width_ratios* and *height_ratios*
68+
# arguments. The *layout* argument is set to `'constrained'` to optimize the
69+
# spacing between the axes.
6170
#
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)
71+
# Note that there are other ways to accomplish the same layout using a gridspec
72+
# and only adding the three desired axes, or by using `~.pyplot.subplots` and
73+
# removing the unused axes in the upper right. The arguments are similar to the ones
74+
# used here.
75+
76+
fig, axs = plt.subplot_mosaic([['histx', '.'],
77+
['scatter', 'histy']],
78+
figsize=(6, 6),
79+
width_ratios=(4, 1), height_ratios=(1, 4),
80+
layout='constrained')
81+
scatter_hist(x, y, axs['scatter'], axs['histx'], axs['histy'])
7982

8083

8184
# %%

0 commit comments

Comments
 (0)