小编Col*_*oin的帖子

使用matplotlib的动画交互式绘图

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

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 …
Run Code Online (Sandbox Code Playgroud)

python interactive widget matplotlib

2
推荐指数
2
解决办法
4017
查看次数

标签 统计

interactive ×1

matplotlib ×1

python ×1

widget ×1