将共享轴图添加到 matplotlib 中的 AxesGrid 图

Ror*_*ory 5 python plot matplotlib

我想在 matplotlib 中创建一个 2x3 的 2d 直方图,在每个子图的顶部有一个共享的颜色条和一个 1d 直方图。除了最后一部分,AxesGrid 为我提供了一切。我尝试按照上述页面上的“scatter_hist.py”示例使用make_axes_locatable. 代码如下所示:

plots = []
hists = []
for i, s in enumerate(sim):
    x = np.log10(s.g['temp']) #just accessing my data
    y = s.g['vr']
    histy = s.g['mdot']
    rmin, rmax = min(s.g['r']), max(s.g['r'])
    plots.append(grid[i].hexbin(x, y, C = s.g['mass'],
                 reduce_C_function=np.sum, gridsize=(50, 50),
                 extent=(xmin, xmax, ymin, ymax),
                 bins='log', vmin=cbmin, vmax=cbmax))
    grid[i].text(0.95 * xmax, 0.95 * ymax,
                 '%2d-%2d kpc' % (round(rmin), round(rmax)),
                 verticalalignment='top',
                 horizontalalignment='right')

    divider = make_axes_locatable(grid[i])
    hists.append(divider.append_axes("top", 1.2, pad=0.1, sharex=plots[i]))
    plt.setp(hists[i].get_xticklabels(), visible=False)
    hists[i].set_xlim(xmin, xmax)
    hists[i].hist(x, bins=50, weights=histy, log=True)

#add color bar
cb = grid.cbar_axes[0].colorbar(plots[i])
cb.set_label_text(r'Mass ($M_{\odot}$)')
Run Code Online (Sandbox Code Playgroud)

这在divider.append_axes() 函数调用中给出了一个错误:

AttributeError: 'LocatablePolyCollection' object has no attribute '_adjustable'
Run Code Online (Sandbox Code Playgroud)

有谁知道是否可以使用轴网格方法轻松地将直方图添加到顶部,还是我需要使用不同的方法?谢谢!

bmu*_*bmu 1

您应该为调用中的关键字提供一个实例AxesSubplot(具有_adjustable属性)。相反,您将返回值赋予此关键字参数,它是 a 的实例。sharexdivider.append_axeshexbinLocatablePolyCollection

因此,如果您在 的调用中替换sharex=plots[i]为,您的代码应该可以工作。sharex=grid[i]divider.append_axes