<tfoot id='pLhXC'></tfoot>

      <small id='pLhXC'></small><noframes id='pLhXC'>

      • <bdo id='pLhXC'></bdo><ul id='pLhXC'></ul>
      <i id='pLhXC'><tr id='pLhXC'><dt id='pLhXC'><q id='pLhXC'><span id='pLhXC'><b id='pLhXC'><form id='pLhXC'><ins id='pLhXC'></ins><ul id='pLhXC'></ul><sub id='pLhXC'></sub></form><legend id='pLhXC'></legend><bdo id='pLhXC'><pre id='pLhXC'><center id='pLhXC'></center></pre></bdo></b><th id='pLhXC'></th></span></q></dt></tr></i><div id='pLhXC'><tfoot id='pLhXC'></tfoot><dl id='pLhXC'><fieldset id='pLhXC'></fieldset></dl></div>
    1. <legend id='pLhXC'><style id='pLhXC'><dir id='pLhXC'><q id='pLhXC'></q></dir></style></legend>
    2. 使用 matplotlib 的动画交互式绘图

      时间:2023-09-03
          <tbody id='MEAvd'></tbody>
            1. <legend id='MEAvd'><style id='MEAvd'><dir id='MEAvd'><q id='MEAvd'></q></dir></style></legend>
              <tfoot id='MEAvd'></tfoot>

              <small id='MEAvd'></small><noframes id='MEAvd'>

            2. <i id='MEAvd'><tr id='MEAvd'><dt id='MEAvd'><q id='MEAvd'><span id='MEAvd'><b id='MEAvd'><form id='MEAvd'><ins id='MEAvd'></ins><ul id='MEAvd'></ul><sub id='MEAvd'></sub></form><legend id='MEAvd'></legend><bdo id='MEAvd'><pre id='MEAvd'><center id='MEAvd'></center></pre></bdo></b><th id='MEAvd'></th></span></q></dt></tr></i><div id='MEAvd'><tfoot id='MEAvd'></tfoot><dl id='MEAvd'><fieldset id='MEAvd'></fieldset></dl></div>

              • <bdo id='MEAvd'></bdo><ul id='MEAvd'></ul>
                本文介绍了使用 matplotlib 的动画交互式绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                在寻找使用 matplotlib 制作动画交互式绘图的方法时,我在 Stack Overflow 文档中遇到了这段代码:

                While looking for a way to make animated interactive plot using matplotlib, I encountered this piece of code on Stack overflow documentation:

                import numpy as np
                import matplotlib.pyplot as plt
                import matplotlib.animation as animation
                from matplotlib.widgets import Slider
                
                TWOPI = 2*np.pi
                
                fig, ax = plt.subplots()
                
                t = np.arange(0.0, TWOPI, 0.001)
                initial_amp = .5
                s = initial_amp*np.sin(t)
                l, = plt.plot(t, s, lw=2)
                
                ax = plt.axis([0,TWOPI,-1,1])
                
                axamp = plt.axes([0.25, .03, 0.50, 0.02])
                # Slider
                samp = Slider(axamp, 'Amp', 0, 1, valinit=initial_amp)
                
                def update(val):
                    # amp is the current value of the slider
                    amp = samp.val
                    # update curve
                    l.set_ydata(amp*np.sin(t))
                    # redraw canvas while idle
                    fig.canvas.draw_idle()
                
                # call update function on slider value change
                samp.on_changed(update)
                
                plt.show()
                

                这段代码几乎完全符合我的要求,但我希望为情节设置动画,即让滑块自动从左向右移动,例如每秒 0.01 的进度.有什么简单的方法吗?知道我还想保留滑块上的手动控制(使用点击事件).

                This code does almost exactly what I'm looking for, but I would wish to animate the plot, i.e. make the slider moves automatically from left to right, for instance progressing of 0.01 every second. Is there any simple way of doing that? Knowing that I also want to keep the manual control on the slider (using click event).

                推荐答案

                这里是你的代码添加动画的简单改编:

                Here is a simple adaptation of your code to add animation:

                import numpy as np
                import matplotlib.pyplot as plt
                import matplotlib.animation as animation
                from matplotlib.widgets import Slider
                
                TWOPI = 2*np.pi
                
                fig, ax = plt.subplots()
                
                t = np.arange(0.0, TWOPI, 0.001)
                initial_amp = .5
                s = initial_amp*np.sin(t)
                l, = plt.plot(t, s, lw=2)
                
                ax = plt.axis([0,TWOPI,-1,1])
                
                axamp = plt.axes([0.25, .03, 0.50, 0.02])
                # Slider
                samp = Slider(axamp, 'Amp', 0, 1, valinit=initial_amp)
                
                # Animation controls
                is_manual = False # True if user has taken control of the animation
                interval = 100 # ms, time between animation frames
                loop_len = 5.0 # seconds per loop
                scale = interval / 1000 / loop_len
                
                def update_slider(val):
                    global is_manual
                    is_manual=True
                    update(val)
                
                def update(val):
                    # update curve
                    l.set_ydata(val*np.sin(t))
                    # redraw canvas while idle
                    fig.canvas.draw_idle()
                
                def update_plot(num):
                    global is_manual
                    if is_manual:
                        return l, # don't change
                
                    val = (samp.val + scale) % samp.valmax
                    samp.set_val(val)
                    is_manual = False # the above line called update_slider, so we need to reset this
                    return l,
                
                def on_click(event):
                    # Check where the click happened
                    (xm,ym),(xM,yM) = samp.label.clipbox.get_points()
                    if xm < event.x < xM and ym < event.y < yM:
                        # Event happened within the slider, ignore since it is handled in update_slider
                        return
                    else:
                        # user clicked somewhere else on canvas = unpause
                        global is_manual
                        is_manual=False
                
                # call update function on slider value change
                samp.on_changed(update_slider)
                
                fig.canvas.mpl_connect('button_press_event', on_click)
                
                ani = animation.FuncAnimation(fig, update_plot, interval=interval)
                
                plt.show()
                

                主要变化是增加了update_plot函数,用于在倒数第二行制作一个FuncAnimation.动画从设置的最后一个滑块值开始递增.

                The main change is the addition of the update_plot function, which is used to make a FuncAnimation in the second to last line. The animation increments from the last slider value that was set.

                变量is_manual 跟踪用户何时点击了滑块.用户点击后,变量设置为True,动画将不再更新剧情.

                The variable is_manual keeps track of when the user has clicked on the slider. After the user clicks on it, the variable is set to True and the animation will no longer update the plot.

                为了恢复动画,我添加了一个 on_click 函数,当用户单击画布上的某个位置而不是滑块时设置 is_manual = False.

                To resume animation, I added an on_click function which sets is_manual = False when the user clicks somewhere on the canvas OTHER than the slider.

                由于这是一个简单粗暴的脚本,我将变量保留为全局变量,但您可以轻松地将其写在适当的类中.

                Since this is a quick-and-dirty script I left variables as global, but you could easily write it up in a proper class.

                注意调用samp.set_val会隐式调用update_slider函数,当用户直接点击滑块时也会调用该函数,所以我们要重置is_manualupdate_plot 函数中.

                Note that calling samp.set_val implicitly calls the update_slider function, which is also called when the user clicks directly on the slider, so we have to reset is_manual in the update_plot function.

                这篇关于使用 matplotlib 的动画交互式绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何对 Django Select 小部件中的选择进行分组? 下一篇:如何在散景 python 中捕获下拉小部件的值?

                相关文章

              • <small id='MhT6w'></small><noframes id='MhT6w'>

                <tfoot id='MhT6w'></tfoot>
              • <legend id='MhT6w'><style id='MhT6w'><dir id='MhT6w'><q id='MhT6w'></q></dir></style></legend>

                      <bdo id='MhT6w'></bdo><ul id='MhT6w'></ul>
                    <i id='MhT6w'><tr id='MhT6w'><dt id='MhT6w'><q id='MhT6w'><span id='MhT6w'><b id='MhT6w'><form id='MhT6w'><ins id='MhT6w'></ins><ul id='MhT6w'></ul><sub id='MhT6w'></sub></form><legend id='MhT6w'></legend><bdo id='MhT6w'><pre id='MhT6w'><center id='MhT6w'></center></pre></bdo></b><th id='MhT6w'></th></span></q></dt></tr></i><div id='MhT6w'><tfoot id='MhT6w'></tfoot><dl id='MhT6w'><fieldset id='MhT6w'></fieldset></dl></div>