2
$\begingroup$

I wanted to adopt this procedure to label only specific countourlines in this ContourPlot. The only difference is that the lowest value is not zero and I added a colorfunction.

data = Table[{m/100, n/100, 2 + (m^2 + n^2)/20000}, {m, 0, 100}, {n, 0, 100}];
data = Flatten[data, 1];
contour = Range[2, 3, 0.05];
contourplot = 
  ListContourPlot[data, 
    Axes -> True, 
    ColorFunction ->ColorData["BlueGreenYellow"], 
    Contours -> contour, 
    PlotRange -> {2, 3},
    PlotLegends -> Automatic]

Plot without labels, but with the correct Colorfunction

However, I noticed that the range of the Colorfunction is changed, when the lowest value in my plot is not 0, Consider,

contourplot = 
  ListContourPlot[data, 
    Axes -> True, 
    ColorFunction -> ColorData["BlueGreenYellow"], 
    Contours -> contour, 
    PlotRange -> {2, 3}, 
    ContourLabels -> 
      (If[MemberQ[{2.25, 2.5, 2.75}, #3], 
         Text[Framed[#3], {#1, #2}, Background -> White]] &), 
    PlotLegend->Automatic]

Plot with labels, but with a unwanted coloring

I only wanted to add ContourLabels without changing the color at all. I would be thankful for any advice/workaround, because I don't know what might cause this error.

$\endgroup$
4
$\begingroup$

I finally found the answer: Both functions, BarLegend and Colorfunction need to be rescaled, by adding a range:

data = Table[{m/100, n/100, 2 + (m^2 + n^2)/20000}, {m, 0, 100}, {n, 0, 100}];
data = Flatten[data, 1];
contour = Range[2, 3, 0.05];
contourplot = 
  ListContourPlot[data, 
     Axes -> True, 
     ColorFunction -> (ColorData["BlueGreenYellow"][Rescale[#1, {2, 3}]] &), 
     ColorFunctionScaling -> False, 
     Contours -> contour,  
     ContourLabels -> 
       (If[MemberQ[{2.25, 2.5, 2.75}, #3], 
          Text[Framed[#3], {#1, #2}, Background -> White]] &),   
     PlotLegends -> BarLegend[{Automatic, {2, 3}}]]

enter image description here

$\endgroup$
  • $\begingroup$ You shouldn't have to do this, what version of Mathematica are you using? $\endgroup$ – Jason B. Jul 30 '16 at 1:21
  • $\begingroup$ 10.2 (student version) $\endgroup$ – Paul Saturday Aug 1 '16 at 10:42

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.