【发布时间】:2020-08-29 02:25:54
【问题描述】:
我想将自定义 plt.colorbar 添加到包含多个 healpy 绘图的图形中。我发现了很多关于如何针对多个 axes 对象的常见情况执行此操作的帖子,但 healpy 使其变得困难。
到目前为止,我有以下 MWE:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
import healpy as hp
rows, cols = 8, 8
nplots = rows * cols
npix = 48
data = np.random.uniform(size=(nplots, npix))
fig = plt.figure()
for i in range(len(data)):
hp.mollview(data[i, :], title='', cbar=False, fig=fig,
sub=(rows, cols, i+1), margins=(0, 0, 0, 0),
min=data.min(), max=data.max())
fig, ax = plt.gcf(), plt.gca()
image = ax.get_images()[0]
norm = mpl.colors.Normalize(vmin=data.min(), vmax=data.max())
from mpl_toolkits.axes_grid1 import make_axes_locatable
divider = make_axes_locatable(ax)
cax = divider.new_vertical(size="5%", pad=0.7, pack_start=True)
fig.add_axes(cax)
fig.colorbar(image, cax=cax, norm=norm, orientation='horizontal',
label='colorbar')
plt.show()
如链接图像所示,我最终将colorbar 附加到最后一个ax,而不是整个fig。我想在fig 的底部(或右侧)有一个简单的colorbar,其范围通过Normalize 指定,如上所述。同样,事实上,我使用healpy 生成的数字排除了通常的解决方案,至少据我所知。
【问题讨论】:
标签: python matplotlib healpy