从plot(python)中删除colorbars

时间:2014-07-10 13:53:53

标签: python matplotlib colormap

我写了一些代码来创建一个光栅对象的png(self [:] =一个np数组)。 它应该是一种方法,可以轻松制作情节 代码问题是它第一次运行正常, 但是当我多次运行这个方法时,我会得到一张带有多个图例的图片。

enter image description here

我试图通过放松来摆脱它,但这个传说真的很顽固 任何想法如何解决这个问题都是受欢迎的

以下是代码:

    def plot(self,image_out,dpi=150, rotate = 60):
        xur = self.xur()
        xll = self.xll()
        yur = self.yur()
        yll = self.yll()
        fig = plt.figure()
    #tmp = range(len(fig.axes))
    #tmp = tmp[::-1]
    #for x in tmp:
    #    fig.delaxes(fig.axes[x])
        ax = fig.add_subplot(111)
        cax = ax.imshow(self[:],cmap='jet', extent = [yll,yur,xll,xur],
                            interpolation = 'nearest')
        cbar = fig.colorbar()
        plt.xticks(rotation=70)
        plt.tight_layout(pad = 0.25)
        plt.savefig(image_out,dpi=dpi)
        return

3 个答案:

答案 0 :(得分:2)

你需要关闭情节。 I had this same problem

在plt.savefig之后,添加plt.close()

答案 1 :(得分:1)

更好的选择是指定颜色栏您希望将其渲染到哪些轴,请参阅示例here

答案 2 :(得分:1)

我遇到了同样的问题,另一篇文章中的答案解决了它

remove colorbar from figure in matplotlib

请参阅第二个答案

  

我有类似的问题并且玩了一下。我想出了两个可能稍微优雅的解决方案:

     

清除整个图形并再次添加子图(如果需要,可以添加+ colorbar)。

     

如果总是有一个颜色条,你只需用自动缩放更新轴,这也会更新颜色条。

     

我已经尝试使用imshow,但我猜它与其他绘图方法类似。

特别是,我使用了第一种方法,即通过clf()清除图形,然后每次重新添加轴。