6
$\begingroup$

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?

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Artes
    Dec 11, 2012 at 12:53
  • 1
    $\begingroup$ ListPlot[{1, 2, 3}, Background -> Directive[Opacity[0.1], Green]] $\endgroup$
    – chris
    Dec 11, 2012 at 13:21

3 Answers 3

16
$\begingroup$

Background 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]

enter image description here

$\endgroup$
5
$\begingroup$

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

enter image description here

$\endgroup$
4
$\begingroup$

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:

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

p1

ListPlot[Sin[Range[0, 2 Pi, Pi/10]],
 Prolog -> gradientBackground,
 PlotRangePadding -> None
 ]

p2

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
 ]

p3

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

p4

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.