# How to apply a color gradient to a Rectangle primitive? [duplicate]

How can we apply a color gradient (say "Rainbow") to a Rectangle primitive ? Of course, I searched the documentation :

and also the site :

but didn't found something clear about the Rectangle primitive. I have the impression that this graphics primitive cannot have a color gradient applied to it.

Here's a MWE code to work with (the Red color in the Rectangle primitive should be remplaced with an horizontal 50% translucent "rainbow" gradient) :

SpectreVisible = Graphics[{Red, Rectangle[{380, -1}, {700, 8}]}];

Manipulate[
Show[
SpectreVisible,
PlotRange -> {{0, 1000}, {0, 7}},
AspectRatio -> 1,
Frame -> True,
GridLines -> Automatic,
GridLinesStyle -> Directive[Gray, Dashed],
Ticks -> Automatic,
ImageSize -> {600, 600}
],
FrameMargins -> None
]


The Manipulate code isn't essential here, but that rectangle would be used inside a Manipulate box.

EDIT : The possible duplicates aren't helping much. The codes are very complicated there, and doesn't address the issue with a simple Rectangle primitive.

## marked as duplicate by Michael E2, MarcoB, RunnyKine, user9660, Bob HanlonMar 16 '16 at 14:23

• Use Raster[] instead. – J. M. will be back soon Mar 16 '16 at 1:45
• Why ? Rectangle can't have a gradient color ? – Cham Mar 16 '16 at 1:46
• – Michael E2 Mar 16 '16 at 1:49
• These codes aren't obvious at all ! I'm unable to define a simple rectangle (with coordinates) using ColorFunction -> "Rainbow", and a transparency. Mathematica is so ackward to use ! – Cham Mar 16 '16 at 1:54

Rectangle cannot have a gradient color.

Here's one way to construct such a gradient. Under the hood it is using Polygon with the VertexColors option.

Graphics[ChartElementData["GradientRectangle",
"ColorScheme" -> "Rainbow",
"GradientOrigin" -> Left][{{0, 1}, {0, 1}}]]


{{0, 1}, {0, 1}} corresponds to {{xmin, xmax}, {ymin, ymax}} for the rectangle.

• I cannot search ChartElementData in documentation and I can search GradientRectangle either.If you put some specification about how to get it.I'll appreciate you. – yode Mar 16 '16 at 2:54
• This code works very well. It's far from obvious, though. – Cham Mar 16 '16 at 2:56
• Ok,I have found that. – yode Mar 16 '16 at 3:01

Perhaps ou mean this?

    Show[SetAlphaChannel[LinearGradientImage["Rainbow"], 0.5], Axes -> True]


The version is derived from J.M.

Graphics[{Opacity[0.5],
Raster[{Range[0, 1, 1/100]}, {{0, 0}, {1, 1}},
ColorFunction -> "Rainbow"]}]


• How do you add a transparency effet (say 50%) ? And how do you define the edges coordinates in a graphics ? – Cham Mar 16 '16 at 1:57
• Hmm, MMA 7 doesn't seem to know the LinearGradientImage command – Cham Mar 16 '16 at 1:58
• Feel so sorry to hear you use 7.0 – yode Mar 16 '16 at 2:04
• Well, nobody is perfect. Upgrading MMA implies that I upgrade the whole computer with its OS. It is too costly. Not worth all the troubles for me. So does that mean that there is no solution in my case ? I can't believe that I can't do a simple rectangle inside a graphics with an horizontal gradient applied to it ! – Cham Mar 16 '16 at 2:06
• If you use 7.0,that mean the option of ColorOutput does works? – yode Mar 16 '16 at 2:18

I may have found a solution, but it needs some improvements :

Spectre = RegionPlot[380 < x < 700, {x, 380, 700}, {y, -1, 8}, ColorFunction -> Function[{x, y}, ColorData["Rainbow"][x]]];

Manipulate[
Show[Spectre,
PlotRange -> {{0, 1000}, {0, 7}},
AspectRatio -> 1,
Frame -> True,
GridLines -> Automatic,
GridLinesStyle -> Directive[Gray, Dashed],
Ticks -> Automatic,
ImageSize -> {600, 600}
],
FrameMargins -> None]


Preview :

The problem with this code is that the color region doesn't show the grid behind it. The grid should be above it.

Also, the color tones are much too strong. How do you add a transparency effect to the gradient ?

And lastly, I had to define the range {y, -1, 8} instead of {y, 0, 7}. Is there a better way of doing this, so the rectangle doesn't have a white "margin" below and above it ?

• If you specify PlotRange inside RegionPlot you won't get the white margin. You also won't get it if you set PlotRangePadding -> None inside Show. – Brett Champion Mar 16 '16 at 3:20