General Format#
This page introduces the general visualization template for building multi-panel figures in Python using consistent formatting and automated output naming.
🧰 Goal#
This template provides a standard way to:
Auto-name figure outputs to match the notebook or script
Create a consistent 2×3 subplot layout
Style each panel with
GeoCAT
utilitiesSave visualizations with professional layout and naming conventions
🧱 1. Load Required Packages#
We organize imports into functional groups for clarity:
import pandas as pd
import xarray as xr
import numpy as np
import os
import ipynbname
import geocat.comp as gccomp
import geocat.viz as gv
import geocat.viz.util as gvutil
import cmaps
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
ipynbname
: Detects the name of the current Jupyter Notebookgeocat.viz
: Provides consistent plotting styles (titles, labels, colors)cmaps
: Loads climate-specific colormaps (e.g., AMWG)
📁 2. Auto-Naming Output Figure Files#
To reduce manual errors in file naming, define a function that auto-generates the output filename:
def get_filename():
try:
filename = os.path.splitext(os.path.basename(__file__))[0]
except NameError:
nb_path = ipynbname.path()
filename = os.path.splitext(os.path.basename(str(nb_path)))[0]
return filename
fnFIG = get_filename() + ".png"
This will save your figure as your_notebook_name.png
, automatically matching the filename.
🖼 3. Create a 2×3 Multi-Panel Layout#
Define your plot layout using plt.subplots()
with nrows=2, ncols=3
, which creates a grid of six subplots.
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(7, 12))
col = cmaps.amwg
Each panel can be accessed using an index like this:
ip = 0
axes[ip].plot(dat.year, series)
Use gvutil.set_titles_and_labels()
to apply consistent title formatting, label size, and axis annotation:
gvutil.set_titles_and_labels(axes[ip],
maintitle="",
lefttitle="(a)",
righttitle="",
ylabel="Some Variable",
xlabel="Year",
maintitlefontsize=14,
lefttitlefontsize=12,
labelfontsize=10)
🔁 Repeat for each panel#
Update the index (ip = 1
, ip = 2
, …) and change data, titles, or labels as needed for each subplot.
🎨 Tips for Styling#
Use consistent colormaps from
cmaps.amwg
to match climate science standardsAdd horizontal lines (
axhline(0)
) for referenceSet axis limits and ticks using
gvutil.set_axes_limits_and_ticks()
orMultipleLocator
💾 4. Save and Show the Output#
Save the figure using the generated filename:
plt.tight_layout()
plt.savefig(fnFIG, dpi=300, bbox_inches="tight")
plt.show()
tight_layout()
prevents label and title clippingdpi=300
ensures publication-quality resolutionbbox_inches="tight"
trims whitespace around the plot
✅ Summary#
This general template streamlines plotting by:
Automating repetitive tasks
Enforcing visual consistency
Saving high-quality figures automatically
Adapt this template for EOF maps, correlation fields, seasonal plots, and other visualizations.