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 scatterplot 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
+ * the axes positions are defined via a gridspec;
13
+ * the axes positions are defined via a inset_axes.
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
19
toolkit is shown in the
17
- :doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` example.
20
+ :doc:`/gallery/axes_grid1/scatter_hist_locatable_axes` example. Finally, it is
21
+ also possible to simply position all axes in absolute coordinates using
22
+ `.Figure.add_axes` (not shown here).
18
23
19
24
Let us first define a function that takes x and y data as input, as well
20
25
as three axes, the main axes for the scatter, and two marginal axes. It will
@@ -50,62 +55,54 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
50
55
ax_histy .hist (y ,bins = bins ,orientation = 'horizontal' )
51
56
52
57
53
- #############################################################################
54
- #
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
58
#############################################################################
86
59
#
87
60
# Using a gridspec
88
61
# ----------------
89
62
#
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 ))
63
+ # We define a gridspec with unequal width- and height-ratios to achieve desired
64
+ # layout. Also see the :doc:`/tutorials/intermediate/gridspec` tutorial.
96
65
66
+ # Start with a square Figure.
67
+ fig = plt .figure (figsize = (6 ,6 ))
97
68
# Add a gridspec with two rows and two columns and a ratio of 2 to 7 between
98
69
# the size of the marginal axes and the main axes in both directions.
99
70
# Also adjust the subplot parameters for a square plot.
100
71
gs = fig .add_gridspec (2 ,2 ,width_ratios = (7 ,2 ),height_ratios = (2 ,7 ),
101
72
left = 0.1 ,right = 0.9 ,bottom = 0.1 ,top = 0.9 ,
102
73
wspace = 0.05 ,hspace = 0.05 )
103
-
74
+ # Create the Axes.
104
75
ax = fig .add_subplot (gs [1 ,0 ])
105
76
ax_histx = fig .add_subplot (gs [0 ,0 ],sharex = ax )
106
77
ax_histy = fig .add_subplot (gs [1 ,1 ],sharey = ax )
78
+ # Draw the scatter plot and marginals.
79
+ scatter_hist (x ,y ,ax ,ax_histx ,ax_histy )
107
80
108
- # use the previously defined function
81
+
82
+ #############################################################################
83
+ #
84
+ # Using inset_axes
85
+ # ----------------
86
+ #
87
+ # Despite its name, `~.Axes.inset_axes` can also be used to position marginals
88
+ # *outside* the main axes. The advantage of doing so is that the aspect ratio
89
+ # of the main axes can be fixed, regardless of the figure size.
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 ()
@@ -121,5 +118,6 @@ def scatter_hist(x, y, ax, ax_histx, ax_histy):
121
118
# - `matplotlib.figure.Figure.add_axes`
122
119
# - `matplotlib.figure.Figure.add_subplot`
123
120
# - `matplotlib.figure.Figure.add_gridspec`
121
+ # - `matplotlib.axes.Axes.inset_axes`
124
122
# - `matplotlib.axes.Axes.scatter`
125
123
# - `matplotlib.axes.Axes.hist`