Skip to content

Commit f8e28ce

Browse files
yvonnefroehlichseismanmichaelgrund
authored
DOC/Gallery example "Scatter plot with histograms": General improvements (#3628)
Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Michael Grund <[email protected]>
1 parent 243b488 commit f8e28ce

File tree

3 files changed

+47
-42
lines changed

3 files changed

+47
-42
lines changed

examples/gallery/histograms/scatter_and_histograms.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,75 @@
22
Scatter plot with histograms
33
============================
44
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`.
109
"""
1110

1211
# %%
1312
import numpy as np
1413
import pygmt
1514

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()
1919
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)
2121

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
2427

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
2533

2634
fig = pygmt.Figure()
2735
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"],
3139
)
3240

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)
3744

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)
4047

4148
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],
4753
data=x,
48-
fill=fillcol,
54+
fill=fill,
4955
pen="0.1p,white",
5056
histtype=0,
51-
series=0.1,
57+
series=0.2,
5258
)
5359

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)
5662

63+
# Plot the horizontal histogram.
5764
fig.histogram(
5865
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],
6470
data=y,
65-
fill=fillcol,
71+
fill=fill,
6672
pen="0.1p,white",
6773
histtype=0,
68-
series=0.1,
74+
series=0.2,
6975
)
70-
7176
fig.show()

examples/tutorials/advanced/cartesian_histograms.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@
7979
fill="red3",
8080
pen="1p,darkgray,solid",
8181
histtype=0,
82-
# Use horizontal bars
83-
# Please note the flip of the x and y axes regarding annotations, ticks, gridlines,
84-
# and labels
82+
# Use horizontal bars. Note that the x- and y-axis are flipped, with the x-axis
83+
# plotted vertically and the y-axis plotted horizontally.
8584
horizontal=True,
8685
)
8786

pygmt/src/histogram.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ def histogram(self, data, **kwargs):
104104
Draw a stairs-step diagram which does not include the internal bars
105105
of the default histogram.
106106
horizontal : bool
107-
Plot the histogram using horizontal bars instead of the
108-
default vertical bars.
107+
Plot the histogram horizontally from x = 0 [Default is vertically from y = 0].
108+
The plot dimensions remain the same, but the two axes are flipped, i.e., the
109+
x-axis is plotted vertically and the y-axis is plotted horizontally.
109110
series : int, str, or list
110111
[*min*\ /*max*\ /]\ *inc*\ [**+n**\ ].
111112
Set the interval for the width of each bar in the histogram.

0 commit comments

Comments
 (0)