python - set_data 和 autoscale_view matplotlib

标签 python matplotlib

我在同一轴上绘制了多条线,每条线都是动态更新的(我使用 set_data),问题是我不知道每条线的 x 和 y 限制。而且 axes.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

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

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

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

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

使用 matplotlib 0.99.3

最佳答案

来自 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.

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

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

关于python - set_data 和 autoscale_view matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7187504/

相关文章:

python - 创建具有相对频率的直方图

python - 如何更新交互式绘图中的颜色条

python - Matplotlib savefig 带有图外的图例

python - ffmpeg 将图像序列转换为视频

python - 获取传递给函数的参数的列表/元组/字典?

python - 诱变剂:如何在 mp3、flac 和 mp4 中检测和嵌入专辑封面

python - 更改函数内的绘图样式表

Python:在元组列表中插入计数

python - Pandas : OR If value contains "-" then do

python - 如何绘制多列的条形图 3D 投影