使用colorbar时如何保持图像大小?

2024-04-26 04:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着把同一个图像的两个版本并排绘制出来。当我为其中一个图像绘制不带颜色条的图形时,它的大小似乎是正确的:

without the color bar

但是当我在左边的图像中添加一个颜色条时,它会以某种方式缩小图像:

scales the image down

下面是我注释掉颜色条的代码:

def plot_amaps(self, anisotropy_map, parallel):
        timepoint = self.t * self.timestep
        amap_directory = self.directory + "amaps/"
        fig = plt.figure(facecolor='w', dpi=180)

        ax1 = fig.add_subplot(121)
        fig.subplots_adjust(top=0.85)
        ax1.grid(False)
        txt = "Mean(r) = %.3f SD(r)= %.3f t=%dmin"
        txt = txt %(self.mean, self.sd, timepoint)
        ax1.set_title(txt)

        amap = ax1.imshow(anisotropy_map, cmap="jet", clim = self.clim)
        #divider = make_axes_locatable(ax1)
        #cax = divider.append_axes('right', size='5%', pad=0.05)
        #fig.colorbar(amap, cax=cax)

        ax2 = fig.add_subplot(122)
        ax2.set_title("Intensity image", fontsize=10)
        ax2.imshow(parallel, cmap="gray")
        ax2.grid(False)
        ax1.axis('off')
        ax2.axis('off')

        if self.save is True:
            self.make_plot_dir(amap_directory)
            name = self.cell + "_time_"+str(timepoint)
            plt.savefig(amap_directory+name+self.saveformat, bbox_inches='tight')
        else:
            plt.show()
        plt.close('all')

我做错了什么?我如何确保这两幅图像的大小相同?在


Tags: 图像selftxtplot颜色fig绘制plt
2条回答

当您使用append_axes()时,它实际上减少了ax1的大小,以便为颜色贴图腾出空间。 如果要确保轴的大小不会更改,则应显式创建它们。 以下是我的尝试:

^{1}$

enter image description here

使用时

^{1}$

你明确要求一个5%更小的轴。因此,如果您不希望这样做,就不应该使用make_axes_locatable为colorbar创建轴。在

相反,您可以使用

^{pr2}$

其中left, bottom, width, height是0到1的图形单位。然后添加颜色条。
如果您希望colorbar位于中间,可以使用

plt.subplots_adjust(wspace=0.3)

当然,你得对数字做一点实验

相关问题 更多 >