4
$\begingroup$

I have two histograms histo1 and histo2:

data1 = RandomVariate[NormalDistribution[0, 1], 500]; 
data2 = RandomVariate[NormalDistribution[1, 2], 500]; 
data3 = RandomVariate[NormalDistribution[0.5, 1.5], 500];
data4 = RandomVariate[NormalDistribution[2, 3], 500];

histo1 = Histogram[{data1, data2}, {-10, 120, 0.5}, 
   PlotRange -> {{-10, 12}, {0, 100}}, Frame -> True, Axes -> False]

histo2 = Histogram[{data3, data4}, {-10, 120, 0.5}, 
   ChartStyle -> {Red, Green}, PlotRange -> {{-10, 12}, {0, 100}}, 
   Frame -> True, Axes -> False]

and I want them to appear alternating on the same place. With

ListAnimate[{histo1, histo2}]

they do. But I would like them to appear, depending on which button is pressed by an active observer. I assume, that I have to put the button generation

ButtonBar[{"blue" :> Print[histo1], "green" :> Print[histo2]}]

into a Dynamic or Manipulate surrounding. But I didn't find a proper way yet. Any suggestions?

$\endgroup$
2
$\begingroup$

This will easily generalize to more than two histograms:

Manipulate[
 Switch[whichHistogram,
  1, histo1,
  2, histo2
 ],
 {{whichHistogram, 1, "Choose histogram"}, {1 -> "blue", 2 -> "green"}}
]
$\endgroup$
6
  • $\begingroup$ Thanks, helped a lot. I did the same for a list of tables instead of histograms. They have the same labels, so is it possible to get them into one environment, where I only have to push e.g. the button "blue" and get the histogram and the table at once? $\endgroup$ – Lea Feb 27 '15 at 12:39
  • $\begingroup$ If I understood you correctly, you can replace histo1 in the above with Column[{histo1, table1}]. $\endgroup$ – Gerli Feb 27 '15 at 15:44
  • $\begingroup$ Perfect! That's the solution I needed! $\endgroup$ – Lea Mar 1 '15 at 15:33
  • $\begingroup$ I'm displaying 6 histograms, which leads to a drop-down menu instead of buttons. Any idea how to avoid this? Place would be enough for all the six buttons. $\endgroup$ – Lea Mar 2 '15 at 8:58
  • $\begingroup$ Add the option ControlType -> SetterBar to the Manipulate (see the documentation for more examples on this) $\endgroup$ – Gerli Mar 2 '15 at 9:22
4
$\begingroup$

FlipView[{histo1, histo2}] flips between displaying histo1 and histo2 each time it is clicked.

With some styling:

FlipView[Panel[##, {{Top, Center}}] & @@@ 
   Transpose[{{histo1, histo2}, Style[#, 18] & /@ {"histo1", "histo2"}}]]

enter image description here

$\endgroup$
3
$\begingroup$

You might want to consider a simple implementation with a Toggler. The only change you will need to make to your code is to explicitly set the image size of the histograms (because if the image size option is left at the default Automatic, the Toggler will shrink them down).

Reproducible data.

SeedRandom[42]; 
data1 = RandomVariate[NormalDistribution[0, 1], 500];
data2 = RandomVariate[NormalDistribution[1, 2], 500];
data3 = RandomVariate[NormalDistribution[0.5, 1.5], 500];
data4 = RandomVariate[NormalDistribution[2, 3], 500];

Now the slightly modified histograms.

histo1 = Histogram[{data1, data2}, {-10, 120, 0.5}, 
  PlotRange -> {{-10, 12}, {0, 100}},
  ImageSize -> Medium,
  Frame -> True,
  Axes -> False];

histo2 = Histogram[{data3, data4}, {-10, 120, 0.5}, 
  ChartStyle -> {Red, Green},
  PlotRange -> {{-10, 12}, {0, 100}},
  ImageSize -> Medium,
  Frame -> True,
  Axes -> False];

The Toggler is simply

Toggler[Framed["Click here", RoundingRadius -> 5], {histo1, histo2}]

It starts out looking like this:

button

After the first click it looks like this:

histo1

After the second click it looks like this:

histo2

Any subsequent clicks causes the displayed histogram to be replaced by the other.

Toggler has the advantages of being simple and being its own button. The list of items to toggled can be any length, so any number items can be displayed by a Toggler. Shift-clicking moves backward in the list of items, which can be handy when there are a lot of items.

$\endgroup$

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.