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

Here's a sample data table:


The MatrixPlot of the above data is as such:


enter image description here

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

  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

MatrixPlot[f, BaseStyle->{FontWeight->"Plain",FontSize->25}],

but to no avail.

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

  • $\begingroup$ It seems the labels need to be string. ToString@Min[f], ToString@Max[f] works for me. $\endgroup$
    – Meng Lu
    Oct 9, 2012 at 15:06
  • $\begingroup$ @MengLu Will try that out. I was thinking it had to do with "string" related stuff.. $\endgroup$
    – dearN
    Oct 9, 2012 at 15:27

1 Answer 1


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.

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"}, 
        ArrayPlot[#, ColorFunction -> cf, FrameTicks -> True, opts], 
        Show[colorbar[Through[{Min, Max}[#]], cf], opts]
] &@f

  • 2
    $\begingroup$ re: PlotLegends : i.stack.imgur.com/tvuEr.jpg $\endgroup$
    – Mr.Wizard
    Oct 9, 2012 at 15:44
  • $\begingroup$ @Mr.Wizard The monster isn't quite uggers.... -1 to you sir... :P hehehe! $\endgroup$
    – dearN
    Oct 10, 2012 at 19:15
  • $\begingroup$ @rm-rf Thats a nice answer.. I'll speak with the folks I am collaborating with on this mini project and direct them to your answer... $\endgroup$
    – dearN
    Oct 10, 2012 at 19:16

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.