子图投影覆盖 tick_params

subplot projection overwrites tick_params

我尝试使用 fits header 提供的坐标绘制图像,并通过 astropy 的 WCS 获得。

from astropy import wcs
import numpy as np
import matplotlib.pyplot as plt
hdr = r[1].header #r ist the repsective fits files, I copied content "w" at the end of the page
w = wcs.WCS(hdr)
ax = plt.subplot(projection=w)
ax.imshow(np.ones((100,100)),origin='lower')

ax.tick_params(axis='x', which='major', labelsize='large',width=25)
ax.tick_params(axis='y', which='major', labelsize='large')
plt.show()

可以看到 tick_params 被忽略了。

wenn 我也这样做,但是关闭投影,即:

ax = plt.subplot()
ax.imshow(np.ones((100,100)),origin='lower')

ax.tick_params(axis='x', which='major', labelsize='large',width=25)
ax.tick_params(axis='y', which='major', labelsize='large')
plt.show()

Tick_params 又开始工作了。

知道这里出了什么问题吗?

WCS 是:

 print(w)
 WCS Keywords

 Number of WCS axes: 2
 CTYPE : 'RA---TAN'  'DEC--TAN'  
 CRVAL : 266.41798186205955  -29.006968367892327  
 CRPIX : 248.5  340.0  
 NAXIS : 497  680

WCS 投影完全取代了 matplotlib 轴,参见 Ticks, tick labels, and grid lines。因此,您不能再使用 matplotlib 方法,或者至少不能期望它们对实际绘图有任何影响。

相反,需要使用 WCS 的方法。 所以如果

ax = plt.subplot(projection=wcs)

是一个WCSAxesSubplot,你可能得到x轴为ax.coords[0],y轴为ax.coords[1]。然后你可以设置 ticklabel size

ax.coords[0].set_ticklabel(size="large")

刻度宽度为

ax.coords[0].set_ticks(width=25)

set_ticklabelset_ticks这两个方法是astropy.visualization.wcsaxes.coordinate_helpers.CoordinateHelperclass的方法。我不确定是否有对可用方法的完整参考,但您可以随时查看 the source code 以检查公开了哪些方法。

一些完整示例(基于文档中的 one of the examples):

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.coords.grid(True, color='white', ls='solid')
ax.coords[0].set_axislabel('Galactic Longitude')
ax.coords[1].set_axislabel('Galactic Latitude')

ax.coords[0].set_ticks(width=25)
ax.coords[0].set_ticklabel(size="large")

plt.show()