-
Notifications
You must be signed in to change notification settings - Fork 235
Add a gallery example to show coloring of points by categories #1006
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 35 commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
7ad7f00
Provide gallery example to show coloring of points by categories
michaelgrund 78c8f31
Merge branch 'master' into gallery-points-cat
michaelgrund c413c65
Merge branch 'master' into gallery-points-cat
michaelgrund 57b9a38
updated content based on reviews
michaelgrund 87dce83
[format-command] fixes
actions-bot 34bd57b
Update examples/gallery/plot/points-categorial.py
michaelgrund 65966e1
Update examples/gallery/plot/points-categorial.py
michaelgrund 68616b5
Update examples/gallery/plot/points-categorial.py
michaelgrund 96c82f8
Update examples/gallery/plot/points-categorial.py
michaelgrund 61be08c
Merge branch 'master' into gallery-points-cat
michaelgrund bc7dc1b
updated content based on reviews
michaelgrund 8c3c4f9
updated content based on reviews
michaelgrund 52b23c0
Update examples/gallery/plot/points-categorical.py
michaelgrund e5c71eb
corrected typo
michaelgrund cc9756e
modified docstrings
michaelgrund 1974ba5
[format-command] fixes
actions-bot 5434d2f
updates
michaelgrund e096fc6
Update examples/gallery/plot/points-categorical.py
michaelgrund b99319f
Update examples/gallery/plot/points-categorical.py
michaelgrund 9a94c90
Update examples/gallery/plot/points-categorical.py
michaelgrund a7d6db5
Update examples/gallery/plot/points-categorical.py
michaelgrund 571d99b
Update examples/gallery/plot/points-categorical.py
michaelgrund 7c8462c
Update examples/gallery/plot/points-categorical.py
michaelgrund ee153fd
Update examples/gallery/plot/points-categorical.py
michaelgrund 6a81b1b
Update examples/gallery/plot/points-categorical.py
michaelgrund 7822612
Merge branch 'master' into gallery-points-cat
michaelgrund 439d544
updates based on code review
michaelgrund 8897f7e
Update examples/gallery/plot/points-categorical.py
michaelgrund 0103af4
Update examples/gallery/plot/points-categorical.py
michaelgrund 3d54b39
Update examples/gallery/plot/points-categorical.py
michaelgrund 0164e86
Update examples/gallery/plot/points-categorical.py
michaelgrund 1f47a38
Merge branch 'master' into gallery-points-cat
michaelgrund 544f475
use underscore in filename
michaelgrund 1d01644
Merge branch 'master' into gallery-points-cat
seisman 076ad45
Move to symbols directory
seisman 6c010b0
Update examples/gallery/symbols/points_categorical.py
michaelgrund a60af87
Update examples/gallery/symbols/points_categorical.py
michaelgrund a178ae6
Update examples/gallery/symbols/points_categorical.py
michaelgrund b06b51c
Update examples/gallery/symbols/points_categorical.py
michaelgrund 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,72 @@ | ||
| """ | ||
| Color points by categories | ||
| --------------------------- | ||
| The :meth:`pygmt.Figure.plot` method can be used to plot symbols which are | ||
| color-coded by categories. In the example below, we show how the | ||
| `palmerpenguins dataset <https://github.com/allisonhorst/palmerpenguins>`__ | ||
| can be visualized. Here, we can pass the individual categories included in | ||
| the species column directly to the ``color`` parameter via | ||
michaelgrund marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ``color=df.species.cat.codes.astype(int)``. Additionally, we have to set | ||
| ``cmap=True``. A desired colormap can be selected via the :meth:`pygmt.makecpt` | ||
| method. | ||
| """ | ||
|
|
||
| import pandas as pd | ||
| import pygmt | ||
|
|
||
| # Load sample penguins data and convert 'species' column to categorical dtype | ||
| df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/penguins.csv") | ||
| df.species = df.species.astype(dtype="category") | ||
|
|
||
| # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax) | ||
| # The below example will return a numpy array like [30.0, 60.0, 12.0, 22.0] | ||
| region = pygmt.info( | ||
| table=df[["bill_length_mm", "bill_depth_mm"]], # x and y columns | ||
| per_column=True, # report the min/max values per column as a numpy array | ||
| # round the min/max values of the first two columns to the nearest multiple | ||
| # of 3 and 2, respectively | ||
| spacing=(3, 2), | ||
| ) | ||
|
|
||
| # Make a 2D categorical scatter plot, coloring each of the 3 species differently | ||
| fig = pygmt.Figure() | ||
|
|
||
| # Generate a basemap of 10 cm x 10 cm size | ||
| fig.basemap( | ||
| region=region, | ||
| projection="X10c/10c", | ||
| frame=[ | ||
| 'xafg+l"Bill length (mm)"', | ||
| 'yafg+l"Bill depth (mm)"', | ||
| 'WSen+t"Penguin size at Palmer Station"', | ||
| ], | ||
| ) | ||
|
|
||
| # Define a colormap to be used for three categories, | ||
| # define the range of the new discrete CPT using the series parameter | ||
| # (lowest_value, highest_value, interval), use color_model="+c" to write | ||
| # the discrete color palette "inferno" in categorical format | ||
michaelgrund marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| pygmt.makecpt(cmap="inferno", series=(0, 3, 1), color_model="+c") | ||
|
|
||
| fig.plot( | ||
| # Use bill length and bill depth as x and y data input, respectively | ||
| x=df.bill_length_mm, | ||
| y=df.bill_depth_mm, | ||
| # Vary each symbol size according to another feature (body mass, scaled by 7.5*10e-5) | ||
| sizes=df.body_mass_g * 7.5 * 10 ** -5, | ||
michaelgrund marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # Points colored by categorical number code | ||
| color=df.species.cat.codes.astype(int), | ||
| # Use colormap created by makecpt | ||
| cmap=True, | ||
| # Do not clip symbols that fall close to the map bounds | ||
| no_clip=True, | ||
| # Use circles as symbols with size in centimeter units | ||
| style="cc", | ||
| # Set transparency level for all symbols to deal with overplotting | ||
| transparency=40, | ||
| ) | ||
|
|
||
| # A colorbar displaying the different penguin species types will be added | ||
| # once GMT 6.2.0 is released. | ||
|
|
||
| fig.show() | ||
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.