用两个滑块交互式绘制matplotlib图

我用matplotlib来创build一些图,这取决于8个variables。 我想研究一下当我改变一些情节时情节如何改变。 我创build了一些调用matplotlib的脚本,并生成不同的快照,稍后我将其转换成电影,这不是坏事,但有点笨拙。

  1. 我想知道是否可以通过键盘按键与graphics重新生成相互作用,从而增加/减less一些variables的值,并立即查看graphics如何变化。

  2. 什么是最好的办法呢?

  3. 另外如果你能指向我有趣的链接或一个只有两个滑块的情节例子的链接?

除了提到的@triplepoint之外,还要看看滑块小部件。

matplotlib示例页面上有一个示例 。 这是一个graphics滑动条,而不是键盘绑定,但是对于你想要做的事情来说,它工作的很好。

另外请注意,要保证滑块和button保持响应,而不是垃圾回收,对象( amp_sliderfreq_slider等)的引用应由您自己维护。

(我正在制作这个社区wiki,因为我只是从示例中复制粘贴from pylab import * ),这个特殊的例子教会了一些不好的习惯(例如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有一些相当不错的GUIfunction。 在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() 

我正在使用这个来移位通过在按键上堆叠显示不同的图像,但是你应该能够把逻辑放入键盘input来修改你的值。

如果你想做的事情就像有用户input的值,我认为这些例子有对话框的选项,但如果你只是想增加/减less一些variables,只是以这种方式定义他们的键盘对可能会运作良好

我不认为使用plt.plot简单地绘制图表可以让你做到这一点。 您将需要通过将Matplotlibembedded到其中来自定义GUI脚本/应用程序。 目前,Matplotlib支持所有主要的GUI工具包 – PyGTK +,PyQt4和wxPython。
我使用wxPython并在其中embeddedmatplotlib是相当容易的。 其他GUI工具包的情况也是如此。 你可以在书中得到你需要的所有信息 – 在这里输入图像说明

它在亚马逊这里可用。

使用waitforbuttonpress(timeout=0.001)然后情节将看到你的鼠标滴答声。