简体   繁体   English

如何在tkinter中更新matplotlib图中的x限制

[英]how do I update the x limits in a matplotlib figure in tkinter

I made a GUI with TKinter that reads a scope trace from a agilent scope. 我使用TKinter制作了一个GUI,该GUI从安捷伦范围读取范围跟踪。 I want the x axis to update when I change time/div. 我希望在更改时间/格时更新x轴。 To update the x and y data I use set_xdata and set_ydata. 要更新x和y数据,我使用set_xdata和set_ydata。 Is there a similar method for updating the x limits? 是否有类似的方法来更新x限制?

You need to understand a bit of the object hierarchy. 您需要了解一些对象层次结构。 You are calling set_xdata on a Line2D object, which is an Artist which is associated with an Axes object (which handles things like log vs linear, the x/y limits, axis label, tick location and labels) which is associated with a Figure object (which groups together a bunch of axes objects, deals with the window manager (for gui), ect) and a canvas object (which actually deals with translating all of the other objects to a picture on the screen). 要调用set_xdata一个上Line2D对象,它是一个Artist ,其具有相关联的Axes对象(处理诸如日志VS线性的东西,在X / Y的限制,轴线标签,蜱位置和标签),其与相关联的Figure对象(将一堆轴对象组合在一起,处理窗口管理器(对于gui),ect等)和一个canvas对象(实际上处理将所有其他对象转换为屏幕上的图片)。

If you are using Tkinter, I assume that you have an axes object, (that I will call ax ). 如果您使用的是Tkinter,则假定您有一个axes对象(我将其称为ax )。

ax = fig.subplot(111) # or where ever you want to get you `Axes` object from.
my_line = ax.plot(data_x, data_y)
# whole bunch of code
#
# more other code


# update your line object
my_line.set_xdata(new_x_data)
my_line.set_ydata(new_y_data)

# update the limits of the axes object that you line is drawn on.
ax.set_xlim([top, bottom])
ax.set_ylim([left, right])

so to update the data in the line, you need to update my_line , to update the axes limits, you need to update ax . 因此,要更新该行中的数据,您需要更新my_line ,要更新轴限制,您需要更新ax

set_xlim doc and set_ylim doc set_xlim 文档set_ylim 文档

The pyplot.xlim() function changes the range of the x axis (of the current Axes). pyplot.xlim()函数更改(当前轴的)x轴的范围。

The set_xticks() method of Axes sets the ticks. 轴的set_xticks()方法设置刻度。 The current Axes can be obtained for instance with gca() . 当前轴可以通过gca()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM