|
17 | 17 | import pandas as pd
|
18 | 18 | import pygmt
|
19 | 19 |
|
20 |
| -# Load sample iris data and convert 'species' column to categorical dtype |
| 20 | +# Load sample iris data |
21 | 21 | 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 |
22 | 28 | 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) |
23 | 34 |
|
24 | 35 | # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax, zmin, zmax)
|
25 | 36 | # The below example will return a numpy array [0.0, 3.0, 4.0, 8.0, 1.0, 7.0]
|
|
35 | 46 | fig = pygmt.Figure()
|
36 | 47 |
|
37 | 48 | # 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 |
40 | 51 | # palette "cubhelix" in categorical format and add the species names as
|
41 | 52 | # annotations for the colorbar
|
42 | 53 | 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), |
44 | 61 | )
|
45 | 62 |
|
46 | 63 | fig.plot3d(
|
|
0 commit comments