使用python中的滑块更改散点图中点的大小和颜色

使用python中的滑块更改散点图中点的大小和颜色,python,matplotlib,colors,slider,scatter-plot,Python,Matplotlib,Colors,Slider,Scatter Plot,我试图使用滑块来更改三维散点图中几个点的大小和颜色。我不明白为什么颜色不会更新,我也找不到更新尺寸的命令 from mpl_toolkits.mplot3d import Axes3D from matplotlib import pyplot from matplotlib import pyplot as plt from matplotlib.widgets import Slider def on_change(val): time=int(val)/1 point1.

我试图使用滑块来更改三维散点图中几个点的大小和颜色。我不明白为什么颜色不会更新,我也找不到更新尺寸的命令

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot
from matplotlib import pyplot as plt
from matplotlib.widgets import Slider

def on_change(val):
    time=int(val)/1
    point1.set_color(col[time][0])
    point2.set_color(col[time][1])
    #point1.size(sizes[time][0])
    #point2.size(sizes[time][1])
    fig.canvas.draw()

x=[[.3,.7],[.3,.7],[.3,.7]]
y=[[0.5,0.5],[0.5,0.5],[0.5,0.5]]
z=[[0.5,0.5],[0.5,0.5],[0.5,0.5]]
p=0
col=[['blue','red'],['green','green'],['red','blue']]
sizes=[[100,10],[55,55],[10,100]]
fig = pyplot.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot([0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,0],[0,1,1,0,0,0,0,0,1,1,0,0,1,1,1,1],[0,0,1,1,0,0,1,1,1,1,1,0,0,1,0,0],c='black',zorder=10)
point1=ax.scatter(x[p][0],y[p][0],z[p][0],s=sizes[p][0],color=col[p][0],zorder=0)
point2=ax.scatter(x[p][1],y[p][1],z[p][1],s=sizes[p][1],color=col[p][1],zorder=0)
ax.set_xlabel('Width')
ax.set_ylabel('Depth')
ax.set_zlabel('Height')
slider_ax = plt.axes([0.15, 0.05, 0.7, 0.02])
slider = Slider(slider_ax, "min", 0, 2, valinit=1, color='blue')
slider.on_changed(on_change)
pyplot.show()

当然,这不是实现您的要求的最优雅、最正确的方法,但在您找到答案之前,您可以在每次调用函数时重新初始化axes3dsublot。在这种情况下,它如下所示

def on_change(val):
   time=int(val)/1   
   global ax
   ax = fig.add_subplot(111, projection='3d')
   ax.plot([0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,0],[0,1,1,0,0,0,0,0,1,1,0,0,1,1,1,1],[0,0,1,1,0,0,1,1,1,1,1,0,0,1,0,0],c='black',zorder=10)

   p = 0
   ax.scatter(x[p][0],y[p][0],z[p][0],s=sizes[time][0],color=col[time][0],zorder=0)
   ax.scatter(x[p][1],y[p][1],z[p][1],s=sizes[time][1],color=col[time][1],zorder=0)

   fig.canvas.draw()

只是猜测:
point1
point2
可能在您的
on\u change
功能范围内未定义这有帮助,但当程序扩展到更多点时,运行速度会非常慢。是否有类似.remove()方法的功能?[已尝试。删除(),但未发生任何问题。]