0

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

1 Answer 1

0

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.

import matplotlib.pyplot as plt
from matplotlib.widgets import Button, Slider
import numpy as np

# - setup default grid values
nx = 20
ny = 10
dx = 2

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)
ax.axis([0, nx + 1, 0, np.max(Y)])

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, 100, valinit=nx, valstep=dx)
NY = Slider(axny, 'ny', 10, 100, valinit=ny, valstep=dx)

l = ax.plot(X, Y, marker='o', markerfacecolor='None', markeredgecolor='k', linestyle="None")[0]


def update(val):
    nx1 = NX.val
    ny1 = NY.val
    X, Y = np.meshgrid(np.linspace(0, nx1, nx1), np.linspace(0, ny1 * dx, ny1))
    ax.clear()
    ax.axis([0, nx1, 0, np.max(Y)])
    fig.canvas.draw()
    fig.canvas.flush_events()
    ax.plot(X, Y, marker='o', markerfacecolor='None', markeredgecolor='k', linestyle="None")


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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.