Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

   Dynamic[z], {ListLinePlot[p1, Filling -> Axis, 
      FillingStyle -> Blue] -> Desktop, 
    ListLinePlot[p2, Filling -> Axis, FillingStyle -> LightBlue] -> 
    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 'ListLinePlot`can 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

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

share|improve this question
By "stacked" do you mean that you need the sum of datasets shown? – Szabolcs May 30 '12 at 8:12
Jennifer, I have cleaned up and formatted your question. Please try to do it yourself the next time. – Mr.Wizard May 30 '12 at 8:14
Yes. By stacked, I meant that first listlineplot should begin from axis (as always) and the other listlineplot should begin from the first listlineplot. – Jennifer May 30 '12 at 8:30
Ok. Mr. Wizard, I will do from next time. – Jennifer May 30 '12 at 8:33
@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. – Heike May 31 '12 at 12:13
up vote 4 down vote accepted

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:

    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

share|improve this answer
Beaten by both of you. I surely am "Fastest Gun in the West" no longer. – Mr.Wizard May 30 '12 at 8:37
@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 :) – Mike Honeychurch May 30 '12 at 8:49
@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.) – Mr.Wizard May 30 '12 at 8:52
@Mr.Wizard I gave up on CheckboxBar quickly and went with Checkbox instead. – Szabolcs May 30 '12 at 8:55
@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? – Szabolcs May 30 '12 at 10:12

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

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

enter image description here

For a cumulative plot you could do this:

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

enter image description here

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

share|improve this answer
You could use Accumulate[p[[z]]] instead of Transpose[Accumulate /@ Transpose[p[[#]] & /@ z]] – Heike May 30 '12 at 9:09
@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! – Mike Honeychurch May 30 '12 at 11:49
@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. – Jennifer May 31 '12 at 11:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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