Skip to content
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 gallery example showing how to adjust line segment ends (caps and joints) #3015

Merged
merged 20 commits into from
Jan 27, 2024
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/gallery/lines/line_segment_ends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""
Line segment caps and joints
============================
PyGMT offers different appearance of line segment caps and joints. The desired
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
appearance can be set via the GMT default parameters :gmt-term:`PS_LINE_CAP`
(``"butt"``, ``"round"``, or ``"square"`` [Default]) as well as :gmt-term:`PS_LINE_JOIN`
(``"bevel"``, ``"round"``, and ``"miter"`` [Default]) and :gmt-term:`PS_MITER_LIMIT`
(limit on the angle at the mitered joint below which a bevel is applied).
"""

# %%
import numpy as np
import pygmt

# Set up dictionary for colors
dict_col = {
"round": "green4",
"square": "steelblue",
"butt": "orange",
"miter": "steelblue",
"bevel": "orange",
}
seisman marked this conversation as resolved.
Show resolved Hide resolved

# Create new Figure instance
fig = pygmt.Figure()

# -----------------------------------------------------------------------------
# Top: PS_LINE_CAP

# Create sample data
x = np.array([30, 170])
y = np.array([70, 70])

fig.basemap(region=[0, 250, 0, 100], projection="x1p", frame="rltb")

for line_cap in ["butt", "round", "square"]:
# Change GMT default locally using a context manager
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
# The change applies only to the code under the with statement
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
with pygmt.config(PS_LINE_CAP=line_cap):
# Plot dashed line
color = dict_col[line_cap]
fig.plot(x=x, y=y, pen=f"10p,{color},20_20:0")

fig.plot(x=x, y=y, pen="1p")
fig.plot(x=x, y=y, style="c0.1c", fill="white", pen="0.5p")
fig.text(text=line_cap, x=x[-1] + 20, y=y[-1], justify="LM")

y = y - 20

fig.shift_origin(yshift="-h")

# -----------------------------------------------------------------------------
# Bottom: PS_LINE_JOIN and PS_MITER_LIMIT

# Create sample data
x = np.array([[5, 95, 65], [80, 170, 140], [155, 235, 205]])
y = np.array([[10, 70, 10], [10, 70, 10], [10, 70, 10]])
seisman marked this conversation as resolved.
Show resolved Hide resolved

fig.basemap(region=[0, 250, 0, 100], projection="x1p", frame="rltb")

for i_line, line_join in enumerate(["bevel", "round", "miter"]):
x_temp = x[i_line, :]
y_temp = y[i_line, :]
# Change GMT default locally using a context manager
# The change applies only to the code under the with statement
yvonnefroehlich marked this conversation as resolved.
Show resolved Hide resolved
with pygmt.config(PS_LINE_JOIN=line_join, PS_MITER_LIMIT=1):
# Plot solid line
color = dict_col[line_cap]
fig.plot(x=x_temp, y=y_temp, pen=f"7p,{color},solid")

fig.plot(x=x_temp, y=y_temp, pen="1p")
fig.plot(x=x_temp, y=y_temp, style="c0.1c", fill="white", pen="0.5p")
fig.text(text=line_join, x=x_temp[1] - 10, y=y_temp[1], justify="RB")

fig.show()