如何通过单击小部件按钮将所有小部件值设置为默认值?
how to set all widgets values to default by clicking on a widget button?
使用几个滑块在模拟中改变一些属性,我想创建一个 ipywidgets.Button 将所有滑块值设置为一些默认值,有人知道怎么做吗?
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)
我需要在点击按钮时设置默认值:
here's how it must look like
提前感谢您的关注!
这是一个简单的实现,只需为小部件设置一个default_value
属性,并在创建它们时将这些小部件编译成列表。
一个问题是,如果您将这些小部件连接到交互功能,那么将值设置为默认值也会导致触发交互功能。这可能是您想要的,也可能不是!
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)
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)
使用几个滑块在模拟中改变一些属性,我想创建一个 ipywidgets.Button 将所有滑块值设置为一些默认值,有人知道怎么做吗?
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)
我需要在点击按钮时设置默认值:
here's how it must look like
提前感谢您的关注!
这是一个简单的实现,只需为小部件设置一个default_value
属性,并在创建它们时将这些小部件编译成列表。
一个问题是,如果您将这些小部件连接到交互功能,那么将值设置为默认值也会导致触发交互功能。这可能是您想要的,也可能不是!
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)
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)