-
Notifications
You must be signed in to change notification settings - Fork 235
Add gallery example "Scale bar" #2822
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 43 commits
d5394f2
3808564
38540cb
bbf06d2
8b8bad7
0851337
1bec367
0804898
6d14f88
617b715
5c169a7
0875174
7a5c127
dcd8941
54e7ed3
aa9fa95
19468ea
9b8281c
ec322e4
019732f
0981d57
ad63900
9af9b1e
047d01d
55bbaf4
b7fc7da
7222642
909678c
64c811a
e6fc3a0
94c9ac4
14a7cfd
4430223
93bcff0
beaa1a6
937fe2e
6023891
8f29791
fcf3164
0cfdf75
88baf28
b08b6be
d8065ed
fd504d0
cf0d1a6
1335750
5e519ad
311cca7
40fd4f8
c8f27c1
60dea40
9f73a45
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 | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,141 @@ | ||||||||||||||||||||||||||||||||
| r""" | ||||||||||||||||||||||||||||||||
| Scale bar | ||||||||||||||||||||||||||||||||
| ========= | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| The ``map_scale`` parameter of the :meth:`pygmt.Figure.basemap` and | ||||||||||||||||||||||||||||||||
| :meth:`pygmt.Figure.coast` methods is used to add a scale bar to a map. | ||||||||||||||||||||||||||||||||
| This example shows how such a scale bar can be customized: | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| - position: **g**\|\ **j**\|\ **J**\|\ **n**\|\ **x**. Set the position | ||||||||||||||||||||||||||||||||
| of the reference point. Choose from | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| - **g**: Give map coordinates as *longitude*\/\ *latitude*. | ||||||||||||||||||||||||||||||||
| - **j**\|\ **J**: Specify a two-character (order independent) code. | ||||||||||||||||||||||||||||||||
| Choose from vertical **T**\(op), **M**\(iddle), or **B**\(ottom) and | ||||||||||||||||||||||||||||||||
| horizontal **L**\(eft), **C**\(entre), or **R**\(ight). Lower / upper | ||||||||||||||||||||||||||||||||
| case **j** / **J** mean inside / outside of the map bounding box. | ||||||||||||||||||||||||||||||||
| - **n**: Give normalized bounding box coordinates as *nx*\/\ *ny*. | ||||||||||||||||||||||||||||||||
| - **x**: Give plot coordinates as *x*\/\ *y*. | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| - length: **+w**. Give a distance value, and, optionally a distance unit. | ||||||||||||||||||||||||||||||||
| Choose from **e** (meters), **f** (feet), **k** (kilometers) [Default], | ||||||||||||||||||||||||||||||||
| **M** (statute miles), **n** (nautical miles), or **u** (US survey feet). | ||||||||||||||||||||||||||||||||
| - origin: **+c**\ [*slon*/]\ *slat*. Control where on the map the scale bar | ||||||||||||||||||||||||||||||||
| applies. If **+c** is not given the reference point is used. If only | ||||||||||||||||||||||||||||||||
| **+c** is appended the middle of the map is used. Note that *slon* is only | ||||||||||||||||||||||||||||||||
| optional for projections with constant scale along parallels, e.g., | ||||||||||||||||||||||||||||||||
| Mercator projection. | ||||||||||||||||||||||||||||||||
| - justify: **+j**. Set the anchor point. Specify a two-character (order | ||||||||||||||||||||||||||||||||
| independent) code. Choose from vertical **T**\(op), **M**\(iddle), or | ||||||||||||||||||||||||||||||||
| **B**\(ottom) and horizontal **L**\(eft), **C**\(entre), or **R**\(ight). | ||||||||||||||||||||||||||||||||
| - offset: **+o**\ *offset* or **+o**\ *xoffset*/\ *yoffset*. Give either a | ||||||||||||||||||||||||||||||||
| common shift or individual shifts in x (longitude) and y (latitude) | ||||||||||||||||||||||||||||||||
| directions. | ||||||||||||||||||||||||||||||||
| - height: Use :gmt-term:`MAP_SCALE_HEIGHT` via :func:`pygmt.config`. | ||||||||||||||||||||||||||||||||
| - fancy style: **+f**. Get a scale bar that looks like train tracks. | ||||||||||||||||||||||||||||||||
| - unit: **+u**. Add the distance unit given via **+w** to the single | ||||||||||||||||||||||||||||||||
| distance values. | ||||||||||||||||||||||||||||||||
| - label: **+l**. Add the distance unit given via **+w** as label. Append | ||||||||||||||||||||||||||||||||
| text to get a customized label instead. | ||||||||||||||||||||||||||||||||
| - alignment: **+a**. Set the label alignment. Choose from **t**\(op) | ||||||||||||||||||||||||||||||||
| [Default], **b**\(ottom), **l**\(eft), or **r**\(ight). | ||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # %% | ||||||||||||||||||||||||||||||||
| import pygmt | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Create a new Figure instance | ||||||||||||||||||||||||||||||||
| fig = pygmt.Figure() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| # Add a plain scale bar | ||||||||||||||||||||||||||||||||
| fig.basemap( | ||||||||||||||||||||||||||||||||
| region=[-45, -25, -15, 0], | ||||||||||||||||||||||||||||||||
| projection="M0/0/10c", # Mercator projection with 10 centimeters width | ||||||||||||||||||||||||||||||||
| frame=["WSne", "af"], | ||||||||||||||||||||||||||||||||
| # The scale bar is placed at position (j) MiddleCenter, applies at the | ||||||||||||||||||||||||||||||||
| # reference point (+c is not given), and represents a length (+w) of 1000 | ||||||||||||||||||||||||||||||||
| # kilometers | ||||||||||||||||||||||||||||||||
| map_scale="jMC+w1000k", | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| fig.shift_origin(xshift="+w1c") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| # Add a fancy scale bar | ||||||||||||||||||||||||||||||||
| fig.basemap( | ||||||||||||||||||||||||||||||||
| region=[-45, -25, -15, 0], | ||||||||||||||||||||||||||||||||
| projection="M10c", | ||||||||||||||||||||||||||||||||
| frame=["wSnE", "af"], | ||||||||||||||||||||||||||||||||
| # Place the scale bar at position (j) MiddleLeft by using MiddleLeft as | ||||||||||||||||||||||||||||||||
| # anchor point (+j) with an offset (+o) of 1 centimeter in x direction | ||||||||||||||||||||||||||||||||
| # (longitude) and 0 centimeters in y direction (latitude) | ||||||||||||||||||||||||||||||||
| # Use a fancy style (+f) to get a scale bar that looks like train tracks | ||||||||||||||||||||||||||||||||
| # Add the distance unit (+u) to the single distance values | ||||||||||||||||||||||||||||||||
| map_scale="jML+jML+o1c/0c+w1000k+f+u", | ||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||
yvonnefroehlich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| fig.show() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # %% | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # Create a new Figure instance | ||||||||||||||||||||||||||||||||
| fig = pygmt.Figure() | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| # Add a thick scale bar | ||||||||||||||||||||||||||||||||
| # Adjust the GMT default parameter MAP_SCALE_HEIGHT locally (the change applies | ||||||||||||||||||||||||||||||||
| # only to the code within the "with" block) | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| # only to the code within the "with" block) | |
| # only to the code within the "with" statement) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. I am still not 100 % sure whether within the "with" statement sounds better than within the "with" block and makes sense. Maybe we can write under the "with" statement if we want to use statement instead of block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel 'within the "with" statement' is better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Should I also update the formulations in the Figure.inset line example:
Lines 119 to 131 in 06ae818
| >>> # Use a "with" statement to initialize the inset context manager | |
| >>> # Setting the position to top left and a width of 3.5 centimeters | |
| >>> with fig.inset(position="jTL+w3.5c+o0.2c", margin=0, box="+pgreen"): | |
| ... # Map elements under the "with" statement are plotted in the inset | |
| ... fig.coast( | |
| ... region="g", | |
| ... projection="G47/-20/3.5c", | |
| ... land="gray", | |
| ... water="white", | |
| ... dcw="MG+gred", | |
| ... ) | |
| ... | |
| >>> # Map elements outside the "with" block are plotted in the main figure |
Uh oh!
There was an error while loading. Please reload this page.