2

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.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Paul Saturday
  • 415
  • 3
  • 9

1 Answers1

4

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Paul Saturday
  • 415
  • 3
  • 9