【问题标题】:Adding a shared axis plot to an AxesGrid plot in matplotlib在 matplotlib 中将共享轴图添加到 AxesGrid 图
【发布时间】:2011-07-04 20:23:19
【问题描述】:

我想在 matplotlib 中创建一个 2x3 的 2d 直方图,在每个子图的顶部有一个共享的颜色条和一个 1d 直方图。 AxesGrid 得到了我所有的东西,除了最后一部分。我尝试使用make_axes_locatable 遵循上一页上的"scatter_hist.py" example,将二维直方图添加到每个子图的顶部。代码如下所示:

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}$)')

这会在 divider.append_axes() 函数调用时出错:

AttributeError: 'LocatablePolyCollection' object has no attribute '_adjustable'

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

【问题讨论】:

  • 你还在努力解决这个问题吗?你找到解决办法了吗?

标签: python matplotlib plot


【解决方案1】:

您应该在调用divider.append_axes 时为sharex 关键字提供AxesSubplot 的实例(具有_adjustable 属性)。取而代之的是,您将 hexbin 的返回值赋予此关键字参数,它是 LocatablePolyCollection 的一个实例。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-27
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多