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

python – 将共享轴图添加到matplotlib中的AxesGrid图,第1张

概述我想在matplotlib中创建一个2×3的2d直方图,每个子图的顶部都有一个共享的颜色条和一个直方图.除了最后一部分, AxesGrid给了我一切.我尝试使用make_axes_locatable按照上面的页面上的 “scatter_hist.py” example在每个子图的顶部添加一个2d直方图.代码看起来像这样: plots = []hists = []for i, s in enum 我想在matplotlib中创建一个2×3的2d直方图,每个子图的顶部都有一个共享的颜色条和一个直方图.除了最后一部分,AxesGrid给了我一切.我尝试使用make_axes_locatable按照上面的页面上的 “scatter_hist.py” example在每个子图的顶部添加一个2d直方图.代码看起来像这样:

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

解决方法@H_404_23@ 您应该在调用divIDer.append_axes时将shareessubplot(具有_adjustable属性)的实例提供给sharex关键字.而不是这个,您将hexbin的返回值赋予此关键字参数,该参数是LocatablepolyCollection的一个实例.

因此,如果在调用divIDer.append_axes时将sharex = plots [i]替换为sharex = grID [i],则代码应该可以正常工作.

@H_404_34@ 总结

以上是内存溢出为你收集整理的python – 将共享轴图添加到matplotlib中的AxesGrid图全部内容,希望文章能够帮你解决python – 将共享轴图添加到matplotlib中的AxesGrid图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1196523.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存