Transparent background export not working in pyGMT on windows

I have a rather long map (250 lines of python) I’m exporting composed of a few subplots. I’ve spent a few hours trying to export it with a transparent background and referred to some questions here on the forum, but I’ve been unable to find a solution. I’ve copied an example from the forums and modified to for a minimum working example that should yield transparent backgrounds, but doesn’t. I figure this is the right place to start.

All exported images end up with white backgrounds.

import pygmt
fig = pygmt.Figure()
fig.basemap(region=[0, 10, 0, 10], projection="X10c", frame=True)
fig.savefig(fname="test.png", transparent=True, gs_path=r"C:\Program Files\gs\gs10.05.1\bin\gswin64c")

my configs are:

PyGMT information:
  version: v0.15.0
System information:
  python: 3.12.3 | packaged by conda-forge | (main, Apr 15 2024, 18:20:11) [MSC v.1938 64 bit (AMD64)]
  executable: C:\Users\USERNAME\anaconda3\python.exe
  machine: Windows-10-10.0.19045-SP0
Dependency information:
  numpy: 2.2.3
  pandas: 2.2.3
  xarray: 2024.11.0
  netCDF4: 1.7.2
  packaging: 24.2
  contextily: None
  geopandas: 0.14.4
  IPython: 8.30.0
  pyarrow: 19.0.0
  rioxarray: None
  gdal: None
  ghostscript: 9.54.0
GMT library information:
  version: 6.5.0
  padding: 2
  share dir: c:/programs/gmt6/share
  plugin dir: c:/programs/gmt6/bin/gmt_plugins
  library path: c:/programs/gmt6/bin/gmt_w64.dll
  cores: 24
  grid layout: rows
  image layout: 
  binary version: 6.5.0

I installed gs on windows and manually put the path in as gs_path in my code, but no difference in output was observed between manual and auto gs_path.

I’d also settle for setting a background color in any unique color so I can remove it manually.

What am I missing?

Hi @tw21,

Welcome to the GMT forum :slightly_smiling_face: !

Hm. Maybe it is easier to just update the gs version in your conda environment (Anaconda.org)?

A general aspect: the path to the desired gs version contains white spaces, which is not optimal (but probably not directly changeable in this case).

Thank you - I’ve done that to yield ghostscript: 10.04.0 in pygmt.show_versions() However, transparency is still not working in the provided example. Any further suggestions?

Hm. Difficult to say, what’s going wrong here :slightly_frowning_face:. I tried different things (see codes below). For me, transparency works with gs 10.04.0 (with GMT 6.5).

Did you try using transparency for plotting data points (e.g., Scatter plots with a legend — PyGMT)? Does this also not work?


import pygmt

# (I)
fig = pygmt.Figure()
fig.basemap(region=[-5, 5] * 2, projection="X5c", frame="afg")
fig.show()
# fig.savefig(fname="test_00.png")

# (II)
fig = pygmt.Figure()
fig.basemap(region=[-5, 5] * 2, projection="X5c", frame=["afg"])
fig.show()
# fig.savefig(fname="test_01.png", transparent=True)

# (III)
fig = pygmt.Figure()
fig.basemap(region=[-5, 5] * 2, projection="X5c", frame=["afg", "+gwhite"])
fig.show()
# fig.savefig(fname="test_02.png", transparent=True)

(I)

(II)

(III)

Externally combined on top of a blue background: