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

Skip to content

Commit 3aefb71

Browse files
Doc: Scatter Hist example update
1 parent af80d57 commit 3aefb71

File tree

3 files changed

+134
-37
lines changed

3 files changed

+134
-37
lines changed

.flake8

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

8787
examples/animation/frame_grabbing_sgskip.py: E402
8888
examples/axes_grid1/inset_locator_demo.py: E402
89+
examples/axes_grid1/scatter_hist_locatable_axes.py: E401, E402
8990
examples/axisartist/demo_curvelinear_grid.py: E402
9091
examples/color/color_by_yvalue.py: E402
9192
examples/color/color_cycle_default.py: E402
@@ -140,6 +141,7 @@ per-file-ignores =
140141
examples/lines_bars_and_markers/fill_between_demo.py: E402
141142
examples/lines_bars_and_markers/filled_step.py: E402
142143
examples/lines_bars_and_markers/joinstyle.py: E402
144+
examples/lines_bars_and_markers/scatter_hist.py: E402
143145
examples/lines_bars_and_markers/scatter_piecharts.py: E402
144146
examples/lines_bars_and_markers/scatter_with_legend.py: E402
145147
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 Hist (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: 96 additions & 28 deletions
Original file li 9E7A ne numberDiff line numberDiff line change
@@ -3,23 +3,63 @@
33
Scatter Hist
44
============
55
6-
Creates histogram from scatter plot
7-
and adds them to the sides of the plot.
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.
818
919
"""
20+
21+
#############################################################################
22+
# Let us first define a function that takes x and y data as input, as well
23+
# as three axes, the main axes for the scatter, and two marginal axes. It will
24+
# then create the scatter and histograms inside the provided axes.
25+
1026
import numpy as np
1127
import matplotlib.pyplot as plt
12-
from matplotlib.ticker import NullFormatter
1328

1429
# Fixing random state for reproducibility
1530
np.random.seed(19680801)
16-
17-
18-
# the random data
31+
# some random data
1932
x = np.random.randn(1000)
2033
y = np.random.randn(1000)
2134

22-
nullfmt = NullFormatter() # no labels
35+
36+
def scatter_hist(x, y, axScatter, axHistx, axHisty):
37+
38+
# no labels
39+
axHistx.tick_params(axis="x", labelbottom=False)
40+
axHisty.tick_params(axis="y", labelleft=False)
41+
42+
# the scatter plot:
43+
axScatter.scatter(x, y)
44+
45+
# now determine nice limits by hand:
46+
binwidth = 0.25
47+
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
48+
lim = (int(xymax/binwidth) + 1) * binwidth
49+
50+
bins = np.arange(-lim, lim + binwidth, binwidth)
51+
axHistx.hist(x, bins=bins)
52+
axHisty.hist(y, bins=bins, orientation='horizontal')
53+
54+
55+
#############################################################################
56+
#
57+
# Axes in figure coordinates
58+
# --------------------------
59+
#
60+
# To define the axes positions `Figure.add_axes` is provided with a rectangle
61+
# ``[left, bottom, width, height]`` in figure coordinates. The marginal axes
62+
# share one dimension with the main axes.
2363

2464
# definitions for the axes
2565
left, width = 0.1, 0.65
@@ -30,33 +70,61 @@
3070
rect_histx = [left, bottom_h, width, 0.2]
3171
rect_histy = [left_h, bottom, 0.2, height]
3272

33-
# start with a rectangular Figure
34-
plt.figure(figsize=(8, 8))
73+
# start with a rectangular Figure and add axes to it
74+
fig = plt.figure(figsize=(8, 8))
75+
76+
axScatter = fig.add_axes(rect_scatter)
77+
axHistx = fig.add_axes(rect_histx, sharex=axScatter)
78+
axHisty = fig.add_axes(rect_histy, sharey=axScatter)
3579

36-
axScatter = plt.axes(rect_scatter)
37-
axHistx = plt.axes(rect_histx)
38-
axHisty = plt.axes(rect_histy)
80+
# call the function
81+
scatter_hist(x, y, axScatter, axHistx, axHisty)
82+
83+
plt.show()
3984

40-
# no labels
41-
axHistx.xaxis.set_major_formatter(nullfmt)
42-
axHisty.yaxis.set_major_formatter(nullfmt)
4385

44-
# the scatter plot:
45-
axScatter.scatter(x, y)
86+
#############################################################################
87+
#
88+
# Using a gridspec
89+
# ----------------
90+
#
91+
# We may equally define a gridspec with unequal width- and height-ratios to
92+
# achieve desired layout. Also see the :doc:`/tutorials/intermediate/gridspec`
93+
# tutorial.
4694

47-
# now determine nice limits by hand:
48-
binwidth = 0.25
49-
xymax = max(np.max(np.abs(x)), np.max(np.abs(y)))
50-
lim = (int(xymax/binwidth) + 1) * binwidth
95+
# start with a rectangular Figure
96+
fig = plt.figure(figsize=(8, 8))
5197

52-
axScatter.set_xlim((-lim, lim))
53-
axScatter.set_ylim((-lim, lim))
98< F987 /code>+
# Add a gridspec with two rows and two columns and a ratio of 2 to 7 between
99+
# the size of the marginal axes and the main axes in both directions.
100+
# Also adjust the subplot parameters for a square plot.
101+
gs = fig.add_gridspec(2, 2, width_ratios=(7, 2), height_ratios=(2, 7),
102+
left=0.1, right=0.9, bottom=0.1, top=0.9,
103+
wspace=0.05, hspace=0.05)
54104

55-
bins = np.arange(-lim, lim + binwidth, binwidth)
56-
axHistx.hist(x, bins=bins)
57-
axHisty.hist(y, bins=bins, orientation='horizontal')
105+
axScatter = fig.add_subplot(gs[1, 0])
106+
axHistx = fig.add_subplot(gs[0, 0], sharex=axScatter)
107+
axHisty = fig.add_subplot(gs[1, 1], sharey=axScatter)
58108

59-
axHistx.set_xlim(axScatter.get_xlim())
60-
axHisty.set_ylim(axScatter.get_ylim())
109+
# call the function
110+
scatter_hist(x, y, axScatter, axHistx, axHisty)
61111

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

0 commit comments

Comments
 (0)
0