|
5 | 5 | The :meth:`pygmt.Figure.plot3d` method can be used to plot symbols in 3D.
|
6 | 6 | In the example below, we show how the
|
7 | 7 | `Iris flower dataset <https://en.wikipedia.org/wiki/Iris_flower_data_set>`__
|
8 |
| -can be visualized using a perspective 3-dimensional plot. The ``region`` |
| 8 | +can be visualized using a perspective 3D plot. The ``region`` |
9 | 9 | parameter has to include the :math:`x`, :math:`y`, :math:`z` axis limits in the
|
10 | 10 | form of (xmin, xmax, ymin, ymax, zmin, zmax), which can be done automatically
|
11 | 11 | using :meth:`pygmt.info`. To plot the z-axis frame, set ``frame`` as a
|
|
19 | 19 |
|
20 | 20 | # Load sample iris data, and convert 'species' column to categorical dtype
|
21 | 21 | df = pd.read_csv("https://github.com/mwaskom/seaborn-data/raw/master/iris.csv")
|
22 |
| -df["species"] = df.species.astype(dtype="category") |
| 22 | +df.species = df.species.astype(dtype="category") |
23 | 23 |
|
24 | 24 | # Use pygmt.info to get region bounds (xmin, xmax, ymin, ymax, zmin, zmax)
|
25 |
| -# The below example will return a numpy array like [0., 3., 4., 8., 1., 7.] |
| 25 | +# The below example will return a numpy array like [0.0, 3.0, 4.0, 8.0, 1.0, 7.0] |
26 | 26 | region = pygmt.info(
|
27 | 27 | table=df[["petal_width", "sepal_length", "petal_length"]], # x, y, z columns
|
28 |
| - per_column=True, # report output as a numpy array |
29 |
| - spacing="1/2/0.5", # rounds x, y and z intervals by 1, 2 and 0.5 respectively |
| 28 | + per_column=True, # report the min/max values per column as a numpy array |
| 29 | + # round the min/max values of the first three columns to the nearest multiple |
| 30 | + # of 1, 2 and 0.5, respectively |
| 31 | + spacing=(1, 2, 0.5), |
30 | 32 | )
|
31 | 33 |
|
32 |
| -# Make our 3D scatter plot, coloring each of the 3 species differently |
| 34 | +# Make a 3D scatter plot, coloring each of the 3 species differently |
33 | 35 | fig = pygmt.Figure()
|
| 36 | + |
| 37 | +# 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), |
| 39 | +# use color_model="+c" to write the discrete color palette "cubhelix" in |
| 40 | +# categorical format |
34 | 41 | pygmt.makecpt(cmap="cubhelix", color_model="+c", series=(0, 3, 1))
|
| 42 | + |
35 | 43 | fig.plot3d(
|
| 44 | + # Use petal width, sepal length and petal length as x, y and z data input, |
| 45 | + # respectively |
36 | 46 | x=df.petal_width,
|
37 | 47 | y=df.sepal_length,
|
38 | 48 | z=df.petal_length,
|
39 |
| - sizes=0.1 * df.sepal_width, # Vary each symbol size according to a data column |
40 |
| - color=df.species.cat.codes.astype(int), # Points colored by categorical number code |
41 |
| - cmap=True, # Use colormap created by makecpt |
42 |
| - region=region, # (xmin, xmax, ymin, ymax, zmin, zmax) |
| 49 | + # Vary each symbol size according to another feature (sepal width, scaled by 0.1) |
| 50 | + sizes=0.1 * df.sepal_width, |
| 51 | + # Use 3D cubes ("u") as symbols, with size in centimeter units ("c") |
| 52 | + style="uc", |
| 53 | + # Points colored by categorical number code |
| 54 | + color=df.species.cat.codes.astype(int), |
| 55 | + # Use colormap created by makecpt |
| 56 | + cmap=True, |
| 57 | + # Set map dimensions (xmin, xmax, ymin, ymax, zmin, zmax) |
| 58 | + region=region, |
| 59 | + # Set frame parameters |
43 | 60 | frame=[
|
44 | 61 | "WsNeZ3", # z axis label positioned on 3rd corner
|
45 | 62 | 'xafg+l"Petal Width"',
|
46 | 63 | 'yafg+l"Sepal Length"',
|
47 | 64 | 'zafg+l"Petal Length"',
|
48 | 65 | ],
|
49 |
| - style="uc", # 3D cUbe, with size in centimeter units |
50 |
| - perspective=[315, 25], # Azimuth NorthWest (315°), at elevation 25° |
51 |
| - zscale=1.5, # Vertical exaggeration factor |
| 66 | + # Set perspective to azimuth NorthWest (315°), at elevation 25° |
| 67 | + perspective=[315, 25], |
| 68 | + # Vertical exaggeration factor |
| 69 | + zscale=1.5, |
52 | 70 | )
|
53 | 71 | fig.show()
|
0 commit comments