Skip to content

Commit 311b951

Browse files
DOC/advanced Tutorial "Cartesian histograms": General improvements (#3626)
1 parent 5522814 commit 311b951

File tree

1 file changed

+46
-60
lines changed

1 file changed

+46
-60
lines changed

examples/tutorials/advanced/cartesian_histograms.py

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Cartesian histograms
33
====================
44
5-
Cartesian histograms can be generated using the :meth:`pygmt.Figure.histogram`
6-
method. In this tutorial, different histogram related aspects are addressed:
5+
Cartesian histograms can be generated using the :meth:`pygmt.Figure.histogram` method.
6+
In this tutorial, different histogram related aspects are addressed:
77
88
- Using vertical and horizontal bars
99
- Using stair-steps
@@ -38,39 +38,36 @@
3838
# Vertical and horizontal bars
3939
# ----------------------------
4040
#
41-
# To define the width of the bins, the ``series`` parameter has to be
42-
# specified. The bars can be filled via the ``fill`` parameter with either a
43-
# color or a pattern (see later in this tutorial). Use the ``pen`` parameter
44-
# to adjust width, color, and style of the outlines. By default, a histogram
45-
# with vertical bars is created. Horizontal bars can be achieved via
46-
# ``horizontal=True``.
47-
48-
# Create new figure instance
41+
# To define the width of the bins, the ``series`` parameter has to be specified. The
42+
# bars can be filled via the ``fill`` parameter with either a color or a pattern (see
43+
# later in this tutorial). Use the ``pen`` parameter to adjust width, color, and style
44+
# of the outlines. By default, a histogram with vertical bars is created. Horizontal
45+
# bars can be achieved via ``horizontal=True``.
46+
4947
fig = pygmt.Figure()
5048

5149
# Create histogram for data01 with vertical bars
5250
fig.histogram(
5351
# Define the plot range as a list of xmin, xmax, ymin, ymax
54-
# Let ymin and ymax determined automatically by setting both to the same
55-
# value
52+
# Let ymin and ymax determined automatically by setting both to the same value
5653
region=[0, 200, 0, 0],
5754
projection="X10c", # Cartesian projection with a width of 10 centimeters
58-
# Add frame, annotations (a), ticks (f), and y-axis label (+l) "Counts"
59-
# The numbers give the steps of annotations and ticks
55+
# Add frame, annotations ("a"), ticks ("f"), and y-axis label ("+l") "Counts"; the
56+
# numbers give the steps of annotations and ticks
6057
frame=["WStr", "xaf10", "ya1f1+lCounts"],
6158
data=data01,
6259
# Set the bin width via the "series" parameter
6360
series=10,
6461
# Fill the bars with color "red3"
6562
fill="red3",
66-
# Draw a 1-point thick solid outline in "darkgray" around the bars
63+
# Draw a 1-point thick, solid outline in "darkgray" around the bars
6764
pen="1p,darkgray,solid",
6865
# Choose counts via the "histtype" parameter
6966
histtype=0,
7067
)
7168

72-
# Shift plot origin 12 centimeters to the right
73-
fig.shift_origin(xshift="12c")
69+
# Shift plot origin by the figure width ("w") plus 2 centimeters to the right
70+
fig.shift_origin(xshift="w+2c")
7471

7572
# Create histogram for data01 with horizontal bars
7673
fig.histogram(
@@ -83,8 +80,8 @@
8380
pen="1p,darkgray,solid",
8481
histtype=0,
8582
# Use horizontal bars
86-
# Please note the flip of the x and y axes regarding annotations, ticks,
87-
# gridlines, and axis labels
83+
# Please note the flip of the x and y axes regarding annotations, ticks, gridlines,
84+
# and labels
8885
horizontal=True,
8986
)
9087

@@ -95,10 +92,9 @@
9592
# Stair-steps
9693
# -----------
9794
#
98-
# A stair-step diagram can be created by setting ``stairs=True``. Then only
99-
# the outer outlines of the bars are drawn, and no internal bars are visible.
95+
# A stair-step diagram can be created by setting ``stairs=True``. Then only the
96+
# outer outlines of the bars are drawn, and no internal bars are visible.
10097

101-
# Create new figure instance
10298
fig = pygmt.Figure()
10399

104100
# Create histogram for data01
@@ -108,15 +104,14 @@
108104
frame=["WSne", "xaf10", "ya1f1+lCounts"],
109105
data=data01,
110106
series=10,
111-
# Draw a 1-point thick dotted outline in "red3"
107+
# Draw a 1-point thick, dotted outline in "red3"
112108
pen="1p,red3,dotted",
113109
histtype=0,
114110
# Draw stair-steps in stead of bars
115111
stairs=True,
116112
)
117113

118-
# Shift plot origin 12 centimeters to the right
119-
fig.shift_origin(xshift="12c")
114+
fig.shift_origin(xshift="w+2c")
120115

121116
# Create histogram for data02
122117
fig.histogram(
@@ -125,7 +120,7 @@
125120
frame=["WSne", "xaf10", "ya1f1+lCounts"],
126121
data=data02,
127122
series=10,
128-
# Draw a 1.5-point thick dashed outline in "orange"
123+
# Draw a 1.5-points thick, dashed outline in "orange"
129124
pen="1.5p,orange,dashed",
130125
histtype=0,
131126
stairs=True,
@@ -138,12 +133,10 @@
138133
# Counts and frequency percent
139134
# ----------------------------
140135
#
141-
# By default, a histogram showing the counts in each bin is created
142-
# (``histtype=0``). To show the frequency percent set the ``histtpye``
143-
# parameter to ``1``. For further options please have a look at the
144-
# documentation of :meth:`pygmt.Figure.histogram`.
136+
# By default, a histogram showing the counts in each bin is created (``histtype=0``).
137+
# To show the frequency percent set the ``histtpye`` parameter to ``1``. For further
138+
# options please have a look at the documentation of :meth:`pygmt.Figure.histogram`.
145139

146-
# Create new figure instance
147140
fig = pygmt.Figure()
148141

149142
# Create histogram for data02 showing counts
@@ -159,8 +152,7 @@
159152
histtype=0,
160153
)
161154

162-
# Shift plot origin 11 centimeters to the right
163-
fig.shift_origin(xshift="11c")
155+
fig.shift_origin(xshift="w+1c")
164156

165157
# Create histogram for data02 showing frequency percent
166158
fig.histogram(
@@ -183,12 +175,11 @@
183175
# Cumulative values
184176
# -----------------
185177
#
186-
# To create a histogram showing the cumulative values set ``cumulative=True``.
187-
# Here, the bars of the cumulative histogram are filled with a pattern via
188-
# the ``fill`` parameter. Annotate each bar with the counts it represents
189-
# using the ``annotate`` parameter.
178+
# To create a histogram showing the cumulative values set ``cumulative=True``. Here,
179+
# the bars of the cumulative histogram are filled with a pattern via the ``fill``
180+
# parameter. Annotate each bar with the counts it represents using the ``annotate``
181+
# parameter.
190182

191-
# Create new figure instance
192183
fig = pygmt.Figure()
193184

194185
# Create histogram for data01 showing the counts per bin
@@ -205,8 +196,7 @@
205196
annotate=True,
206197
)
207198

208-
# Shift plot origin 11 centimeters to the right
209-
fig.shift_origin(xshift="11c")
199+
fig.shift_origin(xshift="w+1c")
210200

211201
# Create histogram for data01 showing the cumulative counts
212202
fig.histogram(
@@ -215,15 +205,15 @@
215205
frame=["wSnE", "xaf10", "ya5f1+lCumulative counts"],
216206
data=data01,
217207
series=10,
218-
# Use pattern (p) number 8 as fill for the bars
219-
# Set the background (+b) to white [Default]
220-
# Set the foreground (+f) to black [Default]
208+
# Use pattern ("p") number 8 as fill for the bars
209+
# Set the background ("+b") to white [Default]
210+
# Set the foreground ("+f") to black [Default]
221211
fill="p8+bwhite+fblack",
222212
pen="1p,darkgray,solid",
223213
histtype=0,
224214
# Show cumulative counts
225215
cumulative=True,
226-
# Offset (+o) the label by 10 points in negative y-direction
216+
# Offset ("+o") the label by 10 points in negative y-direction
227217
annotate="+o-10p",
228218
)
229219

@@ -234,17 +224,16 @@
234224
# Overlaid bars
235225
# -------------
236226
#
237-
# Overlaid or overlapping bars can be achieved by plotting two or several
238-
# histograms, each for one data set, on top of each other. The legend entry
239-
# can be specified via the ``label`` parameter.
227+
# Overlaid or overlapping bars can be achieved by plotting two or several histograms,
228+
# each for one data set, on top of each other. The legend entry can be specified via
229+
# the ``label`` parameter.
240230
#
241231
# Limitations of histograms with overlaid bars are:
242232
#
243233
# - Mixing of colors or/and patterns
244234
# - Visually more colors or/and patterns than data sets
245235
# - Visually a "third histogram" (or more in case of more than two data sets)
246236

247-
# Create new figure instance
248237
fig = pygmt.Figure()
249238

250239
# Create histogram for data01
@@ -283,10 +272,10 @@
283272
# Stacked bars
284273
# ------------
285274
#
286-
# Histograms with stacked bars are not directly supported by PyGMT. Thus,
287-
# before plotting, combined data sets have to be created from the single data
288-
# sets. Then, stacked bars can be achieved similar to overlaid bars via
289-
# plotting two or several histograms on top of each other.
275+
# Histograms with stacked bars are not directly supported by PyGMT. Thus, before
276+
# plotting, combined data sets have to be created from the single data sets. Then,
277+
# stacked bars can be achieved similar to overlaid bars via plotting two or several
278+
# histograms on top of each other.
290279
#
291280
# Limitations of histograms with stacked bars are:
292281
#
@@ -296,7 +285,6 @@
296285
# Combine the two data sets to one data set
297286
data_merge = np.concatenate((data01, data02), axis=None)
298287

299-
# Create new figure instance
300288
fig = pygmt.Figure()
301289

302290
# Create histogram for data02 by using the combined data set
@@ -309,8 +297,7 @@
309297
fill="orange",
310298
pen="1p,darkgray,solid",
311299
histtype=0,
312-
# The combined data set appears in the final histogram visually
313-
# as data set data02
300+
# The combined data set appears in the final histogram visually as data set data02
314301
label="data02",
315302
)
316303

@@ -346,7 +333,6 @@
346333
# Width used for binning the data
347334
binwidth = 10
348335

349-
# Create new figure instance
350336
fig = pygmt.Figure()
351337

352338
# Create histogram for data01
@@ -359,10 +345,10 @@
359345
fill="red3",
360346
pen="1p,darkgray,solid",
361347
histtype=0,
362-
# Calculate the bar width in respect to the bin width, here for two
363-
# data sets half of the bin width
364-
# Offset (+o) the bars to align each bar with the left limit of the
365-
# corresponding bin
348+
# Calculate the bar width in respect to the bin width, here for two data sets half
349+
# of the bin width
350+
# Offset ("+o") the bars to align each bar with the left limit of the corresponding
351+
# bin
366352
barwidth=f"{binwidth/2}+o-{binwidth/4}",
367353
label="data01",
368354
)

0 commit comments

Comments
 (0)