Skip to content

Add support for world coordinates (WCSAxes)#47

Merged
ErwindeGelder merged 5 commits intoErwindeGelder:mainfrom
HealthyPear:support_wcs
Feb 3, 2026
Merged

Add support for world coordinates (WCSAxes)#47
ErwindeGelder merged 5 commits intoErwindeGelder:mainfrom
HealthyPear:support_wcs

Conversation

@HealthyPear
Copy link
Copy Markdown
Contributor

This PR adds support for a class used in the visualization of astronomical data via astropy.wcs.

These can be plots such as this one, showing radio data of the supernova remnant W49B from the THOR survey:
image

that can be generated with a snippet such as this one:

from pathlib import Path

import matplot2tikz
import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits

with fits.open("path/to/a/compatible/file.fits")][0]) as hdul:
    hdu = hdul[0]
    wcs = WCS(hdu.header)
    data = hdu.data

fig, ax = plt.subplots(subplot_kw=dict(projection=wcs))
vmin=data.min()
vmax=data.max()
mappable = ax.imshow(data, vmin=vmin, vmax=vmax, origin='lower', cmap="mako")
ax.grid(color='white', ls='solid', alpha=0.5)
levels = np.linspace(vmin, vmax, 5)
contours = ax.contour(data, levels=levels, colors='white', alpha=0.5)
ax.set(xlabel='Galactic Longitude', ylabel='Galactic Latitude')
cbar = fig.colorbar(mappable, ax=ax, label=hdu.header["BUNIT"])
ax.clabel(contours, fontsize=15, fmt="%.1f")

matplot2tikz.save("test.tex")

Before this PR a warning is generated saying that matplot2tikz does not know how to handle the underlying artist class and the resulting plot when compiled in latex shows as empty.

Changes:

_save.py

  • Added support for handling _WCSAxesArtist in the _recurse() function

The _WCSAxesArtist is a placeholder artist from astropy's WCS visualization that has no visible rendering content
By skipping it gracefully, we eliminate the warning while preserving all actual plot content
This approach is consistent with how other placeholder artists (Spines, XAxis, YAxis) are handled

test_wcs_axes.py

  • Tests WCS axes with images, grids, colorbars, and contours
  • Verifies no warnings about _WCSAxesArtist are generated
  • Validates TikZ output contains expected elements
  • Gracefully handles environments without astropy installed (uses pytest skip)

@ErwindeGelder
Copy link
Copy Markdown
Owner

ErwindeGelder commented Feb 3, 2026

Thanks for your contribution!

Also thanks for adding the test script. I removed the pytest skip part, because I think it is good to always do the test. I have added astropy to the requirements lists (you already did that for the pyproject.toml, thank you), so the tests run fine.

@ErwindeGelder ErwindeGelder merged commit 998d9bf into ErwindeGelder:main Feb 3, 2026
1 check passed
@HealthyPear HealthyPear deleted the support_wcs branch February 3, 2026 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants