设置数据和自动缩放视图matplotlib

2024-05-16 17:46:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我有多条线要在同一个轴上绘制,并且每一条线都是动态更新的(我使用set_data),问题是我不知道每条线的x和y限制。和轴。自动缩放视图(真,真,真)/轴。设置自动缩放为开(真)并没有做它们应该做的。如何自动缩放轴?

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

我已经提到过这些,thisthis。 在我遇到的所有情况下,x,y极限都是已知的。我在轴上有多条线,它们的范围也在变化,跟踪整个数据的ymax是不实际的

一点点探索让我明白了这一点

xmin,xmax,ymin,ymax = matplotlib.figure.FigureImage.get_extent(FigureImage) 

但是在这里,我不知道如何从Figure实例访问FigureImage。

使用matplotlib 0.99.3


Tags: trueautodataplotmatplotlibshowfigplt
1条回答
网友
1楼 · 发布于 2024-05-16 17:46:34

matplotlib docs for autoscale_view

The data limits are not updated automatically when artist data are changed after the artist has been added to an Axes instance. In that case, use matplotlib.axes.Axes.relim() prior to calling autoscale_view.

因此,您需要在plt.draw()调用之前和set_data调用之后添加两行:

axes.relim()
axes.autoscale_view(True,True,True)

相关问题 更多 >