-
Notifications
You must be signed in to change notification settings - Fork 229
Improve the gallery example for line styles #664
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
10f7cc6
5c828a8
1550022
5b87e06
99fb1db
dae7dc8
77f18fa
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 |
---|---|---|
|
@@ -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
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. Could we avoid using a for-loop here as there are some things here like 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 these are very basic syntax even for Python beginners. Perhaps only changing 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. 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. 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. Looping over a dictionary seems a more advanced technique. What about using 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. 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 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() |
||
|
||
# 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 | ||
for linestyle in ["5p,black", "4p,white,20p_20p"]: | ||
fig.plot(x=x, y=y, pen=linestyle) | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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() |
Uh oh!
There was an error while loading. Please reload this page.