使用星形WCS投影时清除matplotlib轴标签、刻度线和定位符

h9vpoimq  于 2023-05-18  发布在  其他
关注(0)|答案(2)|浏览(146)

我正在用matplotlib创建一个网格图,它使用了天体世界坐标系投影。
我想完成明确的轴之一(没有标题,标签或刻度),但我还没有能够与传统的命令。
此脚本再现问题:

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)

ax = plt.subplot(projection=wcs)
ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
ax.grid(color='white', ls='solid')
ax.update({'xlabel': 'Galactic Longitude', 'ylabel': ''})
ax.yaxis.set_ticklabels([], minor=True)
ax.yaxis.set_major_locator(plt.NullLocator())
plt.show()

我想知道有没有人能给我点建议
谢谢大家。

  • 更新:

根据@Chang Ye的解决方案,我将代码修改为:

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)

ax = plt.subplot(projection=wcs[0])
ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
ax.grid(color='white', ls='solid')
ax.update({'xlabel': 'Galactic Longitude', 'ylabel': ''})
ax.yaxis.set_ticklabels([], minor=True)
ax.yaxis.set_major_locator(plt.NullLocator())
plt.show()

但是,此脚本在astropy中产生了以下问题(图仍然显示,但没有所需和非所需轴特征):

Traceback (most recent call last):
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_qt.py", line 455, in _draw_idle
    self.draw()
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 436, in draw
    self.figure.draw(self.renderer)
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 73, in draw_wrapper
    result = draw(artist, renderer, *args, **kwargs)
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/figure.py", line 2810, in draw
    mimage._draw_list_compositing_images(
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/core.py", line 464, in draw
    super().draw(renderer, **kwargs)
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/artist.py", line 50, in draw_wrapper
    return draw(artist, renderer)
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 3082, in draw
    mimage._draw_list_compositing_images(
  File "/anaconda3/lib/python3.8/site-packages/matplotlib/image.py", line 132, in _draw_list_compositing_images
    a.draw(renderer)
  File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/core.py", line 45, in draw
    self.axes.draw_wcsaxes(renderer)
  File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/core.py", line 414, in draw_wcsaxes
    coord._draw_grid(renderer)
  File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/coordinate_helpers.py", line 571, in _draw_grid
    self._update_grid_lines_1d()
  File "/anaconda3/lib/python3.8/site-packages/astropy/visualization/wcsaxes/coordinate_helpers.py", line 864, in _update_grid_lines_1d
    x_ticks_pos = [a[0] for a in self.ticks.pixel['b']]
KeyError: 'b'

我的astropy版本是5.0.4,matplotlib版本是3.5.1

vybvopom

vybvopom1#

我发现你可以通过欺骗 set_ticks 方法将刻度放在图外来做到这一点。不幸的是,不允许出现任何滴答声(数量=0)

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)

ax = plt.subplot(projection=wcs)
ax.imshow(hdu.data, vmin=-2.e-5, vmax=2.e-4, origin='lower')
ax.grid(color='white', ls='solid')
ax.update({'xlabel': 'Galactic Longitude', 'ylabel': ''})
lat = ax.coords[1]
lat.set_ticks([10,20]*u.deg)
plt.show()

生产
enter image description here

v1uwarro

v1uwarro2#

股票代码和标签不会改变,因为投影将填充默认值时,它是空的。您可以将它们设置为白色,例如'ylabel': ' '
但是将投影定义从wcs更改为wcs[0]是创建您想要的图形的最快方法。

ax = plt.subplot(projection=wcs[0])

输出:

相关问题