代码之家  ›  专栏  ›  技术社区  ›  Stefano Potter

plt.sublot_adjust()工作不正常

  •  0
  • Stefano Potter  · 技术社区  · 6 年前

    我正在做一些密度图,比如:

    导入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轴。有没有一种方法可以使图形之间的宽度间距更接近?

    这给了我一个这样的情节:

    enter image description here

    不管我改变什么 wspace 与情节保持一致。我想这是因为当我转动Y轴时 ax1 我只是将文本设为空白,而不是将Y轴全部删除。有没有一种方法可以使图形之间的宽度间距更接近?

    1 回复  |  直到 6 年前
        1
  •  2
  •   ImportanceOfBeingErnest    6 年前

    如前所述, wspace set the minimal Distance between Plots.在等宽轴的情况下,此距离可能更大。然后它将取决于图形大小、图形方面和图像方面。

    A.使用自动相位

    您可以在您的 imshow plots, aspect=“auto”

    ax.imshow(…,aspect=“auto”)
    

    b.调整子批次参数

    您可以将左或右子批次参数设置为较小的值。例如

    plt.subflots_adjust(wspace=0.0,hspace=0,right=0.7)
    

    c.调整图形大小

    使用较小的图形宽度(更接近实际图像方面)也会减少图形周围的空白。

    例如,使这个数字只有11英寸宽,在右边使用5%的填充物,

    plt.subflots(…,figsize=(11,5))。
    请调整子批次(wspace=0.0,hspace=0,right=0.95)
    

    将取决于图形大小、图形方面和图像方面。

    A.使用自动相位

    你可以设定aspect = "auto"在你的imshow情节,

    ax.imshow(..., aspect = "auto")
    

    enter image description here

    b.调整子批次参数

    您可以将左或右子批次参数设置为较小的值。例如。

    plt.subplots_adjust(wspace=0.0, hspace=0, right=0.7)
    

    enter image description here

    c.调整图形大小

    使用较小的图形宽度(更接近实际图像方面)也会减少图形周围的空白。

    例如,使这个数字只有11英寸宽,在右边使用5%的填充物,

    plt.subplots(..., figsize = (11, 5))
    plt.subplots_adjust(wspace=0.0, hspace=0, right=.95)
    

    enter image description here