An Ri
An Ri

Reputation: 476

Why the view doesn't change with the new data that plotted by FuncAnimation?

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()

Upvotes: 0

Views: 40

Answers (1)

Rory Yorke
Rory Yorke

Reputation: 2236

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()

Upvotes: 1

Related Questions