python – 将共享轴图添加到matplotlib中的AxesGrid图
发布时间:2020-12-20 13:23:55 所属栏目:Python 来源:网络整理
导读:我想在matplotlib中创建一个2×3的2d直方图,每个子图的顶部都有一个共享的颜色条和一个直方图.除了最后一部分,AxesGrid给了我一切.我尝试使用make_axes_locatable按照上面的页面上的 “scatter_hist.py” example在每个子图的顶部添加一个2d直方图.代码看起
我想在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 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方法轻松地将直方图添加到顶部,或者我是否需要使用不同的方法?谢谢! 解决方法
您应该在调用divider.append_axes时将shareesSubplot(具有_adjustable属性)的实例提供给sharex关键字.而不是这个,您将hexbin的返回值赋予此关键字参数,该参数是LocatablePolyCollection的一个实例.
因此,如果在调用divider.append_axes时将sharex = plots [i]替换为sharex = grid [i],则代码应该可以正常工作. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |