|
2 | 2 | Scatter plot with histograms
|
3 | 3 | ============================
|
4 | 4 |
|
5 |
| -To create a scatter plot with histograms at the sides of the plot one |
6 |
| -can use :meth:`pygmt.Figure.plot` in combination with |
7 |
| -:meth:`pygmt.Figure.histogram`. The positions of the histograms are plotted |
8 |
| -by offsetting them from the main scatter plot figure using |
9 |
| -:meth:`pygmt.Figure.shift_origin`. |
| 5 | +To create a scatter plot with histograms at the sides of the plot one can use |
| 6 | +:meth:`pygmt.Figure.plot` in combination with :meth:`pygmt.Figure.histogram`. The |
| 7 | +positions of the histograms are plotted by offsetting them from the main scatter plot |
| 8 | +using :meth:`pygmt.Figure.shift_origin`. |
10 | 9 | """
|
11 | 10 |
|
12 | 11 | # %%
|
13 | 12 | import numpy as np
|
14 | 13 | import pygmt
|
15 | 14 |
|
16 |
| -# Generate random data from a standard normal distribution centered on 0 |
17 |
| -# with a standard deviation of 1 |
18 |
| -rng = np.random.default_rng(seed=19680801) |
| 15 | +# Generate random x, y coordinates from a standard normal distribution. |
| 16 | +# x values are centered on 0 with a standard deviation of 1, and y values are centered |
| 17 | +# on 30 with a standard deviation of 2. |
| 18 | +rng = np.random.default_rng() |
19 | 19 | x = rng.normal(loc=0, scale=1, size=1000)
|
20 |
| -y = rng.normal(loc=0, scale=1, size=1000) |
| 20 | +y = rng.normal(loc=30, scale=2, size=1000) |
21 | 21 |
|
22 |
| -# Get axis limits |
23 |
| -xymax = max(np.max(np.abs(x)), np.max(np.abs(y))) |
| 22 | +# Get axis limits from the data limits. Extend the limits by 0.5 to add some margin. |
| 23 | +xmin = np.floor(x.min()) - 0.5 |
| 24 | +xmax = np.ceil(x.max()) + 0.5 |
| 25 | +ymin = np.floor(y.min()) - 0.5 |
| 26 | +ymax = np.ceil(y.max()) + 0.5 |
24 | 27 |
|
| 28 | +# Set fill color for symbols and bars. |
| 29 | +fill = "seagreen" |
| 30 | + |
| 31 | +# Set the dimensions of the scatter plot. |
| 32 | +width, height = 10, 8 |
25 | 33 |
|
26 | 34 | fig = pygmt.Figure()
|
27 | 35 | fig.basemap(
|
28 |
| - region=[-xymax - 0.5, xymax + 0.5, -xymax - 0.5, xymax + 0.5], |
29 |
| - projection="X10c/10c", |
30 |
| - frame=["WSrt", "a1"], |
| 36 | + region=[xmin, xmax, ymin, ymax], |
| 37 | + projection=f"X{width}/{height}", |
| 38 | + frame=["WSrt", "af"], |
31 | 39 | )
|
32 | 40 |
|
33 |
| -fillcol = "seagreen" |
34 |
| - |
35 |
| -# Plot data points as circles with a diameter of 0.15 centimeters |
36 |
| -fig.plot(x=x, y=y, style="c0.15c", fill=fillcol, transparency=50) |
| 41 | +# Plot data points as circles with a diameter of 0.15 centimeters and set transparency |
| 42 | +# level for all circles to deal with overplotting. |
| 43 | +fig.plot(x=x, y=y, style="c0.15c", fill=fill, transparency=50) |
37 | 44 |
|
38 |
| -# Shift the plot origin and add top margin histogram |
39 |
| -fig.shift_origin(yshift="10.25c") |
| 45 | +# Shift the plot origin and add top margin histogram. |
| 46 | +fig.shift_origin(yshift=height + 0.25) |
40 | 47 |
|
41 | 48 | fig.histogram(
|
42 |
| - projection="X10c/2c", |
43 |
| - frame=["Wsrt", "xf1", "y+lCounts"], |
44 |
| - # Give the same value for ymin and ymax to have ymin and ymax |
45 |
| - # calculated automatically |
46 |
| - region=[-xymax - 0.5, xymax + 0.5, 0, 0], |
| 49 | + projection=f"X{width}/3", |
| 50 | + frame=["Wsrt", "xf", "yaf+lCounts"], |
| 51 | + # Give the same value for ymin and ymax to have them calculated automatically. |
| 52 | + region=[xmin, xmax, 0, 0], |
47 | 53 | data=x,
|
48 |
| - fill=fillcol, |
| 54 | + fill=fill, |
49 | 55 | pen="0.1p,white",
|
50 | 56 | histtype=0,
|
51 |
| - series=0.1, |
| 57 | + series=0.2, |
52 | 58 | )
|
53 | 59 |
|
54 |
| -# Shift the plot origin and add right margin histogram |
55 |
| -fig.shift_origin(yshift="-10.25c", xshift="10.25c") |
| 60 | +# Shift the plot origin and add right margin histogram. |
| 61 | +fig.shift_origin(yshift=-height - 0.25, xshift=width + 0.25) |
56 | 62 |
|
| 63 | +# Plot the horizontal histogram. |
57 | 64 | fig.histogram(
|
58 | 65 | horizontal=True,
|
59 |
| - projection="X2c/10c", |
60 |
| - # Note that the y-axis annotation "Counts" is shown in x-axis direction |
61 |
| - # due to the rotation caused by horizontal=True |
62 |
| - frame=["wSrt", "xf1", "y+lCounts"], |
63 |
| - region=[-xymax - 0.5, xymax + 0.5, 0, 0], |
| 66 | + projection=f"X3/{height}", |
| 67 | + # Note that the x- and y-axis are flipped, with the y-axis plotted horizontally. |
| 68 | + frame=["wSrt", "xf", "yaf+lCounts"], |
| 69 | + region=[ymin, ymax, 0, 0], |
64 | 70 | data=y,
|
65 |
| - fill=fillcol, |
| 71 | + fill=fill, |
66 | 72 | pen="0.1p,white",
|
67 | 73 | histtype=0,
|
68 |
| - series=0.1, |
| 74 | + series=0.2, |
69 | 75 | )
|
70 |
| - |
71 | 76 | fig.show()
|
0 commit comments