Python 三维交互式图形赢得';t更新

Python 三维交互式图形赢得';t更新,python,matplotlib,3d,graph-visualization,matplotlib-widget,Python,Matplotlib,3d,Graph Visualization,Matplotlib Widget,我用这段代码创建了一个交互式绘图(2d),它可以工作 import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider, Button, RadioButtons fig, ax = plt.subplots() plt.subplots_adjust(left=0.25, bottom=0.25) t = np.arange(0.0, 1.0, 0.001) a0 = 5 f0 =

我用这段代码创建了一个交互式绘图(2d),它可以工作

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
a0 = 5
f0 = 3
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -10, 10])

axcolor = 'lightgoldenrodyellow'
axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg=axcolor)
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg=axcolor)

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)


def update(val):
    amp = samp.val
    freq = sfreq.val
    l.set_ydata(amp*np.sin(2*np.pi*freq*t))
    fig.canvas.draw_idle()
sfreq.on_changed(update)
samp.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')


def reset(event):
    sfreq.reset()
    samp.reset()
button.on_clicked(reset)

rax = plt.axes([0.025, 0.5, 0.15, 0.15], axisbg=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)


def colorfunc(label):
    l.set_color(label)
    fig.canvas.draw_idle()
radio.on_clicked(colorfunc)

plt.show()
然后,我尝试通过简单地将轴更改为axes3d来修改它以创建交互式3d绘图。我添加了如下所示的import语句,并将“fig”和“ax”的定义替换为下面所示的定义,使之成为3d

from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
情节不再更新,我也不知道为什么。函数fig.canvas.draw_idle()似乎不适用于3d图形,但我没有其他方法来更新图形

任何帮助都将不胜感激


谢谢

通过查看更新后的颜色,可以看到函数fig.canvas.draw_idle()确实存在,并按预期工作

问题在于
set_ydata
函数,它在3d空间中的工作方式不同。 假设您希望y坐标更新,z坐标保持不变,
set_data
将获得常量值,同时需要设置一个附加属性来控制y坐标

以下是工作示例代码:

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.widgets import Slider, Button, RadioButtons

fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plt.subplots_adjust(left=0.25, bottom=0.25)
t = np.arange(0.0, 1.0, 0.001)
### create constant z-coordinate
z = np.zeros_like(t)    #               <------------ here
a0 = 5
f0 = 3
s = a0*np.sin(2*np.pi*f0*t)
l, = plt.plot(t, s, lw=2, color='red')
plt.axis([0, 1, -10, 10])

axfreq = plt.axes([0.25, 0.1, 0.65, 0.03], axisbg="w")
axamp = plt.axes([0.25, 0.15, 0.65, 0.03], axisbg="w")

sfreq = Slider(axfreq, 'Freq', 0.1, 30.0, valinit=f0)
samp = Slider(axamp, 'Amp', 0.1, 10.0, valinit=a0)


def update(val):
    amp = samp.val
    freq = sfreq.val
    #set constant z coordinate
    l.set_data(t, z)  #               <------------ here
    # set values to y-coordinate
    l.set_3d_properties(amp*np.sin(2*np.pi*freq*t), zdir="y") #<------------ here
    fig.canvas.draw_idle()

sfreq.on_changed(update)
samp.on_changed(update)

resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', color=axcolor, hovercolor='0.975')


def reset(event):
    sfreq.reset()
    samp.reset()
button.on_clicked(reset)

rax = plt.axes([0.025, 0.5, 0.15, 0.15], axisbg=axcolor)
radio = RadioButtons(rax, ('red', 'blue', 'green'), active=0)


def colorfunc(label):
    l.set_color(label)
    fig.canvas.draw_idle()
radio.on_clicked(colorfunc)

plt.show()
将numpy导入为np
将matplotlib.pyplot作为plt导入
从mpl_toolkits.mplot3d导入Axes3D
从matplotlib.widgets导入滑块、按钮、单选按钮
图=plt.图()
ax=图添加子地块(111,投影=“3d”)
plt.子批次调整(左=0.25,下=0.25)
t=np.arange(0.0,1.0,0.001)
###创建恒定的z坐标
z=np.类零(t)#