5
$\begingroup$

I am using 3 checkboxes as desktops, portables and ipods in such a way that when I click to desktops it should shows ListLinePlot of p1, when I click to portables, it should show ListLinePlot of p2 and in the same way, when I click to ipods, it shows ListLinePlot of p3.

p1 = {.955, .914, .956, 1.516, 1.355}; 
p2 = {1.462, 1.357, 1.577, 1.917, 2.054};
p3 = {0,0, 1.57, 1.619, 3.997};

Column[{CheckboxBar[
   Dynamic[z], {ListLinePlot[p1, Filling -> Axis, 
      FillingStyle -> Blue] -> Desktop, 
    ListLinePlot[p2, Filling -> Axis, FillingStyle -> LightBlue] -> 
     Portables, 
    ListLinePlot[p3, Filling -> Axis, FillingStyle -> Green] -> Ipods}], Spacer[5], Dynamic[z]}]

This is working correctly. But I want when I click both desktops and portables on, it should show the plots of p1 and p2 in a stacked way. Here I am explaining it with the help of BarChart because I have no idea whether ListLinePlotcan do this .

BarChart[Table[{p1[[i]], p2[[i]]}, {i, 1, 5, 1}], ChartLayout -> "Stacked"]

If I click Desktops, Portables and ipods, then it should dynamically take data of p1, p2 and p3 in stacked order as

BarChart[Table[{p1[[i]],p2[[i]], p3[[i]]}, {i, 1, 5, 1}], ChartLayout -> "Stacked"]

and so on. But I want stacked ListLinePlot and I don't know any function like ChartLayout in BarChart. I have seen the Accumulate function but it does not work as I wanted. For more clarification, just have a look at

http://graphics.thomsonreuters.com/12/01/US_APPLEBREAK0112_VT.html

I want to do as it is, as in the product button of this link. Please help me.

$\endgroup$
7
  • $\begingroup$ By "stacked" do you mean that you need the sum of datasets shown? $\endgroup$
    – Szabolcs
    May 30, 2012 at 8:12
  • 2
    $\begingroup$ Jennifer, I have cleaned up and formatted your question. Please try to do it yourself the next time. $\endgroup$
    – Mr.Wizard
    May 30, 2012 at 8:14
  • $\begingroup$ Yes. By stacked, I meant that first listlineplot should begin from axis (as always) and the other listlineplot should begin from the first listlineplot. $\endgroup$
    – Jennifer
    May 30, 2012 at 8:30
  • $\begingroup$ Ok. Mr. Wizard, I will do from next time. $\endgroup$
    – Jennifer
    May 30, 2012 at 8:33
  • $\begingroup$ @Jennifer I'm a bit confused here. Both Szabolcs's and Mike's answer are consistent with the link in your question. In all these the lines are connecting data points belonging to the same device. Yet in your comment to Szabolcs's answer you seem to be asking for a graph where the data points belonging to the same year are connected instead which is different from the plot in the link you referred to. $\endgroup$
    – Heike
    May 31, 2012 at 12:13

3 Answers 3

4
$\begingroup$

I don't believe there is a built-in function to do this automatically, so you will need to implement it from scratch. It's easy, it just takes a little bit of work.

A useful function is Accumulate, if your datasets are of equal length. If they are not, you may want to linearly interpolate them (Interpolation) and use Plot instead of ListPlot.

Another useful tool is the Filling option of plotting functions, which you are already familiar with. Look up its advanced syntax in the documentation.

This implementation is a little clumsy, but it could be a start:

Column[{Dynamic@
   ListLinePlot[
    Accumulate[{ConstantArray[0, Length[p1]], If[q1, p1, 0], 
      If[q2, p2, 0], If[q3, p3, 0]}], 
    Filling -> {2 -> {{1}, LightPurple}, 2 -> {{3}, LightOrange}, 
      3 -> {{4}, LightGreen}}, PlotStyle -> Black],
  Grid[{{Labeled[Checkbox@Dynamic[q1], "Desktop"], 
     Labeled[Checkbox@Dynamic[q2], "Portables"], 
     Labeled[Checkbox@Dynamic[q3], "iPods"]}}, Spacings -> 1]
  }]

Mathematica graphics

$\endgroup$
9
  • $\begingroup$ Beaten by both of you. I surely am "Fastest Gun in the West" no longer. $\endgroup$
    – Mr.Wizard
    May 30, 2012 at 8:37
  • $\begingroup$ @Mr.Wizard haven't been on here much in recent weeks. I just stumbled across this at the right time. You are still the fastest gun in the west :) $\endgroup$ May 30, 2012 at 8:49
  • $\begingroup$ @Mike I'm afraid that's not it: I found the question right after it was posted. It just took me longer than you guys to get a nice working answer. I'll guard my pride by pretending that the three minutes I took to edit the question made the difference. :o) (Honestly the reordering nature of CheckboxBar was giving me fits.) $\endgroup$
    – Mr.Wizard
    May 30, 2012 at 8:52
  • $\begingroup$ @Mr.Wizard I gave up on CheckboxBar quickly and went with Checkbox instead. $\endgroup$
    – Szabolcs
    May 30, 2012 at 8:55
  • 2
    $\begingroup$ @Jennifer ListLinePlot[{{.955,1.462},{.914,1.357},{.956,1.577},{1.516,1.917},{1.355,2.054}}] is something completely different from the BarChart you showed. Can you update your question and clarify, preferably including an image as well? $\endgroup$
    – Szabolcs
    May 30, 2012 at 10:12
3
$\begingroup$

I'm assuming you want BarChart but it isn't 100% clear from your question. You might possibly be wanting a cumulative list plot?

p = {{.955, .914, .956, 1.516, 1.355}, {1.462, 1.357, 1.577, 1.917, 
    2.054}, {0, 0, 1.57, 1.619, 3.997}};

Column[{
  CheckboxBar[Dynamic[z], {1, 2, 3}],
  Spacer[{0, 20}],
  Dynamic@If[z =!= {},
    BarChart[p[[#]] & /@ Sort[z], ChartLayout -> "Stacked", 
     ImageSize -> 400],
    Spacer[0]
    ]
  }]

enter image description here

For a cumulative plot you could do this:

    Column[{
      CheckboxBar[Dynamic[z], {1, 2, 3}],
      Spacer[{0, 20}],
      Dynamic@If[z =!= {},
        ListLinePlot[Accumulate[p[[Sort[z]]]], 
 Filling -> Axis,ImageSize -> 400],
        Spacer[0]
        ]
      }]

enter image description here

This can be made prettier but I think it seems to capture what you are after.

$\endgroup$
3
  • $\begingroup$ You could use Accumulate[p[[z]]] instead of Transpose[Accumulate /@ Transpose[p[[#]] & /@ z]] $\endgroup$
    – Heike
    May 30, 2012 at 9:09
  • $\begingroup$ @Heike yes you're correct. When I first tried this I hadn't defined p correctly so Accumulate[p[[z]]] didn't work. But then I fixed p and went off on a tangent! $\endgroup$ May 30, 2012 at 11:49
  • $\begingroup$ @Heike: thanks for reply. But this is not what I actually wanted. I have updated my question and added a link to it which will more clarify my question. I wanted to make my code for product button given in the link. $\endgroup$
    – Jennifer
    May 31, 2012 at 11:48
2
$\begingroup$

StackedListPlot in versions 11.2+.

StackedListPlot[{p1, p2, p3}, PlotRange -> {{0, 5}, All}]

enter image description here

$\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.