Reputation: 128
Using several sliders that change some propoerties in a simulation, I'd like tu create an ipywidgets.Button that sets all sliders values to some default values, is anyone know how to do so?
from ipywidgets import FloatSlider, IntSlider, IntText, Button
A = FloatSlider(value=4, min=-10, max=20, step=0.1)
B = IntSlider(value=2, min=0, max=8, step=2)
C = IntText()
default_value_button = Button(description='click to set default values')
...
#I want this button to set specific values for A,B,C
#I need its action to be:
A.set_state('value') = 3.7
B.set_state('value') = 4
C.set_state('value') = 547
...
display(A,B,C,default_value_button)
I need to set default values when I click on the button:
thank you in advance for your attention!
Upvotes: 1
Views: 1581
Reputation: 128
import numpy as np
from ipywidgets import *
from matplotlib import cm
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import axes3d
def hist_3D(xdata, ydata, xtitle='X',ytitle='Y'):
SQUARE_BIN_NUMBER = IntSlider(description="bins" ,min=5, max=40, value=20)
AZIM = IntSlider(description="azimuth", min=-180, max=180, value=-60, layout=widgets.Layout(width='600px'))
ELEV = IntSlider(description="elevation", min=-90, max=90, value=30, layout=widgets.Layout(height='400px'), orientation='vertical')
VU = Dropdown (options=[('X histogram', 1), ('Y histogram', 2), ('XY colored', 3), ('Random view', 4)],
value=4, description='options :')
SHADE = Checkbox (description="shade", value=True)
DEFAULT_VALUE_BUTTON = Button(description='default values')
SQUARE_BIN_NUMBER.default_value = 20
AZIM.default_value = -60
ELEV.default_value = 30
VU.default_value = 4
SHADE.default_value = True
defaulting_widgets = [SQUARE_BIN_NUMBER,AZIM,ELEV,VU,SHADE]
def run(square_bin_number, azim, elev, vu=None, shade=True):
if(vu == 1): azim, elev, shade = -90, 0, True
elif(vu == 2): azim, elev, shade = 0, 0, False
elif(vu == 3): azim, elev, shade = -90, 90, True
x = np.array(xdata) #turn x,y data into numpy arrays
y = np.array(ydata)
fig = plt.figure(figsize=(10,7)) #create a canvas, tell matplotlib it's 3d
ax = fig.add_subplot(111, projection='3d')
#make histogram stuff - set bins - I choose 20x20 because I have a lot of data
hist, xedges, yedges = np.histogram2d(x, y, bins=(square_bin_number, square_bin_number))
xpos, ypos = np.meshgrid(xedges[:-1]+xedges[1:], yedges[:-1]+yedges[1:], indexing='ij')
#attenetion : meshgrid(indexing must be 'ij' and it's 'xy' by default)
xpos = xpos.flatten()/2.
ypos = ypos.flatten()/2.
zpos = np.zeros_like (xpos)
dx = xedges [1] - xedges [0]
dy = yedges [1] - yedges [0]
dz = hist.flatten()
cmap = cm.get_cmap('jet') # Get desired colormap - you can change this!
max_height = np.max(dz) # get range of colorbars so we can normalize
min_height = np.min(dz)
# scale each z to [0,1], and get their rgb values
rgba = [cmap((k-min_height)/max_height) for k in dz]
ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=rgba, zsort='average', shade=shade)
ax.view_init(azim=azim, elev=elev)
plt.xlabel(xtitle)
plt.ylabel(ytitle)
# plt.savefig("Your_title_goes_here")
plt.show()
def set_default(button):
for widget in defaulting_widgets:
widget.value = widget.default_value
DEFAULT_VALUE_BUTTON.on_click(set_default)
out = interactive_output(run, {'vu':VU, 'square_bin_number':SQUARE_BIN_NUMBER,
'azim':AZIM, 'elev':ELEV, 'shade':SHADE})
display(VU, HBox([out, ELEV]), VBox([AZIM, HBox([SQUARE_BIN_NUMBER, SHADE])]),
DEFAULT_VALUE_BUTTON)
A = np.random.multivariate_normal([0,0], np.identity(2), size=10000)
x = A[:,0]
y = A[:,1]
hist_3D(x,y)
Upvotes: 0
Reputation: 5565
This is a simple implementation, just set a default_value
attribute for the widget, and compile these widgets into a list as you create them.
One issue with this is, if you have these widgets hooked up to an interactive function, then setting the value to default will also cause the interactive function to trigger. This might be what you want or might not be!
from ipywidgets import *
A = FloatSlider(value=4, min=-10, max=20, step=0.1)
B = IntSlider(value=2, min=0, max=8, step=2)
C = IntText()
A.default_value = 3.7
B.default_value = 4
C.default_value = 547
defaulting_widgets = [A,B,C]
default_value_button = Button(description='click to set default values')
def set_default(button):
for widget in defaulting_widgets:
widget.value = widget.default_value
default_value_button.on_click(set_default)
display(A,B,C,default_value_button)
Upvotes: 1