4

There is a well documented way to "Dynamically Hide and Show Plots" (link). In the example 'Opacity' option is effectively used. Is there an efficient way to make the same functionality with "Show"? I can only see a way using "if" statement, but it looks somewhat cumbersome as it wants dummy Graphic[] as a placeholder. For instance:

Manipulate[    
 a = Plot[x, {x, 0, 1}];    
 b = Plot[1 - z, {z, 0, 1}];    
 Show[    
  If[cond1 == 1, a, Graphics[]],
  If[cond2 == 1, b, Graphics[]]
 ],
 Grid[
  {
   {Control[{{cond1, 1, ""}, {0, 1}}], "a"},
   {Control[{{cond2, 1, ""}, {0, 1}}], "b"}
  }
 ]    
]        

Another drawback is that unchecking 'a' causes Plot axes to disappear.
Any ideas?

garej
  • 4,865
  • 2
  • 19
  • 42
  • 1
    Re the "drawback": use the option Axes -> True in Show, or use Graphics[{}, Axes -> True] instead of Graphics[] inside If. – kglr May 24 '15 at 16:14
  • @kguler, thank you; is there a way to have Axes 'stable' in this case? Plot 'form' changes when 'a' is unchecked (though Axes are in place =))). – garej May 24 '15 at 17:05
  • you can also add PlotRange-> ... to Show or to the empty Graphics. – kglr May 24 '15 at 17:12
  • unfortunately, it does not work for me (AspectRatio does not do the job either) – garej May 24 '15 at 17:20
  • 1
    garej, Show[If[cond1 == 1, a, Graphics[]], If[cond2 == 1, b, Graphics[]], Axes -> True, PlotRange -> {{0, 1}, {0, 1}}, AspectRatio -> 1/GoldenRatio], works as expected in version 9.0.1.0 on Windows 8 (64bit). – kglr May 24 '15 at 17:25
  • 2
    Related: http://mathematica.stackexchange.com/a/86230/18476 and http://mathematica.stackexchange.com/a/86143/18476 – Karsten7 Dec 06 '15 at 17:01
  • @Karsten7, I've admitted that, thank you. – garej Dec 06 '15 at 17:05

1 Answers1

4
Manipulate[a = Plot[x, {x, 0, 1}]; b = Plot[1 - z, {z, 0, 1}];
 Show[If[cond1 == 1, a, Graphics[]], If[cond2 == 1, b, Graphics[]], 
  Axes -> (cond1+cond2>=1), PlotRange -> {{0, 1}, {0, 1}}, AspectRatio -> 1/GoldenRatio], 
 Grid[{{Control[{{cond1, 1, ""}, {0, 1}}], "a"}, 
       {Control[{{cond2, 1, ""}, {0, 1}}], "b"}}]]

enter image description here

Change Axes -> (cond1 + cond2 >= 1) to Axes->True if you want to have the axes always visible.

You can get the same result by changing the PlotStyle of plots a and b based on the cond1 and cond2, respectivly.

Manipulate[ a = Plot[x, {x, 0, 1}, PlotStyle -> If[cond1 == 0, Opacity[0], Opacity[1]]];
 b = Plot[1 - z, {z, 0, 1}, PlotStyle -> If[cond2 == 0, Opacity[0], Opacity[1]]];
 Show[a, b, Axes -> (cond1+cond2>=1), PlotRange -> {{0, 1}, {0, 1}}, 
     AspectRatio -> 1/GoldenRatio], 
 Grid[{{Control[{{cond1, 1, ""}, {0, 1}}], "a"},
       {Control[{{cond2, 1, ""}, {0, 1}}], "b"}}]]

Alternatively, use a single plot with two curves:

Manipulate[Plot[{x, 1 - x}, {x, 0, 1}, 
   PlotStyle -> {If[cond1 == 0, Opacity[0], Opacity[1]], 
                 If[cond2 == 0, Opacity[0], Opacity[1]]},
   Axes -> (cond1+cond2>=1), PlotRange -> {{0, 1}, {0, 1}}, AspectRatio -> 1/GoldenRatio], 
 Grid[{{Control[{{cond1, 1, ""}, {0, 1}}], "a"}, 
       {Control[{{cond2, 1, ""}, {0, 1}}], "b"}}]]

Mathematica graphics

If you have to use Show:

Manipulate[ab = Plot[{x, 1 - x}, {x, 0, 1}, 
   PlotStyle -> {If[cond1 == 0, Opacity[0], Opacity[1]], 
     If[cond2 == 0, Opacity[0], Opacity[1]]}];
 Show[ab, Axes -> (cond1 + cond2 >= 1), PlotRange -> {{0, 1}, {0, 1}}, 
      AspectRatio -> 1/GoldenRatio], 
 Grid[{{Control[{{cond1, 1, ""}, {0, 1}}], "a"},
       {Control[{{cond2, 1, ""}, {0, 1}}], "b"}}]]
kglr
  • 394,356
  • 18
  • 477
  • 896