[英]Clearing the Matplotlib plot without clearing slider
I'm currently creating an application where I need to update matplotlib graphs.我目前正在创建一个需要更新 matplotlib 图的应用程序。 I'm stuck at following step.我坚持以下步骤。
I have a scatter plot and slider in the same figure.我在同一张图中有一个散点 plot 和 slider 。 Upon changing the slider value, I need to clear the scatter plot without clearing the slider.在更改 slider 值后,我需要在不清除 slider 的情况下清除散点图 plot。 Following is the code I implemented, but it does not clear the plot.以下是我实现的代码,但它没有清除 plot。 The .clf()
function removes both the slider and scatter plot. .clf()
function 删除 slider 和分散 plot。 Is there a way I could remove only the plot without impacting the slider?有没有办法只删除 plot 而不会影响 slider?
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
X1=np.arange(10)
Y1=np.arange(10)
Z1=np.arange(10)
fig,ax=plt.subplots()
ax=plt.scatter(X1,Y1,Z1)
axSlider1=plt.axes([0.3,0.9,0.3,0.05])
Slder2=Slider(ax=axSlider1,label='Slider',valmin=1,valmax=4,valinit=2)
plt.show()
# Function to clear the scatter plot without effecting slider.
def val_update(val):
plt.cla()
Slder2.on_changed(val_update)
fig,ax=plt.subplots()
Assigned the Axes object to ax
- then将轴 object 分配给ax
- 然后
ax=plt.scatter(X1,Y1,Z1)
assigns a PathCollection object to ax
.将 PathCollection object 分配给ax
。 So val_update
is tying to act on the wrong thing.所以val_update
对错误的事情采取行动。
This seems to solve the issue you asked about.这似乎解决了您询问的问题。
X1=np.arange(10)
Y1=np.arange(10)
Z1=np.arange(10)
fig,ax=plt.subplots()
path=plt.scatter(X1,Y1,Z1)
axSlider1=plt.axes([0.3,0.9,0.3,0.05])
Slder2=Slider(ax=axSlider1,label='Slider',valmin=1,valmax=4,valinit=2)
# Function to clear the scatter plot without effecting slider.
def val_update(val):
print(val)
ax.clear()
Slder2.on_changed(val_update)
plt.show()
The Matplotlib Gallery is a good resource you can browse through it looking for features you want and see how they were made. Matplotlib 图库是一个很好的资源,您可以浏览它以查找您想要的功能并查看它们是如何制作的。 - Like this slider demo - 就像这个slider 演示
If you think you will be using matplotlib in the future I would recommend working your way through the Introductory tutorials and at least the Artists tutorial如果您认为将来会使用 matplotlib,我建议您按照入门教程和至少艺术家教程的方式进行操作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.