Python Matplotlib 滑块已绘制但不移动或更新

2022-01-24 00:00:00 python matplotlib slider

问题描述

我正在尝试绘制一个散点图,其中一个点根据滑块调整的参数移动.我需要参数是坐标列表中的位置.我有它,所以绘制了散点图,我可以通过更改位置手动移动点,但是当我尝试实现滑块时,它会显示出来,但不能用于更新绘图.任何帮助都会很棒.到目前为止我所拥有的如下.谢谢.

I am trying to draw a scatter plot with a point that moves based on a parameter adjusted by a slider. I need the parameter to be the position in a list of the coordinates. I have it so the scatter plot gets drawn and I can manually move the point by change the position, but when I try to implement the slider it is displayed, but can not be drug to update the plot. Any help would be great. What I have so far is below. Thanks.

%pylab
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import animation

def on_change(val):
   p=int(val)/1

def chart():
    x=[0,0.5,1]
    y=[0,0.5,1]
    z=[0,0.5,1]
    p=0
    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)
    ax.scatter(x[p],y[p],z[p],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')
    pyplot.show()

chart()


解决方案

您必须保留对 Slider 对象的引用,否则它(以及它的回调)会被垃圾收集.请参阅 https://github.com/matplotlib/matplotlib/issues/3105 上的长时间讨论.

You have to keep a reference to the Slider object around or it (and it's call backs) get garbage collected. See long discussion at https://github.com/matplotlib/matplotlib/issues/3105.

关于这方面的文档已在 1.4 中得到澄清.

The documentation on this has been clarified for 1.4.

相关文章