Skip to content

Improve the gallery example for line styles - #664

Merged
seisman merged 7 commits into
masterfrom
improve-linestyle
Oct 24, 2020
Merged

Improve the gallery example for line styles#664
seisman merged 7 commits into
masterfrom
improve-linestyle

Conversation

@seisman

@seismanseisman commented Oct 23, 2020

Copy link
Copy Markdown
Member

Description of proposed changes

Old exampleNew example
imageimage

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

  • Run make format and make check to make sure the code follows the style guide.
  • Add tests for new features or tests that would have caught the bug that you're fixing.
  • Add new public functions/methods/classes to doc/api/index.rst.
  • Write detailed docstrings for all functions/methods.
  • If adding new functionality, add an example to docstrings or tutorials.

@seismanseisman added the documentation Improvements or additions to documentation label Oct 23, 2020
@seismanseisman added this to the 0.2.1 milestone Oct 23, 2020

@weiji14weiji14 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The new example looks great, especially with the text labels!

Comment on lines +35 to +45
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")

Copy link
Copy Markdown
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
Copy Markdown
MemberAuthor

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?

@weiji14weiji14Oct 23, 2020

Copy link
Copy Markdown
Member

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

fory, linestylein {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
Copy Markdown
MemberAuthor

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
Copy Markdown
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 stylefig.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 linefig.plot(x=x, y=y-1, pen="1p,red,-", style='qn1:+l" 1p,red,- "')
# dotted linefig.plot(x=x, y=y-2, pen="1p,blue,.", style='qn1:+l" 1p,blue,. "')
# dash-dotted linefig.plot(x=x, y=y-3, pen="1p,lightblue,-.", style='qn1:+l" 1p,lightblue,-. "')
# dot-dot-dashed linefig.plot(x=x, y=y-4, pen="2p,blue,..-", style='qn1:+l" 2p,blue,..- "')
# dash-dash-dotted linefig.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 segmentfig.plot(x=x, y=y-6, pen="2p,tomato,4_2:2p", style=r'qn1:+l" 2p,tomato,4_2\:2p "')
fig.show()

temp

Comment threadexamples/gallery/line/linestyles.py Outdated
Co-authored-by: Wei Ji <23487320+weiji14@users.noreply.github.com>
Comment on lines +35 to +45
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")

Copy link
Copy Markdown
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 stylefig.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 linefig.plot(x=x, y=y-1, pen="1p,red,-", style='qn1:+l" 1p,red,- "')
# dotted linefig.plot(x=x, y=y-2, pen="1p,blue,.", style='qn1:+l" 1p,blue,. "')
# dash-dotted linefig.plot(x=x, y=y-3, pen="1p,lightblue,-.", style='qn1:+l" 1p,lightblue,-. "')
# dot-dot-dashed linefig.plot(x=x, y=y-4, pen="2p,blue,..-", style='qn1:+l" 2p,blue,..- "')
# dash-dash-dotted linefig.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 segmentfig.plot(x=x, y=y-6, pen="2p,tomato,4_2:2p", style=r'qn1:+l" 2p,tomato,4_2\:2p "')
fig.show()

temp

Comment threadexamples/gallery/line/linestyles.py Outdated
@seisman

Copy link
Copy Markdown
MemberAuthor

somehow I couldn't escape the colon : character on the tomato line:

Looks like another upstream bug. Here is a minimum bash script to reproduce the bug.

gmt plot -R0/10/0/10 -JX10c -Sqn1:+lA:B -W2p,tomato,4_2:2p -pdf map << EOF
0 5
10 5
EOF

@weiji14weiji14 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@seisman

Copy link
Copy Markdown
MemberAuthor

gmt plot -R0/10/0/10 -JX10c -Sqn1:+lA:B -W2p,tomato,4_2:2p -pdf map << EOF
0 5
10 5
EOF

FYI, I opened a bug report in upstream GMT (GenericMappingTools/gmt#4369).

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentationImprovements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@seisman@weiji14