3
3
Scatter plot with histograms
4
4
============================
5
5
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
7
7
the plot.
8
8
9
9
For a nice alignment of the main axes with the marginals, two options are shown
10
- below.
10
+ below:
11
11
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.
14
17
15
18
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).
18
22
19
23
Let us first define a function that takes x and y data as input, as well
20
24
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):
52
56
53
57
#############################################################################
54
58
#
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
+ # --------------------------------------------
89
61
#
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.
96
64
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
98
68
# the size of the marginal axes and the main axes in both directions.
99
69
# 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 ),
101
71
left = 0.1 , right = 0.9 , bottom = 0.1 , top = 0.9 ,
102
72
wspace = 0.05 , hspace = 0.05 )
103
-
73
+ # Create the Axes.
104
74
ax = fig .add_subplot (gs [1 , 0 ])
105
75
ax_histx = fig .add_subplot (gs [0 , 0 ], sharex = ax )
106
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 )
79
+
107
80
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.
109
106
scatter_hist (x , y , ax , ax_histx , ax_histy )
110
107
111
108
plt .show ()
@@ -118,8 +115,8 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
118
115
# The use of the following functions, methods, classes and modules is shown
119
116
# in this example:
120
117
#
121
- # - `matplotlib.figure.Figure.add_axes`
122
118
# - `matplotlib.figure.Figure.add_subplot`
123
119
# - `matplotlib.figure.Figure.add_gridspec`
120
+ # - `matplotlib.axes.Axes.inset_axes`
124
121
# - `matplotlib.axes.Axes.scatter`
125
122
# - `matplotlib.axes.Axes.hist`
0 commit comments