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

发布于 2024-11-18 13:45:58 字数 1641 浏览 2 评论 0 原文

我想在 matplotlib 中创建 2x3 的 2d 直方图图,并在每个子图的顶部使用共享颜色条和 1d 直方图。 AxesGrid 为我提供了除最后一部分之外的所有内容。我尝试按照 "scatter_hist.py" 示例。代码看起来像这样:

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 方法轻松地将直方图添加到顶部,或者我需要使用不同的方法吗?谢谢!

I would like to create a 2x3 plot of 2d histograms in matplotlib with a shared colorbar and a 1d histogram at the top of each subplot. AxesGrid got me everything except for the last part . I tried to add a 2d histogram to the top of each subplot by following the "scatter_hist.py" example on the above page using make_axes_locatable. The code looks something like this:

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

This gives an error at the divider.append_axes() function call:

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

Does anyone know if it's possible to easily add the histograms to the top with the axesgrid approach, or do I need to use a different approach? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2024-11-25 13:45:58

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

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

You should give an instance of AxesSubplot (which has an _adjustable attribute) to the sharex keyword in your call of divider.append_axes. Instead of this you are giving the return value of hexbin to this keyword argument, which is an instance of a LocatablePolyCollection.

So your code should work if you replace sharex=plots[i] with sharex=grid[i] in your call of divider.append_axes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文