我正在做一些密度图,比如:
导入matplotlib.pyplot as plt
将numpy导入为np
从sklearn.metrics导入r2_分数
导入Matplotlib
从scipy导入统计
将matplotlib.gridspec导入为gridspec
从mpl_toolkits.axes_grid1.inset_locator import insetposition
从matplotlib.ticker导入格式strFormatter
将matplotlib.cm导入为cm
从scipy.ndimage.filters导入高斯滤波器
随机导入
matplotlib.rcparams.update('font.size':16)
matplotlib.rcparams['xtick.direction']='in'
matplotlib.rcparams['ytick.direction']='in'
x=随机样本(范围(10001),1000)
Y=随机样本(范围(10001),1000)
定义myplot(x,y,s,bin=1000):
heatmap,xedges,yedges=np.histogram2d(x,y,bin=bin)
heatmap=高斯滤波器(heatmap,sigma=s)
范围=[xedges[0]、xedges[-1]、yedges[0]、yedges[-1]]
返回heatmap.t,范围
cmap=cm.ylorrd
Fig,(ax,ax1,cax)=plt.子批次(ncols=3,FigSize=(15,5),
网格规格kw=“宽度比率”:[1,1,0.5])
img,范围=myplot(x,y,20)
im=ax.imshow(img,extent=extent,origin='lower',cmap=cmap)
ax.text(0.05,0.92,$r^2$='。格式(np.round(r2_score(x,y),2)),fontsize=14,color='k',transform=ax.transaxs)
ax.plot(ax.get xlim(),ax.get ylim(),ls=“--”,c=“3”)。
ax.set xlabel(黑色天空)
ax.set伊拉贝尔(“蓝天”)。
img2,extent2=myplot(x,y,20)
ax1.imshow(img2,范围=扩展2,原点=降低,cmap=cmap)
ax1.text(0.05,0.92,$R^2$='。格式(np.round(r2_score(x,y),2)),fontsize=14,color='k',transform=ax1.transaxs)
ax1.axs.get_Yaxis()。设置_Visible(false)
ax1.yaxis.set_ticks([])
ax1.plot(ax1.get xlim(),ax1.get ylim(),ls=“--”,c=“3”)。
ax1.设置标签(“白色天空”)
ip=插入位置(ax1,[1.05,0,0.05,1])
设置轴定位器(IP)
图颜色条(im,cax=cax,ax=[ax,ax1],使用_gridspec=true)
plt.子批次调整(wspace=0.1,hspace=0)
这给了我一个这样的情节:

无论我更改什么,绘图都保持不变。我认为这是因为当我在ax1>中转动y轴时,我只是将文本设为空白,而不是同时移除y轴。有没有一种方法可以使图形之间的宽度间距更接近?
这给了我一个这样的情节:
不管我改变什么
wspace
与情节保持一致。我想这是因为当我转动Y轴时
ax1
我只是将文本设为空白,而不是将Y轴全部删除。有没有一种方法可以使图形之间的宽度间距更接近?