如何更新 tkinter 中 matplotlib 图形中的 x 限制
我使用 TKinter 制作了一个 GUI,它可以读取来自安捷伦示波器的示波器跟踪.我想在更改时间/格时更新x轴.要更新x和y数据,我使用 set_xdata
和 set_ydata
.是否有类似的更新 x 限制的方法?
I made a GUI with TKinter that reads a scope trace from a agilent scope. I want the x axis to update when I change time/div. To update the x and y data I use set_xdata
and set_ydata
. Is there a similar method for updating the x limits?
您需要了解一些对象层次结构.您正在 Line2D
对象上调用 set_xdata
,该对象是与 Axes
对象(该对象关联的 Artist
处理与 Figure
对象(将一堆轴对象组合在一起,处理窗口管理器)关联的日志与线性、x/y 限制、轴标签、刻度位置和标签等内容(用于 gui)等)和一个 canvas
对象(它实际上处理将所有其他对象转换为屏幕上的图片).
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).
如果您使用 Tkinter,我假设您有一个 axes
对象(我将其称为 ax
).
If you are using Tkinter, I assume that you have an axes
object, (that I will call 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])
所以要更新行中的数据,您需要更新my_line
,要更新轴限制,您需要更新ax
.
so to update the data in the line, you need to update my_line
, to update the axes limits, you need to update ax
.