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

Conversation

seisman
Copy link
Member

@seisman seisman commented Oct 23, 2020

Description of proposed changes

Old example New example
image image

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.

@seisman seisman added the documentation Improvements or additions to documentation label Oct 23, 2020
@seisman seisman added this to the 0.2.1 milestone Oct 23, 2020
Copy link
Member

@weiji14 weiji14 left a comment

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
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

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
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

@seisman
Copy link
Member Author

seisman commented Oct 24, 2020

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

Copy link
Member

@weiji14 weiji14 left a comment

Choose a reason for hiding this comment

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

LGTM!

@seisman
Copy link
Member Author

seisman commented Oct 24, 2020

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 free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants