Matplotlib - 用户通过图输入数字输入 [英] Matplotlib - user enters numeric input through figure
问题描述
我想要我的图形有一个小的输入窗口,用户可以在其中键入一个数字,而绘制的数据将跨越这许多分钟。如果他们输入30,他们将看到一个30分钟的时间窗口,如果他们键入5,matplotlib选择这一点,数据被修剪,只显示5分钟的数据。
I want my figure to have a small entry window, in which the user can type a number, and the data plotted will span that many minutes. If they enter 30, they will look at a 30-minute time window, if they type 5, matplotlib picks this up, data gets trimmed, and only 5 minutes worth of data gets shown.
我如何做到这一点?我注意到,SO的人推荐使用TkAgg,有没有办法做到没有它?如果我使用TkAgg,你能指点我的一个最小的例子,这是以互动的方式,即挑选用户做的新条目?
How can I do this? I noticed that people on SO recommend using TkAgg, is there a way to do this without it? If I do use TkAgg, can you point me to a minimal example that does this in an interactive manner, i.e. picks up new entries that the user makes?
谢谢
编辑:这是STREAMING数据,所以我想条件为动态形式,如给我最后15分钟,而不是在2:10和2:25'。
此外,我将自己执行数据的手动修剪,gui不必这样做。 gui只需要读一个数字,并让我可以使用。
This is STREAMING data, so I want to condition to be of dynamic form, such as 'give me last 15 minutes' rather than 'give me between 2:10 and 2:25'. Also, I will performing the trimming of the data manually myself, the gui doesn't have to do it. The gui only needs to read a single number and make it available to me.
一个更详细:不要担心窗帘背后发生了什么,我知道如何搞定此事。所有我想知道的只是如何读取一个数字从一个文本框中的数字在matplotlib。
ONE MORE DETAIL: Don't worry about what happens behind the curtains, I know how to take care of it. All I want to know is simply how to read a number from a text box on a figure in matplotlib.
推荐答案
认为你可以使用文本框做任何你想要的,而不使用第三方GUI程序。下面的例子显示了如何使用滑块只使用matplotlib本身来改变绘图的x限制。
I don't think you can do what you want using a text box without using a 3rd party GUI program. The example below shows how a slider can be used to change the x limits of a plot using just matplotlib itself.
示例使用 Slider widget 来控制xlimits。您可以在此处找到另一个使用许多小部件的示例。
The example used a Slider widget to control the xlimits. You can find another example of using many widgets 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()
下面是一些显示不同位置滑块的图:
Below are some plots showing the slider at different positions:
这篇关于Matplotlib - 用户通过图输入数字输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!