# Slider with ticks

I am not happy with the "Labeled" appearance of the slider in Animate or Manipulate. Instead, I wish ticks with multiple of pi under the slider.

Animate[Plot[Sin[x + phi], {x, 0, 2 Pi}], {phi, 0, 2 Pi,
Appearance -> "Labeled"}]


What would be the best way to do it? Ideally it should look like this:

I do not think the solution given in https://mathematica.stackexchange.com/a/8264 is practical or even elegant. Following this logic of adding an additional axis I cannot make it look the way I want and, in general, it seems highly "artificial".

• You could add an increment Pi/2 inside slider: Animate[Plot[Sin[x + phi], {x, 0, 2 Pi}], {phi, 0, 2 Pi, Pi/2, Appearance -> "Labeled" to get only discrete values for phi. Aug 21 '20 at 10:20
• perhaps you can use HorizontalGauge?
– kglr
Aug 21 '20 at 10:41

1. You can use HorizontalGauge as a control in Manipulate:

Manipulate[Plot[Sin[x + phi], {x, 0, 2 Pi}],
{phi, 0, 2 Pi, Panel[HorizontalGauge[##, ScaleDivisions -> None,
Axes -> {True, False}, ImageSize -> 250,
Ticks -> {Transpose[{Subdivide[8], Subdivide[0, 2 Pi, 8]}], None}], #, Right] &}]


To allow only discrete values, say multiples of Pi/4, for the control variable phi:

Manipulate[Plot[Sin[x + phi], {x, 0, 2 Pi}],
{phi, 0, 2 Pi, Panel[HorizontalGauge[Dynamic[phi, (phi = Round[#, Pi/4]) &], ##2,
ScaleDivisions -> None, ImagePadding -> 15,
PlotRange -> {{0, 1}, Automatic},
Axes -> {True, False}, ImageSize -> 250,
Ticks -> {Thread[{Subdivide[8], Subdivide[## & @@ #2, 8], {0, .05}},
List, 2], None}], #, Right] &}]


2. Alternatively, you can use a poor-man's custom control that combines a slider and an empty graphics with horizontal axis:

Manipulate[Plot[Sin[x + phi], {x, 0, 2 Pi}],
{phi, 0, 2 Pi, Labeled[Panel @ Column[{Slider[##, ImageSize -> 250],
Graphics[{}, Axes -> {True, False},
Ticks -> {Subdivide[## & @@ #2, 8], None}, ImageSize -> 250,
PlotRange -> {#2, {0, .05}}]}, Alignment -> Center,
Spacings -> 0], #, Right] &}]


3. We can also modify the function ValueThumbSlider from tutorial/AdvancedManipulateFunctionality >> CustomControl Appearances to get a custom slider with axis:

ClearAll[sliderWithAxis]
sliderWithAxis[Dynamic[var_], {min_, max_, d_}, o : OptionsPattern[]] :=
LocatorPane[Dynamic[If[! NumberQ[var], var = min];
{var, 0}, (var = First[#]) &],
Panel[Graphics[{Dynamic[
Polygon[Offset[#, {var, 0}] & /@ {{0, 5}, {-5, 15}, {5, 15}}]]},
o, ImageSize -> 300, ImagePadding -> {{15, 15}, {30, 5}},
Axes -> {True, False},
Ticks -> {{#, #, {0, .025}} & /@ Range[min, max, d], None},
PlotRange -> {{min, max}, {-1, 1}}, AspectRatio -> 1/10],
Dynamic[Style[Round[var, d], 16]], Right, Background -> LightBlue,
Alignment -> Center],
{{min, 0}, {max, 0}, {d, 0}}, Appearance -> None]

Manipulate[ Plot[{Sin[x + phi], Cos[x + psi]}, {x, 0, 2 Pi}],
{phi, 0, 2 Pi, Pi/4, sliderWithAxis[##] &},
{psi, 0, 2 Pi, Pi/2, sliderWithAxis[##] &}]


• Unfortunately, the same tricks do not work with Animate.
– kglr
Aug 21 '20 at 11:39
• Thanks, I like the HorizontalGauge solution a lot. Just one thing yet: how do I get rid of the overhang of the axis line on the left and right of the interval? I tried it with PlotRange -> {{0, 1}, {0, 0.05}}, but the large marker gets cut on the left. So I would have to choose a thin marker. Is there a better way to do it? Aug 21 '20 at 13:31
• @kglr very nice answer. How could I change your code to allow only discrete values of phi={0,Pi/4,Pi/2,...}? THanks! Aug 21 '20 at 15:31
• @Artem, you can add the option ImagePadding -> 15.
– kglr
Aug 21 '20 at 18:27
• @UlrichNeumann, perhaps we can use the second argument of Dynamic as in HorizontalGauge[Dynamic[phi, (phi = Round[#, Pi/4]) &], ##2, options]&
– kglr
Aug 21 '20 at 18:44