Skip to content
25 changes: 8 additions & 17 deletions pygmt_helper/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def gen_region_fig(
# Merge with default
plot_kwargs = copy.deepcopy(DEFAULT_PLT_KWARGS) | (plot_kwargs or {})


if title:
plot_kwargs["frame_args"] = plot_kwargs.get("frame_args", []) + [
f"+t{title}".replace(" ", r"\040")
Expand All @@ -300,31 +301,21 @@ def gen_region_fig(
f"+s{subtitle}".replace(" ", r"\040")
]

config_options = {'COLOR_NAN': plot_kwargs['water_color']} | (config_options or {})
pygmt.config(**config_options)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Modifying the global GMT configuration directly can lead to unexpected side effects in other parts of the code that might use pygmt. It is safer to use a context manager (with pygmt.config(...)) to temporarily apply these settings only for the duration of this function's execution. This will prevent the configuration from 'leaking' and affecting other plots.

This would require some restructuring. For example:

def gen_region_fig(...):
    # ... (setup code before config)

    config_options = {'COLOR_NAN': plot_kwargs['water_color']} | (config_options or {})
    
    if fig is None:
        fig = pygmt.Figure()

    with pygmt.config(**config_options):
        # ... (all plotting logic using fig and pygmt)
    
    return fig

This change would make the function more robust and self-contained.

Copy link
Contributor Author

@lispandfound lispandfound Aug 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I tossed up using this in a with context before making the PR. In my opinion, this is not a good idea because many of the config options are set in gen_region_fig with the expectation that they persist outside the invocation of the function. For example, fonts. I also thought about just setting the COLOR_NAN locally and everything else globally. @claudio525 what do you think? Would the user be surprised if the NaN colour is the ocean? Maybe it will make them think something is wrong?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is fine as is for now, it matches with the current implementation. If this turns out to be an issue down the line, then I think we do a refactor, and change all config_options to be applied using a context manager, but don't think there is a good reason to do this now.


if fig is None:
fig = pygmt.Figure()

if config_options:
pygmt.config(**config_options)

water_color = plot_kwargs['water_color']
plot_kwargs['frame_args'] = plot_kwargs.get('frame_args', []) + [f'+g{water_color}']
fig.basemap(
region=region if region else "NZ",
projection=projection,
frame=plot_kwargs["frame_args"],
)

# Plot coastline and background water
bg_region = region if region else fig.region
water_bg = geopandas.GeoSeries(
geometry.LineString(
[
(bg_region[0], bg_region[2]),
(bg_region[1], bg_region[2]),
(bg_region[1], bg_region[3]),
[bg_region[0], bg_region[3]],
]
)
)
fig.plot(water_bg, fill=plot_kwargs["water_color"], straight_line=True)
# Plot coastline
fig.plot(
data=map_data.coastline_df,
pen=f"{plot_kwargs['coastline_pen_width']}p,{plot_kwargs['coastline_pen_color']}",
Expand Down Expand Up @@ -379,14 +370,14 @@ def gen_region_fig(
continuous=plot_kwargs["topo_cmap_continous"],
cmap=plot_kwargs["topo_cmap"],
reverse=plot_kwargs["topo_cmap_reverse"],
no_bg=True
)

# Plot topography
fig.grdimage(
grid=topo_grid,
shading=topo_shading_grid,
cmap=True,
nan_transparent=True,
)

# Plot inland water
Expand Down
Loading