Skip to content

Commit 096b9de

Browse files
committed
added further content
1 parent 7ed3b49 commit 096b9de

File tree

5 files changed

+117
-76
lines changed

5 files changed

+117
-76
lines changed

doc/api/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Plotting data and laying out the map:
3131
Figure.grdview
3232
Figure.image
3333
Figure.inset
34+
Figure.hlines
3435
Figure.legend
3536
Figure.logo
3637
Figure.meca
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Plot horizontal lines
3+
---------------------
4+
5+
The :meth:`pygmt.Figure.hlines` method can plot horizontal lines based on
6+
a given y value. Optionally, the lower and upper limits of the lines can be
7+
defined, otherwise the current map boundaries are taken.
8+
9+
"""
10+
11+
import pygmt
12+
13+
fig = pygmt.Figure()
14+
fig.basemap(region=[0, 10, 0, 11], projection="X10c/10c", frame=True)
15+
16+
fig.hlines(1, label="line1")
17+
fig.hlines([2, 3], pen="2p,dodgerblue4", label="line2")
18+
fig.hlines([4, 5], xmin=2, xmax=8, pen="2p,red3", label="line3")
19+
fig.hlines([6, 7], xmin=[1, 3], xmax=[8, 7], pen="3p,seagreen", label="line4")
20+
fig.hlines(
21+
[8, 9, 10],
22+
xmin=[1.3, 3, 2],
23+
xmax=[6.5, 7, 5],
24+
pen=["4p,darkmagenta", "2p,gold,--", "3.5p,blue,."],
25+
label=["line5", "line6", "line7"],
26+
)
27+
28+
fig.legend()
29+
fig.show()

pygmt/figure.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ def _repr_html_(self):
383383
grdcontour,
384384
grdimage,
385385
grdview,
386+
hlines,
386387
image,
387388
inset,
388389
legend,

pygmt/src/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from pygmt.src.grdinfo import grdinfo
1616
from pygmt.src.grdtrack import grdtrack
1717
from pygmt.src.grdview import grdview
18+
from pygmt.src.hlines import hlines
1819
from pygmt.src.image import image
1920
from pygmt.src.info import info
2021
from pygmt.src.inset import inset

pygmt/src/hlines.py

Lines changed: 85 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,50 @@
44
import numpy as np
55
from pygmt.clib import Session
66
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+
815

916
@fmt_docstring
1017
@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+
)
2734
@kwargs_to_strings(R="sequence", p="sequence")
2835
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.
3138
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
3340
lower and upper x value limits as input.
3441
3542
Must provide *y*.
3643
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**).
4351
4452
Parameters
4553
----------
@@ -73,83 +81,84 @@ def hlines(self, y=None, xmin=None, xmax=None, pen=None, label=None, **kwargs):
7381
{XY}
7482
zvalue : str or float
7583
``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"``).
8088
label : str
8189
Add a legend entry for the line being plotted.
8290
{p}
8391
{t}
8492
*transparency* can also be a 1d array to set varying transparency
8593
for lines.
86-
8794
"""
88-
95+
8996
kwargs = self._preprocess(**kwargs)
90-
97+
9198
list_length = len(np.atleast_1d(y))
92-
99+
93100
# prepare x vals
94-
if xmin is None and xmax is None:
101+
if xmin is None and xmax is None:
95102
# 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)
100107
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:
104113
# 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)
108117
else:
109118
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
115124
if "l" in kwargs:
116125
# if several lines belong to the same label, first take the label,
117126
# then set all to None and reset the first entry to the given label
118127
if not isinstance(kwargs["l"], list):
119128
label2use = kwargs["l"]
120129
kwargs["l"] = np.repeat(None, list_length)
121-
kwargs["l"][0] = label2use
130+
kwargs["l"][0] = label2use
122131
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
127135
if "W" in kwargs:
128136
# select pen, no series
129137
if not isinstance(kwargs["W"], list):
130138
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)
133141
kwargs["W"] = np.repeat("1p,black", list_length)
134142

135-
# loop over entries
143+
# loop over entries
136144
kwargs_copy = kwargs.copy()
137-
145+
138146
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

Comments
 (0)