我正在尝试编写一个函数来显示顶部有一个彩色条的天文图像(自动地与x轴长度相同)。我有问题,因为当我试图将勾子放在顶部时,anything...it不会将滴答保持在颜色条的底部(以及柱状轴的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()谢谢!
发布于 2018-09-21 16:18:59
您可以使用matplotlib的轴类修复此问题。
...
import matplotlib.axes as maxes
cax = divider.append_axes("top", size="5%", axes_class=maxes.Axes)
...发布于 2017-11-02 12:08:19
您需要使用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)发布于 2019-04-26 22:58:48
为了让这样的东西工作起来,我需要一些额外的参数:
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')因为默认的轴gad在网格上,所以左边的标签和x轴标签被启用。我不知道为什么最初的帖子对此没有异议。
https://stackoverflow.com/questions/47060939
复制相似问题