4
$\begingroup$

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?

$\endgroup$
7
  • 1
    $\begingroup$ Re the "drawback": use the option Axes -> True in Show, or use Graphics[{}, Axes -> True] instead of Graphics[] inside If. $\endgroup$
    – kglr
    Commented May 24, 2015 at 16:14
  • $\begingroup$ @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 =))). $\endgroup$
    – garej
    Commented May 24, 2015 at 17:05
  • $\begingroup$ you can also add PlotRange-> ... to Show or to the empty Graphics. $\endgroup$
    – kglr
    Commented May 24, 2015 at 17:12
  • 1
    $\begingroup$ 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). $\endgroup$
    – kglr
    Commented May 24, 2015 at 17:25
  • 2
    $\begingroup$ Related: mathematica.stackexchange.com/a/86230/18476 and mathematica.stackexchange.com/a/86143/18476 $\endgroup$
    – Karsten7
    Commented Dec 6, 2015 at 17:01

1 Answer 1

4
$\begingroup$
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"}}]]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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