Matplotlib - 用户输入通过数字数值输入(Matplotlib - user enter

2019-10-20 18:41发布

我希望我的身材有一个小的输入窗口中,用户可以输入一个数字,并绘制该数据将跨越许多分钟。 如果他们进入30日,他们将看30分钟的时间窗口,如果他们键入5,matplotlib发现这一点,数据被修剪,只有5分钟的数据被显示。

我怎样才能做到这一点? 我注意到,人们对SO推荐使用TkAgg,是有办法做到这一点没有呢? 如果我不使用TkAgg,你可以点我到一个小例子,这是否以交互的方式,即拿起用户做出新的项目?

谢谢

编辑:这是数据流,所以我要条件是动态的形式,如“给我的最后15分钟”,而不是“给我2:10和2:25之间”。 另外,我会进行手动将数据的修整自己的GUI没有做到这一点。 桂只需要读取一个数字,并将其提供给我。

一个细节:不要担心窗帘后面会发生什么,我知道如何照顾它。 所有我想知道的是只是如何阅读在matplotlib一个身影从一个文本框中输入一个数字。

Answer 1:

我不认为你可以做你想要使用的是什么文本框,而无需使用第三方GUI程序。 下面的例子示出了滑块如何能够被用于改变仅使用matplotlib本身的曲线图的x限制。

该示例中使用的滑块控件来控制xlimits。 你可以找到使用许多小部件的另一个例子在这里 。

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()

下面是一些图,显示在不同位置的滑块:

默认(起始)位置

滑块设置为随机值

滑块设置为最大值



文章来源: Matplotlib - user enters numeric input through figure