Skip to content

Commit 056483a

Browse files
Improvement of two gallery examples regarding categorical colormaps (#1934)
Clarify that by default the labels for the colorbar must be given in alphabetical order. * add remark regarding categorical number code * use flexible arguments in makecpt * shorten and replace remark regarding categorical number code * fix typos and shorten text based on review
1 parent 25c06dc commit 056483a

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

examples/gallery/3d_plots/scatter3d.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@
1717
import pandas as pd
1818
import pygmt
1919

20-
# Load sample iris data and convert 'species' column to categorical dtype
20+
# Load sample iris data
2121
df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/iris.csv")
22+
# Convert 'species' column to categorical dtype
23+
# By default, pandas sorts the individual categories in an alphabetical order.
24+
# For a non-alphabetical order, you have to manually adjust the list of
25+
# categories. For handling and manipulating categorical data in pandas,
26+
# have a look at:
27+
# https://pandas.pydata.org/docs/user_guide/categorical.html
2228
df.species = df.species.astype(dtype="category")
29+
# Make a list of the individual categories of the 'species' column
30+
# ['setosa', 'versicolor', 'virginica']
31+
# They are (corresponding to the categorical number code) by default in
32+
# alphabetical order and later used for the colorbar labels
33+
labels = list(df.species.cat.categories)
2334

2435
# Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax, zmin, zmax)
2536
# The below example will return a numpy array [0.0, 3.0, 4.0, 8.0, 1.0, 7.0]
@@ -35,12 +46,18 @@
3546
fig = pygmt.Figure()
3647

3748
# Define a colormap to be used for three categories, define the range of the
38-
# new discrete CPT using series=(lowest_value, highest_value, interval), use
39-
# color_model="+cSetosa,Versicolor,Virginica" to write the discrete color
49+
# new discrete CPT using series=(lowest_value, highest_value, interval),
50+
# use color_model="+csetosa,versicolor,virginica" to write the discrete color
4051
# palette "cubhelix" in categorical format and add the species names as
4152
# annotations for the colorbar
4253
pygmt.makecpt(
43-
cmap="cubhelix", color_model="+cSetosa,Versicolor,Virginica", series=(0, 2, 1)
54+
cmap="cubhelix",
55+
# Use the minimum and maximum of the categorical number code
56+
# to set the lowest_value and the highest_value of the CPT
57+
series=(df.species.cat.codes.min(), df.species.cat.codes.max(), 1),
58+
# convert ['setosa', 'versicolor', 'virginica'] to
59+
# 'setosa,versicolor,virginica'
60+
color_model="+c" + ",".join(labels),
4461
)
4562

4663
fig.plot3d(

examples/gallery/symbols/points_categorical.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,20 @@
1414
import pandas as pd
1515
import pygmt
1616

17-
# Load sample penguins data and convert 'species' column to categorical dtype
17+
# Load sample penguins data
1818
df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/penguins.csv")
19+
# Convert 'species' column to categorical dtype
20+
# By default, pandas sorts the individual categories in an alphabetical order.
21+
# For a non-alphabetical order, you have to manually adjust the list of
22+
# categories. For handling and manipulating categorical data in pandas,
23+
# have a look at:
24+
# https://pandas.pydata.org/docs/user_guide/categorical.html
1925
df.species = df.species.astype(dtype="category")
26+
# Make a list of the individual categories of the 'species' column
27+
# ['Adelie', 'Chinstrap', 'Gentoo']
28+
# They are (corresponding to the categorical number code) by default in
29+
# alphabetical order and later used for the colorbar labels
30+
labels = list(df.species.cat.categories)
2031

2132
# Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax)
2233
# The below example will return a numpy array like [30.0, 60.0, 12.0, 22.0]
@@ -48,7 +59,14 @@
4859
# use color_model="+cAdelie,Chinstrap,Gentoo" to write the discrete color
4960
# palette "inferno" in categorical format and add the species names as
5061
# annotations for the colorbar
51-
pygmt.makecpt(cmap="inferno", series=(0, 2, 1), color_model="+cAdelie,Chinstrap,Gentoo")
62+
pygmt.makecpt(
63+
cmap="inferno",
64+
# Use the minimum and maximum of the categorical number code
65+
# to set the lowest_value and the highest_value of the CPT
66+
series=(df.species.cat.codes.min(), df.species.cat.codes.max(), 1),
67+
# convert ['Adelie', 'Chinstrap', 'Gentoo'] to 'Adelie,Chinstrap,Gentoo'
68+
color_model="+c" + ",".join(labels),
69+
)
5270

5371
fig.plot(
5472
# Use bill length and bill depth as x and y data input, respectively

0 commit comments

Comments
 (0)