
I’m trying to visualize a 3D dataset displaying 2D slices of it and using a slider to change from one slice to another.
However although I don’t seem to get errors the slider has no effect on the plot. Below the code.
# Generate some sample data
x = np.linspace(-10, 10, 100)
y = np.linspace(-10, 10, 100)
z = np.linspace(-10, 10, 50)
xx, yy, zz = np.meshgrid(x, y, z, indexing='ij')
data = np.sin(xx) * np.cos(yy) * np.sin(zz)
# Create a ColumnDataSource
source = ColumnDataSource(data=dict(image=[data[:,:,0]]))
# Create a plot
plot = figure(x_range=(x.min(), x.max()), y_range=(y.min(), y.max()))
# Add the image glyph
image = plot.image(image='image', source=source, x=x.min(), y=y.min(), dw=(x.max()-x.min()), dh=(y.max()-y.min()))
# Create a slider to switch between different slices
slider = Slider(start=0, end=z.size-1, step=1, value=0)
slider.js_on_change('value', CustomJS(args=dict(source=source, slider=slider, data=data.tolist()), code="""
console.log("Slider value:", slider.value);
const image = [data[slider.value]]
console.log("image value:", image);
console.log("source.data value:", source.data);
source.data.image = image;
"""))
# Create a layout for the plot and slider
layout = row(plot, slider)
# Show the plot
# output_file("3d_to_2d_with_slider.html")
show(layout)

