3
$\begingroup$

I am creating many graphs using Plot, Plot3D, VectorPlot3D, etc... I like to toggle my curves, surface and vector fields on and off. I do this with the If function like so...

Manipulate[
 If[a, Plot[Sin[x], {x, -3, 3}], 
  Plot[, {x, -3, 3}]], {{a, True, "Toggle"}, {True, False}}]

This works fine, except that it requires me to include a blank Plot as shown when a is false. Is there a way to do this without having to include a blank plot? I just want the curve to appear and disappear but everything else to remain the same.

This becomes even more of an issue when I am working with options that I want to keep in both values of a. This can include changes to the axis, plot range, view, etc. The way I'm writing this, I have to retype everything twice for the second blank plot. Is there way to get around this? It seems like there must be an easier way to do this.

@Bob Hanlon - Your suggestion doesn't seem to work. See animated gif below.

enter image description here

$\endgroup$
  • 2
    $\begingroup$ Manipulate[If[a, Plot[Sin[x], {x, -3, 3}], ""], {{a, True, "Toggle"}, {True, False}}] $\endgroup$ – Bob Hanlon Dec 8 '15 at 6:17
  • $\begingroup$ @BobHanlon This doesn't seem to work. See animated gif in my edit above. I would like the axis to stay and the same view. I would like to just see the curve appear and disappear without any other changes. $\endgroup$ – Michael McCain Dec 8 '15 at 6:23
  • $\begingroup$ Ok. I think I figure it out using your code. $\endgroup$ – Michael McCain Dec 8 '15 at 7:15
  • 1
    $\begingroup$ A related thread. $\endgroup$ – J. M. will be back soon Dec 9 '15 at 5:04
5
$\begingroup$

Perhaps the option Opacity can be considered

Manipulate[Plot[Sin[x], {x, -3, 3}, 
     PlotStyle -> Opacity[a]], {{a, 1, "Toggle"}, {1, 0}}]
$\endgroup$
  • $\begingroup$ Yes! This works too. Thank you! $\endgroup$ – Michael McCain Dec 8 '15 at 7:17
  • $\begingroup$ @Kuba - I'm having a hard time understanding your comment. Can you clarify? $\endgroup$ – Michael McCain Dec 13 '15 at 1:45
  • $\begingroup$ @MichaelMcCain Sorry, I could bet that Dynamic PlotStyle was working for my case, can't reproduce it now. The point was, if it is possible to set Dynamic PlotStyle, then the whole Plot doesn't have to be regenerated but just styling directives are changed. $\endgroup$ – Kuba Dec 13 '15 at 8:49
  • $\begingroup$ I see. I thought I was missing something. I find that changing opacity inside manipulate dramatically slows down plotting. Your comment makes sense. I wish the code would work. $\endgroup$ – Michael McCain Dec 13 '15 at 8:53
3
$\begingroup$

Alternatively, you can take a look at FlipView, e.g. like that:

FlipView[{plot = Plot[Sin[x]^2, {x, -3, 3}], ReplacePart[plot, 1 -> {}]}]

Clicking anywhere on the plot will toggle between display of the first and the second expression.

enter image description here

You could further consider a small helper/wrapper function like:

   flipplot[plot_] := FlipView[{plot, ReplacePart[plot, 1 -> {}]}]

One drawback especially for Graphics3D object is that mouse activity is caught by FlipView, so you will not be able to interactively rotate/zoom etc. as usual.

$\endgroup$
1
$\begingroup$

Ok this works. Not sure why I didn't think of this before. I feel dumb. lol

Manipulate[
 Plot[If[a, Sin[x], ""], {x, -3, 3}], {{a, True, "Toggle"}, {True, 
   False}}]
$\endgroup$
  • 1
    $\begingroup$ A possible alternative without the If statement: Manipulate[ Plot[a Sin[x] + (1 - a) "", {x, -3, 3}], {{a, 1, "Toggle"}, {1, 0}}]. $\endgroup$ – user31159 Dec 14 '15 at 2:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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