1

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])
M.plotGrid()

#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
    M.plotGrid()

xnode_inc.on_changed(update)
ynode_inc.on_changed(update)

plt.show()
4

1 回答 1

1

我只能给出一个交互式更改numpy网格的示例,因为我不使用SimPEG. 该示例位于 Ipython/Jupyter 笔记本中。这是迄今为止生成交互式滑块、下拉菜单、单选按钮等的最简单方法,即比使用原始 matplotlib 滑块进行类似设置的复杂设置有了很大改进。为了简单起见,我为颜色图添加了一个组合框。

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


def gridandplot(dx,mp):
    x=np.arange(0,5,dx)
    y=np.arange(0,5,dx)
    x,y=np.meshgrid(x,y)
    z= np.sin(x)* np.cos(y)
    if mp=='gray':
        cmap=p.cm.gray
    else:
        cmap= p.cm.jet
    p.imshow(z,interpolation='none',cmap=cmap)

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

在此处输入图像描述

在此处输入图像描述

于 2016-04-24T21:06:04.757 回答