Matplotlib - 用户通过数字输入数字输入

时间:2014-08-22 14:28:59

标签: python user-interface matplotlib charts

我希望我的数字有一个小的输入窗口,用户可以在其中输入一个数字,并且绘制的数据将跨越很多分钟。如果他们输入30,他们将看30分钟的时间窗口,如果他们输入5,matplotlib会选择这个,数据被修剪,只显示5分钟的数据。

我该怎么做?我注意到SO上的人推荐使用TkAgg,有没有办法在没有它的情况下做到这一点?如果我使用TkAgg,你能指出一个以交互方式做到这一点的最小例子,即拿起用户做出的新条目吗?

谢谢

编辑:这是STREAMING数据,因此我希望条件为动态形式,例如“给我最后15分钟”而不是“在2:10和2:25之间给我”。 此外,我将自己手动修剪数据,gui不必这样做。 gui只需要阅读一个数字并将其提供给我。

更多细节:不要担心窗帘后面会发生什么,我知道如何照顾它。我想知道的只是如何从matplotlib中的图形上的文本框中读取数字。

1 个答案:

答案 0 :(得分:1)

我不认为你可以在不使用第三方GUI程序的情况下使用文本框做你想做的事。下面的示例显示了如何使用滑块来仅使用matplotlib本身来更改绘图的x限制。

该示例使用Slider widget来控制xlimits。您可以找到另一个使用许多小部件here的示例。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

# Create some random data
x = np.linspace(0,100,1000)
y = np.sin(x) * np.cos(x)

left, bottom, width, height = 0.15, 0.02, 0.7, 0.10

fig, ax = plt.subplots()

plt.subplots_adjust(left=left, bottom=0.25) # Make space for the slider

ax.plot(x,y)

# Set the starting x limits
xlims = [0, 1]
ax.set_xlim(*xlims)

# Create a plt.axes object to hold the slider
slider_ax = plt.axes([left, bottom, width, height])
# Add a slider to the plt.axes object
slider = Slider(slider_ax, 'x-limits', valmin=0.0, valmax=100.0, valinit=xlims[1])

# Define a function to run whenever the slider changes its value.
def update(val):
    xlims[1] = val
    ax.set_xlim(*xlims)

    fig.canvas.draw_idle()

# Register the function update to run when the slider changes value
slider.on_changed(update)

plt.show()

以下是一些显示不同位置滑块的图:

默认(开始)位置

fig 1

将滑块设置为随机值

fig 2

将滑块设置为最大值

fig 3