|
1 | 1 | """
|
2 |
| -============ |
3 |
| -Scatter Hist |
4 |
| -============ |
5 |
| -
|
6 |
| -Creates histogram from scatter plot |
7 |
| -and adds them to the sides of the plot. |
| 2 | +============================ |
| 3 | +Scatter plot with histograms |
| 4 | +============================ |
8 | 5 |
|
| 6 | +Create a scatter plot with histograms to its sides. |
9 | 7 | """
|
10 | 8 | import numpy as np
|
11 | 9 | import matplotlib.pyplot as plt
|
12 |
| -from matplotlib.ticker import NullFormatter |
13 | 10 |
|
14 | 11 | # Fixing random state for reproducibility
|
15 | 12 | np.random.seed(19680801)
|
16 | 13 |
|
17 |
| - |
18 | 14 | # the random data
|
19 | 15 | x = np.random.randn(1000)
|
20 | 16 | y = np.random.randn(1000)
|
21 | 17 |
|
22 |
| -nullfmt = NullFormatter() # no labels |
23 |
| - |
24 | 18 | # definitions for the axes
|
25 | 19 | left, width = 0.1, 0.65
|
26 | 20 | bottom, height = 0.1, 0.65
|
27 |
| -bottom_h = left_h = left + width + 0.02 |
| 21 | +spacing = 0.005 |
| 22 | + |
28 | 23 |
|
29 | 24 | rect_scatter = [left, bottom, width, height]
|
30 |
| -rect_histx = [left, bottom_h, width, 0.2] |
31 |
| -rect_histy = [left_h, bottom, 0.2, height] |
| 25 | +rect_histx = [left, bottom + height + spacing, width, 0.2] |
| 26 | +rect_histy = [left + width + spacing, bottom, 0.2, height] |
32 | 27 |
|
33 | 28 | # start with a rectangular Figure
|
34 | 29 | plt.figure(figsize=(8, 8))
|
35 | 30 |
|
36 |
| -axScatter = plt.axes(rect_scatter) |
37 |
| -axHistx = plt.axes(rect_histx) |
38 |
| -axHisty = plt.axes(rect_histy) |
39 |
| - |
40 |
| -# no labels |
41 |
| -axHistx.xaxis.set_major_formatter(nullfmt) |
42 |
| -axHisty.yaxis.set_major_formatter(nullfmt) |
| 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) |
43 | 37 |
|
44 | 38 | # the scatter plot:
|
45 |
| -axScatter.scatter(x, y) |
| 39 | +ax_scatter.scatter(x, y) |
46 | 40 |
|
47 | 41 | # now determine nice limits by hand:
|
48 | 42 | binwidth = 0.25
|
49 |
| -xymax = max(np.max(np.abs(x)), np.max(np.abs(y))) |
50 |
| -lim = (int(xymax/binwidth) + 1) * binwidth |
51 |
| - |
52 |
| -axScatter.set_xlim((-lim, lim)) |
53 |
| -axScatter.set_ylim((-lim, lim)) |
| 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)) |
54 | 46 |
|
55 | 47 | bins = np.arange(-lim, lim + binwidth, binwidth)
|
56 |
| -axHistx.hist(x, bins=bins) |
57 |
| -axHisty.hist(y, bins=bins, orientation='horizontal') |
| 48 | +ax_histx.hist(x, bins=bins) |
| 49 | +ax_histy.hist(y, bins=bins, orientation='horizontal') |
58 | 50 |
|
59 |
| -axHistx.set_xlim(axScatter.get_xlim()) |
60 |
| -axHisty.set_ylim(axScatter.get_ylim()) |
| 51 | +ax_histx.set_xlim(ax_scatter.get_xlim()) |
| 52 | +ax_histy.set_ylim(ax_scatter.get_ylim()) |
61 | 53 |
|
62 | 54 | plt.show()
|
0 commit comments