-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide gallery example to show coloring of points by categories
To close the >2 year old issue #244 here's a gallery example highlighting the coloring of points by categories.
- Loading branch information
1 parent
ce5cd2f
commit 7ad7f00
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,48 @@ | ||
""" | ||
Color points by categories | ||
--------------------------- | ||
The :meth:`pygmt.Figure.plot` method can be used to plot symbols which are | ||
color-coded by categories. | ||
""" | ||
|
||
import numpy as np | ||
import pandas as pd | ||
import pygmt | ||
|
||
# Load sample iris data, and convert 'species' column to categorical dtype | ||
df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/iris.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 [2. 4.4 4.3 7.9] | ||
region = pygmt.info( | ||
table=df[["sepal_width", "sepal_length"]], # x and y columns | ||
per_column=True, # report output as a numpy array | ||
) | ||
|
||
# Make our 2D categorial scatter plot, coloring each of the 3 species differently | ||
fig = pygmt.Figure() | ||
|
||
# Generate basemap of 10cm x 10cm size | ||
fig.basemap( | ||
region=region, | ||
projection="X10c/10c", | ||
frame=['xafg+l"Sepal Width"', 'yafg+l"Sepal Length"', "WSen"], | ||
) | ||
|
||
# Define colormap to use for three categories | ||
pygmt.makecpt(cmap="inferno", color_model="+c", series=(0, 3, 1)) | ||
|
||
fig.plot( | ||
x=df.sepal_width, # Use one feature as x data input | ||
y=df.sepal_length, # Use another feature as y data input | ||
sizes=df.petal_width | ||
/ df.petal_length, # Vary each symbol size according to the ratio of the two remaining features | ||
color=df.species.cat.codes.astype(int), # Points colored by categorical number code | ||
cmap=True, # Use colormap created by makecpt | ||
no_clip=True, # Do not clip symbols that fall exactly on the map bounds | ||
style="cc", # Use circles as symbols with size in centimeter units | ||
transparency=40, # Set transparency level for all symbols to deal with overplotting | ||
) | ||
|
||
fig.show() |