7
$\begingroup$

Let's start with what I already found on the internet:

data = {{10, 4, 3, 0, 8, 8, 10, 8, 9, 10}, 
        {2, 1, 2, 15, 5, 8, 7, 10, 9, 15},
        {1, 4, 8, 1, 2, 8, 12, 10, 9, 15}};

ListLinePlot[Accumulate[data], Filling -> {1 -> {Axis, LightRed}, 
    2 -> {{1}, LightOrange}, 3 -> {{2}, LightBlue}}, PlotStyle -> {Red, Orange, Blue}]

This one will produce a stacked graph like the one i want:

enter image description here

but the data I have is not directly usable like in this example because accumulate wouldn't work unless I process data in some way.


Let's say the data i have is something like this:

data = {
 { {1, 10}, {3, 94.94}, {5, 41.52}, {10, 15.25}, {100, 2.19} },
 { {4, 2.35}, {8, 10.64}, {12, 90.28}, {36, 50.74} },
 { {2, 11.55}, {10, 15.65}, {20, 41.81} },
 { {1, 20.13}, {3, 57.44}, {5, 42.74}, {10, 3.59}, {40, 2.05}, {100, 2.05} },
 { {4, 13.74}, {20, 65.49}, {40, 112.62}, {80, 10.42} }};

If plot all together with a ListLinePlot, this is what I will get:

ListLinePlot[Table[data[[i]], {i, 1, Length[data] - 1}], Filling -> Axis]

Where each line is the junction of points for each set..

enter image description here

I would like to obtain a stacked graph exactly with these lines.

Could you help me to achieve this result?

Best Regards

PS: if possible would be cool see both cases where the values outside domains gives 0 contribute or the case were we continue using the same angular coefficient for the borders like in this image:

enter image description here

With the functions gets anyways = 0 if value < 0 :)

$\endgroup$
5
  • 2
    $\begingroup$ How do you intend to stack lines that don't go all the way across? For example, the red line stops at 36. If there is another line on top of it, where does that one go? $\endgroup$
    – user484
    Apr 30, 2014 at 16:04
  • $\begingroup$ Oh, in order to keep the question simpler at the beginning i assumed the contribute for the parts with no value as zero. But if someone would answer, i would be curious to see a secondary way where lines are completed keeping the angular coefficient of the stopped line (this on both sides) unless the value becomes zero. $\endgroup$ Apr 30, 2014 at 16:18
  • $\begingroup$ I've just found closely related topic: 32308 $\endgroup$
    – Kuba
    May 1, 2014 at 13:21
  • $\begingroup$ @Kuba related ok, but this one (i mean the one u answered) covers a lot the graphic part and is more open to the noobs like me that want to achieve this specific result :D $\endgroup$ May 1, 2014 at 17:12
  • $\begingroup$ @user3450548 It's just a link, the more connection the better database :) I'm glady you find my answer educational :) $\endgroup$
    – Kuba
    May 1, 2014 at 18:35

1 Answer 1

11
$\begingroup$

I'm assuming that the function is equal to 0 outside outer points:

Plot[
 Piecewise[{{
  Interpolation[#, x, InterpolationOrder -> 1], #[[1, 1]] <= x <= #[[-1, 1]]}}, 
  0] & /@ data // Accumulate // Reverse,
{x, 1, 100}, Evaluated -> True, Axes -> False, Frame -> True, ImageSize -> 500,
PlotStyle -> Thick, BaseStyle -> {18, Bold}, Filling -> Axis]

enter image description here

And for common region PlotRange -> {{4, 20}, All}:

enter image description here


Functions extrapolated but clipped at y == 0:

Off[InterpolatingFunction::dmval];

Plot[Clip[Interpolation[#, x, InterpolationOrder -> 1], {0, ∞}] & /@ data,
     {x, 1, 100}, Evaluated -> True, Axes -> False, Frame -> True, ImageSize -> 500, 
                  Filling -> Axis, PlotStyle -> Thick,  BaseStyle -> {Bold, 18},
                  PlotLabel -> "Extrapolated but clipped at 0."]

enter image description here

+ accumulated

enter image description here


Here is the case where outside the domain value is equal to that on the edge:

Plot[Piecewise[{
       {Interpolation[#, x, InterpolationOrder -> 1], #[[1, 1]] <= x <= #[[-1, 1]]},
       {#[[1, 2]], x < #[[1, 1]]},
       {#[[-1, 2]], x > #[[-1, 1]]}           
       }] & /@ data // Accumulate // Reverse,
 {x, 1, 100}, 
 Filling -> Axis, Evaluated -> True, PlotStyle -> Thick, Axes -> False, Frame -> True, 
 ImageSize -> 500, BaseStyle -> {18, Bold}]

enter image description here

$\endgroup$
4
  • $\begingroup$ I guess u right maybe hue colors would worked better :DD Thanks for reply i'm going to try it a second ^^' $\endgroup$ Apr 30, 2014 at 16:24
  • $\begingroup$ @user3450548 done. If you have a questions, feel free to ask. But I'm encouraging you to parse it by yourself. The idea is just to interpolate and plot partial sums of the set of functions. $\endgroup$
    – Kuba
    Apr 30, 2014 at 16:41
  • $\begingroup$ @Öskå oh, indeed, and thank you, it is better now :) $\endgroup$
    – Kuba
    Apr 30, 2014 at 16:43
  • $\begingroup$ very complete answer, thank you very much :) $\endgroup$ Apr 30, 2014 at 17:14

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.