我使用 TKinter 制作了一个 GUI,可以从安捷伦示波器读取示波器轨迹。我希望 x 轴在更改时间/格时更新。为了更新 x 和 y 数据,我使用 set_xdata
和 set_ydata
。是否有类似的方法来更新 x 限制?
最佳答案
您需要了解一些对象层次结构。您正在对 Line2D
对象调用 set_xdata
,该对象是与 Axes
对象关联的 Artist
(处理诸如对数与线性、x/y 限制、轴标签、刻度位置和标签等与 Figure
对象关联的内容(它将一堆轴对象组合在一起,处理窗口管理器) (对于 gui)等)和一个 canvas
对象(实际上处理将所有其他对象转换为屏幕上的图片)。
如果您使用 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])
因此,要更新行中的数据,您需要更新 my_line
,要更新轴限制,您需要更新 ax
。
关于python - 如何更新 tkinter 中 matplotlib 图中的 x 限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14514321/