3
$\begingroup$

I would like to extract the range values Mathematica is assigning colours to in a ListContourPlot (without the range affected by clipping), in order to build custom BarLegends with MaTeX.

I came across this question: ShowLegend values, but since this was already more than 5 years ago, I thought somebody might have a better idea? The function provided in the answer seemingly does not work without modifying in Mathematica 11.3.

Ideas?

Edit

Sorry for not providing an example. Let's look at this plot for instance:

SeedRandom[1234]
data = Function[row, 
Append[row, RandomReal[{95, 105}]]] /@ (Flatten[
Table[{i, j}, {i, 1, 3}, {j, 1, 3}], 1]);
data[[1, 3]] = 1;
ListContourPlot[data, PlotLegends -> Automatic]

Which gives

Test_Contour

How can I now find out where Mathematica begins to assign the ColorFunction? Clearly, the value 1 in data[[1,3]] is clipping and therefore not part of the scale. In order to exactly rebuild such a BarLegend, I need the maximum and minimum values for it.

Edit 2

What I finally want, is to rebuild the original legend like so

BarLegend[{ColorData["M10DefaultDensityGradient"], {86, 106}}, 9, 
"Ticks" -> Range[88, 104, 2], LegendLabel -> "DIY BarLegend", 
ColorFunctionScaling -> True]

Test_Contour_Legend

without having to "guess" the range for the colours (in this case {86,106}) but get them via code, to avoid incorrect Ticks. Because of the clipping, these values won't just be the minimum and maximum of the data anymore, so I'm interested in where Mathematica is clipping.

$\endgroup$
1
  • 2
    $\begingroup$ Gives us at least an example with corresponding data and code to generate the plot (doesn't have to be your specific case; in fact, a simple one would actually be preferred). $\endgroup$
    – MarcoB
    Commented May 28, 2018 at 17:44

1 Answer 1

3
$\begingroup$

If I understand correctly, you'd like to get the RGB values for each of the 10 color swatches in the legend. If so, here's one way to do it. It requires that the ContourPlot has already been created and stored in a symbol. I used your code and o = ListContourPlot[data, PlotLegends -> Automatic].

Clear[legendExtract];
Options[legendExtract] = {"RGBasReal" -> False};
legendExtract[plot_Legended, OptionsPattern[]] := Module[
  {legend = plot[[2, 1]], colordata, range, out},
  (* Extract colorfunction used in plot and the range of values used in the color scaling *)
  {colordata, range} = legend[[1]] /. {b_Blend & :> b[[1]]};
  (* Create list of values and colors used in legend *)
  out = {#, ColorData[colordata][Rescale[#, range]]} & /@ 
     legend[[2]] /. {{y_Real, _} :> y};
  (* If desired, output RGB values as real numbers instead of swatches *)
  If[OptionValue["RGBasReal"] == True, 
   out /. {RGBColor[vals__] :> vals}, out]
  ]

It has not be thoroughly tested, but seems to work for your test case:

enter image description here

$\endgroup$
3
  • $\begingroup$ Thanks, that's the right direction and a useful function! I made a second edit to my question to hopefully finally make clear what I want... $\endgroup$
    – T_202
    Commented May 28, 2018 at 20:25
  • 1
    $\begingroup$ The value range in my function has the range you want. You can get this value from o[[2,1,1,2]]. Any solution I propose will require that you make a legended plot first and extract the desired information to remake your bar legend. Others around here are better equipped at the fancy all-in-one solutions. $\endgroup$ Commented May 28, 2018 at 20:43
  • $\begingroup$ Indeed, I overlooked this. This is neat, thank you! $\endgroup$
    – T_202
    Commented May 28, 2018 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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