【问题标题】:Changing the size and color of points in a scatter plot using a slider in python使用python中的滑块更改散点图中点的大小和颜色
【发布时间】: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()

【问题讨论】:

  • 只是猜测:point1point2 可能在您的 on_change 函数范围内未定义

标签: python matplotlib colors slider scatter-plot


【解决方案1】:

当然不是完成您所要求的最优雅和正确的方法,但在您弄清楚之前,您可以在每次调用您的函数时重新初始化您的 Axes3DSubplot。在这种情况下,如下所示。

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()

【讨论】:

  • 这有帮助,但是当程序放大到更多点时运行速度会很慢。有没有像 .remove() 方法一样工作的东西? [尝试 .remove() 并没有发生任何事情。]
猜你喜欢
  • 1970-01-01
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
相关资源
最近更新 更多