Enhance text with extra functionality and aliases#481
Conversation
So that we can set angle=True or angle="", and users can parse the angle from a textfile (in GMT it will be `-F+a`. Added a test case for this, and ensure that angle/font/justify=True (i.e. setting a boolean type) works.
|
These lines convert a list or a tuple into comma-separated strings. They don't make sense here. Lines 820 to 822 in 0267dd1 |
True. I can understand the rationale for |
Equivalent to using `-F+c` in GMT.
The current implementation only allows -F+a+f+j, so the input format should be |
Co-Authored-By: Dongdong Tian <seisman.info@gmail.com>
Done. I seem to recall reading somewhere (was it upstream in GMT?) that the reason was because we want all the numerical values first, and then the string values after. |
So that we don't plot the map frame four times in a loop.
Because it doesn't check for remote @... files. GMT will raise an appropriate GMTClibError if the file isn't found.
Fixes issue with having a blankspace offset in front of TL and BL before. The text can be directly given to the -F+t argument, and we skip using the temporary file. Also made sure that spaces in textstring will work properly.
| pd.DataFrame.from_dict( | ||
| { | ||
| "x": np.atleast_1d(x), | ||
| "y": np.atleast_1d(y), | ||
| "text": np.atleast_1d(text), | ||
| } | ||
| ).to_csv( | ||
| tmpfile.name, | ||
| sep="\t", | ||
| header=False, | ||
| index=False, | ||
| quoting=csv.QUOTE_NONE, | ||
| ) |
There was a problem hiding this comment.
| pd.DataFrame.from_dict( | |
| { | |
| "x": np.atleast_1d(x), | |
| "y": np.atleast_1d(y), | |
| "text": np.atleast_1d(text), | |
| } | |
| ).to_csv( | |
| tmpfile.name, | |
| sep="\t", | |
| header=False, | |
| index=False, | |
| quoting=csv.QUOTE_NONE, | |
| ) | |
| file_context = lib.virtualfile_from_vectors( | |
| np.atleast_1d(x), np.atleast_1d(y), np.atleast_1d(text) | |
| ) |
@leouieda mentioned before at #321 (comment) to use virtualfiles, and I finally figured out what he meant. Unfortunately, virtualfile_from_vectors doesn't yet support str types. This is the error on _check_dtype_and_dim:
if array.dtype.name not in DTYPES:
raise GMTInvalidInput(
> "Unsupported numpy data type '{}'.".format(array.dtype.name)
)
E pygmt.exceptions.GMTInvalidInput: Unsupported numpy data type 'str864'.
From what I can tell, we'll need to use GMT_Put_Strings is that right? We should do this refactor in a separate PR, this one is already getting a bit too long.
There was a problem hiding this comment.
Yes, as I understand it, we need to use GMT_Put_Strings to pass trailing strings to GMT.
Co-Authored-By: Dongdong Tian <seisman.info@gmail.com>
There was a problem hiding this comment.
See the comment above.
Besides that, I think the PR is good to merge. There are still some missing features about the text() method:
- Using virtual files instead of temporary file
- Let angle, font, and justify accept list/array as input
- more flexible input format, e.g., -F+a+j+f and -F+a+f+j.
I think we can address them in separate PRs in the feature.
Co-Authored-By: Dongdong Tian <seisman.info@gmail.com>
|
Great, thanks for taking the time to review this so thoroughly @seisman! I'll open a new issue for tackling those future improvements. |
Description of proposed changes
Adding extra functionality to
text, including aliases that were missing in initial implementation at #321. In support of the text tutorial at #480. Preview documentation at https://pygmt-git-enhance-text.gmt.vercel.app/api/generated/pygmt.Figure.text.htmlTODO:
angle=Trueso that users can parse the angle from a textfile (in GMT it will be-F+a. (4c52613)I'd be quite keen to enable text placement without setting an x or y by using
position(-F+cin GMT) to get the x/y from the map frame's region. For example, the GMT gallery example at https://docs.generic-mapping-tools.org/latest/gallery/ex40.html doesgmt text -Dj0.1i/0.1i -F+cLT+jTL+f18p+t"T = 100 km"to print the text at the top-left (TL) position in the map frame.Fixes #
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.