首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >从matplotlib中的图形中删除colorbar

从matplotlib中的图形中删除colorbar
EN

Stack Overflow用户
提问于 2011-03-11 00:55:04
回答 5查看 45.9K关注 0票数 32

这应该很容易,但我很难做到。基本上,我在matplotlib中有一个子图,每次调用一个函数时,我都会在其中绘制一个六进制图,但每次我调用该函数时,我都会得到一个新的颜色条,所以我真正想做的是更新颜色条。不幸的是,这似乎不起作用,因为subplot.hexbin正在重新创建colorbar附加到的对象。

def foo(self):
   self.subplot.clear()
   hb = self.subplot.hexbin(...)
   if self.cb:
      self.cb.update_bruteforce() # Doesn't work (hb is new)
   else:
      self.cb = self.figure.colorbar(hb)

我现在在这个恼人的地方,我试图完全删除颜色条轴,并简单地重新创建它。不幸的是,当我删除颜色条轴时,子图轴并没有回收空间,并且调用self.subplot.reset_position()并没有像我想的那样做。

def foo(self):
   self.subplot.clear()
   hb = self.subplot.hexbin(...)
   if self.cb:
      self.figure.delaxes(self.figure.axes[1])
      del self.cb
      # TODO: resize self.subplot so it fills the 
      #    whole figure before adding the new colorbar
   self.cb = self.figure.colorbar(hb)

有人有什么建议吗?

非常感谢!亚当

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2011-03-11 04:37:15

好了,这是我的解决方案。不是非常优雅,但也不是一个可怕的黑客。

def foo(self):
   self.subplot.clear()
   hb = self.subplot.hexbin(...)
   if self.cb:
      self.figure.delaxes(self.figure.axes[1])
      self.figure.subplots_adjust(right=0.90)  #default right padding
   self.cb = self.figure.colorbar(hb)

这可以满足我的需要,因为我只有一个子图。在使用多个子图或在不同位置绘制颜色条时遇到相同问题的人将需要调整。

票数 12
EN

Stack Overflow用户

发布于 2015-11-25 19:38:48

我设法用fig.clear()和display.clear_output()解决了同样的问题。

import matplotlib.pyplot as plt
import IPython.display as display
import matplotlib.tri as tri
from pylab import *
%matplotlib inline

def plot_res(fig):
    ax=fig.add_axes([0,0,1,1])
    ax.set_xlabel("x")
    ax.set_ylabel('y')
    plotted=ax.imshow(rand(250, 250))
    ax.set_title("title")
    cbar=fig.colorbar(mappable=plotted)
    display.clear_output(wait=True)
    display.display(plt.gcf())
    fig.clear()

fig=plt.figure()
N=20
for j in range(N):
    plot_res(fig)
票数 4
EN

Stack Overflow用户

发布于 2014-08-26 12:54:29

我使用的是matplotlib 1.4.0。这是我解决这个问题的方法:

import matplotlib
import numpy as np
import matplotlib.cm as cm
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt

# A contour plot example:
delta = 0.025
x = np.arange(-3.0, 3.0, delta)
y = np.arange(-2.0, 2.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
Z = 10.0 * (Z2 - Z1)
#

# first drawing
fig = plt.figure()
ax = fig.add_subplot(111)  # drawing axes
c = ax.contourf(Z)   # contour fill c
cb = fig.colorbar(c)  # colorbar for contour c

# clear first drawimg
ax.clear()  # clear drawing axes
cb.ax.clear()  # clear colorbar axes

# replace with new drawing
# 1. drawing new contour at drawing axes
c_new = ax.contour(Z)  
# 2. create new colorbar for new contour at colorbar axes
cb_new = ax.get_figure().colorbar(c_new, cax=cb.ax) 

plt.show()

上面的代码绘制了一个带有颜色条的等高线填充图,清除它,并在同一图中使用新的颜色条绘制新的等高线图。

通过使用cb.ax,我能够识别颜色条轴并清除旧的颜色条。并且指定cax=cb.ax只是在旧的颜色条轴上绘制新的颜色条。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5263034

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档