Skip to content

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 39 commits into from
Mar 12, 2021
Merged
Changes from all 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 Mar 4, 2021
78c8f31
Merge branch 'master' into gallery-points-cat
michaelgrund Mar 4, 2021
c413c65
Merge branch 'master' into gallery-points-cat
michaelgrund Mar 5, 2021
57b9a38
updated content based on reviews
michaelgrund Mar 5, 2021
87dce83
[format-command] fixes
actions-bot Mar 5, 2021
34bd57b
Update examples/gallery/plot/points-categorial.py
michaelgrund Mar 6, 2021
65966e1
Update examples/gallery/plot/points-categorial.py
michaelgrund Mar 6, 2021
68616b5
Update examples/gallery/plot/points-categorial.py
michaelgrund Mar 6, 2021
96c82f8
Update examples/gallery/plot/points-categorial.py
michaelgrund Mar 6, 2021
61be08c
Merge branch 'master' into gallery-points-cat
michaelgrund Mar 6, 2021
bc7dc1b
updated content based on reviews
michaelgrund Mar 6, 2021
8c3c4f9
updated content based on reviews
michaelgrund Mar 7, 2021
52b23c0
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 7, 2021
e5c71eb
corrected typo
michaelgrund Mar 7, 2021
cc9756e
modified docstrings
michaelgrund Mar 7, 2021
1974ba5
[format-command] fixes
actions-bot Mar 7, 2021
5434d2f
updates
michaelgrund Mar 7, 2021
e096fc6
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 9, 2021
b99319f
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
9a94c90
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
a7d6db5
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
571d99b
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
7c8462c
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
ee153fd
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
6a81b1b
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
7822612
Merge branch 'master' into gallery-points-cat
michaelgrund Mar 10, 2021
439d544
updates based on code review
michaelgrund Mar 10, 2021
8897f7e
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 10, 2021
0103af4
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 11, 2021
3d54b39
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 11, 2021
0164e86
Update examples/gallery/plot/points-categorical.py
michaelgrund Mar 11, 2021
1f47a38
Merge branch 'master' into gallery-points-cat
michaelgrund Mar 11, 2021
544f475
use underscore in filename
michaelgrund Mar 11, 2021
1d01644
Merge branch 'master' into gallery-points-cat
seisman Mar 12, 2021
076ad45
Move to symbols directory
seisman Mar 12, 2021
6c010b0
Update examples/gallery/symbols/points_categorical.py
michaelgrund Mar 12, 2021
a60af87
Update examples/gallery/symbols/points_categorical.py
michaelgrund Mar 12, 2021
a178ae6
Update examples/gallery/symbols/points_categorical.py
michaelgrund Mar 12, 2021
b06b51c
Update examples/gallery/symbols/points_categorical.py
michaelgrund Mar 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions examples/gallery/symbols/points_categorical.py
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
`Palmer Penguins 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
``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 series=(lowest_value, highest_value, interval),
# use color_model="+c" to write the discrete color palette "inferno" in
# categorical format
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.5e-5,
# 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()