I\'m plotting data in Python using matplotlib. I am updating the data of the plot based upon some calculations and want the ylim and xlim to be rescaled automatically. Inste
You will need to update the axes' dataLim, then subsequently update the axes' viewLim based on the dataLim. The approrpiate methods are axes.relim()
and ax.autoscale_view()
method.
Your example then looks like:
import random
import matplotlib.pyplot as pyplot
pyplot.ion()
x = range(10)
y = lambda m: [m*random.random() for i in range(10)]
pLine, = pyplot.plot(x, y(1))
for i in range(10):
pLine.set_ydata(y(i+1))
ax = pyplot.gca()
# recompute the ax.dataLim
ax.relim()
# update ax.viewLim using the new dataLim
ax.autoscale_view()
pyplot.draw()