-
Notifications
You must be signed in to change notification settings - Fork 235
Add gallery example for 3-D bar plot #4315
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
examples/gallery/3d_plots/3d_bar.py
Outdated
| # Convert the grid into a table and add a column "color" for the quantity used for the | ||
| # color-coding of the bars, here the elevation ("z") | ||
| grd2tab = pygmt.grd2xyz(grid=grid, region=region) | ||
| grd2tab["color"] = grd2tab["z"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the "color" column used anywhere?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the "color" column is used for the color-coding.
Alternatively, we could use the incols parameter and use the second column ("z") twice:
# %%
import pygmt
from pygmt.params import Position
# Define a study area with huge elevation changes
region = [141, 147, 36, 43]
# Download a grid for the Earth relief with a resolution of 10 arc-minutes
grid = pygmt.datasets.load_earth_relief(resolution="10m", region=region)
# Convert the grid into a pandas DataFrame
grd_df = pygmt.grd2xyz(grid=grid)
zmin = grd_df["z"].min() - 50
zmax = grd_df["z"].max() + 50
# Create a 3-D bar plot with color-coding
fig = pygmt.Figure()
fig.basemap(
region=[*region, zmin, zmax],
projection="M10c",
zsize="8c",
frame=["WSneZ", "xaf", "yag", "za1000f500+lElevation / m"],
perspective=(195, 30),
)
pygmt.makecpt(cmap="SCM/oleron", series=(zmin, zmax))
fig.plot3d(
data=grd_df,
# Use "o" to plot bars and give the desired size
# The base of the bars is set via "+b"
style=f"o0.34c+b{zmin}",
cmap=True,
pen="0.01p,gray30",
perspective=True,
# Use the elevation for the color-coding of the bars
# Zero-based indexing for columns
incols=[0, 1, 2, 2], # lon, lat, z for elevation, z for color-coding
)
fig.colorbar(
frame=["xa1000f500+lElevation", "y+lm"],
position=Position("TR", cstype="inside", offset=1.4),
orientation="vertical",
length=7,
move_text="label",
label_as_column=True,
)
fig.show()There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see the point. Need to explain that plot3d requires four columns. The four column is used for color coding. In this example, the fourth column is identical to the z column, but other values are allowed
Co-authored-by: Dongdong Tian <[email protected]>
Co-authored-by: Dongdong Tian <[email protected]>
| A 3-D bar plot of a grid can be created in two steps: (i) convert the grid into a table | ||
| via :func:`pygmt.grd2xyz` and (ii) plot this table as bars in 3-D using | ||
| :meth:`pygmt.Figure.plot3d`. The bars can be outlined, and the fill can be one color or | ||
| based on a quantity using a colormap. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can create a 3-D bar plot for any collection of XYZ points, whether they lie on a regular grid or are irregularly scattered. Therefore, it’s clearer to start with a general description of the 3-D bar plot itself and then mention that, as a special case, we can convert a grid into XYZ and visualize it with the same routine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR adds a new gallery example demonstrating how to create a 3-D bar plot in PyGMT using Earth relief data. The example shows how to convert grid data to a table format and visualize it as color-coded bars in 3D space.
Changes:
- Adds a new 3-D bar plot example to the gallery
- Demonstrates grid-to-table conversion using
pygmt.grd2xyz - Shows color-coded visualization of elevation data using bars in 3D
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| import pygmt | ||
| from pygmt.params import Position | ||
|
|
||
| # Define a study area with huge elevation changes |
Copilot
AI
Jan 16, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment mentions 'huge elevation changes' but this is subjective and not specific. Consider being more descriptive about the region (e.g., 'Define a study area covering Japan's Honshu island').
| # Define a study area with huge elevation changes | |
| # Define a study area over northern Japan (around northern Honshu and southern Hokkaido) with large elevation changes |
Description of proposed changes
This PR tests to create a 3-D bar plot in PyGMT and addes a gallery example.
Preview: https://pygmt-dev--4315.org.readthedocs.build/en/4315/gallery/3d_plots/3d_bar.html
Guidelines
Slash Commands
You can write slash commands (
/command) in the first line of a comment to performspecific operations. Supported slash command is:
/format: automatically format and lint the code