如果在使用 astropy WCS 时清除轴,Matplotlib 轴标签会丢失
我想知道是否有人可以分享一些关于我的这个问题的见解:
我正在使用 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)
显示的图像保留其轴标签:
但是,如果我们使用 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:
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)
How could I avoid this issue or restore the labels?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论