In the following example:
ListPlot[{1, 2, 3}, Background -> Green]
how can I set the Opacity[]
for the background such that the opacity increases from top to bottom?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityBackground takes only simple parameters.
You could try this:-
lp = ListPlot[Prime[Range[25]], Filling -> Axis,
(* Default PlotRangePadding *)
PlotRangePadding -> {Scaled[0.02], Scaled[0.02]}];
{{xmin, xmax}, {ymin, ymax}} = {#1, #2*1.02} & @@@
(PlotRange /. Options[lp, PlotRange]);
grad = Graphics[Polygon[
{{xmin, ymin}, {xmax, ymin}, {xmax, ymax}, {xmin, ymax}},
VertexColors -> {White, White, Green, Green}]];
Show[lp, grad, lp]
An alternative is to use Overlay
and overlay the list plot with a gradient as per this answer. Keys are to make sure your image size and image padding in both plots match up. In the DensityPlot
I'm using the 4th argument to RGBColor
to give the varying opacity.
Overlay[{
DensityPlot[y, {x, 0, 1}, {y, 0, 100},
AspectRatio -> 2/5,
ColorFunction -> Function[{x, y}, RGBColor[0, 1, 0, x]],
Frame -> False, ImageSize -> {535, 235},
ImagePadding -> {{30, 5}, {30, 5}}, PlotRangePadding -> 0],
ListPlot[{1, 2, 3, 4, 5, 6}, AspectRatio -> 2/5, Filling -> Bottom,
Frame -> True, ImageSize -> {535, 235},
ImagePadding -> {{30, 5}, {30, 5}}]
}]
There are several issues one has to contend with when implementing gradients.
VertexColor
is unfortunately quite broken. It doesn't export properly to PDF
, and it causes loss of antialiasing when drawing plots on top of polygons with VertexColors
(see this question). So here is an approach that tries to deal with these issues. It allows you to add the desired background uniformly to any plot by only adding Prolog-> gradientBackground
.
First I define the background in such a way that it can adjust itself to any plot in which it is placed:
gradientBackground = With[
{
bottomColor = Lighter[Orange],
topColor = Lighter[Cyan]
},
Inset[
Show[
Rasterize[Graphics[
Polygon[
{{0, 0}, {1, 0}, {1, 1}, {0, 1}},
VertexColors -> {
bottomColor, bottomColor, topColor, topColor
}
],
PlotRangePadding -> 0,
ImagePadding -> 0
], "Image"],
AspectRatio -> Full],
{Left, Bottom},
{0, 0},
ImageScaled[{1, 1}]
]
];
Now we just have to test it with a few examples:
ListPlot[{1, 2, 3},
Prolog -> gradientBackground,
PlotRangePadding -> None,
PlotRange -> {{0, 3.5}, {0, 3.5}}
]
ListPlot[Sin[Range[0, 2 Pi, Pi/10]],
Prolog -> gradientBackground,
PlotRangePadding -> None
]
In the above two plots, I've added PlotRangePadding -> None
so that the background only appears inside the plot range. But if you want the whole region of the plot to be filled, you can do that by adding PlotRangeClipping -> False
instead of suppressing the padding:
ListPlot[Sin[Range[0, 2 Pi, Pi/10]],
Prolog -> gradientBackground,
PlotRangeClipping -> False
]
Plot[Sin[x], {x, 0, 2 Pi},
Frame -> True,
FrameLabel -> {"x", "y"},
Prolog -> gradientBackground,
PlotRangeClipping -> False]
The last plot shows that it doesn't just work for ListPlot
.
Although I use VertexColors
in the definition of gradientBackground
, I then immediately rasterize it. You could leave that out and try the last plot again, to see how bad it would look without rasterization. To achieve the adjustability of the background, I wrapped the rasterized gradient in Show
with the option AspectRatio -> Full
, which makes the inset rubbery when I then specify the third argument of Inset
to be 1
in ImageScaled
coordinates.
ListPlot[{1, 2, 3}, Background -> Directive[Opacity[0.1], Green]]
$\endgroup$