Categories
computing

PlotRange and ColorFunctionScaling in Mathematica

A density plot on mathematica and its accompanying colorbar decides its own range from the maximum and minimum in the data, as a result specifying a PlotRange is no good contrary to matplotlib where specifying a “vmin” and “vmax” simply does the job.
This is an artifact of mathematica having the “ColorFunctionScaling->True” by default, hence one needs to set that to “False” and then manually specify the “ColorFunction” to go from the bottom of the PlotRange to the top.

For instance, here is a code which takes adjusts the colorbar as well as the plot range of the density plot. This is shown as a video of the “Manipulate” function where the plot range is scanned over.



Manipulate[
DensityPlot[Sin[x] Sin[y], {x, -4, 4}, {y, -3, 3},
PlotRange -> {Automatic, Automatic, {-k, k}},
PlotLegends -> BarLegend[{Automatic, {-k, k}}],
ColorFunctionScaling -> False,
ColorFunction -> (ColorData["Rainbow"][Rescale[#, {-k, k}]] &),
PlotPoints -> 30], {k, 1, 10}]

test

Leave a comment