Fixed color scale in multiple density plots with Fixed PlotLegends scale

From the great answer of Mr. Wizard, I would like to learn how to implement the associated PlotLegends scaled too. Here is the simplest example with PlotLegends, which does not look to be scaled:

data = {{{2, 4, 5, 4}, {3, 3, 7, 2}, {7, 2, 6, 5}, {7, 4, 8, 2}},
{{2, 1, 7, 9}, {3, 2, 1, 8}, {3, 3, 9, 7}, {0, 9, 7, 9}},
{{2, 2, 9, 0}, {5, 7, 0, 9}, {6, 9, 1, 9}, {5, 3, 8, 8}}};

Table[ListDensityPlot[Rescale[data[[i]], {0, 9}],
ColorFunctionScaling -> False, Mesh -> All,
ColorFunction -> "TemperatureMap", PlotLegends -> Automatic], {i, 1, 4}]


The first legend shows a different scale from the other three.

– rm -rf
Jun 14, 2013 at 3:39
• The first legend looks different because the max value is 8/9 < 1, whereas in the rest, it is 9/9 == 1. Perhaps you were looking for the 3 argument form of Rescale?
– rm -rf
Jun 14, 2013 at 3:42
• mathematica.stackexchange.com/questions/13046/… Jun 14, 2013 at 4:46
• What is crucial is that I am using DensityPlot, not ListDensityPlot (tha was an example), on many functions. But the color scale should be exactly the same on the plots as well as on the plot legends. Jun 14, 2013 at 4:50
• The actual plotting function doesn't really matter, as the issue is that of rescaling. It would be helpful to move the rescaling operation to the color function, as it can then be used with both DP and LDP. Please see the ColorFunction I used in this answer. Specifically, it uses the 3 argument version of Rescale to scale the input so that it is between fixed limits. Of course, this also requires that you know a priori, the min/max of your function/list, but it is easy to workaround it if you don't.
– rm -rf
Jun 14, 2013 at 5:07

Maybe you can try this:

data = {{{2, 4, 5, 4}, {3, 3, 7, 2}, {7, 2, 6, 5}, {7, 4, 8, 2}}, {{2,
1, 7, 9}, {3, 2, 1, 8}, {3, 3, 9, 7}, {0, 9, 7, 9}}, {{2, 2, 9,
0}, {5, 7, 0, 9}, {6, 9, 1, 9}, {5, 3, 8, 8}}};

Table[ListDensityPlot[Rescale[data[[i]], {0, 9}],
ColorFunctionScaling -> False, Mesh -> All,
ColorFunction -> "TemperatureMap",
PlotLegends -> BarLegend["TemperatureMap"]], {i, 1, 3}]


Result:

To see all available colors you can use:

Table[ListDensityPlot[Rescale[data[[i]], {0, 9}],
ColorFunctionScaling -> False, Mesh -> All, ColorFunction -> #,
PlotLegends -> BarLegend[#]], {i, 1, 3}] & /@