Sliders and plot together in one windows


I am preparing a lecture on Hypothesis testing for students and plan to go from the very basic, hopefully helping them understand the problems.

Last year, my notebook was in Jupyter with Interact package, which was a nightmare to set up. This year, I want to switch to Pluto. Currently, my biggest problem is that I cannot display sliders together with Plot. Specifically, I would like this

@bind μ Slider(-2:0.1:2; default=1, show_value=true)
@bind σ² Slider(0.001:0.1:2, default = 1,show_value = true)
@bind n Slider(2:1:1000, default = 1000, show_value = true)
p₁ = Normal(μ,σ²)
ts = [EqualVarianceTTest(rand(p₁,n), rand(p₁,n)).t for _ in 1:10000];
p = histogram(ts, normalized = true,alpha = 0.25)
plot!(p, μ - 5σ²:0.2σ²:μ + 5σ², x -> pdf(TDist(2n-2), x),label = "Student-T distribution")

to be rendered such that I will have sliders over the plot in one output.

I can do this my putting every slider to its own line and the plot afterwards, but if the notebook contains several of these blocks, I need to choose everytime a different name, which is a bit annoying.

Thanks for answers in advance.


1 Like


Awesome to hear that you are using Pluto for your classroom! This is exactly what I am designing Pluto for, so please let me know what else comes up, and if you like we can take a look at your notebooks together:

The way I see it, you mentioned two separate issues:

1. Combining sliders

You can combine the sliders into a single cell, have a look at the Interactivity sample notebook from the Pluto main menu to learn how. Unfortunately, you can not also place the plot inside that cell: the sliders need to live in a different cell than the plot.

The reason is that interactivity works through reactivity. In your example, moving the first slider sets the global variable μ. Pluto finds which cells depend on μ, and runs those as a response. If both the slider and the plot are in the same cell, then the cell responding to the change is also the cell that generates a new slider. This would reset the slider to its initial state.

2. You need to choose new variable names

This is true, and it can be a bit annoying. The only tip I can give is to not name your variables: x1, x2, x3, but something more descriptive: x_advection, x_diffusion, x_both.

A main difference between Interact and Pluto’s @bind is that Interact controls local variables (exist within one cell), while @bind controls global variables (exist in all cells). I believe that the latter can be more intuitive, because it reuses the same reactivity paradigm for interactivity. But it does mean that converting Interact-based code to Pluto can be tricky.


Hi Fons,

thanks for the answer. I understand your design decisions (I have seen your Juliacon talk) and I have expected that the problem with naming is not currently solvable, though it makes it a bit cumbersome. I have resolved the naming problem as you have said, although I have used the underscore notation because the variables were not that important.

Thanks for pointing me to the example of combining the sliders. It works very nicely. I was trying to use the same trick with plot the plot, and although the rendering was as I wanted, the interactivity was lost. I guess that this was caused by the fact that cell was not updated by the change. But I guess this is sufficient and cool.

Thanks a lot for help and of course for Pluto.

1 Like