4
$\begingroup$

I'm relatively new to Mathematica. I tried looking for a solution online but couldn't find anything. I want to fill a plotted line both above the line (one color) and below (another color). For example, the line might be given by y = 1 - x over domain (0, 1) and with range (0, 1).

Any suggestions?

$\endgroup$
6
$\begingroup$

This works:

Plot[1 - x, {x, 0, 1}, 
 Filling -> { 1 -> Top, 1 -> {Bottom, {Green, Blue}}}
]

but I think there may be a neater solution.

$\endgroup$
  • $\begingroup$ Great- this works perfectly. Thanks to Jonie and Mike! $\endgroup$ – Sam Jul 26 '13 at 1:33
4
$\begingroup$

For an above and below type fill, as an alternative to @Jonie you can use FillingStyle:

FillingStyle->{Subscript[g, -],Subscript[g, +]} specifies that the filling should be done with Subscript[g, -] when a point, curve or surface lies below the object being filled to, and with Subscript[g, +] when it lies above.

Plot[1 - x, {x, 0, 1}, Filling -> {1 -> Top, 1 -> Bottom}, 
 FillingStyle -> {Green, Blue}]

enter image description here

Edit

After re-reading I am not entirely sure what you are looking for. If you want the fill constrained in the x direction as well then:

Plot[{If[0 <= x <= 1, 1 - x], 1 - x}, {x, 0, 2}, 
 Filling -> {1 -> Top, 1 -> Axis}, FillingStyle -> {Green, Blue}]

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.