我有一个类似的绘图功能
def fct():
f=figure()
ax=f.add_subplot(111)
x,y=mgrid[0:5,0:5]
z=sin(x**2+y**2)
ax.pcolormesh(x,y,z)
当我在ipython
(使用--pylab
选项)中定义上述函数时,然后调用
fct()
colorbar()
我有个错误
"RuntimeError: No mappable was found to use for colorbar creation.".
def fct():
f=figure()
x,y=mgrid[0:5,0:5]
z=sin(x**2+y**2)
pcolormesh(x,y,z)
那就行了。我想这与垃圾收集有关—在第一个示例中,如何防止出现此问题?
这是因为您的第一个例子,您使用的是
ax.polormesh
,而不是pyplot.polotmesh
(由pylab
导入的名称空间),当您调用colorbar()
(实际上是plt.colorbar()
)时,它失去了跟踪哪个可映射的以及它应该使colorbar指向哪个ax的信息。因此,添加这些行将使其工作:
现在你提到你的真实情节要复杂得多。您需要确保它是
ax.get_children()[2]
,或者可以通过查找matplotlib.collections.QuadMesh
实例来选择它。我认为这与
pylab
状态机和作用域有关。一个更好的做法是这样做(显式比隐式好):
您的函数很小,不需要参数,所以您真的需要将绘图包装在函数中吗?怎么办:
这是因为您的第一个例子,您使用的是
ax.polormesh
,而不是pyplot.polotmesh
(由pylab
导入的名称空间),当您调用colorbar()
(实际上是plt.colorbar()
)时,它丢失了哪个可映射的以及它应该将colorbar设置为哪个ax的跟踪。因此,添加这些行将使其工作:
现在你提到你的真实情节要复杂得多。您需要确保它是
ax.get_children()[2]
,或者可以通过查找matplotlib.collections.QuadMesh
实例来选择它。相关问题 更多 >
编程相关推荐