Improve the gallery example for line styles#664
Conversation
b535d2f to
10f7cc6
Compare
weiji14
left a comment
There was a problem hiding this comment.
The new example looks great, especially with the text labels!
| 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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I think these are very basic syntax even for Python beginners. Perhaps only changing y -= 1 to y = y - 1 is enough?
There was a problem hiding this comment.
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.
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)?
There was a problem hiding this comment.
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()Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
| 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") |
There was a problem hiding this comment.
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()
Looks like another upstream bug. Here is a minimum bash script to reproduce the bug. |
FYI, I opened a bug report in upstream GMT (GenericMappingTools/gmt#4369). |

Description of proposed changes
Address the comment in #604 (comment).
Preview: https://pygmt-git-improve-linestyle.gmt.vercel.app/gallery/line/linestyles.html
The last line (white/black railway track style) is a little tricky. I like it but I'm OK if you think we should remove it.
Reminders
make formatandmake checkto make sure the code follows the style guide.doc/api/index.rst.