Matplotlib 2子图,1色条我花了很长时间研究如何让两个子图共享同一个y轴,在Matplotlib中,两者共享一个色条。发生的事情是当我打电话给colorbar()功能subplot1或subplot2,它会自动调整地块的大小,使彩色条加上图将适合在“子图”包围框中,从而使两个并排的地块具有两个非常不同的大小。为了解决这个问题,我尝试创建第三个子图,然后我黑了这个子图,使其不使用一个彩色条来渲染任何情节。唯一的问题是,现在这两个地块的高度和宽度是不均匀的,我不知道如何使它看起来还好。这是我的代码:from __future__ import divisionimport matplotlib.pyplot as pltimport numpy as npfrom matplotlib import patchesfrom matplotlib.ticker import NullFormatter# SIS FunctionsTE = 1 # Einstein radiusg1 = lambda x,y: (TE/2) * (y**2-x**2)/((x**2+y**2)**(3/2)) g2 = lambda x,y: -1*TE*x*y / ((x**2+y**2)**(3/2))kappa = lambda x,y: TE / (2*np.sqrt(x**2+y**2))coords = np.linspace(-2,2,400)X,Y = np.meshgrid(coords,coords)g1out = g1(X,Y)g2out = g2(X,Y)kappaout = kappa(X,Y)for i in range(len(coords)): for j in range(len(coords)): if np.sqrt(coords[i]**2+coords[j]**2) <= TE: g1out[i][j]=0 g2out[i][j]=0fig = plt.figure()fig.subplots_adjust(wspace=0,hspace=0)# subplot number 1ax1 = fig.add_subplot(1,2,1,aspect='equal',xlim=[-2,2],ylim=[-2,2])plt.title(r"$\gamma_{1}$",fontsize="18")plt.xlabel(r"x ($\theta_{E}$)",fontsize="15")plt.ylabel(r"y ($\theta_{E}$)",rotation='horizontal',fontsize="15")plt.xticks([-2.0,-1.5,-1.0,-0.5,0,0.5,1.0,1.5])plt.xticks([-2.0,-1.5,-1.0,-0.5,0,0.5,1.0,1.5])plt.imshow(g1out,extent=(-2,2,-2,2))plt.axhline(y=0,linewidth=2,color='k',linestyle="--")plt.axvline(x=0,linewidth=2,color='k',linestyle="--")e1 = patches.Ellipse((0,0),2,2,color='white')ax1.add_patch(e1)# subplot number 2ax2 = fig.add_subplot(1,2,2,sharey=ax1,xlim=[-2,2],ylim=[-2,2])plt.title(r"$\gamma_{2}$",fontsize="18")plt.xlabel(r"x ($\theta_{E}$)",fontsize="15")ax2.yaxis.set_major_formatter( NullFormatter() )plt.axhline(y=0,linewidth=2,color='k',linestyle="--")plt.axvline(x=0,linewidth=2,color='k',linestyle="--")plt.imshow(g2out,extent=(-2,2,-2,2))e2 = patches.Ellipse((0,0),2,2,color='white')ax2.add_patch(e2)# subplot for colorbarax3 = fig.add_subplot(1,1,1)ax3.axis('off')cbar = plt.colorbar(ax=ax2)plt.show()
添加回答
举报
0/150
提交
取消