set_data和autoscale_view matplotlib [英] set_data and autoscale_view matplotlib

查看:89
本文介绍了set_data和autoscale_view matplotlib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have multiple lines to be drawn on the same axes, and each of them are dynamically updated (I use set_data), The issue being that i am not aware of the x and y limits of each of the lines. And axes.autoscale_view(True,True,True) / axes.set_autoscale_on(True) are not doing what they are supposed to. How do i auto scale my axes?

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 . 在我遇到的所有情况下,x,y限制都是已知的.我在轴上有多条线,并且它们的范围会变化,因此跟踪整个数据的ymax是不现实的

I have referred to these already, this , this. In all cases I have come across, the x,y limits are known. I have multiple lines on the axes and their ranges change, keeping track of the ymax for the entire data is not practical

一些探索使我明白了这一点,

A little bit of exploring got me to this,

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

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

But here again, i do not know how to access FigureImage from the Figure instance.

使用matplotlib 0.99.3

Using matplotlib 0.99.3

推荐答案

来自用于autoscale_view的matplotlib文档:

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

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()调用之前添加两行:

So, you'll need to add two lines before your plt.draw() call after the set_data call:

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

这篇关于set_data和autoscale_view matplotlib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆