5
$\begingroup$

It is needed to visualize some data set and built a color bar for describing the data values. The following array is given

TestArray = {{0.0288, 0.0413, 0.0772, 0.0369, 0.0296, 0.029, 0.0294, 
0.0334, 0.0771, 0.0755}, {0.0311, 0.0363, 0.0874, 0.0324, 0.025, 
0.0432, 0.0602, 0.0498, 0.1174, 0.2587}, {0.0291, 0.0452, 0.0484, 
0.0372, 0.0182, 0.0971, 0.0688, 0.0561, 0.0303, 0.0382}, {0.0174, 
0.0494, 0.0378, 0.0244, 0.0637, 0.0667, 0.1013, 0.0247, 0.0266, 
0.092}, {0.0234, 0.0118, 0.0235, 0.0239, 0.0336, 0.0498, 0.0366, 
0.0578, 0.0431, 0.0859}};

TestArray is visualized by the MatrixPlot function

MatrixPlot[TestArray, ColorFunction -> "ThermometerColors"]

enter image description here

Next, the color bar is built

BarLegend[{"ThermometerColors", {0, Max[TestArray]}}, LabelStyle -> {FontSize -> 13, FontFamily -> "Verdana", FontWeight -> Bold}, LegendFunction -> "Frame"]

enter image description here

However, the color bar does not conform with the plot: array has entries with values below 0.05, but they aren't blue on the plot according to the bar. How to make them conform?

$\endgroup$
5
$\begingroup$
BarLegend[{ColorData["ThermometerColors"]@Rescale[#, MinMax@TestArray, {.5, 1}] &, 
      MinMax@TestArray}, 
 ColorFunctionScaling -> False, 
 LabelStyle -> {FontSize -> 13, FontFamily -> "Verdana", FontWeight -> Bold}, 
 LegendFunction -> "Frame"]

legend

An easier way to get the legend is to use the option PlotLegends in MatrixPlot and extract the legend:

MatrixPlot[TestArray, ColorFunction -> "ThermometerColors", 
  PlotLegends -> BarLegend[Automatic, 
    LabelStyle -> {FontSize -> 13, FontFamily -> "Verdana", 
      FontWeight -> Bold}, LegendFunction -> "Frame"]][[2, 1]]

legend

Note: Color scaling in MatrixPlot is quite different from other plot functions. See Matrix Plot >> Details and Options for an explanation:

enter image description here

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you, this works $\endgroup$ – Konstantin May 26 at 9:48
  • $\begingroup$ Surely, your answer is very helpful ! $\endgroup$ – Konstantin May 26 at 9:58

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.