我用matplotlib创建了一些情节,这取决于8个变量。我想研究当我改变一些情节时情节如何变化。我创建了一些调用matplotlib的脚本并生成不同的快照,稍后我将其转换为电影,它不错,但有点笨拙。
除了@triplepoint提到的内容之外,还可以看一下滑块小部件。
有一个example on the matplotlib examples page。它是一个图形滑块而不是键盘绑定,但它适用于您想要做的事情。
另请注意,为了保证滑块和按钮保持响应而不是垃圾收集,应自行维护对象(amp_slider
,freq_slider
等)的引用。
(我正在制作这个社区维基,因为我只是从示例中复制粘贴。
这个特殊的例子教导了坏习惯(例如from pylab import *
),但它得到了重点。
该示例已得到修复,以避免使用pylab
。)
from numpy import pi, sin
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons
def signal(amp, freq):
return amp * sin(2 * pi * freq * t)
axis_color = 'lightgoldenrodyellow'
fig = plt.figure()
ax = fig.add_subplot(111)
# Adjust the subplots region to leave some space for the sliders and buttons
fig.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
amp_0 = 5
freq_0 = 3
# Draw the initial plot
# The 'line' variable is used for modifying the line later
[line] = ax.plot(t, signal(amp_0, freq_0), linewidth=2, color='red')
ax.set_xlim([0, 1])
ax.set_ylim([-10, 10])
# Add two sliders for tweaking the parameters
# Define an axes area and draw a slider in it
amp_slider_ax = fig.add_axes([0.25, 0.15, 0.65, 0.03], axisbg=axis_color)
amp_slider = Slider(amp_slider_ax, 'Amp', 0.1, 10.0, valinit=amp_0)
# Draw another slider
freq_slider_ax = fig.add_axes([0.25, 0.1, 0.65, 0.03], axisbg=axis_color)
freq_slider = Slider(freq_slider_ax, 'Freq', 0.1, 30.0, valinit=freq_0)
# Define an action for modifying the line when any slider's value changes
def sliders_on_changed(val):
line.set_ydata(signal(amp_slider.val, freq_slider.val))
fig.canvas.draw_idle()
amp_slider.on_changed(sliders_on_changed)
freq_slider.on_changed(sliders_on_changed)
# Add a button for resetting the parameters
reset_button_ax = fig.add_axes([0.8, 0.025, 0.1, 0.04])
reset_button = Button(reset_button_ax, 'Reset', color=axis_color, hovercolor='0.975')
def reset_button_on_clicked(mouse_event):
freq_slider.reset()
amp_slider.reset()
reset_button.on_clicked(reset_button_on_clicked)
# Add a set of radio buttons for changing color
color_radios_ax = fig.add_axes([0.025, 0.5, 0.15, 0.15], axisbg=axis_color)
color_radios = RadioButtons(color_radios_ax, ('red', 'blue', 'green'), active=0)
def color_radios_on_clicked(label):
line.set_color(label)
fig.canvas.draw_idle()
color_radios.on_clicked(color_radios_on_clicked)
plt.show()
Matplotlib有一些相当不错的gui功能。 matplotlib的源tarball中有一些文档示例,在/ examples / user_interfaces和matplotlib> / examples / event_handling中。特别是关键处理是:http://matplotlib.sourceforge.net/examples/event_handling/keypress_demo.html
我做了类似于你的目标的事情:
import numpy as np
import pylab
class plotter:
def __init__(self, initial_values):
self.values
self.fig = pylab.figure()
pylab.gray()
self.ax = self.fig.add_subplot(111)
self.draw()
self.fig.canvas.mpl_connect('key_press_event',self.key)
def draw(self):
im = your_function(self.values)
pylab.show()
self.ax.imshow(im)
def key(self, event):
if event.key=='right':
self.values = modify()
elif event.key == 'left':
self.values = modify()
self.draw()
self.fig.canvas.draw()
我正在使用它来通过按键在堆栈中显示不同的图像,但是您应该能够在给定键盘输入的情况下输入逻辑来修改您的值。
如果你想做像用户输入值这样的事情,我认为这些例子有对话框的选项,但是如果你只想增加/减少一些变量,只需用这种方式为它们定义键盘对就可以了。
使用waitforbuttonpress(timeout=0.001)
然后绘图将看到你的鼠标蜱。
我不认为简单地使用plt.plot
绘制图形将允许您这样做。您需要自己通过将Matplotlib嵌入其中来制作自定义GUI脚本/应用程序。目前,Matplotlib支持所有主要的GUI工具包 - PyGTK +,PyQt4和wxPython。
我使用wxPython并在其中嵌入matplotlib相当容易。类似于其他GUI工具包也应如此。您可以在书中获得所需的所有信息 -
它可以在amazon here上找到。
对于ipython或jupyter笔记本,您可以使用ipywidgets:
from ipywidgets import *
def update(w=0,h=0):
print(h+w)
interact(update, w= widgets.IntSlider(value=1, min=0, max=7, step=1) ,
h= widgets.IntSlider(value=1, min=0, max=7, step=1) );
请参阅此处的文档:https://ipywidgets.readthedocs.io/en/stable/examples/Using%20Interact.html