# How can I set different opacity values for the background of a ListPlot

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?

-
Welcome to Mathematica.stackexchange.com. Could you improve your question (including an example where you find your problems) so that it makes more sense ? Otherwise most likely it'll be closed. – Artes Dec 11 '12 at 12:53
ListPlot[{1, 2, 3}, Background -> Directive[Opacity[0.1], Green]] – chris Dec 11 '12 at 13:21

Background takes only simple parameters.

You could try this:-

lp = ListPlot[Prime[Range[25]], Filling -> Axis,

{{xmin, xmax}, {ymin, ymax}} = {#1, #2*1.02} & @@@
(PlotRange /. Options[lp, PlotRange]);

{{xmin, ymin}, {xmax, ymin}, {xmax, ymax}, {xmin, ymax}},
VertexColors -> {White, White, Green, Green}]];

-

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},

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.

• First, 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).
• Second, you would ideally not want to have to custom-draw the dimensions of the gradient background from scratch for every plot you create. It should be as simple as specifying a single option.

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:

{
bottomColor = Lighter[Orange],
topColor = Lighter[Cyan]
},
Inset[
Show[
Rasterize[Graphics[
Polygon[
{{0, 0}, {1, 0}, {1, 1}, {0, 1}},
VertexColors -> {
bottomColor, bottomColor, topColor, topColor
}
],
], "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},
PlotRange -> {{0, 3.5}, {0, 3.5}}
]

ListPlot[Sin[Range[0, 2 Pi, Pi/10]],
]

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]],
PlotRangeClipping -> False
]

Plot[Sin[x], {x, 0, 2 Pi},
Frame -> True,
FrameLabel -> {"x", "y"},