Matplotlib colorbar和WCS投影

问题描述 投票:2回答:3

我正在尝试编写一个函数来显示顶部带有颜色条的天文图像(自动与x轴的长度相同)。我遇到了问题,因为当我试图将标记放在顶部时它没有做任何事情......它将标记保留在颜色条的底部(以及字母表的y轴上的标记)。我认为这可能是x轴WCS坐标的一个问题,因为当我尝试在没有投影的情况下这样做时效果很好!

import numpy as np
import matplotlib.pyplot as plt
from astropy import wcs
from matplotlib.colors import PowerNorm
from mpl_toolkits.axes_grid1 import make_axes_locatable
from matplotlib import cm

#WCS coordinate system
w = wcs.WCS(naxis=2)
w.wcs.crpix = [23.5, 23.5]
w.wcs.cdelt = np.array([-0.0035, 0.0035])
w.wcs.crval = [266.8451, -28.151658]
w.wcs.ctype = ["RA---TAN", "DEC--TAN"]
w.wcs.set_pv([(2, 1, 45.0)])

#generate an array as image test
data = (np.arange(10000).reshape((100,100)))

#display image
fig = plt.figure()
ax = plt.gca(projection=w)
graf = ax.imshow(data, origin='lower', cmap=cm.viridis, norm=PowerNorm(1))

#colorbar
divider = make_axes_locatable(ax)
cax = divider.append_axes("top", size="5%")
cbar = fig.colorbar(graf, cax=cax, orientation='horizontal')
cax.xaxis.set_ticks_position('top')
fig.show()

谢谢!

matplotlib astropy
3个回答
1
投票

你可以使用matplotlib的axes class解决这个问题。

...
import matplotlib.axes as maxes
cax = divider.append_axes("top", size="5%", axes_class=maxes.Axes)
...

2
投票

您需要使用WCSAxes的内部机制来处理WCS投影中的刻度。看起来WCSAxes通过坐标映射容器处理颜色条标记(您可以在cbar.ax.coords中找到它)而不是xaxis / yaxis属性(似乎没有太多使用)。

因此,在运行代码之后,以下技巧对我起作用并且xticks向上移动:

c_x = cbar.ax.coords['x']
c_x.set_ticklabel_position('t')
cbar.update_normal(cax)

0
投票

为了使这样的东西起作用,我需要一些额外的参数:

            from mpl_toolkits.axes_grid1 import make_axes_locatable
            divider = make_axes_locatable(ax)
            cax = divider.append_axes("right", size="5%", pad=0.05)
            cax.coords[0].grid(False)
            cax.coords[1].grid(False)
            cax.tick_params(direction='in')
            cax.coords[0].set_ticks(alpha=0, color='w', size=0, values=[]*u.dimensionless_unscaled)
            cax.coords[1].set_ticklabel_position('r')
            cax.coords[1].set_axislabel_position('r')

因为默认轴将网格打开,左边的标签和启用的x轴标签。我不确定为什么原帖不会有这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.