Skip to content

Commit c245644

Browse files
Add gallery example for 3-D bar plot (#4315)
1 parent d09c5a9 commit c245644

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
3-D bar plot
3+
============
4+
5+
A 3-D bar plot can be created from any collection of three-dimensional tabular data. The
6+
data points can lie on a regular grid or be irregularly scattered. A special case is
7+
creating such a 3-D bar plot based on a grid. This can be done in two steps:
8+
9+
1. Converting the grid into a table via :func:`pygmt.grd2xyz`, with columns "x", "y",
10+
and "z" for longitude, latitude, and the quantity displayed by the grid,
11+
respectively.
12+
2. Plotting this table as bars in 3-D using :meth:`pygmt.Figure.plot3d`.
13+
14+
The bars can be outlined, and the fill can be one color or based on a quantity using a
15+
colormap. For the latter, a fourth column needs to be added containing the values of the
16+
quantity for the color-coding.
17+
"""
18+
19+
# %%
20+
import pygmt
21+
from pygmt.params import Position
22+
23+
# Define a study area around northern Japan with large elevation changes
24+
region = [141, 147, 36, 43]
25+
26+
# Download a grid for the Earth relief with a resolution of 10 arc-minutes
27+
grid = pygmt.datasets.load_earth_relief(resolution="10m", region=region)
28+
29+
# Convert the grid into a pandas DataFrame, with columns for longitude ("x"), latitude
30+
# ("y") and elevation ("z")
31+
grd_df = pygmt.grd2xyz(grid=grid)
32+
zmin = grd_df["z"].min() - 50
33+
zmax = grd_df["z"].max() + 50
34+
35+
# Add a fourth column "color" for the quantity used for the color-coding of the bars,
36+
# here we use the elevation ("z")
37+
grd_df["color"] = grd_df["z"]
38+
39+
# Create a 3-D bar plot with color-coding
40+
fig = pygmt.Figure()
41+
42+
fig.basemap(
43+
region=[*region, zmin, zmax],
44+
projection="M10c",
45+
zsize="8c",
46+
frame=["WSneZ", "xaf", "yag", "za1000f500+lElevation / m"],
47+
perspective=(195, 30),
48+
)
49+
50+
pygmt.makecpt(cmap="SCM/oleron", series=(zmin, zmax))
51+
fig.plot3d(
52+
data=grd_df,
53+
# Use "o" to plot bars and give the desired size
54+
# The base of the bars is set via "+b"
55+
style=f"o0.34c+b{zmin}",
56+
cmap=True,
57+
pen="0.01p,gray30",
58+
perspective=True,
59+
)
60+
fig.colorbar(
61+
frame=["xa1000f500+lElevation", "y+lm"],
62+
position=Position("TR", cstype="inside", offset=1.4),
63+
orientation="vertical",
64+
length=7,
65+
move_text="label",
66+
label_as_column=True,
67+
)
68+
69+
fig.show()

0 commit comments

Comments
 (0)