# How do I set the boundaries of the legend for a ListDensityPlot?

I have a 3-d array imported from a text file. One dimension represents time, and the other two represent space. It's basically an array of 2-d arrays of temperature values. I want to plot these values in a ListDensityPlot using the manipulate command to change the time I'm looking at. I did this with the following command:

Manipulate[ ListDensityPlot[data[[x]], PlotLegends -> Automatic, ColorFunction -> "Warm"], {x, 1, Length[data] - 1}]

where "data" is the variable containing the array. The only problem (apart from it being a little slow) is that every time I move the slider, the range of the legend adjusts based on the values currently shown in the plot. This means that as you slide the bar from start to finish, the change in plot does not accurately represent the data because the scale changes.

How do I make the scale stay the same?

-
Try fixing the plot range in ListDensityPlot and see what happens – dwa Mar 22 '13 at 2:22

I was very surprised to find that manually setting PlotRange to cover the entire data range doesn't work. The legend and colorfunction both seem to pick out the range of the data set; so PlotRange can only be used to limit the range:

min = Min[data];
max = Max[data];
Manipulate[
ListDensityPlot[data[[All, All, k]], ColorFunction -> "Warm",
PlotLegends -> Automatic, PlotRange -> {All, All, {min, max}}], {k,
1, 51, 1}]


One way around this behaviour is to use a custom ColorFunction that maps the data range to [0, 1], so that we can deactivate ColorFunctionScaling. We can then create our own fixed legend using BarLegend. Now everything works as intended:

colfunc = ColorData["Warm"][#/max] &;
leg = BarLegend[{colfunc, {min, max}}];
Manipulate[
ListDensityPlot[data[[All, All, k]], ColorFunction -> colfunc,
PlotLegends -> leg, ColorFunctionScaling -> False,
PlotRange -> All], {k, 1, 51, 1}]


-
That did exactly what I wanted, thank you! – Ross Tajvar Mar 22 '13 at 21:37