Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Why does this work (displays the curve):

Graphics[Plot[Sin[x], {x, 0, Pi}]]

and this doesn't (no curve)

Graphics[Plot[Sin[a x], {x, 0, Pi}], {Slider[a, 0, 4]}]

?

I would like to work without Manipulate.

share|improve this question
add comment

2 Answers

You can make the slider to appear as part of the plot:

Overlay[{
  Dynamic@Plot[Sin[a x], {x, 0, Pi}, PlotRange -> {{0, Pi}, {-1, 1}},Frame -> True],
  Panel@VerticalSlider[Dynamic[a], {1, 9}, ImageSize -> {15, 60}]
  }, All, 2, Alignment -> {.9, .8}]

enter image description here

And even develop something more elaborate like this:

Overlay[{
  Dynamic@Plot[Sin[a x], {x, 0, Pi}, PlotRange -> {{0, Pi}, {-1, 1}}, 
    Frame -> True, PlotStyle -> Thick],
  Framed[Row[{Graphics[{}, PlotRange -> {{0, 1}, {1, 9}}, 
      Frame -> {{False, True}, {False, False}}, 
      FrameTicks -> {None, Range[9]}, ImageSize -> 20],
     VerticalSlider[Dynamic[a], {1, 9}, ImageSize -> {20, 95}, 
      Appearance -> "LeftArrow"]
     }], FrameMargins -> 0, 
   Background -> Directive[Black, Opacity[.2]], RoundingRadius -> 6]
  }, All, 2, Alignment -> {.9, .8}]

enter image description here

share|improve this answer
add comment

It doesn't work for a few reasons:

  • The syntax for Slider is not correct. It should be Slider[a, {0, 4}]
  • The variable a needs to by Dynamic so that it is updated when you move the slider.
  • Slider is not a graphics primitive.

Additionally, you don't need the Graphics and it is a good idea to localize the dynamic variable using DynamicModule. I would suggest something like:

DynamicModule[{a}, 
    Column[{
        Dynamic@Plot[Sin[a x], {x, 0, Pi}, PlotRange -> {All, {-1, 1}}], 
        Slider[Dynamic@a, {0, 4}]
    }]
]

enter image description here

You need to set PlotRange so that the figure doesn't jump around when you move the slider.

share|improve this answer
    
Thanks for the correction notes to Edit. –  Alel Jul 12 '12 at 18:51
    
And thanks for the suggested solution. The reason I THOUGHT I need graphics is that I need to find the mouse position using" MousePosition" in the graphics coordinates. But I might be worng. Not sure how to do it. –  Alel Jul 12 '12 at 18:53
2  
You probably also want to include a PlotRange option in Plot, because otherwise the x-axis jumps around as you move the slider (and the y-axis minimum and maximum change as well). –  murray Jul 12 '12 at 20:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.