3
3
Scatter plot with histograms
4
4
============================
5
5
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.
8
11
9
12
For a nice alignment of the main Axes with the marginals, two options are shown
10
13
below:
15
18
While `.Axes.inset_axes` may be a bit more complex, it allows correct handling
16
19
of main Axes with a fixed aspect ratio.
17
20
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.
26
24
"""
27
25
28
26
import matplotlib .pyplot as plt
@@ -55,27 +53,22 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
55
53
56
54
57
55
# %%
56
+ # Defining the Axes positions using subplot_mosaic
57
+ # ------------------------------------------------
58
58
#
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' ])
79
72
80
73
81
74
# %%
@@ -109,13 +102,27 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
109
102
110
103
# %%
111
104
#
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
+ #
112
118
# .. admonition:: References
113
119
#
114
120
# The use of the following functions, methods, classes and modules is shown
115
121
# in this example:
116
122
#
123
+ # - `matplotlib.figure.Figure.subplot_mosaic`
124
+ # - `matplotlib.pyplot.subplot_mosaic`
117
125
# - `matplotlib.figure.Figure.add_subplot`
118
- # - `matplotlib.figure.Figure.add_gridspec`
119
126
# - `matplotlib.axes.Axes.inset_axes`
120
127
# - `matplotlib.axes.Axes.scatter`
121
128
# - `matplotlib.axes.Axes.hist`
0 commit comments