Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force Plot to use the specificed ImageSize in 3D when using the mouse to rotate an image

(Mathematica version: 8.0.4, on Windows 7)

Could someone please remind me how to tell M not to change the ImageSize in the following case:

I have a Manipulate, where I make a grid, and inside the grid, I either show one plot, or 2 plots, depending on a control choice.

To keep the overall displayed image the same, then if I am displaying one plot I use one size, and if I am displaying 2 plots, I use half the length for each plot. Easy enough so far.

The strange thing is that when I use the mouse to rotate the one plot case, and then switch back to 2 plots, the plot size now does not use the ImageSize I specified.

It seems by using the mouse to rotate one plot, it affected the next plot shown on the same screen location.

Using SphericalRegion -> True or not, has no effect. Using RotationAction -> "Fit" has no effect.

Here is a small example of what I mean, and then I show how I currently solve this problem. But I solve it by using GraphicsGrid in place of Grid. I wanted to keep using Grid if possible.

Manipulate[
 Module[{opt = {Spacings -> {0, 0}, Frame -> All}, p,
   size, data = Table[RandomReal[], {10}, {10}], wid = 300, len = 300},

  size = If[choice == 1, {wid, len}, {wid, len/2}];
  Print[size];
  p = ListPlot3D[data,SphericalRegion->True,ImagePadding -> 10,ImageSize ->size];

  If[choice == 1,
   Grid[{{p}}, Sequence@opt], Grid[{{p}, {p}}, Sequence@opt]
   ]
  ],

 Row[{SetterBar[Dynamic[choice], {1, 2}]}],
 {{choice, 2}, None}
 ]

To reproduce the problem, is simple: first I note the size, this is how I want to keep it. Now I click on choice 1, now using the mouse I rotate the one plot. Now I click on choice 2 to go back, then I see the plot size is not what I expected it to be.

enter image description here

I am sure it is an option I need to use. Just have not found it yet.

ps. Actually what seems to happen, is that the SAME plot that was rotated, stays on the content area, and was used in place of one of the 2 plots in the second case. Very strange. I must be doing something silly somewhere, as this is too strange.

Update 2:48 am This is in response to using Dynamic in the Manipulate expression as shown below by MrWizard. On V 8.04, it does not work. Here is the code:

Manipulate[
 Module[{p, size, data = Table[RandomReal[], {10}, {10}], wid = 300, 
   len = 300},

  size = If[choice == 1, {wid, len}, {wid, len/2}];
  p = ListPlot3D[data, SphericalRegion -> True, ImagePadding -> 10, 
    ImageSize -> size];

  If[choice == 1,
   Grid[{{p}}],
   Dynamic@Grid[{{p}, {p}}]
   ]
  ],

 Row[{SetterBar[Dynamic[choice], {1, 2}]}],
 {{choice, 2}, None}
 ]

enter image description here

Update 3:03 am

This below works by keeping the Grid. Adding a Frame around the grid makes it works. (Thanks to Mike answer showing that using Frame instead of Grid made it work, I figured let me try to add a Frame around the Grid)

One of the strangest things I've seen using Mathematica for long time :)

Manipulate[
 Module[{p, size, data = Table[RandomReal[], {10}, {10}], wid = 300, 
   len = 300},

  size = If[choice == 1, {wid, len}, {wid, len/2}];
  p = ListPlot3D[data, SphericalRegion -> True, ImagePadding -> 10, 
    ImageSize -> size];

  If[choice == 1,
   Framed@Grid[{{p}}],
   Grid[{{p}, {p}}]
   ]
  ],

 Row[{SetterBar[Dynamic[choice], {1, 2}]}],
 {{choice, 2}, None}
 ]

Thanks

like image 821
Nasser Avatar asked Dec 27 '11 07:12

Nasser


3 Answers

This is related to another puzzle re how//why Plot3D remembers image options why does Plot3D remember.... The solution happens to be the same in this case too: that is, add PreserveImageOptions -> False as an option to Plot3D. Somehow, the hacks like the ones suggested by MrW and Mike force Plot3D to "forget".

like image 108
kglr Avatar answered Nov 03 '22 03:11

kglr


I haven't got long but the use of Grid seems to be the main thing messing this up, though I haven't had time to identify how/why. If you replace the If statement with this:

If[choice == 1, Framed@p, Grid[{{p}, {p}}, Sequence@opt]]

then it works fine. There are some other things going on in the code that don't seem optimal at first glance but I have just focussed on the graphics sizing due to time constraints. This is not intended as an explanation but might help you or someone else figure out why this is behaving like this. Sorry but short on time but thought it was worth posting the observation about Grid.

like image 24
Mike Honeychurch Avatar answered Nov 03 '22 04:11

Mike Honeychurch


Without doing any actual analysis, here is my conjecture.

I believe this may the result of an optimization technique which observes that the apparent content of the displayed graphic did not change. I suppose that the key is therefore to make the apparent content different between each graphic that is displayed in each position of the Grid. Using something like Identity will not work as it vanishes from the expression. However if this conjecture is correct I expect any persistent change to result in an updated graphic.

I have had success using each of these for the first Grid expression:

Grid[{{ Framed@p }}, opt]

Grid[{{ Panel@p }}, opt]

Grid[{{ Pane@p }}, opt]

Grid[{{ {p} }}, opt]

Grid[{{ Item@p }}, opt]

Grid[{{ Style@p }}, opt]

like image 1
Mr.Wizard Avatar answered Nov 03 '22 05:11

Mr.Wizard