5

I am trying to generate a blue-cyan-green-yellow-red color gradient that is linear in Hue. The code

ContourPlot[x, {x, 0, 1}, {y, 0, 1}, ContourStyle -> None, 
ColorFunction -> Function[{x, y}, Hue[2/3*(1 - x)]], 
ColorFunctionScaling -> False, AspectRatio -> 0.125, 
ImageSize -> 840]

gives

ContoursDefault
(source: ucdavis.edu)

which seems okay. However, adding the option Contours -> 500 to the above, i.e., evaluating

ContourPlot[x, {x, 0, 1}, {y, 0, 1}, Contours -> 500, 
ContourStyle -> None, 
ColorFunction -> Function[{x, y}, Hue[2/3*(1 - x)]], 
ColorFunctionScaling -> False, AspectRatio -> 0.125, 
ImageSize -> 840]

produces

Contours500
(source: ucdavis.edu)

By trying different numbers for Contours, I can reduce the odd artifact at the red end of this gradient. But I suspect I am missing something. Any suggestions? Thanks a lot.

volk
  • 65
  • 4
  • This doesn't seem to have anything to do with the custom ColorFunction, but with whatever internal function decides on the contour levels. Just look at the output from ContourPlot[x, {x, 0, 1}, {y, 0, 1}, Contours -> 60].... – Jason B. Jun 27 '16 at 19:31
  • 2
    I see this bug as far back as version 9, as well as the current versions. A workaround is to supply the contour levels manually, via something like Contours -> Subdivide[500] – Jason B. Jun 27 '16 at 19:33
  • JasonB, thanks a lot for the quick response! "Subdivide" does what I need, and your reply ended several days of agony for me ... – volk Jun 27 '16 at 19:51
  • 1
    So you would think that the contour levels chosen automatically would correspond to (1.0/61 Range[60]) when Contours -> 60 is chosen. But instead, the contours correspond to Round[1/61, .001] Range[60]...... Bug report filed – Jason B. Jun 27 '16 at 20:08
  • 2
    my eyes must be going.. what artifact are we supposed to see here? – george2079 Jun 27 '16 at 20:25
  • 3
    @george2079. The solid-color red rectangle on the right side of the second image. The colors aren't continuous there. (It actually is pretty subtle.) – march Jun 27 '16 at 20:26
  • I see.. You can see this more clearly if you do ColorFunction -> Function[{x, y}, Hue[RandomReal[]]] . I'm not sure this is a bug though, if you consider that you don't typically have an iso-line exactly on the boundary of a contour plot, ContourPlot's spacing algorithm maybe doesn't try to make the boundary bands equal spaced. (The left end is slightly wider too..) – george2079 Jun 27 '16 at 21:00
  • The band is much starker when you use GrayLevel: ContourPlot[x, {x, 0, 1}, {y, 0, 1}, AspectRatio -> 0.125, ColorFunction -> GrayLevel, ColorFunctionScaling -> False, Contours -> 500, ContourStyle -> None, ImageSize -> 840] – J. M.'s missing motivation Jun 27 '16 at 23:39
  • There's still a problem with much fewer contours: ContourPlot[x, {x, 0, 1}, {y, 0, 1}, AspectRatio -> 0.125, ColorFunction -> GrayLevel, Contours -> 5] – Michael E2 Jun 28 '16 at 05:10
  • I observe the same behavior with version 8.0.4. – Alexey Popkov Feb 17 '17 at 06:42
  • As for the coloring scheme itself, this is a related thread. – J. M.'s missing motivation Apr 02 '19 at 23:12

1 Answers1

4

It is easy to see what is happening if you leave out ContourStyle -> None. For some reason ContourPlot does not automatically select contours near to maximum value in the plot. Some Contour values are worse than others in this regard. A solution is to specify contours manually.

ContourPlot[x, {x, 0, 1}, {y, 0, 1}
 , ColorFunction -> Function[{x, y}, Hue[2/3*(1 - x)]]
 , ColorFunctionScaling -> False
 , AspectRatio -> 0.125
 , ImageSize -> 640
 , Contours -> #
] & /@ {76, Subdivide[1, 76]} // Column

enter image description here

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371