|
2 | 2 | Cartesian histograms |
3 | 3 | ==================== |
4 | 4 |
|
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: |
7 | 7 |
|
8 | 8 | - Using vertical and horizontal bars |
9 | 9 | - Using stair-steps |
|
38 | 38 | # Vertical and horizontal bars |
39 | 39 | # ---------------------------- |
40 | 40 | # |
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 | + |
49 | 47 | fig = pygmt.Figure() |
50 | 48 |
|
51 | 49 | # Create histogram for data01 with vertical bars |
52 | 50 | fig.histogram( |
53 | 51 | # 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 |
56 | 53 | region=[0, 200, 0, 0], |
57 | 54 | 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 |
60 | 57 | frame=["WStr", "xaf10", "ya1f1+lCounts"], |
61 | 58 | data=data01, |
62 | 59 | # Set the bin width via the "series" parameter |
63 | 60 | series=10, |
64 | 61 | # Fill the bars with color "red3" |
65 | 62 | 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 |
67 | 64 | pen="1p,darkgray,solid", |
68 | 65 | # Choose counts via the "histtype" parameter |
69 | 66 | histtype=0, |
70 | 67 | ) |
71 | 68 |
|
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") |
74 | 71 |
|
75 | 72 | # Create histogram for data01 with horizontal bars |
76 | 73 | fig.histogram( |
|
83 | 80 | pen="1p,darkgray,solid", |
84 | 81 | histtype=0, |
85 | 82 | # 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 |
88 | 85 | horizontal=True, |
89 | 86 | ) |
90 | 87 |
|
|
95 | 92 | # Stair-steps |
96 | 93 | # ----------- |
97 | 94 | # |
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. |
100 | 97 |
|
101 | | -# Create new figure instance |
102 | 98 | fig = pygmt.Figure() |
103 | 99 |
|
104 | 100 | # Create histogram for data01 |
|
108 | 104 | frame=["WSne", "xaf10", "ya1f1+lCounts"], |
109 | 105 | data=data01, |
110 | 106 | series=10, |
111 | | - # Draw a 1-point thick dotted outline in "red3" |
| 107 | + # Draw a 1-point thick, dotted outline in "red3" |
112 | 108 | pen="1p,red3,dotted", |
113 | 109 | histtype=0, |
114 | 110 | # Draw stair-steps in stead of bars |
115 | 111 | stairs=True, |
116 | 112 | ) |
117 | 113 |
|
118 | | -# Shift plot origin 12 centimeters to the right |
119 | | -fig.shift_origin(xshift="12c") |
| 114 | +fig.shift_origin(xshift="w+2c") |
120 | 115 |
|
121 | 116 | # Create histogram for data02 |
122 | 117 | fig.histogram( |
|
125 | 120 | frame=["WSne", "xaf10", "ya1f1+lCounts"], |
126 | 121 | data=data02, |
127 | 122 | series=10, |
128 | | - # Draw a 1.5-point thick dashed outline in "orange" |
| 123 | + # Draw a 1.5-points thick, dashed outline in "orange" |
129 | 124 | pen="1.5p,orange,dashed", |
130 | 125 | histtype=0, |
131 | 126 | stairs=True, |
|
138 | 133 | # Counts and frequency percent |
139 | 134 | # ---------------------------- |
140 | 135 | # |
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`. |
145 | 139 |
|
146 | | -# Create new figure instance |
147 | 140 | fig = pygmt.Figure() |
148 | 141 |
|
149 | 142 | # Create histogram for data02 showing counts |
|
159 | 152 | histtype=0, |
160 | 153 | ) |
161 | 154 |
|
162 | | -# Shift plot origin 11 centimeters to the right |
163 | | -fig.shift_origin(xshift="11c") |
| 155 | +fig.shift_origin(xshift="w+1c") |
164 | 156 |
|
165 | 157 | # Create histogram for data02 showing frequency percent |
166 | 158 | fig.histogram( |
|
183 | 175 | # Cumulative values |
184 | 176 | # ----------------- |
185 | 177 | # |
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. |
190 | 182 |
|
191 | | -# Create new figure instance |
192 | 183 | fig = pygmt.Figure() |
193 | 184 |
|
194 | 185 | # Create histogram for data01 showing the counts per bin |
|
205 | 196 | annotate=True, |
206 | 197 | ) |
207 | 198 |
|
208 | | -# Shift plot origin 11 centimeters to the right |
209 | | -fig.shift_origin(xshift="11c") |
| 199 | +fig.shift_origin(xshift="w+1c") |
210 | 200 |
|
211 | 201 | # Create histogram for data01 showing the cumulative counts |
212 | 202 | fig.histogram( |
|
215 | 205 | frame=["wSnE", "xaf10", "ya5f1+lCumulative counts"], |
216 | 206 | data=data01, |
217 | 207 | 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] |
221 | 211 | fill="p8+bwhite+fblack", |
222 | 212 | pen="1p,darkgray,solid", |
223 | 213 | histtype=0, |
224 | 214 | # Show cumulative counts |
225 | 215 | 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 |
227 | 217 | annotate="+o-10p", |
228 | 218 | ) |
229 | 219 |
|
|
234 | 224 | # Overlaid bars |
235 | 225 | # ------------- |
236 | 226 | # |
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. |
240 | 230 | # |
241 | 231 | # Limitations of histograms with overlaid bars are: |
242 | 232 | # |
243 | 233 | # - Mixing of colors or/and patterns |
244 | 234 | # - Visually more colors or/and patterns than data sets |
245 | 235 | # - Visually a "third histogram" (or more in case of more than two data sets) |
246 | 236 |
|
247 | | -# Create new figure instance |
248 | 237 | fig = pygmt.Figure() |
249 | 238 |
|
250 | 239 | # Create histogram for data01 |
|
283 | 272 | # Stacked bars |
284 | 273 | # ------------ |
285 | 274 | # |
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. |
290 | 279 | # |
291 | 280 | # Limitations of histograms with stacked bars are: |
292 | 281 | # |
|
296 | 285 | # Combine the two data sets to one data set |
297 | 286 | data_merge = np.concatenate((data01, data02), axis=None) |
298 | 287 |
|
299 | | -# Create new figure instance |
300 | 288 | fig = pygmt.Figure() |
301 | 289 |
|
302 | 290 | # Create histogram for data02 by using the combined data set |
|
309 | 297 | fill="orange", |
310 | 298 | pen="1p,darkgray,solid", |
311 | 299 | 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 |
314 | 301 | label="data02", |
315 | 302 | ) |
316 | 303 |
|
|
346 | 333 | # Width used for binning the data |
347 | 334 | binwidth = 10 |
348 | 335 |
|
349 | | -# Create new figure instance |
350 | 336 | fig = pygmt.Figure() |
351 | 337 |
|
352 | 338 | # Create histogram for data01 |
|
359 | 345 | fill="red3", |
360 | 346 | pen="1p,darkgray,solid", |
361 | 347 | 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 |
366 | 352 | barwidth=f"{binwidth/2}+o-{binwidth/4}", |
367 | 353 | label="data01", |
368 | 354 | ) |
|
0 commit comments