4
$\begingroup$

Writing:

plot1 = ParametricPlot[{x, x}, {x, 0, 1}];
plot2 = ParametricPlot[{x, x^2}, {x, 0, 1}];
plot3 = ParametricPlot[{x, x^3}, {x, 0, 1}];

DynamicModule[{
  select = {1, 2, 3}},
 Column[{
   TogglerBar[Dynamic[select],
    {1 -> "p1", 2 -> "p2", 3 -> "p3"}],
   Dynamic@Show[
     {plot1, plot2, plot3}[[select]],
     PlotRange -> {{0, 1}, {0, 1}}]
   }]
 ]

I get:

enter image description here

but if I deselect all three boxes I get an error.

  1. How can I avoid this error by simply plotting nothing?

  2. How can I plot everything in the middle of the page, including the boxes?

Thank you!

$\endgroup$
5
$\begingroup$

To align things in the center of the notebook, you can use Pane. To center the toggler bar and the graphic, you can use the Alignment option of Column. Finally, one idea to avoid the error when no graphic is selected is to use an empty Graphics object as the first argument of Show instead. Here is an example doing this:

DynamicModule[{select={1,2,3}},
    Pane[
        Column[
            {
            TogglerBar[Dynamic[select],{1->"p1",2->"p2",3->"p3"}],
            Dynamic@Show[
                (* control graphics option by giving them in an empty graphic *)
                Graphics[{}, Axes->True,PlotRange->{{0,1},{1,0}}],
                {plot1,plot2,plot3}[[select]]]
            },
            Alignment->Center (* center toggler bar and graphic *)
        ],
        ImageSize->Full, Alignment->Center (* center within notebook *)
    ]
]

enter image description here

And here is the output after deselecting all plots:

enter image description here

$\endgroup$
  • $\begingroup$ Fantastic, thank you! One last thing: is it possible to enlarge the three boxes to taste and color them? Thank you! $\endgroup$ – TeM Feb 21 '18 at 16:09
  • 1
    $\begingroup$ @TeM That should probably be a separate question. You can modify the size using something like BaseStyle -> {SetterBoxOptions -> {FrameMargins -> {{20, 20}, {0, 0}}}}. I don't know how to change the color. $\endgroup$ – Carl Woll Feb 21 '18 at 16:56
2
$\begingroup$
colors = ColorData[63, "ColorList"][[;; 5]];
plots = Table[ParametricPlot[{x, x^i}, {x, 0, 1}, PlotStyle -> colors[[i]]], {i, 3}];

DynamicModule[{select = {1, 2, 3}}, Dynamic@Show[{plots[[select]], Graphics[{}]}, 
  Axes -> True, PlotRange -> {{0, 1}, {0, 1}},
  PlotLabel -> TogglerBar[Dynamic[select], 
     Table[i -> Grid[{{Item[Pane[Style[x^i, 16, Bold, "Panel", Opacity[1], colors[[i]],
      Background -> Opacity[0]], ImageSize -> {60, 20}, Alignment -> Center], 
   Background -> Opacity[If[MemberQ[select, i], .5, .2], colors[[i]]], 
   FrameStyle -> If[MemberQ[select, i], Darker@colors[[i]],  Opacity[0]]]}}], {i, 1, 3}],
   Appearance -> "Frameless"]]]

enter image description here

$\endgroup$
  • $\begingroup$ Fantastic, thank you! Is there a way to make sure all choices are disabled at the beginning? Thank you! $\endgroup$ – TeM Feb 22 '18 at 13:42
  • 1
    $\begingroup$ @TeM, use replace select = {1, 2, 3} with select = {}. $\endgroup$ – kglr Feb 22 '18 at 17:06

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.