3
$\begingroup$

I want to use ListContourPlot to display all negative values using one colorscheme and all positive values using another color scheme. This question Define a color function using Piecewise gives some hints, but if I use something like this

colorFunc[x_] := Piecewise[{{"AlpineColors", x >= 0},
{"SouthwestColors", x < 0}}];

ListContourPlot[data, ColorFunction -> colorFunc]

I get the error message:

AlpineColors is not a Graphics primitive or directive

Any idea?

Thanks in advance.

Edit1: I used kguler's suggestion which works with most data. However, in some cases I get results like this: enter image description here

using

ListContourPlot[data, Contours -> 10,ColorFunction -> (Piecewise[{{ColorData["NeonColors"][#], # > 0.5}, {ColorData["Aquamarine"][#], # <= 0.5}}] &),ContourLabels -> All]

The Aquamarine colors should code only for negative values and not for positive ones. What does go wrong?

$\endgroup$
3
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Jan 14, 2015 at 11:13
  • $\begingroup$ Note that according to the docs, "ColorFunction -> "name" is equivalent to ColorFunction -> (ColorData["name"][#i]&) where the slot used is as follows...." Using a string name is a special case and the string has to appear by itself after the -> for it to be handled as a ColorData name for you. To do what you want, you have to do all the work in your program, as in the answer(s). $\endgroup$
    – Michael E2
    Jan 14, 2015 at 11:18
  • $\begingroup$ Regarding your edit try adding ColorFunctionScaling -> False. $\endgroup$
    – Mr.Wizard
    Jan 14, 2015 at 18:26

1 Answer 1

4
$\begingroup$

You can do

ListContourPlot[Table[Sin[i + j^2], {i, 0, 3, 0.1}, {j, 0, 3, 0.1}], 
 ColorFunction -> (Piecewise[{{ColorData["AlpineColors"][#], # >= .5}, 
                              {ColorData["SouthwestColors"][#], # < .5}}] &)]

enter image description here

Update: Rescaling the range of the function ColorData[_scheme_]using the form ColorData[{_scheme_, {min, max}}] together with the option ColorFunctionScaling->False gives more control:

dt = Table[Sin[i + j^2], {i, 0, 3, 0.1}, {j, 0, 3, 0.1}];
{min, max} = Through@{Min, Max}@dt;
ListContourPlot[dt, ColorFunctionScaling -> False,
 ColorFunction -> (Piecewise[{{ColorData[{"AlpineColors", {min, max}}][#], # >= 0},
            {ColorData[{"SouthwestColors", {min, max}}][#], # < 0}}] &)]
(* same picture *)

Update 2: Dealing with the issue mention in the comments:

In version 9

dt = Table[Sin[i + j^2], {i, 0, 3, 0.1}, {j, 0, 3, 0.1}]; dt[[10, 10]] = -0.01;
{min, max} = Through@{Min, Max}@dt;
ListContourPlot[dt, ColorFunctionScaling -> False,
 ContourLabels -> True, MaxPlotPoints -> 500,
 ColorFunction -> (Piecewise[{{ColorData[{"AlpineColors", {min, max}}][#], # >= 0},
      {ColorData[{"SouthwestColors", {min, max}}][#], # < 0}}] &)]

gives

enter image description here

Using the option InterpolationOrder->0 I get: enter image description here

Zooming in around dt[[10,10]] using PlotRange -> {{9.75, 10.25}, {9.75, 10.25}}, I get

enter image description here

$\endgroup$
8
  • 1
    $\begingroup$ It is uncanny how often I start reading an chart/plot answer and say to myself "I bet this is written by @kgular" and then scroll down and sure enough!! $\endgroup$ Jan 15, 2015 at 0:00
  • $\begingroup$ Rescaling helps, but there are still undesired results, e.g. when using: dt = Table[Abs[Sin[i + j^2]], {i, 0, 3, 0.1}, {j, 0, 3, 0.1}]; dt[[10, 10]] = -0.01; $\endgroup$
    – user120162
    Jan 15, 2015 at 7:30
  • $\begingroup$ @user120162, I think due to sampling the isolated point dt[[10,100]] is not visible. You can see that it is colored correctly if you use the option InterpolationOrder->0 or zoom in around that point using, say, PlotRange -> {{9.5, 10.5}, {9.5, 10.5}}. $\endgroup$
    – kglr
    Jan 15, 2015 at 8:04
  • $\begingroup$ @kguler: The isolation point is just an illustration for the problems that can be caused. The colouring, however, is wrong also in other areas. Positive values very close to zero are also depicted as negative, see here: tinypic.com/view.php?pic=149qq8m&s=8#.VLd5hl0UNZx $\endgroup$
    – user120162
    Jan 15, 2015 at 8:28
  • $\begingroup$ @user120162, this may also be version issue. I will post what i get using v9 (windows 8 64 bit). $\endgroup$
    – kglr
    Jan 15, 2015 at 8:31

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.