1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
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, zmin, zmax)
# The below example will return a numpy array like [0., 3., 4., 8., 1., 7.]
region = pygmt.info(
table=df[["petal_width", "sepal_length", "petal_length"]], # x, y, z columns
per_column=True, # report output as a numpy array
spacing="1/2/0.5", # rounds x, y and z intervals by 1, 2 and 0.5 respectively
)
# Make our 3D scatter plot, coloring each of the 3 species differently
fig = pygmt.Figure()
pygmt.makecpt(cmap="cubhelix", color_model="+c", series=(0, 3, 1))
fig.plot3d(
x=df.petal_width,
y=df.sepal_length,
z=df.petal_length,
sizes=0.1 * df.sepal_width, # Vary each symbol size according to a data column
color=df.species.cat.codes.astype(int), # Points colored by categorical number code
cmap=True, # Use colormap created by makecpt
region=region, # (xmin, xmax, ymin, ymax, zmin, zmax)
frame=[
"WsNeZ3", # z axis label positioned on 3rd corner
'xafg+l"Petal Width"',
'yafg+l"Sepal Length"',
'zafg+l"Petal Length"',
],
style="uc", # 3D cUbe, with size in centimeter units
perspective=[315, 25], # Azimuth NorthWest (315°), at elevation 25°
zscale=1.5, # Vertical exaggeration factor
)
fig.show()
|