matplotlib animation with changing colorbar and ti

2019-06-22 23:50发布

I've made an animation in matplotlib. You can find a simplification of my code underneath :

# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

begin = 62
eind = 227
verschil = eind - begin

fig = plt.figure()

def f(x, y):
    return np.sin(x) + (0*y)

x = np.linspace(0, 2 * np.pi, 501)
y = np.linspace(0, 2 * np.pi, 501).reshape(-1, 1)

afbeeldinglijst = []
for i in range(verschil):
    x = x + 1
    titel = begin + 1
    afbeelding = plt.imshow(f(x, y))
    afbeeldinglijst.append([afbeelding])
    plt.colorbar()
    plt.title(titel)
    plt.clim(-0.8,0.8)

A = animation.ArtistAnimation(fig, afbeeldinglijst, interval=verschil, blit=True, repeat_delay=2000)

plt.show()

My problem is that the only thing i can see, are colorbars.. In stead of changing the colorbar for every frame, it just adds colorbars to the figure. I want my colorbar to change with every change that is made in the picture. Same for the title, i want a new title for every change in the image, from 62 to 227. How can i do this? I hope i'm being clear in what i want :). thanks!

I fixed it myself, if someone is interested : Now I have a text in the animation that gives the right number between 62 and 227. I also fixed the colorbar to be the correct one for every frame. I just added a minimum and maximum value to plt.imshow, so all the frames have the same minimum and maximum value; and afterwards I added a colorbar. Underneath my code (as I used it, not in the simplified version as written above) :

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

begin = 62
eind = 227
minwaarde = 0
maxwaarde = 0.05

verschil = eind - begin

fig = plt.figure()
ax = fig.add_subplot(111)

def f(x, y):
    return np.genfromtxt("../e/c"+str(begin)+".txt",delimiter="") + (0*y)

x = np.arange(0, 501)
y = np.arange(0, 501).reshape(-1,1)

afbeeldinglijst = []
for i in range(verschil):
    t = ax.annotate(begin,(50,50),color='w')
    begin = begin + 1
    afbeelding = plt.imshow(f(x, y),vmin=minwaarde,vmax=maxwaarde)
    afbeeldinglijst.append([afbeelding,t])

A = animation.ArtistAnimation(fig, afbeeldinglijst, interval=verschil, blit=True, repeat_delay=2000)
plt.colorbar()

plt.show()

0条回答
登录 后发表回答