user1332577 user1332577 - 5 months ago 11x
Python Question

Update values in evenly spaced mesh with slider

The problem I'm facing is that I'm unable to figure out how to update the figure when I increase/decrease values with my slider.

I am creating an x, y mesh grid, then plotting it, and updating the values within my array to define a new mesh. I would like to be able to move the slider and see a finer/coarser grid and update the plot and axes.

Additionally, I would like the user to be able to move the grid lines interactively with the mouse. This is not included in example code. Does someone know how to implement this?

There are similar questions that have been asked about updating values on a plot with a slider, but perhaps none address this specific problem; can someone help me figure out what I'm doing wrong? I realize I don't have anything that is redrawing the plot (though I have tested various things), but am unsure how to implement this, as M.plotGrid() creates the grid, so it may/may not be as easy as using fig.canvas.draw_idle().

Here is my code:

#Create simple evenly spaced mesh and allow slider to change values

from SimPEG import Mesh, np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, Button, RadioButtons

hx = np.r_[0.1,0.1,0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]
hy = np.r_[0.1,0.1,0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]

M = Mesh.TensorMesh([hx, hy])

#Define slider location, size, and color for adjusting mesh values
axhx = plt.axes([0.25, 0.1, 0.65, 0.03])
axhy = plt.axes([0.25, 0.15, 0.65, 0.03])
xnode_inc = Slider(axhx, 'X mesh Node', 0.1, 1000.0, valinit=0)
anode_inc = Slider(axhy, 'Y mesh Node', 0.1, 1000.0, valinit=0)

def update(val):
framehx = xnode_inc.val + hx
framehy = ynode_inc.val + hy
M = Mesh.TensorMesh([framehx, framehy])
print M, framehx, framehy



I can only give an example of interactively changing a numpy grid as I don't use SimPEG. The example is in an Ipython/Jupyter notebook. This is by far the simplest way today to generate interactive sliders, pulldowns, radiobuttons etc, i.e. much improved over the complex setup of similar setup with the original matplotlib sliders. I threw in a combo-box for the colormap just to simplicity of it.

import numpy as np
import matplotlib.pyplot as p
from ipywidgets import *
%matplotlib inline

def gridandplot(dx,mp):
    z= np.sin(x)* np.cos(y)
    if mp=='gray':

interact(gridandplot, dx=(0.005,0.2,0.001), mp= ('gray','jet'))

enter image description here

enter image description here