5
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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].... $\endgroup$ – Jason B. Jun 27 '16 at 19:31
  • 2
    $\begingroup$ 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] $\endgroup$ – Jason B. Jun 27 '16 at 19:33
  • 1
    $\begingroup$ 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 $\endgroup$ – Jason B. Jun 27 '16 at 20:08
  • 2
    $\begingroup$ my eyes must be going.. what artifact are we supposed to see here? $\endgroup$ – george2079 Jun 27 '16 at 20:25
  • 3
    $\begingroup$ @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.) $\endgroup$ – march Jun 27 '16 at 20:26
4
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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