# Easy code to toggle between a graph and a blank graph

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.

• Manipulate[If[a, Plot[Sin[x], {x, -3, 3}], ""], {{a, True, "Toggle"}, {True, False}}] Dec 8, 2015 at 6:17
• @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. Dec 8, 2015 at 6:23
• Ok. I think I figure it out using your code. Dec 8, 2015 at 7:15
• A related thread. Dec 9, 2015 at 5:04

Perhaps the option Opacity can be considered

Manipulate[Plot[Sin[x], {x, -3, 3},
PlotStyle -> Opacity[a]], {{a, 1, "Toggle"}, {1, 0}}]

• Yes! This works too. Thank you! Dec 8, 2015 at 7:17
• @Kuba - I'm having a hard time understanding your comment. Can you clarify? Dec 13, 2015 at 1:45
• @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.
– Kuba
Dec 13, 2015 at 8:49
• 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. Dec 13, 2015 at 8:53

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.

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.

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}}]

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