Hi,
I’m stuck doing a plot and I am not even sure whether Panel is able to achieve what I am looking for.
My idea is the following: I would like to create a class with param.Parameterized that has the following elements:
- Some sliders
- An action button
- A scatter plot
First, I would like to use the sliders to set some parameters, like the number of dots, or the proportion of dots of one class or another. After setting the sliders a plot should appear on the Panel.
Second, the action perform an action like the following: for each dot in the figure, change its class. Thus, the action should, for each dot in the scatter, change its color and update the figure.
So far I have created this skeleton:
def plotclasschange():
class myClass(param.Parameterized):
slider_N = param.Integer(200, bounds=(10, 1000))
slider_prop = param.Number(1, bounds=(0, 1))
button = param.Action()
@panel.depends('slider_N', 'slider_prop', watch=True)
def create_scatter(self):
color_palette = ['#f09711', '#0eb9ed']
N = self.N_slider
points = np.random.uniform(0, 1, (N, 2))
idx = np.random.choice(np.arange(0, N), int(N * self.slider_prop))
cats = np.zeros(N)
cats[idx] = 1
cats = cats.astype(bool)
fig = figure(plot_height=400, plot_width=400,
tools='', sizing_mode='scale_width', output_backend="webgl")
df = pd.DataFrame({'x': points[:, 0], 'y': points[:, 1], 'c': [color_palette[i] for i in cats]})
fig.scatter(x='x', y='y', source=df, color='c')
return fig
@panel.depends('button', watch=True)
def update_fig(self):
for i in range(len(df)):
df['c'].iloc[i] = '#f09711' if df['c'].iloc[i] == '#0eb9ed' else '#0eb9ed'
????
sss = myClass(title='')
pr = panel.Row(panel.Param(sss.param, widgets={
'slider_N': {'size': 19, 'name': 'N'},
'slider_prop': {'size': 10, 'name': 'Proportion'},
'button': {'name': 'Go'},
}), sss.create_scatter)
return pr
At this point I am stuck. I know how I should update the figure, but I don’t know how to do it when the button is pressed, and how to update it after each iteration. Of course, if any of the sliders change, I would like to update the plot to zero, and then press the button again to do the change.
Thanks for the help!