-
Notifications
You must be signed in to change notification settings - Fork 234
Add an advanced tutorial for creating legends #3594
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
60 commits
Select commit
Hold shift + click to select a range
42c07fb
Add code legend via tringio
yvonnefroehlich 6f7751d
Merge remote-tracking branch 'origin/main' into add-gallery-legend-spec
yvonnefroehlich c4e2baa
Merge remote-tracking branch 'origin/main' into add-gallery-legend-spec
yvonnefroehlich b1d10ea
Merge remote-tracking branch 'origin/main' into add-gallery-legend-spec
yvonnefroehlich 9db4522
Merge remote-tracking branch 'origin/main' into add-gallery-legend-spec
yvonnefroehlich 79f6b3e
Update docstring
yvonnefroehlich 497a275
Addm more docs
yvonnefroehlich 3012359
Add remaining section | Move file to tutorials
yvonnefroehlich 3cfefea
Add width
yvonnefroehlich b81a42a
Fix shinx gallery layout
yvonnefroehlich 8ec4388
Fix forma | Remove execution permission
yvonnefroehlich 17d5f15
Add more documentation
yvonnefroehlich f429342
Use Cartesian projection
yvonnefroehlich 9633aad
Update title
yvonnefroehlich 4fe8473
Adjust file name
yvonnefroehlich b6f6045
Use Mercator projection
yvonnefroehlich bf84f6a
Adjust position of example data point
yvonnefroehlich ac0b469
Adjust width of legend
yvonnefroehlich 6d40cea
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich e4a966b
Improve docs
e0ef881
Use long list for 'region' | Fix typo | Improve comments
yvonnefroehlich 46e8340
Adjust position of legend instead of datapoint
yvonnefroehlich 907c219
Add docs
yvonnefroehlich 92f76bc
Fix highlighting | Remove sentence
yvonnefroehlich f806a06
Fix typos
yvonnefroehlich de182e8
Add highighting
yvonnefroehlich 7077497
Use link syntax
yvonnefroehlich 991d56f
Add hilighting
yvonnefroehlich 0ea13d4
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich 4e9d842
Fix link
yvonnefroehlich 49773b4
Mention other plotting method with have the 'label' parameter for legend
yvonnefroehlich a47babf
Use multiple symbols to show legend layout
yvonnefroehlich a367101
Improve docs
yvonnefroehlich db75fee
Add blank line
yvonnefroehlich eb72131
Add comments
yvonnefroehlich 02da95a
Combine subsections
yvonnefroehlich 3afe1fd
Fix typo
yvonnefroehlich 038f9c4
Improve formulation
yvonnefroehlich 5eeff8b
Fix coding style
yvonnefroehlich 380a58f
Add docs for legend codes
yvonnefroehlich 55c8215
Rewrap to 88 chars
yvonnefroehlich 60054fc
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich d2cefc0
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich 63e5b29
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich 21a3d53
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich eb4ca3b
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich 3717f2f
Update docs
yvonnefroehlich fc68bea
Show line usage | Update docs
yvonnefroehlich dc48e3b
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich ddfd838
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich e0c2434
Mention 'hline' and 'vline' methods
yvonnefroehlich 31273a6
Improve formulation
yvonnefroehlich 4fcfd53
Rewrap
yvonnefroehlich 8f580dc
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich 984549c
Set links to GMT docs | Fix typos
yvonnefroehlich 601ccdf
Improve formulation
yvonnefroehlich 96ae208
Fix typo
yvonnefroehlich 85911c4
Fix typos
yvonnefroehlich 03ba644
Fix typos
yvonnefroehlich 2ca1619
Merge branch 'main' into add-gallery-legend-spec
yvonnefroehlich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| """ | ||
| Creating legends | ||
| ================ | ||
|
|
||
| The :meth:`pygmt.Figure.legend` method creates legends, whereby auto-legends as | ||
| well as manually created legends are supported. | ||
|
|
||
| Users can adjust the position of the legend and add a box around the legend. | ||
| """ | ||
|
|
||
| # %% | ||
| import io | ||
|
|
||
| import pygmt | ||
|
|
||
| # %% | ||
| # Create an auto-legend | ||
| # --------------------- | ||
| # | ||
| # For auto-legends, the ``label`` parameter of :meth:`pygmt.Figure.plot` has to | ||
| # be specified to state the desired text in the legend entry (white spaces are | ||
| # allowed). Optionally, to adjust the legend, users can append different modifiers | ||
| # to the string passed to ``label`. A list of all available modifiers can be | ||
| # found at :gmt-docs:`gmt.html#l-full`. To create a multiple-column legend **+N** | ||
| # is used with the desired number of columns; see also gallery example | ||
| # https://www.pygmt.org/dev/gallery/embellishments/legend.html. | ||
yvonnefroehlich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| fig = pygmt.Figure() | ||
| fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
|
|
||
| fig.plot(x=0, y=-3, style="c0.25c", fill="orange", label="orange circle") | ||
| fig.legend() | ||
|
|
||
| fig.show() | ||
|
|
||
|
|
||
| # %% | ||
| # Adjust the position | ||
| # ------------------- | ||
| # Use the ``position`` parameter to adjust the position of the legend. | ||
| # j within, J outside of bounding box | ||
| # also adjust ``width`` via **+w** modifier | ||
| # The default of ``box`` is changed, i.e. no box plotted anymore. | ||
|
|
||
| fig = pygmt.Figure() | ||
| fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
|
|
||
| fig.plot(x=0, y=-3, style="c0.25c", fill="orange", label="orange circle") | ||
| fig.legend(position="jMC") | ||
seisman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| fig.show() | ||
|
|
||
|
|
||
| # %% | ||
| # Add a box | ||
| # --------- | ||
| # ``box`` parameter, Default: 1-point thick, black, solid outline with white fill. | ||
| # The default of ``position`` is preserved. | ||
| # **+p** for outline | ||
| # **+g** for fill | ||
|
|
||
| fig = pygmt.Figure() | ||
| fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
|
|
||
| fig.plot(x=0, y=-3, style="c0.25c", fill="orange", label="orange circle") | ||
| fig.legend(position="jMC", box=True) | ||
|
|
||
| fig.shift_origin(xshift="w+1c") | ||
| fig.basemap(region=[-5, 5, -5, 5], projection="X5c", frame=True) | ||
|
|
||
| fig.plot(x=0, y=-3, style="c0.25c", fill="orange", label="orange circle") | ||
| fig.legend(position="jMC", box="+p2p,cyan+gblue@70") | ||
|
|
||
| fig.show() | ||
|
|
||
|
|
||
| # %% | ||
| # Create a manual legend | ||
| # ---------------------- | ||
| # | ||
| # For more complicated legends in GMT, users need to write an ASCII file with | ||
| # instructions for the layout of the legend items. In PyGMT it is addionaly | ||
yvonnefroehlich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # possible to provide this information as an ``io.StringIO`` object. Both, the | ||
yvonnefroehlich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # ASCII file or the ``io.StringIO`` object are passed to the ``spec`` parameter | ||
yvonnefroehlich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # of :meth:`pygmt.Figure.legend`. | ||
| # | ||
seisman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # The example below is orientated on the upstream GMT example at | ||
| # https://docs.generic-mapping-tools.org/dev/legend.html#examples. | ||
|
|
||
|
|
||
| # %% | ||
| # First, we set up an ``io.StringIO`` object. | ||
yvonnefroehlich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| spec_io = io.StringIO( | ||
| """ | ||
| G -0.1c | ||
| H 24p,Times-Roman My Map Legend | ||
| D 0.2c 1p | ||
| N 2 | ||
| V 0 1p | ||
| S 0.1c c 0.15c p300/12 0.25p 0.3c This circle is hachured | ||
| S 0.1c e 0.15c yellow 0.25p 0.3c This ellipse is yellow | ||
| S 0.1c w 0.15c green 0.25p 0.3c This wedge is green | ||
| S 0.1c f 0.25c blue 0.25p 0.3c This is a fault | ||
| S 0.1c - 0.15c - 0.25p,- 0.3c A contour | ||
| S 0.1c v 0.25c magenta 0.5p 0.3c This is a vector | ||
| S 0.1c i 0.15c cyan 0.25p 0.3c This triangle is boring | ||
| D 0.2c 1p | ||
| V 0 1p | ||
| N 1 | ||
| M 5 5 600+u+f | ||
| G 0.05c | ||
| I @SOEST_block4.png 3i CT | ||
| G 0.05c | ||
| G 0.05c | ||
| L 9p,Times-Roman R Smith et al., @%5%J. Geophys. Res., 99@%%, 2000 | ||
| G 0.1c | ||
| T Let us just try some simple text that can go on a few lines. | ||
| T There is no easy way to predetermine how many lines may be required | ||
| T so we may have to adjust the height to get the right size box. | ||
| """ | ||
| ) | ||
|
|
||
| # %% | ||
| # Now, we can add a legend based on this `io.StringIO` object. For | ||
| # multi-columns legends, width (**+w**) has to be specified via a the | ||
| # ``position`` parameter. | ||
|
|
||
| fig = pygmt.Figure() | ||
| # Note, that we are now using a Mercator projection | ||
| fig.basemap(region=[-5, 5, -5, 5], projection="M10c", frame=True) | ||
|
|
||
| # Pass the io.StringIO object to the spec parameter | ||
| fig.legend(spec=spec_io, position="jMC+w9c", box="+p1p,gray50+ggray95") | ||
|
|
||
| fig.show() | ||
|
|
||
| # sphinx_gallery_thumbnail_number = 4 | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.