如何在绘制新的点之前从matplotlib图中删除点? [英] How can I remove points from a matplotlib plot before plotting a new one?

查看:122
本文介绍了如何在绘制新的点之前从matplotlib图中删除点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一个位置由滑块控制的点.但是,每次移动滑块时都会绘制一个新点,而不会删除旧点.如何在绘制新点之前删除旧点?

I am trying to plot a point whose position is controlled by a slider. However, each time the slider is moved a new point is plotted without deleting the old one. How do you remove the old point before the new one is plotted?

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):
    point=ax.scatter(x[int(val)/1],y[int(val)/1],z[int(val)/1])

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)
point=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')
slider.on_changed(on_change)
pyplot.show()

推荐答案

你可以改变point的属性:

def on_change(val):
    point.set_offsets([x[int(val) / 1], y[int(val) / 1]])
    point.set_3d_properties([z[int(val) / 1]], "z")
    fig.canvas.draw()

这篇关于如何在绘制新的点之前从matplotlib图中删除点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆