如果在使用 astropy WCS 时清除轴,Matplotlib 轴标签会丢失

发布于 2025-01-17 05:27:51 字数 1731 浏览 4 评论 0原文

我想知道是否有人可以分享一些关于我的这个问题的见解:

我正在使用 matplotlib 制作一些交互式绘图,我需要根据用户事件清除/重绘这些绘图。

但是,如果投影 WCS(世界坐标系)不是默认的,我会丢失轴标签,并且无法找到恢复它们的方法。

上面的代码重现了预期的行为(代码重新绘制图像几次,然后关闭它):

import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)
print(type(wcs))
fig = plt.figure()
ax = fig.add_subplot(projection=None)

for i in range(10):
    ax.clear()
    ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    ax.update({'xlabel': 'Galactic Longitude', 'ylabel': 'Galactic Latitude'})
    fig.canvas.draw()
    plt.pause(0.1)
plt.close(fig)

显示的图像保留其轴标签:

非 WCS 图像保留其标签

但是,如果我们使用 astropy 世界坐标系我们有:

import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)
fig = plt.figure()
ax = fig.add_subplot(projection=wcs)

for i in range(10):
    ax.clear()
    ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    ax.update({'xlabel': 'Galactic Longitude', 'ylabel': 'Galactic Latitude'})
    fig.canvas.draw()
    plt.pause(0.1)
plt.close(fig)

“在此处输入图像描述”"

如何避免此问题或恢复标签?

谢谢

I wonder if anyone could please share some insight on this issue of mine:

I am making some interactive plots with matplotlib which I need to clear/redraw according to the user events.

However, if the projection WCS (world coordinate system) is not the default, I lose the axis labels and I cannot find the way to restore them.

The code above reproduces the expected behaviour (the code redraws the image several times and then closes it):

import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)
print(type(wcs))
fig = plt.figure()
ax = fig.add_subplot(projection=None)

for i in range(10):
    ax.clear()
    ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    ax.update({'xlabel': 'Galactic Longitude', 'ylabel': 'Galactic Latitude'})
    fig.canvas.draw()
    plt.pause(0.1)
plt.close(fig)

The displayed image keeps its axes labels:

Non WCS image keeps its labels

However, if we use the astropy world coordinate system we have:

import matplotlib.pyplot as plt
from astropy.wcs import WCS
from astropy.io import fits
from astropy.utils.data import get_pkg_data_filename

filename = get_pkg_data_filename('galactic_center/gc_msx_e.fits')

hdu = fits.open(filename)[0]
wcs = WCS(hdu.header)
fig = plt.figure()
ax = fig.add_subplot(projection=wcs)

for i in range(10):
    ax.clear()
    ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
    ax.update({'xlabel': 'Galactic Longitude', 'ylabel': 'Galactic Latitude'})
    fig.canvas.draw()
    plt.pause(0.1)
plt.close(fig)

enter image description here

How could I avoid this issue or restore the labels?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文