【问题标题】:how do I update the x limits in a matplotlib figure in tkinter如何在 tkinter 的 matplotlib 图中更新 x 限制
【发布时间】:2013-01-08 23:24:05
【问题描述】:

我使用 TKinter 制作了一个 GUI,可以从 Agilent 示波器读取示波器轨迹。我希望在更改时间/格时更新 x 轴。要更新 x 和 y 数据,我使用 set_xdataset_ydata。是否有类似的方法来更新 x 限制?

【问题讨论】:

  • 您要更改范围或刻度吗?
  • 你解决了吗?

标签: python matplotlib tkinter


【解决方案1】:

您需要了解一点对象层次结构。您在 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

set_xlimdocset_ylimdoc

【讨论】:

  • 我的代码设置类似于我在听到(答案 2)中找到的代码。我想以与更新 y 数据类似的方式更新 x 限制。 stackoverflow.com/questions/9997869/…
  • @MarkMitry 谢谢很好,投票和接受的答案更好;)
【解决方案2】:

pyplot.xlim() function 改变 x 轴(当前轴)的范围。

Axes 的 set_xticks() method 设置刻度。例如,可以使用gca() 获取当前轴。

【讨论】:

    猜你喜欢
    • 2018-04-12
    • 2015-08-26
    • 2022-08-13
    • 2020-03-18
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多