TransWikia.com

Slider with ticks

Mathematica Asked by Artem on October 19, 2020

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:

Text

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".

One Answer

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] &}]

enter image description here

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] &}]

enter image description here

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] &}]

enter image description here

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[##] &}]

enter image description here

Correct answer by kglr on October 19, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP