Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a dynamic plot. I want to set an initial image size and then be able to resize it by dragging with the mouse. When the image is updated, I want to maintain the "dragged" size.

A naive attempt with a simplified example:

Slider[Dynamic[n], {0, 5}]
Dynamic[Plot[n (x - n)^2, {x, 0, 5}, ImageSize -> Large]]

It sets ImageSize->Large. I can resize the plot by select and dragging, but as soon as I change n with the slider, the plot is evaluated and the size reverts to Large.

So how can I apply setting ImageSize->Large only as an initial condition and keep the "dragged" size as n changes?

Thanks in advance for your expert help!

share|improve this question
up vote 2 down vote accepted

Edit: In order to set initial values in a dynamical system one has to use DynamicModule.

Anyway, if you now change slightly your code in the following way:

Slider[Dynamic[n], {0, 5}]
DynamicModule[{p1 = 200}, 
Dynamic[Plot[n (x - n)^2, {x, 0, 5}, ImageSize -> Dynamic[p1]]]]

the image should remember its size.

share|improve this answer
So it seems you can drop the ImageSizeCache rule as far as I can tell, since Mathematica will put it in automatically. I played a little and found that Mathematica automatically updates p1 as I resize the plot window with the mouse. I never would have thought of that! – Michael Jul 13 '14 at 17:53
@Michael Yup, this makes the code much nicer. – Gregory Rut Jul 13 '14 at 17:58

Is this what you are looking for?

 Plot[n (x - n)^2, {x, 0, 5}, ImageSize -> $size], {n, 0, 
      1}, {{$size, 550}, 100, 1000}]

On my system, Large ~ 550, so I chose that as the starting point.

share|improve this answer
Thanks. I'm not looking for a second slider. I want to be able to use select and drag with the mouse to resize. – Michael Jul 13 '14 at 16:03
I'm still unclear. Ignoring my $size slider for a moment, you can click & drag the plot and changing the value of n won't reset the size. Is that what you want? – bobthechemist Jul 13 '14 at 17:09
Yes! Problem now solved. – Michael Jul 13 '14 at 18:00

How is this?

Slider[Dynamic[n, (imgs = Automatic; n = #) &], {0, 5}]

enter image description here

imgs = 400;
Dynamic[Plot[n (x - n)^2, {x, 0, 5},
  ImageSize -> imgs]]

enter image description here

share|improve this answer
It doesn't work for me. Changing n reverts the plot to some small size. – Michael Jul 13 '14 at 18:08
You can change some code running order. I have changed like over that...don't you want to do that... – Junho Lee Jul 13 '14 at 18:13

Your Answer


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.