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. Instead what happens is the scale is set based upon the limits of the initial plot. A MWE is
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))
pyplot.draw()
The first plot command generates a plot from [0,1]
and I can see everything just fine. At the end, the y-data array goes from [0,10)
with most of it greater than 1
, but the y-limits of the figure remain [0,1]
.
I know I can manually change the limits using pyplot.ylim(...)
, but I don't know what to change them to. In the for
loop, can I tell pyplot to scale the limits as if it was the first time being plotted?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…