Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
It seems the labels need to be string. ToString@Min[f], ToString@Max[f] works for me. – Meng Lu Oct 9 '12 at 15:06
@MengLu Will try that out. I was thinking it had to do with "string" related stuff.. – drN Oct 9 '12 at 15:27

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

share|improve this answer
re: PlotLegends : – Mr.Wizard Oct 9 '12 at 15:44
@Mr.Wizard The monster isn't quite uggers.... -1 to you sir... :P hehehe! – drN Oct 10 '12 at 19:15
@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... – drN Oct 10 '12 at 19:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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