Home > Back-end >  Why the view doesn't change with the new data that plotted by FuncAnimation?
Why the view doesn't change with the new data that plotted by FuncAnimation?

Time:03-19

I am plotting some data, but see nothink. If you zoom out, you will find, that the data is plotted on other side of fig, and the view doesn't...automatically changed for be able to see data. Could someone help me, please?

from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation


fig = plt.figure()
ax = fig.add_subplot()
line, = ax.plot([],[])
x = []
y = []

def animate(i):
    x.append(i)
    y.append((-1)**i)
    line.set_data(x, y)
    return line,

anim = FuncAnimation(fig, animate, frames=200, interval=100, blit=True)
plt.show()

CodePudding user response:

As per https://stackoverflow.com/a/7198623/1008142, you need to call the axes relim and autoscale_view methods.

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


fig = plt.figure()
ax = fig.add_subplot()
line, = ax.plot([],[])
x = []
y = []

def animate(i):
    x.append(i)
    y.append((-1)**i)
    line.set_data(x, y)
    ax.relim()
    ax.autoscale_view()
    return line,

anim = FuncAnimation(fig, animate,
                     frames=200, interval=100, blit=True)
plt.show()
  • Related