Skip to content

Commit e131d26

Browse files
michaelgrundwillschlitzerseismanweiji14Meghan Jones
authored
Wrap rose (#794)
* Wrap rose * Add tests * Add a gallery example Co-authored-by: Will Schlitzer <[email protected]> Co-authored-by: Dongdong Tian <[email protected]> Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Meghan Jones <[email protected]>
1 parent 819fc1d commit e131d26

13 files changed

+470
-0
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ Plotting data and laying out the map:
3636
Figure.meca
3737
Figure.plot
3838
Figure.plot3d
39+
Figure.rose
3940
Figure.set_panel
4041
Figure.shift_origin
4142
Figure.solar
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
"""
2+
Rose diagram
3+
------------
4+
5+
The :meth:`pygmt.Figure.rose` method can plot windrose diagrams or polar histograms.
6+
"""
7+
8+
import pygmt
9+
10+
# Load sample compilation of fracture lengths and azimuth as
11+
# hypothetically digitized from geological maps
12+
data = pygmt.datasets.load_fractures_compilation()
13+
14+
fig = pygmt.Figure()
15+
16+
fig.rose(
17+
# use columns of the sample dataset as input for the length and azimuth
18+
# parameters
19+
length=data.length,
20+
azimuth=data.azimuth,
21+
# specify the "region" of interest in the (r,azimuth) space [r0, r1, az0, az1],
22+
# here, r0 is 0 and r1 is 1, for azimuth, az0 is 0 and az1 is 360 which means
23+
# we plot a full circle between 0 and 360 degrees
24+
region=[0, 1, 0, 360],
25+
# set the diameter of the rose diagram to 7.5 cm
26+
diameter="7.5c",
27+
# define the sector width in degrees, we append +r here to draw a rose
28+
# diagram instead of a sector diagram
29+
sector="10+r",
30+
# normalize bin counts by the largest value so all bin counts range from
31+
# 0 to 1
32+
norm=True,
33+
# use red3 as color fill for the sectors
34+
color="red3",
35+
# define the frame with ticks and gridlines every 0.2
36+
# length unit in radial direction and every 30 degrees
37+
# in azimuthal direction, set background color to
38+
# lightgray
39+
frame=["x0.2g0.2", "y30g30", "+glightgray"],
40+
# use a pen size of 1p to draw the outlines
41+
pen="1p",
42+
)
43+
44+
fig.show()

pygmt/figure.py

+1
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ def _repr_html_(self):
390390
meca,
391391
plot,
392392
plot3d,
393+
rose,
393394
set_panel,
394395
solar,
395396
subplot,

pygmt/src/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Source code for PyGMT modules.
33
"""
44
# pylint: disable=import-outside-toplevel
5+
56
from pygmt.src.basemap import basemap
67
from pygmt.src.blockm import blockmean, blockmedian
78
from pygmt.src.coast import coast
@@ -25,6 +26,7 @@
2526
from pygmt.src.meca import meca
2627
from pygmt.src.plot import plot
2728
from pygmt.src.plot3d import plot3d
29+
from pygmt.src.rose import rose
2830
from pygmt.src.solar import solar
2931
from pygmt.src.subplot import set_panel, subplot
3032
from pygmt.src.surface import surface

pygmt/src/rose.py

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
"""
2+
rose - Plot windrose diagrams or polar histograms.
3+
"""
4+
5+
from pygmt.clib import Session
6+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
7+
8+
9+
@fmt_docstring
10+
@use_alias(
11+
A="sector",
12+
B="frame",
13+
C="cmap",
14+
D="shift",
15+
Em="vectors",
16+
F="no_scale",
17+
G="color",
18+
I="inquire",
19+
JX="diameter",
20+
L="labels",
21+
M="vector_params",
22+
Q="alpha",
23+
R="region",
24+
S="norm",
25+
T="orientation",
26+
U="timestamp",
27+
V="verbose",
28+
W="pen",
29+
X="xshift",
30+
Y="yshift",
31+
Z="scale",
32+
i="columns",
33+
c="panel",
34+
p="perspective",
35+
t="transparency",
36+
)
37+
@kwargs_to_strings(R="sequence", c="sequence_comma", i="sequence_comma", p="sequence")
38+
def rose(self, length=None, azimuth=None, data=None, **kwargs):
39+
"""
40+
Plot windrose diagrams or polar histograms.
41+
42+
Takes a matrix, (length,azimuth) pairs, or a file name as input
43+
and plots windrose diagrams or polar histograms (sector diagram
44+
or rose diagram).
45+
46+
Must provide either ``data`` or ``length`` and ``azimuth``.
47+
48+
Options include full circle and half circle plots. The outline
49+
of the windrose is drawn with the same color as
50+
:gmt-term:`MAP_DEFAULT_PEN`.
51+
52+
Full option list at :gmt-docs:`rose.html`
53+
54+
{aliases}
55+
56+
Parameters
57+
----------
58+
length/azimuth : float or 1d arrays
59+
Length and azimuth values, or arrays of length and azimuth
60+
values
61+
62+
data : str or 2d array
63+
Either a data file name or a 2d numpy array with the tabular data.
64+
Use option ``columns`` to choose which columns are length and
65+
azimuth, respectively. If a file with only azimuths is given,
66+
use ``columns`` to indicate the single column with azimuths; then
67+
all lengths are set to unity (see ``scale = 'u'`` to set actual
68+
lengths to unity as well).
69+
70+
orientation : bool
71+
Specifies that the input data are orientation data (i.e., have a
72+
180 degree ambiguity) instead of true 0-360 degree directions
73+
[Default is 0-360 degrees]. We compensate by counting each record
74+
twice: First as azimuth and second as azimuth +180. Ignored if
75+
range is given as -90/90 or 0/180.
76+
77+
region : str or list
78+
*Required if this is the first plot command*.
79+
*r0/r1/az0/az1*.
80+
Specifies the 'region' of interest in (*r*, *azimuth*) space. Here,
81+
*r0* is 0, *r1* is max length in units. For *az0* and *az1*,
82+
specify either -90/90 or 0/180 for half circle plot or 0/360 for
83+
full circle.
84+
85+
diameter : str
86+
Sets the diameter of the rose diagram. If not given,
87+
then we default to a diameter of 7.5 cm.
88+
89+
sector : str
90+
Gives the sector width in degrees for sector and rose diagram.
91+
Default ``'0'`` means windrose diagram. Append **+r** to draw rose
92+
diagram instead of sector diagram (e.g. ``'10+r'``).
93+
94+
norm : bool
95+
Normalize input radii (or bin counts if ``sector_width`` is used)
96+
by the largest value so all radii (or bin counts) range from 0
97+
to 1.
98+
99+
frame : str
100+
Set map boundary frame and axes attributes. Remember that *x*
101+
here is radial distance and *y* is azimuth. The ylabel may be
102+
used to plot a figure caption. The scale bar length is determined
103+
by the radial gridline spacing.
104+
105+
scale : float or str
106+
Multiply the data radii by scale. E.g., use ``scale = 0.001`` to
107+
convert your data from m to km. To exclude the radii from
108+
consideration, set them all to unity with ``scale = 'u'``
109+
[Default is no scaling].
110+
111+
columns : str or 1d array
112+
Select input columns and transformations. E.g. choose
113+
``columns = [1, 0]`` or ``columns = '1,0'`` if the length values
114+
are stored in the second column and the direction (azimuth)
115+
values in the first one. Note: zero-based indexing is used.
116+
117+
color : str
118+
Selects shade, color or pattern for filling the sectors [Default
119+
is no fill].
120+
121+
cmap : str
122+
Give a CPT. The *r*-value for each sector is used to look-up the
123+
sector color. Cannot be used with a rose diagram.
124+
125+
pen : str
126+
Set pen attributes for sector outline or rose plot, e.g.
127+
``pen = '0.5p'``. [Default is no outline]. To change pen used to
128+
draw vector (requires ``vectors``) [Default is same as sector
129+
outline] use e.g. ``pen = 'v0.5p'``.
130+
131+
labels : str
132+
``'wlabel,elabel,slabel,nlabel'``. Specify labels for the 0, 90,
133+
180, and 270 degree marks. For full-circle plot the default is
134+
WEST,EAST,SOUTH,NORTH and for half-circle the default is
135+
90W,90E,-,0. A **-** in any entry disables that label
136+
(e.g. ``labels = 'W,E,-,N'``). Use ``labels = ''`` to disable
137+
all four labels. Note that the :gmt-term:`GMT_LANGUAGE` setting
138+
will affect the words used.
139+
140+
no_scale : bool
141+
Do NOT draw the scale length bar (``no_scale = True``).
142+
Default plots scale in lower right corner provided ``frame``
143+
is used.
144+
145+
shift : bool
146+
Shift sectors so that they are centered on the bin interval
147+
(e.g., first sector is centered on 0 degrees).
148+
149+
vectors : str
150+
``vectors = 'mode_file'``. Plot vectors showing the
151+
principal directions given in the *mode_file* file.
152+
Alternatively, specify ``vectors`` to compute and plot
153+
mean direction. See ``vector_params`` to control the vector
154+
attributes. Finally, to instead save the computed mean
155+
direction and other statistics, use
156+
``vectors = '+wmode_file'``. The eight items saved to
157+
a single record are: *mean_az*, *mean_r*, *mean_resultant*,
158+
*max_r*, *scaled_mean_r*, *length_sum*, *n*, *sign@alpha*,
159+
where the last term is 0 or 1 depending on whether the mean
160+
resultant is significant at the level of confidence set via
161+
``alpha``.
162+
163+
vector_params : str
164+
Used with ``vectors`` to modify vector parameters. For
165+
vector heads, append vector head size [Default is 0, i.e., a
166+
line]. See :gmt-docs:`rose.html#vector-attributes` for
167+
specifying additional attributes. If ``vectors`` is not
168+
given and the current plot mode is to draw a windrose diagram
169+
then using ``vector_params`` will add vector heads to all
170+
individual directions using the supplied attributes.
171+
172+
alpha : float or str
173+
Sets the confidence level used to determine if the mean
174+
resultant is significant (i.e., Lord Rayleigh test for
175+
uniformity) [``alpha = 0.05``]. Note: The critical
176+
values are approximated [Berens, 2009] and requires at
177+
least 10 points; the critical resultants are accurate to at
178+
least 3 significant digits. For smaller data sets you
179+
should consult exact statistical tables.
180+
181+
Berens, P., 2009, CircStat: A MATLAB Toolbox for Circular
182+
Statistics, *J. Stat. Software*, 31(10), 1-21,
183+
https://doi.org/10.18637/jss.v031.i10.
184+
185+
{U}
186+
{V}
187+
{XY}
188+
{c}
189+
{p}
190+
{t}
191+
"""
192+
193+
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
194+
195+
with Session() as lib:
196+
# Choose how data will be passed into the module
197+
file_context = lib.virtualfile_from_data(
198+
check_kind="vector", data=data, x=length, y=azimuth
199+
)
200+
201+
with file_context as fname:
202+
arg_str = " ".join([fname, build_arg_string(kwargs)])
203+
204+
lib.call_module("rose", arg_str)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 2e440e6db215a619829acb08a0ab55bf
3+
size: 41397
4+
path: test_rose_2d_array_multiple.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: c64abda4022e5e9fcf5592c5c2f6bb44
3+
size: 37742
4+
path: test_rose_2d_array_single.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 2b0ba264c2240a6d2da02466d8848d7a
3+
size: 73857
4+
path: test_rose_bools.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: eba3b08913112996eca8b9d01f5709c3
3+
size: 40610
4+
path: test_rose_data_file.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 67613d130577c8083e1d505155421205
3+
size: 159974
4+
path: test_rose_no_sectors.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 1facbc2eb905197cd6aece39c08c8686
3+
size: 44793
4+
path: test_rose_plot_data_using_cpt.png
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
outs:
2+
- md5: 11af5a2d9971898c96f48e0a5215d759
3+
size: 62490
4+
path: test_rose_plot_with_transparency.png

0 commit comments

Comments
 (0)