在循环中使用matplotlib绘图,删除颜色条但留下空白区域

3

我的代码大概是这样的:

更新:我用一些实际的模拟代码重新做了一遍,反映了我的一般问题。同时意识到颜色条的创建在实际循环中,否则它就没有任何东西可以映射到。对于之前的代码感到抱歉,是在工作日结束时疯狂地输入的 :)

import numpy
import matplotlib as mplot
import matplotlib.pyplot as plt
import os

#make some mock data
x = np.linspace(1,2, 100)
X, Y = np.meshgrid(x, x)
Z = plt.mlab.bivariate_normal(X,Y,1,1,0,0)

fig = plt.figure()
ax = plt.axes()
   '''
   Do some figure-related stuff that take up a lot of time,
    I want to avoid having to do them in the loop over and over again. 
    They hinge on the presence of fig so I can't make 
    new figure to save each time or something, I'd have to do
    them all over again.
    '''  
for i in range(1,1000):
   plotted = plt.plot(X,Y,Z)
   cbar = plt.colorbar(ax=ax, orientation = 'horizontal')
   plt.savefig(os.path.expanduser(os.path.join('~/', str(i))))
   plt.draw()
   mplot.figure.Figure.delaxes(fig, fig.axes[1]) #deletes but whitespace remains

   ''' 
   Here I need something to remove the colorbar otherwise 
   I end up with +1 colorbar on my plot at every iteration. 
   I've tried various things to remove it BUT it keeps adding whitespace instead
   so doesn't actually fix anything.
   '''

有人遇到过这个问题并成功解决了吗?希望这已经足够让您了解问题,如果需要,我可以发布更多代码,但认为只给出概述示例会减少混乱。
谢谢。
2个回答

2

colorbar()函数允许你显式地设置要渲染的轴 - 这可以确保它们始终出现在相同的位置,并且不会从另一个轴中夺取任何空间。此外,你可以重置现有 colorbar 的 .mappable 属性,而不是每次重新定义它。

以下是一个显式轴的示例:

x = np.linspace(1,2, 100)
X, Y = np.meshgrid(x, x)
Z = plt.mlab.bivariate_normal(X,Y,1,1,0,0)

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.1,0.8,0.7])
ax2 = fig.add_axes([0.1,0.85,0.8,0.05])
...    

for i in range(1,5):
   plotted = ax1.pcolor(X,Y,Z)
   cbar = plt.colorbar(mappable=plotted, cax=ax2, orientation = 'horizontal')
   #note "cax" instead of "ax"
   plt.savefig(os.path.expanduser(os.path.join('~/', str(i))))
   plt.draw()

嗨,我已经重新编写了上面的代码,以一种可以让您重现我的问题的方式(尽管您可能需要将循环迭代次数减少几百:))。您能否发布一些关于您所说内容的示例代码? - areuexperienced
谢谢,看起来对我很有用,希望星期一能够证明它是正确的 :)! - areuexperienced

1

我曾经遇到类似的问题,最终通过类似以下方式定义一个颜色条坐标轴来解决: 多个imshow子图,每个带有颜色条

相较于mdurant的回答,这种方法的优点是不需要手动定义坐标轴位置。

import matplotlib.pyplot as plt
import IPython.display as display
from mpl_toolkits.axes_grid1 import make_axes_locatable
from pylab import *
%matplotlib inline

def plot_res(ax,cax):
    plotted=ax.imshow(rand(10, 10))
    cbar=plt.colorbar(mappable=plotted,cax=cax)

fig, axarr = plt.subplots(2, 2)
cax1 = make_axes_locatable(axarr[0,0]).append_axes("right", size="10%", pad=0.05)
cax2 = make_axes_locatable(axarr[0,1]).append_axes("right", size="10%", pad=0.05)
cax3 = make_axes_locatable(axarr[1,0]).append_axes("right", size="10%", pad=0.05)
cax4 = make_axes_locatable(axarr[1,1]).append_axes("right", size="10%", pad=0.05)
# plt.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=0.3, hspace=0.3)
N=10
for j in range(N):
    plot_res(axarr[0,0],cax1)
    plot_res(axarr[0,1],cax2)
    plot_res(axarr[1,0],cax3)
    plot_res(axarr[1,1],cax4)
    display.clear_output(wait=True)
    display.display(plt.gcf())
display.clear_output(wait=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接