-
Notifications
You must be signed in to change notification settings - Fork 228
Add tutorial for pygmt.Figure.text #480
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
Changes from 2 commits
f1d2ab0
5ecf6f4
84c127c
1e95af7
cb9f713
20994aa
3dde4b0
6d968d7
2afb5c8
659860d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
114,0.5,BORNEO |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,128 @@ | ||||||||||||||||||||
""" | ||||||||||||||||||||
Plotting text | ||||||||||||||||||||
============= | ||||||||||||||||||||
|
||||||||||||||||||||
It is often useful to add annotations to a map plot. This is handled by | ||||||||||||||||||||
:meth:`pygmt.Figure.text`. | ||||||||||||||||||||
""" | ||||||||||||||||||||
|
||||||||||||||||||||
import pygmt | ||||||||||||||||||||
|
||||||||||||||||||||
######################################################################################## | ||||||||||||||||||||
# Basic map annotation | ||||||||||||||||||||
# -------------------- | ||||||||||||||||||||
# | ||||||||||||||||||||
# Text annotations can be added to a map using the :meth:`text` method of the | ||||||||||||||||||||
# :class:`pygmt.Figure`. | ||||||||||||||||||||
# | ||||||||||||||||||||
# Full details of the GMT6 command `text` can be found `here<https://docs.generic-mapping-tools.org/latest/text.html>`_. | ||||||||||||||||||||
# The Python binding to this command is documented `here<https://www.pygmt.org/latest/api/generated/pygmt.Figure.text.html>`_. | ||||||||||||||||||||
# | ||||||||||||||||||||
# Here we create a simple map and add an annotation using the ``text``, ``x``, | ||||||||||||||||||||
# and ``y`` arguments to specify the annotation text and position in the | ||||||||||||||||||||
# projection frame. ``text``, ``x``, and ``y`` accept `int`, `str`, or `float`. | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
|
||||||||||||||||||||
fig = pygmt.Figure() | ||||||||||||||||||||
with pygmt.config(MAP_FRAME_TYPE="plain"): | ||||||||||||||||||||
fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") | ||||||||||||||||||||
Comment on lines
+25
to
+26
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. As an introductory tutorial, we probably should avoid using unrelated methods 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. I think it's ok to keep it here for now, this might reduce the number of 'fancy' frames out there in the world 😆 |
||||||||||||||||||||
fig.coast(land="black", water="skyblue") | ||||||||||||||||||||
|
||||||||||||||||||||
# Plotting text annotations using single elements | ||||||||||||||||||||
fig.text(text="SOUTH CHINA SEA", x=112, y=6) | ||||||||||||||||||||
|
||||||||||||||||||||
# Plotting text annotations using lists of elements | ||||||||||||||||||||
fig.text(text=["CELEBES SEA", "JAVA SEA"], x=[119, 112], y=[3.25, -4.6]) | ||||||||||||||||||||
|
||||||||||||||||||||
fig.show() | ||||||||||||||||||||
|
||||||||||||||||||||
######################################################################################## | ||||||||||||||||||||
# Changing font style | ||||||||||||||||||||
# ------------------- | ||||||||||||||||||||
# The size, family/weight, and colour of an annotation can be specified using the ``font`` argument. | ||||||||||||||||||||
# | ||||||||||||||||||||
# A list of all recognised fonts can be found `here<https://docs.generic-mapping-tools.org/latest/cookbook/postscript_fonts.html>`_), | ||||||||||||||||||||
# including details of how to use non-default fonts. | ||||||||||||||||||||
|
||||||||||||||||||||
fig = pygmt.Figure() | ||||||||||||||||||||
with pygmt.config(MAP_FRAME_TYPE="plain"): | ||||||||||||||||||||
fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") | ||||||||||||||||||||
Comment on lines
+48
to
+49
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. Same as above for |
||||||||||||||||||||
fig.coast(land="black", water="skyblue") | ||||||||||||||||||||
|
||||||||||||||||||||
# Customising the font style | ||||||||||||||||||||
fig.text(text="BORNEO", x="114.", y=0.5, font="22p,Helvetica-Bold,white") | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
|
||||||||||||||||||||
fig.show() | ||||||||||||||||||||
|
||||||||||||||||||||
######################################################################################## | ||||||||||||||||||||
# Plotting from a text file | ||||||||||||||||||||
# ------------------------- | ||||||||||||||||||||
# | ||||||||||||||||||||
# It is also possible to add annotations from a file containing `x`, `y`, and | ||||||||||||||||||||
# `text` fields. Here we give a complete example. | ||||||||||||||||||||
|
||||||||||||||||||||
fig = pygmt.Figure() | ||||||||||||||||||||
with pygmt.config(MAP_FRAME_TYPE="plain"): | ||||||||||||||||||||
fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a") | ||||||||||||||||||||
Comment on lines
+65
to
+66
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. Same as above for |
||||||||||||||||||||
fig.coast(land="black", water="skyblue") | ||||||||||||||||||||
|
||||||||||||||||||||
# Plot region names / sea names | ||||||||||||||||||||
fig.text(textfiles="examples.txt", font="22p,Helvetica-Bold,white") | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
|
||||||||||||||||||||
# Plot names of seas | ||||||||||||||||||||
fig.text(text=["CELEBES SEA", "JAVA SEA"], x=[119, 112], y=[3.25, -4.6]) | ||||||||||||||||||||
fig.text(text="SULU SEA", x=119.12, y=7.25, angle=-40) | ||||||||||||||||||||
fig.text(text="SOUTH CHINA SEA", x=112, y=6, angle=40) | ||||||||||||||||||||
fig.text(text="MAKASSAR STRAIT", x=118.4, y=-1, angle=65) | ||||||||||||||||||||
fig.show() | ||||||||||||||||||||
|
||||||||||||||||||||
######################################################################################## | ||||||||||||||||||||
# ``justify`` argument | ||||||||||||||||||||
# -------------------- | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
# | ||||||||||||||||||||
# ``justify`` is used to define the anchor point for the bounding box for text | ||||||||||||||||||||
# being added to a plot. The following code segment demonstrates the | ||||||||||||||||||||
# positioning of the anchor point relative to the text. | ||||||||||||||||||||
# | ||||||||||||||||||||
# The anchor is specified with a two letter (order independent) code, chosen | ||||||||||||||||||||
# from: | ||||||||||||||||||||
# * Horizontal anchor: L(eft), C(entre), R(ight) | ||||||||||||||||||||
# * Vertical anchor: T(op), M(iddle), B(ottom) | ||||||||||||||||||||
|
||||||||||||||||||||
fig = pygmt.Figure() | ||||||||||||||||||||
fig.basemap(region=[0, 4, 0, 4], projection="X10c", frame="WSen") | ||||||||||||||||||||
for i, y_justify in enumerate(["T", "M", "B"]): | ||||||||||||||||||||
y_pos = 3.5 - i*1.5 | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
fig.plot(x=[0., 4.], y=[y_pos, y_pos], W='3p,red@85') | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
for j, x_justify in enumerate(["L", "C", "R"]): | ||||||||||||||||||||
justify_text = x_justify + y_justify | ||||||||||||||||||||
x_pos = 0.5 + j*1.5 | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
fig.text(text=justify_text, x=x_pos, y=y_pos, | ||||||||||||||||||||
font="28p,Helvetica-Bold,black", justify=justify_text) | ||||||||||||||||||||
fig.plot(x=[x_pos, x_pos], y=[0., 4.], W="3p,red@85") | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
fig.show() | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
|
||||||||||||||||||||
######################################################################################## | ||||||||||||||||||||
# ``angle`` argument | ||||||||||||||||||||
# ------------------ | ||||||||||||||||||||
# ``angle`` is an optional argument used to specify the clockwise rotation of | ||||||||||||||||||||
# the text from the horizontal. | ||||||||||||||||||||
|
||||||||||||||||||||
fig = pygmt.Figure() | ||||||||||||||||||||
fig.basemap(region=[0, 4, 0, 4], projection="X10c", frame="WSen") | ||||||||||||||||||||
for i in range(0, 360, 30): | ||||||||||||||||||||
fig.text(text=f"` {i} Degrees", x=2, y=2, justify="LM", angle=i) | ||||||||||||||||||||
weiji14 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
fig.show() | ||||||||||||||||||||
|
||||||||||||||||||||
######################################################################################## | ||||||||||||||||||||
# Additional arguments | ||||||||||||||||||||
# -------------------- | ||||||||||||||||||||
# | ||||||||||||||||||||
# Text can be further configured by passing an argument corresponding to the | ||||||||||||||||||||
# flag names in GMT, following the same convention as described in the GMT | ||||||||||||||||||||
# documentation. It is hoped that over time more bindings to these arguments | ||||||||||||||||||||
# will be written into PyGMT. | ||||||||||||||||||||
|
||||||||||||||||||||
fig = pygmt.Figure() | ||||||||||||||||||||
fig.basemap(region=[0, 1, 0, 1], projection="X5c", frame="WSen") | ||||||||||||||||||||
fig.text(text="Green", x=0.5, y=0.5, G="green") | ||||||||||||||||||||
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. We're actually trying to document only the long-form arguments (see #473), and have made a start in #474. Just that we haven't released it yet, see differences in documentation between https://www.pygmt.org/dev/api/generated/pygmt.Figure.text.html and https://www.pygmt.org/v0.1.1/api/generated/pygmt.Figure.text.html#pygmt.Figure.text. I've been thinking whether we should cut a v0.1.2 release to incorporate this PR and other documentation changes, before GMT 6.1.0 comes out around end of June. Personally I have nothing against using short aliases like this to achieve additional functionality (since I do it too!). Better way would be to add an alias for G=color here, see #385 for an example on how to do so. 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. Yes, I was planning to collect some thoughts on how best to bring the rest of the available flags, or short-form arguments, into the PyGMT fold for So for now, should I remove this last section from the tutorial? 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. Or we could do a quick PR to get some extra aliases into 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. It would make sense to also add an alias for W ( I find having 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.
Yes, W=pen seems like another obvious choice.
Looking at the current v0.1.1 Lines 864 to 872 in 809a888
I think if you want to emulate But yes, you're right that it's an ambiguous beast. We could either document this particular case for GMT 'power' users, or find a better way to implement the code block above, bearing in mind that PyGMT is meant to be accessible to new users (see Project Goals) who might not be as familiar with the GMT suite. It would be nice to somewhat emulate what 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.
True, should allow 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. Ok, I've started PR #481 to handle this, so 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. In which case, perhaps it would be best to wait until the above has been merged, so we can give the example as: fig = pygmt.Figure()
with pygmt.config(MAP_FRAME_TYPE="plain"):
fig.basemap(region=[108, 120, -5, 8], projection="M20c", frame="a")
fig.coast(land="black", water="skyblue")
# Create file
with open("examples.txt", "w") as f:
f.write("114 0.5 0 22p,Helvetica-Bold,white CM BORNEO\n")
f.write("119 3.25 0 12p,Helvetica-Bold,black CM CELEBES SEA\n")
f.write("112 -4.6 0 12p,Helvetica-Bold,black CM JAVA SEA\n")
f.write("112 6 40 12p,Helvetica-Bold,black CM SOUTH CHINA SEA\n")
f.write("119.12 7.25 -40 12p,Helvetica-Bold,black CM SULU SEA\n")
f.write("118.4 -1 65 12p,Helvetica-Bold,black CM MAKASSAR STRAIT\n")
# Plot region names / sea names
fig.text(textfiles="examples.txt", angle=True, font=True, justify=True)
# Cleanup
os.remove("examples.txt")
fig.show() 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. Ok, we've merged in #481! If you can merge in the new changes from master into this branch (e.g. by clicking on the 'Update Branch' button), we can start to simplify some of the example code. The new 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 example here with using
Done at commit 3dde4b0, and I've also kept the section but am using |
||||||||||||||||||||
fig.show() |
Uh oh!
There was an error while loading. Please reload this page.