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

Improve the gallery example for line styles #664

Merged
merged 7 commits into from
Oct 24, 2020
Merged
Changes from all 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
39 changes: 26 additions & 13 deletions examples/gallery/line/linestyles.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,36 @@
import numpy as np
import pygmt

# Generate a sample line for plotting
x = np.linspace(0, 10, 500)
y = np.sin(x)
# Generate a two-point line for plotting
x = np.array([0, 7])
y = np.array([9, 9])

fig = pygmt.Figure()
fig.basemap(region=[0, 10, -3, 3], projection="X15c/8c", frame=["xaf", "yaf", "WSrt"])
fig.basemap(region=[0, 10, 0, 10], projection="X15c/8c", frame='+t"Line Styles"')

# Plot the line using the default line style
fig.plot(x=x, y=y)

# Plot the lines using different line styles
fig.plot(x=x, y=y + 1.5, pen="1p,red,-")
fig.plot(x=x, y=y + 1.0, pen="2p,blue,.")
fig.plot(x=x, y=y + 0.5, pen="1p,red,-.")

fig.plot(x=x, y=y - 0.5, pen="2p,blue,..-")
fig.plot(x=x, y=y - 1.0, pen="3p,tomato,--.")
fig.plot(x=x, y=y - 1.5, pen="3p,tomato,4_2:2p")
fig.text(x=x[-1], y=y[-1], text="solid (default)", justify="ML", offset="0.2c/0c")

# Plot the line using different line styles
for linestyle in [
"1p,red,-", # dashed line
"1p,blue,.", # dotted line
"1p,lightblue,-.", # dash-dotted line
"2p,blue,..-", # dot-dot-dashed line
"2p,tomato,--.", # dash-dash-dotted line
"2p,tomato,4_2:2p", # A pattern of 4-point-long line segment and 2-point-gap between segment
]:
y -= 1 # Move the current line down
fig.plot(x=x, y=y, pen=linestyle)
fig.text(x=x[-1], y=y[-1], text=linestyle, justify="ML", offset="0.2c/0c")
Comment on lines +35 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we avoid using a for-loop here as there are some things here like y -= 1 and x[-1] which might not be as readable for beginner Python users. It will make the code longer, but I think it's worth showing things more explicitly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are very basic syntax even for Python beginners. Perhaps only changing y -= 1 to y = y - 1 is enough?

Copy link
Member

@weiji14 weiji14 Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using a dictionary and doing a for-loop through different y values? Something like

for y, linestyle in {1: "1p,red,-", 2: "1p,blue,.", ...}.items():
    fig.plot(...)
    fig.text(...)

The x values can be hardcoded since they're always the same (0 to 9). Only the y is changing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looping over a dictionary seems a more advanced technique. What about using x[1] and y[1] (or x[0] and y[0] for left-side labels)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I think the for-loop is fine as is.

Went down a rabbit hole of doing quoted lines following https://docs.generic-mapping-tools.org/latest/cookbook/contour-annotations.html, but somehow I couldn't escape the colon : character on the tomato line:

fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X15c/8c", frame='+t"Line Styles"')

# Plot the line using the default line style
fig.plot(x=x, y=y)
fig.text(x=x[-1], y=y[-1], text="solid (default)", justify="ML", offset="0.2c/0c")

## Plot the line using different line styles
# dashed line
fig.plot(x=x, y=y - 1, pen="1p,red,-", style='qn1:+l" 1p,red,- "')
# dotted line
fig.plot(x=x, y=y - 2, pen="1p,blue,.", style='qn1:+l" 1p,blue,. "')

# dash-dotted line
fig.plot(x=x, y=y - 3, pen="1p,lightblue,-.", style='qn1:+l" 1p,lightblue,-. "')
# dot-dot-dashed line
fig.plot(x=x, y=y - 4, pen="2p,blue,..-", style='qn1:+l" 2p,blue,..- "')

# dash-dash-dotted line
fig.plot(x=x, y=y - 5, pen="2p,tomato,--.", style='qn1:+l" 2p,tomato,--. "')
# A pattern of 4-point-long line segment and 2-point-gap between segment
fig.plot(x=x, y=y - 6, pen="2p,tomato,4_2:2p", style=r'qn1:+l" 2p,tomato,4_2\:2p "')
fig.show()

temp


# Plot the line like a railway track (black/white).
# The trick here is plotting the same line twice but with different line styles
y -= 1 # move the current line down
fig.plot(x=x, y=y, pen="5p,black")
fig.plot(x=x, y=y, pen="4p,white,20p_20p")
fig.text(x=x[-1], y=y[-1], text="5p,black", justify="ML", offset="0.2c/0.2c")
fig.text(x=x[-1], y=y[-1], text="4p,white,20p_20p", justify="ML", offset="0.2c/-0.2c")

fig.show()