2
$\begingroup$

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!

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

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

$\endgroup$
  • 2
    $\begingroup$ re: PlotLegends : i.stack.imgur.com/tvuEr.jpg $\endgroup$ – Mr.Wizard Oct 9 '12 at 15:44
  • $\begingroup$ @Mr.Wizard The monster isn't quite uggers.... -1 to you sir... :P hehehe! $\endgroup$ – dearN Oct 10 '12 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 '12 at 19:16

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.