-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Figure.hlines for plotting horizontal lines #923
Draft
michaelgrund
wants to merge
33
commits into
main
Choose a base branch
from
hlines-module
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
7ed3b49
Add new modules to plot horizontal and vertical lines
michaelgrund 096b9de
added further content
michaelgrund 92fa9c0
added some first tests
michaelgrund 975b305
some formatting
michaelgrund f25a89c
corrected typo
michaelgrund 3cee081
Merge branch 'master' into hlines-module
michaelgrund a0c29fc
Merge branch 'master' into hlines-module
michaelgrund 51dcb6a
Merge branch 'master' into hlines-module
michaelgrund 4fdc984
Merge branch 'master' into hlines-module
michaelgrund 154d05e
Merge branch 'master' into hlines-module
michaelgrund ddd04b4
Merge branch 'master' into hlines-module
michaelgrund d0bc35c
moved gallery example to new subfolder /lines
michaelgrund 450d8de
Merge branch 'master' into hlines-module
michaelgrund f75fbe1
disable pylint warnings
michaelgrund a375a77
replace hyphen by underscore in gallery example file name
michaelgrund 71113dd
disable pylint warnings
michaelgrund 1438d43
formatting
michaelgrund e3a564e
Merge branch 'master' into hlines-module
michaelgrund d6a8fa5
Merge branch 'master' into hlines-module
michaelgrund ff13180
Merge branch 'master' into hlines-module
michaelgrund 1881e95
Merge branch 'master' into hlines-module
michaelgrund 7222e9b
Merge branch 'main' into hlines-module
michaelgrund 65eb85d
update hlines module
michaelgrund 2353587
Merge branch 'hlines-module' of https://github.com/GenericMappingTool…
michaelgrund 49008ed
formatting
michaelgrund 457bf72
adjust docstring
michaelgrund f2c84a7
adjust priliminary tests
michaelgrund 50dccf9
Merge branch 'main' into hlines-module
michaelgrund e7726c0
update
michaelgrund 60d6e55
Merge branch 'hlines-module' of https://github.com/GenericMappingTool…
michaelgrund 46f8702
formatting
michaelgrund 5648778
Merge branch 'main' into hlines-module
michaelgrund d553e28
Merge branch 'main' into hlines-module
michaelgrund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Add new modules to plot horizontal and vertical lines
As discussed in #670 here's a new module (**hlines**) to plot a single or a set of horizontal lines with only defining the desired y-value(s). For discussion I only add the module for horizontal lines at the moment, however, the adjustments to prepare the same for vertical lines is done very quickly.
- Loading branch information
commit 7ed3b49ac610ba24a5356f7e62bea5dec52b3e47
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
""" | ||
hlines - Plot horizontal lines. | ||
""" | ||
import numpy as np | ||
from pygmt.clib import Session | ||
from pygmt.exceptions import GMTInvalidInput | ||
from pygmt.helpers import build_arg_string, fmt_docstring, use_alias, kwargs_to_strings, data_kind | ||
|
||
@fmt_docstring | ||
@use_alias( | ||
B="frame", | ||
C="cmap", | ||
D="offset", | ||
J="projection", | ||
N="no_clip", | ||
R="region", | ||
U="timestamp", | ||
V="verbose", | ||
W="pen", | ||
X="xshift", | ||
Y="yshift", | ||
Z="zvalue", | ||
l="label", | ||
p="perspective", | ||
t="transparency", | ||
) | ||
@kwargs_to_strings(R="sequence", p="sequence") | ||
def hlines(self, y=None, xmin=None, xmax=None, pen=None, label=None, **kwargs): | ||
"""" | ||
Plot one or a collection of horizontal lines | ||
|
||
Takes a single y value or a list of individual y values and optionally | ||
lower and upper x value limits as input. | ||
|
||
Must provide *y*. | ||
|
||
If y values are given without x limits then the current map boundaries are | ||
used as lower and upper limits. If only a single set of x limits is given then | ||
all lines will have the same length, otherwise give x limits for each individual | ||
line. If only a single label is given then all lines are grouped under this label | ||
in the legend (if shown). If each line should appear as a single entry in the | ||
legend, give corresponding labels for all lines (same for **pen**). | ||
|
||
Parameters | ||
---------- | ||
y : float or 1d array | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
The y coordinates or an array of y coordinates of the | ||
horizontal lines to plot. | ||
{J} | ||
{R} | ||
{B} | ||
{CPT} | ||
offset : str | ||
``dx/dy``. | ||
Offset the line locations by the given amounts | ||
*dx/dy* [Default is no offset]. If *dy* is not given it is set | ||
equal to *dx*. | ||
no_clip : bool or str | ||
``'[c|r]'``. | ||
Do NOT clip lines that fall outside map border [Default plots | ||
lines whose coordinates are strictly inside the map border only]. | ||
The option does not apply to lines which are always | ||
clipped to the map region. For periodic (360-longitude) maps we | ||
must plot all lines twice in case they are clipped by the | ||
repeating boundary. ``no_clip=True`` will turn off clipping and not | ||
plot repeating lines. Use ``no_clip="r"`` to turn off clipping | ||
but retain the plotting of such repeating lines, or use | ||
``no_clip="c"`` to retain clipping but turn off plotting of | ||
repeating lines. | ||
{W} | ||
{U} | ||
{V} | ||
{XY} | ||
zvalue : str or float | ||
``value``. | ||
Instead of specifying a line color via **pen**, give it a *value* | ||
via **zvalue** and a color lookup table via **cmap**. Requires appending | ||
**+z** to **pen** (e.g. ``pen = "5p,+z"``, ``zvalue = 0.8``, | ||
* ``cmap = "viridis"``). | ||
label : str | ||
Add a legend entry for the line being plotted. | ||
{p} | ||
{t} | ||
*transparency* can also be a 1d array to set varying transparency | ||
for lines. | ||
|
||
""" | ||
|
||
kwargs = self._preprocess(**kwargs) | ||
|
||
list_length = len(np.atleast_1d(y)) | ||
|
||
# prepare x vals | ||
if xmin is None and xmax is None: | ||
# get limits from current map boundings if not given via xmin, xmax | ||
with Session() as lib: | ||
mapbnds = lib.extract_region() | ||
x = np.array([[mapbnds[0]], [mapbnds[1]]]) | ||
x = np.repeat(x, list_length, axis=1) | ||
elif xmin is None or xmax is None: | ||
raise GMTInvalidInput("Must provide both, xmin and xmax if limits are not set automatically.") | ||
|
||
else: | ||
# if only a single xmin and xmax without [], repeat to fit size of y | ||
if isinstance(xmin, int) or isinstance(xmin, float): | ||
x = np.array([[xmin], [xmax]]) | ||
x = np.repeat(x, list_length, axis=1) | ||
else: | ||
if len(xmin) != len(xmax): | ||
GMTInvalidInput("Must provide same length for xmin and xmax.") | ||
else: | ||
x = np.array([xmin, xmax]) | ||
|
||
# prepare labels | ||
if "l" in kwargs: | ||
# if several lines belong to the same label, first take the label, | ||
# then set all to None and reset the first entry to the given label | ||
if not isinstance(kwargs["l"], list): | ||
label2use = kwargs["l"] | ||
kwargs["l"] = np.repeat(None, list_length) | ||
kwargs["l"][0] = label2use | ||
else: | ||
kwargs["l"] = np.repeat(None, list_length) | ||
|
||
|
||
# prepare pens | ||
if "W" in kwargs: | ||
# select pen, no series | ||
if not isinstance(kwargs["W"], list): | ||
pen2use = kwargs["W"] | ||
kwargs["W"] = np.repeat(pen2use, list_length) | ||
else: # use as default if no pen is given (neither single nor series) | ||
kwargs["W"] = np.repeat("1p,black", list_length) | ||
|
||
# loop over entries | ||
kwargs_copy = kwargs.copy() | ||
|
||
for index in range(list_length): | ||
y2plt = [np.atleast_1d(y)[index], np.atleast_1d(y)[index]] | ||
x2plt = [np.atleast_1d(x)[0][index], np.atleast_1d(x)[1][index]] | ||
kind = data_kind(None, x2plt, y2plt) | ||
|
||
with Session() as lib: | ||
if kind == "vectors": | ||
file_context = lib.virtualfile_from_vectors( | ||
np.atleast_1d(x2plt), np.atleast_1d(y2plt)) | ||
else: | ||
raise GMTInvalidInput("Unrecognized data type.") | ||
|
||
kwargs["l"] = kwargs_copy["l"][index] | ||
kwargs["W"] = kwargs_copy["W"][index] | ||
|
||
with file_context as fname: | ||
arg_str = " ".join([fname, build_arg_string(kwargs)]) | ||
lib.call_module("plot", arg_str) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.