set_data和autoscale_view matplotlib

cha*_*itu 26 python matplotlib

我在同一轴上绘制了多条线,并且每条线都是动态更新的(我使用set_data),问题是我不知道每条线的x和y限制.而axis.autoscale_view(True,True,True)/ axes.set_autoscale_on(True)并没有做到他们应该做的事情.如何自动缩放我的轴?

import matplotlib.pyplot as plt

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

axes.set_autoscale_on(True)
axes.autoscale_view(True,True,True)

l1, = axes.plot([0,0.1,0.2],[1,1.1,1.2])
l2, = axes.plot([0,0.1,0.2],[-0.1,0,0.1])

#plt.show() #shows the auto scaled.

l2.set_data([0,0.1,0.2],[-1,-0.9,-0.8])

#axes.set_ylim([-2,2]) #this works, but i cannot afford to do this.  

plt.draw()
plt.show() #does not show auto scaled
Run Code Online (Sandbox Code Playgroud)

我已经提到了这些,这个,这个.在我遇到的所有情况下,x,y限制都是已知的.我在轴上有多条线并且它们的范围发生变化,跟踪整个数据的ymax是不切实际的

一点点探索让我这样,

xmin,xmax,ymin,ymax = matplotlib.figure.FigureImage.get_extent(FigureImage) 
Run Code Online (Sandbox Code Playgroud)

但在这里,我不知道如何从图实例访问FigureImage.

使用matplotlib 0.99.3

Joh*_*yon 41

来自autoscale_view的matplotlib文档:

在将艺术家添加到Axes实例后更改艺术家数据时,不会自动更新数据限制.在这种情况下,请在调用autoscale_view之前使用matplotlib.axes.Axes.relim().

因此,在plt.draw()通话结束后,您需要在通话前添加两行set_data:

axes.relim()
axes.autoscale_view(True,True,True)
Run Code Online (Sandbox Code Playgroud)