8000 Doc: Scatter Hist example update · matplotlib/matplotlib@4e63c18 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e63c18

Browse files
Doc: Scatter Hist example update
1 parent b34c605 commit 4e63c18

File tree

3 files changed

+135
-31
lines changed

3 files changed

+135
-31
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ per-file-ignores =
7070

7171
examples/animation/frame_grabbing_sgskip.py: E402
7272
examples/axes_grid1/inset_locator_demo.py: E402
73+
examples/axes_grid1/scatter_hist_locatable_axes.py: E401, E402
7374
examples/axisartist/demo_curvelinear_grid.py: E402
7475
examples/color/color_by_yvalue.py: E402
7576
examples/color/color_cycle_default.py: E402
@@ -127,6 +128,7 @@ per-file-ignores =
127128
examples/lines_bars_and_markers/filled_step.py: E402
128129
examples/lines_bars_and_markers/horizontal_barchart_distribution.py: E402
129130
examples/lines_bars_and_markers/joinstyle.py: E402
131+
examples/lines_bars_and_markers/scatter_hist.py: E402
130132
examples/lines_bars_and_markers/scatter_piecharts.py: E402
131133
examples/lines_bars_and_markers/scatter_with_legend.py: E402
132134
examples/lines_bars_and_markers/span_regions.py: E402

examples/axes_grid1/scatter_hist_locatable_axes.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
"""
2-
============
3-
Scatter Hist
4-
============
2+
==================================
3+
Scatter Histogram (Locatable Axes)
4+
==================================
55
6+
Show the marginal distributions of a scatter as histograms at the sides of
7+
the plot.
8+
9+
For a nice alignment of the main axes with the marginals, the axes positions
10+
are defined by a ``Divider``, produced via `make_axes_locatable`.
11+
12+
An alternative method to produce a similar figure is shown in the
13+
:doc:`/gallery/lines_bars_and_markers/scatter_hist` example. The advantage of
14+
the locatable axes method shown below is that the marginal axes follow the
15+
fixed aspect ratio of the main axes.
616
"""
17+
718
import numpy as np
819
import matplotlib.pyplot as plt
920
from mpl_toolkits.axes_grid1 import make_axes_locatable
1021

1122
# Fixing random state for reproducibility
1223
np.random.seed(19680801)
1324

14-
1525
# the random data
1626
x = np.random.randn(1000)
1727
y = np.random.randn(1000)
@@ -21,14 +31,16 @@
2131

2232
# the scatter plot:
2333
axScatter.scatter(x, y)
34+
35+
# Set aspect of the main axes.
2436
axScatter.set_aspect(1.)
2537

2638
# create new axes on the right and on the top of the current axes
27-
# The first argument of the new_vertical(new_horizontal) method is
28-
# the height (width) of the axes to be created in inches.
2939
divider = make_axes_locatable(axScatter)
30-
axHistx = divider.append_axes("top", 1.2, pad=0.1, sharex=axScatter)
31-
axHisty = divider.append_axes("right", 1.2, pad=0.1, sharey=axScatter)
40+
axHistx = divider.append_axes("top", 1.2, pad=0.1, # height and pad in inches
41+
sharex=axScatter)
42+
axHisty = divider.append_axes("right", 1.2, pad=0.1, # width and pad in inches
43+
sharey=axScatter)
3244

3345
# make some labels invisible
3446
axHistx.xaxis.set_tick_params(labelbottom=False)
@@ -48,7 +60,22 @@
4860
# axis.
4961

5062
axHistx.set_yticks([0, 50, 100])
51-
5263
axHisty.set_xticks([0, 50, 100])
5364

5465
plt.show()
66+
67+
#############################################################################
68+
#
69+
# ------------
70+
#
71+
# References
72+
# """"""""""
73+
#
74+
# The use of the following functions, methods and classes is shown
75+
# in this example:
76+
77+
import matplotlib, mpl_toolkits
78+
mpl_toolkits.axes_grid1.make_axes_locatable
79+
matplotlib.axes.Axes.set_aspect
80+
matplotlib.axes.Axes.scatter
81+
matplotlib.axes.Axes.hist

examples/lines_bars_and_markers/scatter_hist.py

Lines changed: 97 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,62 @@
33
Scatter plot with histograms
44
============================
55
6-
Create a scatter plot with histograms to its sides.
6+
Show the marginal distributions of a scatter as histograms at the sides of
7+
the plot.
8+
9+
For a nice alignment of the main axes with the marginals, two options are shown
10+
below.
11+
12+
* the axes positions are defined in terms of rectangles in figure coordinates
13+
* the axes positions are defined via a gridspec
14+
15+
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.
18+
19+
Let us first define a function that takes x and y data as input, as well
20+
as three axes, the main axes for the scatter, and two marginal axes. It will
21+
then create the scatter and histograms inside the provided axes.
722
"""
23+
824
import numpy as np
925
import matplotlib.pyplot as plt
1026

1127
# Fixing random state for reproducibility
1228
np.random.seed(19680801)
1329

14-
# the random data
30+
# some random data
1531
x = np.random.randn(1000)
1632
y = np.random.randn(1000)
1733

34+
35+
def scatter_hist(x, y, axScatter, axHistx, axHisty):
36+
# no labels
37+
axHistx.tick_params(axis="x", labelbottom=False)
38+
axHisty.tick_params(axis="y", labelleft=False)
39+
40+
# the scatter plot:
41+
axScatter.scatter(x, y)
42+
43+
# now determine nice limits by hand:
44+
binwidth = 0.25
45+
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
46+
lim = (int(xymax/binwidth) + 1) * binwidth
47+
48+
bins = np.arange(-lim, lim + binwidth, binwidth)
49+
axHistx.hist(x, bins=bins)
50+
axHisty.hist(y, bins=bins, orientation='horizontal')
51+
52+
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+
1862
# definitions for the axes
1963
left, width = 0.1, 0.65
2064
bottom, height = 0.1, 0.65
@@ -25,30 +69,61 @@
2569
rect_histx = [left, bottom + height + spacing, width, 0.2]
2670
rect_histy = [left + width + spacing, bottom, 0.2, height]
2771

28-
# start with a rectangular Figure
29-
plt.figure(figsize=(8, 8))
72+
# start with a square Figure
73+
fig = plt.figure(figsize=(8, 8))
74+
75+
axScatter = fig.add_axes(rect_scatter)
76+
axHistx = fig.add_axes(rect_histx, sharex=axScatter)
77+
axHisty = fig.add_axes(rect_histy, sharey=axScatter)
78+
79+
# use the previously defined function
80+
scatter_hist(x, y, axScatter, axHistx, axHisty)
81+
82+
plt.show()
83+
3084

31-
ax_scatter = plt.axes(rect_scatter)
32-
ax_scatter.tick_params(direction='in', top=True, right=True)
33-
ax_histx = plt.axes(rect_histx)
34-
ax_histx.tick_params(direction='in', labelbottom=False)
35-
ax_histy = plt.axes(rect_histy)
36-
ax_histy.tick_params(direction='in', labelleft=False)
85+
#############################################################################
86+
#
87+
# Using a gridspec
88+
# ----------------
89+
#
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.
3793

38-
# the scatter plot:
39-
ax_scatter.scatter(x, y)
94+
# start with a square Figure
95+
fig = plt.figure(figsize=(8, 8))
4096

41-
# now determine nice limits by hand:
42-
binwidth = 0.25
43-
lim = np.ceil(np.abs([x, y]).max() / binwidth) * binwidth
44-
ax_scatter.set_xlim((-lim, lim))
45-
ax_scatter.set_ylim((-lim, lim))
97+
# Add a gridspec with two rows and two columns and a ratio of 2 to 7 between
98+
# the size of the marginal axes and the main axes in both directions.
99+
# Also adjust the subplot parameters for a square plot.
100+
gs = fig.add_gridspec(2, 2, width_ratios=(7, 2), height_ratios=(2, 7),
101+
left=0.1, right=0.9, bottom=0.1, top=0.9,
102+
wspace=0.05, hspace=0.05)
46103

47-
bins = np.arange(-lim, lim + binwidth, binwidth)
48-
ax_histx.hist(x, bins=bins)
49-
ax_histy.hist(y, bins=bins, orientation='horizontal')
104+
axScatter = fig.add_subplot(gs[1, 0])
105+
axHistx = fig.add_subplot(gs[0, 0], sharex=axScatter)
106+
axHisty = fig.add_subplot(gs[1, 1], sharey=axScatter)
50107

51-
ax_histx.set_xlim(ax_scatter.get_xlim())
52-
ax_histy.set_ylim(ax_scatter.get_ylim())
108+
# use the previously defined function
109+
scatter_hist(x, y, axScatter, axHistx, axHisty)
53110

54111
plt.show()
112+
113+
114+
#############################################################################
115+
#
116+
# ------------
117+
#
118+
# References
119+
# """"""""""
120+
#
121+
# The use of the following functions, methods and classes is shown
122+
# in this example:
123+
124+
import matplotlib
125+
matplotlib.figure.Figure.add_axes
126+
matplotlib.figure.Figure.add_subplot
127+
matplotlib.figure.Figure.add_gridspec
128+
matplotlib.axes.Axes.scatter
129+
matplotlib.axes.Axes.hist

0 commit comments

Comments
 (0)
0