I have a code to plot points and to update the number of points in X,Y with a slider.
There is no way I can make the old plots disappear in the update function, tried all possible variations of .clear() and so on...
Here is my code; comments are some of the things I tried...
import matplotlib.pyplot as plt
from matplotlib.widgets import Button,Slider
import numpy as np
#- setup default grid values
nx = 100
ny = 50
dx = 10
X,Y = np.meshgrid(np.linspace(0,nx*dx,nx+1),np.linspace(0,ny*dx,ny+1))
fig, ax = plt.subplots()
plt.subplots_adjust(left=0.25, bottom=0.25)
axnx = plt.axes([0.25, 0.1, 0.65, 0.03])
axny = plt.axes([0.25, 0.15, 0.65, 0.03])
NX = Slider(axnx, 'nx', 10, 1000, valinit=nx, valstep=dx)
NY = Slider(axny, 'ny', 10, 1000, valinit=ny, valstep=dx)
l = ax.plot(X,Y,marker='o',markerfacecolor='None',markeredgecolor='k',linestyle="None")[0]
def update(val):
#ax.clear()
nx1 = NX.val
ny1 = NY.val
X,Y = np.meshgrid(np.linspace(0,nx1*dx,nx+1),np.linspace(0,ny1*dx,dx+1))
ax.plot(X,Y,marker='o',markerfacecolor='None',markeredgecolor='k',linestyle="None")
#l.set_xdata(X)
#l.set_ydata(Y)
fig.canvas.draw()
fig.canvas.flush_events()
NX.on_changed(update)
NY.on_changed(update)
resetax = plt.axes([0.8, 0.025, 0.1, 0.04])
button = Button(resetax, 'Reset', hovercolor='0.975')
def reset(event):
NX.reset()
NY.reset()
button.on_clicked(reset)
plt.show()
Changed your code somewhat. I made the number of dots smaller so that the effect was better visible. Do the cleaning on ax. Also added axis limits.