1
$\begingroup$

I have a ListPlot with vertical and horizontal lines. I would like to color the resulting rectangles by something like "Fill with Green if less than both line 1 & line 3". However, I can't figure out how to make this logic work with Filling.

Two examples of non-working code below: The first displays a strange band of green underneath line 1.

 ListPlot[{
  (*Horizontal line 1*){{0, 5}, {35, 5}},
  (*Horizontal line 2*){{0, 10}, {35, 10}},
  (*Vertical line 3*){{5, 0}, {5, 35}},
  (*Vertical line 4*){{10, 0}, {10, 35}}},
 Joined -> True,
 Filling -> {1 -> 3}, FillingStyle -> LightGreen]

enter image description here

The second displays no filling at all.

 ListPlot[{
  (*Horizontal line 1*){{0, 5}, {35, 5}},
  (*Horizontal line 2*){{0, 10}, {35, 10}},
  (*Vertical line 3*){{5, 0}, {5, 35}},
  (*Vertical line 4*){{10, 0}, {10, 35}}},
 Joined -> True,
 Filling -> {1 -> {3}}, FillingStyle -> LightGreen]

enter image description here

Eventually the locations of the lines will be controlled via Manipulate. Thanks for any advice!

$\endgroup$
2
$\begingroup$

This is very closely related to this question. Here is a version of kglr's answer adapted to your problem:

ListPlot[{
  (*Horizontal line 1*){{0, 5}, {35, 5}},
  (*Horizontal line 2*){{0, 10}, {35, 10}},
  (*Vertical line 3*){{5, 0}, {5, 35}},
  (*Vertical line 4*){{10, 0}, {10, 35}},
  (*Horizontal line 5*){{0, 0}, {5, 0}}
  },
 Joined -> True,
 Filling -> {1 -> {5}},
 FillingStyle -> LightGreen,
 PlotStyle -> {Automatic, Automatic, Automatic, Automatic, None}
 ]

Output

As you can see, the key is to create a shorter line that goes between $x=0$ and $x=5$ where 5 is the position of the vertical line. Then you can fill between the chosen full length horizontal line and this shorter other horizontal line to get what you want, since you cannot use vertical lines with the Filling option.

For full flexibility, I would consider using Graphics instead:

Graphics[{
  LightGreen, Rectangle[{0, 0}, {5, 5}],
  Gray,
  InfiniteLine[{0, 5}, {1, 0}],
  InfiniteLine[{0, 10}, {1, 0}],
  InfiniteLine[{5, 0}, {0, 1}],
  InfiniteLine[{10, 0}, {0, 1}]
  },
 Axes -> True,
 PlotRange -> {{0, 35}, {0, 35}}
 ]

Output

AspectRatio -> 1/GoldenRatio, which may or may not be wanted, is used here to match the output of ListPlot.

You might also consider adding the Rectangle to the ListPlot using the Prolog or Epilog option (probably Prolog so that the rectangle goes under the lines).

| improve this answer | |
$\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.