5
$\begingroup$

I have a simple question. Why is Filling not working in the following?

ListLinePlot[{{{0,0},{1,1}},{{5,0},{6,1}}}, Filling->{1->{2}}]

I am simply plotting two lines defined by two pairs of points, and I want to fill between them. Why is this not working as expected? It seems really easy.

$\endgroup$
  • 1
    $\begingroup$ I use my axisFlip function for this as illustrated here: (18669) $\endgroup$ – Mr.Wizard May 21 '15 at 7:12
5
$\begingroup$

You can simply add a background polygon:

ListLinePlot[{{{0, 0}, {1, 1}}, {{5, 0}, {6, 1}}}, 
 Prolog -> {LightBlue, Polygon[{{0, 0}, {1, 1}, {6, 1}, {5, 0}}]}]

plot

Or you can flip the axes twice using the following modification of Mr.Wizard's axisFlip function:

axisFlip[l_List] := 
  Replace[l, {x_?NumericQ, y_?NumericQ} :> {y, x}, {-2}];
axisFlip[g_Graphics] := g /. {
    x : (_Point | _Line | _GraphicsComplex) :> MapAt[axisFlip, x, 1],
    x : (PlotRange -> _) :> x~Reverse~2};

ListLinePlot[axisFlip@{{{0, 0}, {1, 1}}, {{5, 0}, {6, 1}}}, 
  Filling -> {1 -> {2}}] // axisFlip

ListPlot[{Table[Sin[x], {x, 0, 2 Pi, 0.1}], 
    Table[Sinc[x], {x, 0, 2 Pi, 0.1}]}, Filling -> {1 -> {2}}, 
  FillingStyle -> {Red, Blue}, Frame -> True] // axisFlip

Plot[Sin[x], {x, 0, 2 Pi}, Filling -> Axis] // axisFlip

plot plot plot

$\endgroup$
  • $\begingroup$ "but this method is dangerous and can have unexpected side-effects in some cases" -- such as? Is it because your replacement is a bit too general? I do not see any problems if I use my own axisFlip function for the outer operation. $\endgroup$ – Mr.Wizard May 21 '15 at 7:10
  • 1
    $\begingroup$ @Mr.Wizard Thank you. For the purposes of this answer I have extended your function for ListPlot and for Lists. $\endgroup$ – Alexey Popkov May 21 '15 at 7:38
5
$\begingroup$

Modifying the MapAt+GeometricTransformation approach in this answer in the related Q/A linked by @Mr.Wizard:

ClearAll[filledLLP]
filledLLP[l_, o : OptionsPattern[{Graphics, ListLinePlot}]] := 
 Graphics[MapAt[GeometricTransformation[#, ReflectionMatrix[{-1, 1}]] &, 
    ListLinePlot[Reverse[l, 3], FilterRules[{o}, Options[ListLinePlot]]], {1}][[1]],
  FilterRules[{o}, Options[Graphics]], 
  AspectRatio -> 1/GoldenRatio, Axes -> True, PlotRange -> All]

Examples:

lists = {{{0, 0}, {1, 1}}, {{5, 0}, {6, 1}}};
llp0 = ListLinePlot[lists, ImageSize -> 400, BaseStyle -> Thick];
llpX = filledLLP[lists, Filling -> {1 -> {2}}, FillingStyle -> Yellow,
    ImageSize -> 400, BaseStyle -> Thick];

Row[{llp0, llpX}, Spacer[10]]

enter image description here

filledLLP[{{{0, 0}, {1, 1}}, {{1, .2}, {4, .5}}, {{5, 0}, {6, 1}}}, 
 Filling -> {2 -> {{3}, Opacity[1, Red]}, 
   2 -> {{1}, Opacity[1, Yellow]}, 1 -> {{3}, Opacity[.25, Cyan]}}, 
 ImageSize -> 400, BaseStyle -> Thick]

Mathematica graphics

$\endgroup$
3
$\begingroup$

Using version 10.

The filling is the intersection of the half-plane to the right of the left line with the half-plane to the left of the right line.

RegionPlot[
 RegionIntersection[
  HalfPlane[{{0, 0}, {1, 1}}, {1, 0}],
  HalfPlane[{{5, 0}, {6, 1}}, {-1, 0}]],
 PlotRange -> {{0, 6}, {0, 1}}]

enter image description here

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