3

I am trying to show the legend with a ColorData for a discrete Fourier transform coefficient table.

Here's a sample data table:

f={{0,26.6185,6.54197,1.32902,0.862353},
  {12.3854,4.09068,3.39286,0.208909,0.340049},
  {1.86011,1.79171,0.294318,0.930939,0.13115},
  {0.402539,0.448201,0.176836,0.240318,0.162534},
  {0.107348,0.0917102,0.13862,0.278679,0.0561954}}

The MatrixPlot of the above data is as such:

MatrixPlot[f]

enter image description here

The MatrixPlot with ColorData (as in the help manual at PlotLegends/ref/ShowLegend) is as follows:

Needs["PlotLegends`"];
ShowLegend[
 MatrixPlot[f, 
  BaseStyle -> {FontWeight -> "Plain", 
    FontSize -> 25}], {ColorData["TemperatureMap"][1 - #1] &, 20, 
  "-1", "1", LegendPosition -> {1.1, -.4}}
 ]

Obviously, changing the "-1", "1" would change the limits on the legend. How should I have the maximum and the minimum of f show up instead of the non-sensical (for this case) 1 and -1?

enter image description here

I tried changing the ShowLegend to

ShowLegend[
MatrixPlot[f, BaseStyle->{FontWeight->"Plain",FontSize->25}],
{ColorData["TemperatureMap"][1-#1]&,20,Max[f],Min[f],
LegendPosition->{1.1,-.4}}
]

but to no avail.

I have looked around and there has to be an easier way out than this dissertation!

dearN
  • 5,341
  • 3
  • 35
  • 74

1 Answers1

9

I modified a previous answer of mine to accept min/max values and shared it in chat a few days ago. I would recommend something along the same lines (example below). I would strongly discourage you from using the ugly monster that is PlotLegends.

Also, MatrixPlot does some internal rescaling of the data and so one-to-one correspondence with the colorbar would not make sense. I recommend using ArrayPlot instead.

Clear[colorbar]
colorbar[{min_, max_}, colorFunction_: Automatic, divs_: 150] := 
    DensityPlot[y, {x, 0, 0.1}, {y, min, max}, AspectRatio -> 10, 
        PlotRangePadding -> 0, PlotPoints -> {2, divs}, MaxRecursion -> 0, 
        FrameTicks -> {None, Automatic, None, None}, ColorFunction -> colorFunction
    ] 

With[{opts = {ImageSize -> {Automatic, 300}, ImagePadding -> 20}, cf = "DarkRainbow"}, 
    Row[{
        ArrayPlot[#, ColorFunction -> cf, FrameTicks -> True, opts], 
        Show[colorbar[Through[{Min, Max}[#]], cf], opts]
    }]
] &@f

rm -rf
  • 88,781
  • 21
  • 293
  • 472