I have a function, which plots the given function array f
. This is a part of the function:
Plot[-f, {x,0,2}, Filling->{1->{2}}, PlotStyle->Green]
When I specify {x^2, 2x}
as the function f
, I get the errors "2 must be an integer between 1 and 1. "
and "{2} is not a valid Filling specification."
. I get this error because I have a minus sign in front of the argument f
. If I plot {-x^2,-2x}
then everything is fine. So how to multiply everything in f
by -1 without getting these errors?
Answer
You need to evaluate the first argument of Plot
before sampling the specific datapoints, as the multiplication (the minus sign before the list of functions) does not get thread over the list by default, as Plot
has attribute HoldAll
. This means that your example Plot
is practically called with only one function (which is a List
) and therefore the Filling
specification does not make sense. Discussed in more detail here.
Plot[Evaluate[-{x^2, 2 x}], {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green]
This effectively equals the following (note that the - signs are in front of the individual functions):
Plot[{-x^2, -2 x}, {x, 0, 2}, Filling -> {1 -> {2}}, PlotStyle -> Green]
Both produce the following correct plot:
Comments
Post a Comment