【问题标题】:Interactive matplotlib plot with two sliders带有两个滑块的交互式 matplotlib 图
【发布时间】:2011-10-05 13:13:49
【问题描述】:

我使用 ma​​tplotlib 创建了一些绘图,它取决于 8 个变量。我想研究当我改变一些情节时情节如何变化。我创建了一些脚本,调用 ma​​tplotlib 并生成不同的快照,然后我将其转换为电影,这还不错,但有点笨拙。

  1. 我想知道是否可以使用键盘键与绘图重新生成交互,以增加/减少某些变量的值,并立即查看绘图的变化情况。

  2. 最好的方法是什么?

  3. 另外,如果您能指出有趣的链接或仅包含两个滑块的情节示例的链接?

【问题讨论】:

    标签: python keyboard matplotlib interactive


    【解决方案1】:

    除了@triplepoint 提到的,看看滑块小部件。

    有一个example on the matplotlib examples page。它是一个图形滑块而不是键盘绑定,但它非常适合您想要做的事情。

    另外请注意,为保证滑块和按钮保持响应且不被垃圾回收,应自行维护对对象(amp_sliderfreq_slider 等)的引用。

    (我正在制作这个社区 wiki,因为我只是从示例中复制粘贴。这个特定示例教坏习惯(例如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], facecolor=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], facecolor=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], facecolor=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()
    

    【讨论】:

    • 是否可以与此工具进行实际交互,还是仅生成图像?如果是这样,我需要什么来运行它?我目前正在使用 IPython
    • @triplebig - 是的,它是交互式的。如果您调用 show() 时没有发生任何事情,那么您的 matplotlib 安装中缺少 av 交互式后端。你是如何安装 matplotlib 的?
    • @triplebig - 通过“ipython”你有一个 ipython 笔记本吗? (而不是普通的 ipython shell。)如果是这样,ipython 笔记本不能使用交互式后端。您需要在常规 ipython shell 中或直接运行(例如“python name_of_your_file.py”)。
    • @triplebig - 是的,默认情况下,matplotlib 将使用某种交互式后端构建。如果您使用的是预构建的二进制文件(就像在 Windows 上一样),那么构建它们的人可能正确地做到了。如果您使用的是 winpython,matplotlib 肯定有一个交互式后端(可能是 TkAgg)。尝试直接运行脚本,看看会发生什么。 (即在终端/cmd 窗口中键入“python name_of_the_file.py”。)
    • 为了解释更多关于发生了什么,ipython notebook 与 ipython 不同。 ipython 是一个交互式 python shell。 ipython notebook 是一个基于 web 的界面,它基本上将代码的 sn-ps 发送回ipython 以评估并返回结果。出于这个原因,ipython 笔记本只是将 matplotlib 图形呈现为静态 .png,而不是弹出交互式窗口。 ipython 本身(或直接使用 python 运行脚本)将为每个 matplotlib 图显示一个交互式 gui 窗口。
    【解决方案2】:

    我按照建议检查了 jupyter 中的小部件,它们运行良好。 示例脚本上传到GitHubhttps://github.com/LeonidBystrykh/course-python-for-beginners/blob/master/Interactive_dots.ipynb

    from __future__ import print_function
    from ipywidgets import interact, interactive, fixed, interact_manual
    import ipywidgets as widgets
    import matplotlib.pyplot as plt, random
    
    def series(dots, colr):
        a,b=[],[]
        for i in range(dots):
            a.append(random.randint(1,100))
            b.append(random.randint(1,100))
        plt.scatter(a,b, c=colr)
        return()
    interact(series, dots=(1,100,1), colr=["red","orange","brown"]);
    

    图片副本如下

    【讨论】:

    • 能否将您的代码添加为文本,以便对其他人更有用。
    • 非常简单直观。谢谢
    • 代码似乎不完整。创建小部件的代码行在哪里?
    • 小部件已导入。使用它们不需要额外的行。您也可以点击原始教程的链接
    【解决方案3】:

    对于 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

    【讨论】:

      【解决方案4】:

      使用waitforbuttonpress(timeout=0.001) 然后绘图会看到你的鼠标滴答声。

      【讨论】:

        【解决方案5】:

        Matplotlib 有一些相当不错的 gui 功能。在 matplotlib 的源代码压缩包中,/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()
        

        我使用它来切换按键时在堆栈中显示不同的图像,但您应该能够在给定键盘输入的情况下输入逻辑来修改您的值。

        如果您想做诸如让用户输入值之类的事情,我认为这些示例具有对话框选项,但如果您只想增加/减少一些变量,只需以这种方式为它们定义键盘对可能好好工作

        【讨论】:

          【解决方案6】:

          我认为简单地使用plt.plot 绘制图表不会让您这样做。您需要自己通过将 Matplotlib 嵌入其中来制作自定义 GUI 脚本/应用程序。目前,Matplotlib 支持所有主要的 GUI 工具包 - PyGTK+、PyQt4 和 wxPython。
          我使用 wxPython 并在其中嵌入 matplotlib 相当容易。其他 GUI 工具包也应如此。您可以在书中获得所需的所有信息 -

          亚马逊here有售。

          【讨论】:

          • 如果你想做到又快又简单,那么matplotlib里面的slider的方法就更好更简单了。只需复制 sn-p 并更改一些内容。这本书也不错,但更适合提前,如果你想改正!
          • 看起来有点像广告,其贡献值得怀疑。的确,通过 GUI 工具包的解决方案是可能的,但并没有被要求这样做。
          猜你喜欢
          • 1970-01-01
          • 2019-04-11
          • 1970-01-01
          • 2020-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-23
          • 1970-01-01
          相关资源
          最近更新 更多