4
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ Try fixing the plot range in ListDensityPlot and see what happens $\endgroup$ – dwa Mar 22 '13 at 2:22
4
$\begingroup$

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}]

Mathematica graphics Mathematica graphics

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}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ That did exactly what I wanted, thank you! $\endgroup$ – Ross Tajvar Mar 22 '13 at 21:37

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.