【发布时间】:2014-09-17 21:15:45
【问题描述】:
我正在尝试使用滑块来更改 3d 散点图中几个点的大小和颜色。我很困惑为什么颜色不会更新,而且我找不到更新尺寸的命令。
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()
【问题讨论】:
-
只是猜测:
point1和point2可能在您的on_change函数范围内未定义
标签: python matplotlib colors slider scatter-plot