Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

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

http://reference.wolfram.com/language/ref/Rectangle.html,

and also the site :

How to colorize a rectangle with a given color function? http://stackoverflow.com/questions/8142179/gradient-fill-using-graphics-in-mathematica

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.

share|improve this question

marked as duplicate by Michael E2, MarcoB, RunnyKine, Louis, Bob Hanlon Mar 16 at 14:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Use Raster[] instead. – J. M. Mar 16 at 1:45
    
Why ? Rectangle can't have a gradient color ? – Cham Mar 16 at 1:46
4  
    
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 at 1:54
up vote 9 down vote accepted

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.

enter image description here

share|improve this answer
    
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 at 2:54
    
This code works very well. It's far from obvious, though. – Cham Mar 16 at 2:56
    
Ok,I have found that. – yode Mar 16 at 3:01

Perhaps ou mean this?

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

enter image description here


The version is derived from J.M.

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

enter image description here

share|improve this answer
    
How do you add a transparency effet (say 50%) ? And how do you define the edges coordinates in a graphics ? – Cham Mar 16 at 1:57
    
Hmm, MMA 7 doesn't seem to know the LinearGradientImage command – Cham Mar 16 at 1:58
2  
Feel so sorry to hear you use 7.0 – yode Mar 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 at 2:06
    
If you use 7.0,that mean the option of ColorOutput does works? – yode Mar 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 :

gradient fill

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 ?

share|improve this answer
2  
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 at 3:20

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