|
4 | 4 | import numpy as np
|
5 | 5 | from pygmt.clib import Session
|
6 | 6 | from pygmt.exceptions import GMTInvalidInput
|
7 |
| -from pygmt.helpers import build_arg_string, fmt_docstring, use_alias, kwargs_to_strings, data_kind |
| 7 | +from pygmt.helpers import ( |
| 8 | + build_arg_string, |
| 9 | + data_kind, |
| 10 | + fmt_docstring, |
| 11 | + kwargs_to_strings, |
| 12 | + use_alias, |
| 13 | +) |
| 14 | + |
8 | 15 |
|
9 | 16 | @fmt_docstring
|
10 | 17 | @use_alias(
|
11 |
| - B="frame", |
12 |
| - C="cmap", |
13 |
| - D="offset", |
14 |
| - J="projection", |
15 |
| - N="no_clip", |
16 |
| - R="region", |
17 |
| - U="timestamp", |
18 |
| - V="verbose", |
19 |
| - W="pen", |
20 |
| - X="xshift", |
21 |
| - Y="yshift", |
22 |
| - Z="zvalue", |
23 |
| - l="label", |
24 |
| - p="perspective", |
25 |
| - t="transparency", |
26 |
| - ) |
| 18 | + B="frame", |
| 19 | + C="cmap", |
| 20 | + D="offset", |
| 21 | + J="projection", |
| 22 | + N="no_clip", |
| 23 | + R="region", |
| 24 | + U="timestamp", |
| 25 | + V="verbose", |
| 26 | + W="pen", |
| 27 | + X="xshift", |
| 28 | + Y="yshift", |
| 29 | + Z="zvalue", |
| 30 | + l="label", |
| 31 | + p="perspective", |
| 32 | + t="transparency", |
| 33 | +) |
27 | 34 | @kwargs_to_strings(R="sequence", p="sequence")
|
28 | 35 | def hlines(self, y=None, xmin=None, xmax=None, pen=None, label=None, **kwargs):
|
29 |
| - """" |
30 |
| - Plot one or a collection of horizontal lines |
| 36 | + """ |
| 37 | + " Plot one or a collection of horizontal lines. |
31 | 38 |
|
32 |
| - Takes a single y value or a list of individual y values and optionally |
| 39 | + Takes a single y value or a list of individual y values and optionally |
33 | 40 | lower and upper x value limits as input.
|
34 | 41 |
|
35 | 42 | Must provide *y*.
|
36 | 43 |
|
37 |
| - If y values are given without x limits then the current map boundaries are |
38 |
| - used as lower and upper limits. If only a single set of x limits is given then |
39 |
| - all lines will have the same length, otherwise give x limits for each individual |
40 |
| - line. If only a single label is given then all lines are grouped under this label |
41 |
| - in the legend (if shown). If each line should appear as a single entry in the |
42 |
| - legend, give corresponding labels for all lines (same for **pen**). |
| 44 | + If y values are given without x limits then the current map boundaries are |
| 45 | + used as lower and upper limits. If only a single set of x limits is given |
| 46 | + then all lines will have the same length, otherwise give x limits for each |
| 47 | + individual line. If only a single label is given then all lines are grouped |
| 48 | + under this label in the legend (if shown). If each line should appear as a |
| 49 | + single entry in the legend, give corresponding labels for all lines |
| 50 | + (same for **pen**). |
43 | 51 |
|
44 | 52 | Parameters
|
45 | 53 | ----------
|
@@ -73,83 +81,84 @@ def hlines(self, y=None, xmin=None, xmax=None, pen=None, label=None, **kwargs):
|
73 | 81 | {XY}
|
74 | 82 | zvalue : str or float
|
75 | 83 | ``value``.
|
76 |
| - Instead of specifying a line color via **pen**, give it a *value* |
77 |
| - via **zvalue** and a color lookup table via **cmap**. Requires appending |
78 |
| - **+z** to **pen** (e.g. ``pen = "5p,+z"``, ``zvalue = 0.8``, |
79 |
| - * ``cmap = "viridis"``). |
| 84 | + Instead of specifying a line color via **pen**, give it a *value* |
| 85 | + via **zvalue** and a color lookup table via **cmap**. Requires |
| 86 | + appending **+z** to **pen** (e.g. ``pen = "5p,+z"``, |
| 87 | + ``zvalue = 0.8``, ``cmap = "viridis"``). |
80 | 88 | label : str
|
81 | 89 | Add a legend entry for the line being plotted.
|
82 | 90 | {p}
|
83 | 91 | {t}
|
84 | 92 | *transparency* can also be a 1d array to set varying transparency
|
85 | 93 | for lines.
|
86 |
| -
|
87 | 94 | """
|
88 |
| - |
| 95 | + |
89 | 96 | kwargs = self._preprocess(**kwargs)
|
90 |
| - |
| 97 | + |
91 | 98 | list_length = len(np.atleast_1d(y))
|
92 |
| - |
| 99 | + |
93 | 100 | # prepare x vals
|
94 |
| - if xmin is None and xmax is None: |
| 101 | + if xmin is None and xmax is None: |
95 | 102 | # get limits from current map boundings if not given via xmin, xmax
|
96 |
| - with Session() as lib: |
97 |
| - mapbnds = lib.extract_region() |
98 |
| - x = np.array([[mapbnds[0]], [mapbnds[1]]]) |
99 |
| - x = np.repeat(x, list_length, axis=1) |
| 103 | + with Session() as lib: |
| 104 | + mapbnds = lib.extract_region() |
| 105 | + x = np.array([[mapbnds[0]], [mapbnds[1]]]) |
| 106 | + x = np.repeat(x, list_length, axis=1) |
100 | 107 | elif xmin is None or xmax is None:
|
101 |
| - raise GMTInvalidInput("Must provide both, xmin and xmax if limits are not set automatically.") |
102 |
| - |
103 |
| - else: |
| 108 | + raise GMTInvalidInput( |
| 109 | + "Must provide both, xmin and xmax if limits are not set automatically." |
| 110 | + ) |
| 111 | + |
| 112 | + else: |
104 | 113 | # if only a single xmin and xmax without [], repeat to fit size of y
|
105 |
| - if isinstance(xmin, int) or isinstance(xmin, float): |
106 |
| - x = np.array([[xmin], [xmax]]) |
107 |
| - x = np.repeat(x, list_length, axis=1) |
| 114 | + if isinstance(xmin, int) or isinstance(xmin, float): |
| 115 | + x = np.array([[xmin], [xmax]]) |
| 116 | + x = np.repeat(x, list_length, axis=1) |
108 | 117 | else:
|
109 | 118 | if len(xmin) != len(xmax):
|
110 |
| - GMTInvalidInput("Must provide same length for xmin and xmax.") |
111 |
| - else: |
112 |
| - x = np.array([xmin, xmax]) |
113 |
| - |
114 |
| - # prepare labels |
| 119 | + GMTInvalidInput("Must provide same length for xmin and xmax.") |
| 120 | + else: |
| 121 | + x = np.array([xmin, xmax]) |
| 122 | + |
| 123 | + # prepare labels |
115 | 124 | if "l" in kwargs:
|
116 | 125 | # if several lines belong to the same label, first take the label,
|
117 | 126 | # then set all to None and reset the first entry to the given label
|
118 | 127 | if not isinstance(kwargs["l"], list):
|
119 | 128 | label2use = kwargs["l"]
|
120 | 129 | kwargs["l"] = np.repeat(None, list_length)
|
121 |
| - kwargs["l"][0] = label2use |
| 130 | + kwargs["l"][0] = label2use |
122 | 131 | else:
|
123 |
| - kwargs["l"] = np.repeat(None, list_length) |
124 |
| - |
125 |
| - |
126 |
| - # prepare pens |
| 132 | + kwargs["l"] = np.repeat(None, list_length) |
| 133 | + |
| 134 | + # prepare pens |
127 | 135 | if "W" in kwargs:
|
128 | 136 | # select pen, no series
|
129 | 137 | if not isinstance(kwargs["W"], list):
|
130 | 138 | pen2use = kwargs["W"]
|
131 |
| - kwargs["W"] = np.repeat(pen2use, list_length) |
132 |
| - else: # use as default if no pen is given (neither single nor series) |
| 139 | + kwargs["W"] = np.repeat(pen2use, list_length) |
| 140 | + else: # use as default if no pen is given (neither single nor series) |
133 | 141 | kwargs["W"] = np.repeat("1p,black", list_length)
|
134 | 142 |
|
135 |
| - # loop over entries |
| 143 | + # loop over entries |
136 | 144 | kwargs_copy = kwargs.copy()
|
137 |
| - |
| 145 | + |
138 | 146 | for index in range(list_length):
|
139 |
| - y2plt = [np.atleast_1d(y)[index], np.atleast_1d(y)[index]] |
140 |
| - x2plt = [np.atleast_1d(x)[0][index], np.atleast_1d(x)[1][index]] |
141 |
| - kind = data_kind(None, x2plt, y2plt) |
142 |
| - |
143 |
| - with Session() as lib: |
144 |
| - if kind == "vectors": |
145 |
| - file_context = lib.virtualfile_from_vectors( |
146 |
| - np.atleast_1d(x2plt), np.atleast_1d(y2plt)) |
147 |
| - else: |
148 |
| - raise GMTInvalidInput("Unrecognized data type.") |
149 |
| - |
150 |
| - kwargs["l"] = kwargs_copy["l"][index] |
151 |
| - kwargs["W"] = kwargs_copy["W"][index] |
152 |
| - |
153 |
| - with file_context as fname: |
154 |
| - arg_str = " ".join([fname, build_arg_string(kwargs)]) |
155 |
| - lib.call_module("plot", arg_str) |
| 147 | + y2plt = [np.atleast_1d(y)[index], np.atleast_1d(y)[index]] |
| 148 | + x2plt = [np.atleast_1d(x)[0][index], np.atleast_1d(x)[1][index]] |
| 149 | + kind = data_kind(None, x2plt, y2plt) |
| 150 | + |
| 151 | + with Session() as lib: |
| 152 | + if kind == "vectors": |
| 153 | + file_context = lib.virtualfile_from_vectors( |
| 154 | + np.atleast_1d(x2plt), np.atleast_1d(y2plt) |
| 155 | + ) |
| 156 | + else: |
| 157 | + raise GMTInvalidInput("Unrecognized data type.") |
| 158 | + |
| 159 | + kwargs["l"] = kwargs_copy["l"][index] |
| 160 | + kwargs["W"] = kwargs_copy["W"][index] |
| 161 | + |
| 162 | + with file_context as fname: |
| 163 | + arg_str = " ".join([fname, build_arg_string(kwargs)]) |
| 164 | + lib.call_module("plot", arg_str) |
0 commit comments