1

Here is my test code.

import os
import matplotlib.pyplot as plt

import numpy as np

a = np.arange(10)
bb, cc = np.meshgrid(a, a)
u = np.random.randint(2, 4, (10, 10))
v = np.random.randint(2, 4, (10, 10))

fig = plt.figure(figsize=(10, 10))
ax = fig.add_subplot(111)
plt.subplots_adjust(left=0.07, bottom=0.01, right=0.95,
                    top=0.94, hspace=0.0, wspace=0.0)

for i in range(10):

    u = np.random.randint(2, 4, (10, 10))
    v = np.random.randint(2, 4, (10, 10))
    ws = np.sqrt(u**2 + v**2)
    cf = plt.contourf(bb, cc, ws)
    cb = plt.colorbar(cf)

    fig.canvas.update()
    fig.canvas.flush_events()

    plt.savefig('test_barb/%i.png' % i, dpi=200)
    for c in cf.collections:
        c.remove()
    cb.remove()

What I find confusing here is that the output images get smaller and smaller the more frames I draw. Is there any way to prevent that from happening?

0.png

1.png The last one

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
Li Ziming
  • 385
  • 2
  • 5
  • 17
  • 1
    The duplicate question shows how to update colorbars. Questions asking for several different issues are off-topic on SO. I would recommend asking a new specific question about barbs, which would be independend on colorbars. – ImportanceOfBeingErnest Oct 11 '17 at 14:13
  • fine, the third question was 'n major question! so i'd like to remove it and no duplicated. – Li Ziming Oct 11 '17 at 15:15
  • The first question is also answered by the duplicate. You may of course remove that as well, but then also remove the colorbar from the code as well as the (then unrelated) images. – ImportanceOfBeingErnest Oct 11 '17 at 15:20
  • I still don't understand. Could you explain it to me again? Thank you for your help. – Li Ziming Oct 11 '17 at 15:28
  • I still don't understand. Could you ez explain it to me again? Thank you for your help. – Li Ziming Oct 11 '17 at 15:32
  • You cannot ask several questions in one. So if the colorbar issue is still not clear, ask one question about updating a `contourf` plot with a colorbar and ask another one about updating a barb plot. The barb plot part of the code from the question is not running (throws an error) so you would also need to modify this bit. – ImportanceOfBeingErnest Oct 11 '17 at 15:41
  • I now removed the barbs from the question and gave an answer to it. Concerning the barbs, please ask a new question (without colorbars) but with a [mcve] of the issue that is actually runnable. – ImportanceOfBeingErnest Oct 11 '17 at 17:03
  • @ImportanceOfBeingErnest I apologize for the way I asked the question. And thank you very much for your patience. +1 – Li Ziming Oct 12 '17 at 01:16

1 Answers1

3

You would want to make sure the colorbar always sits in the same axes. To this end you may create a colorbar axes (cax) outside the loop, e.g. using axes_divider, and always plot the colorbar to that axes, fig.colorbar(cf, cax=cax).

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable
import numpy as np

a = np.arange(10)
bb, cc = np.meshgrid(a, a)
u = np.random.randint(2, 4, (10, 10))
v = np.random.randint(2, 4, (10, 10))

fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111)

# create axes for the colorbar
cax = make_axes_locatable(ax).append_axes("right", size="5%", pad="2%")


for i in range(10):
    # clear colorbar axes
    cax.clear()
    u = np.random.randint(2, 4, (10, 10))
    v = np.random.randint(2, 4, (10, 10))
    ws = np.sqrt(u**2 + v**2)

    cf = ax.contourf(bb, cc, ws)
    # draw new colorbar in existing cax
    cb = fig.colorbar(cf, cax=cax)

    fig.canvas.update()
    fig.canvas.flush_events()

    #plt.savefig('test_barb/%i.png' % i, dpi=200)


plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712