PlotRange in DensityPlot - Mathematica Stack Exchange most recent 30 from mathematica.stackexchange.com 2019-08-18T03:43:41Z https://mathematica.stackexchange.com/feeds/question/154269 http://www.creativecommons.org/licenses/by-sa/3.0/rdf https://mathematica.stackexchange.com/q/154269 4 PlotRange in DensityPlot Fraccalo https://mathematica.stackexchange.com/users/40354 2017-08-22T15:33:26Z 2017-10-03T15:47:19Z <p>I'd need to compare two different DensityPlot by keeping the PlotRange fixed: however I'm not able to do that because DensityPlot automatically fix the range of the plot to the {min,max} of the function. I've been reading something about it, a tried some possible solutions (for example ColorFunctionScaling -> False) but non of them solved my problem.</p> <p>Here is part of the code I'm using:</p> <pre><code>DensityPlot[( E^(-x^2 - y^2) (-1 - 4 x y))/Sqrt[\[Pi]], {x, -4, 4}, {y, -4, 4}, PlotRange -&gt; {Automatic, Automatic, {-0.69, 0.69}}, PlotPoints -&gt; 150, ColorFunction -&gt; (Blend[{RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[ 1, 0.55, 0], White}, #1] &amp;), PlotLegends -&gt; Placed[BarLegend[Automatic, LegendMarkerSize -&gt; 250], Right], ImageSize -&gt; 300, Background -&gt; Transparent ] DensityPlot[( E^(-x^2 - y^2) (2 x - 2 y))/Sqrt[\[Pi]], {x, -4, 4}, {y, -4, 4}, PlotRange -&gt; {Automatic, Automatic, {-0.69, 0.69}}, PlotPoints -&gt; 150, ColorFunction -&gt; (Blend[{RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[ 1, 0.55, 0], White}, #1] &amp;), PlotLegends -&gt; Placed[BarLegend[Automatic, LegendMarkerSize -&gt; 250], Right], ImageSize -&gt; 300, Background -&gt; Transparent ] </code></pre> <p>and the output is the following:</p> <p><a href="https://i.stack.imgur.com/pBeCA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pBeCA.png" alt="enter image description here"></a></p> <p><a href="https://i.stack.imgur.com/HYcuh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HYcuh.png" alt="enter image description here"></a></p> <p>where obviously the range of the plots is not -0.69:0.69 in both of them. Using the correct color scaling the orange/brown background in the first picture would be black as in the second one.</p> <p>Any solution for fixing it? Edit: I think the solution lies in using ColorData function in some way: I noticed that, with a standard color function (e.g. "Heat") I can use the combination:</p> <pre><code>ColorFunctionScaling -&gt; False, ColorFunction -&gt; ColorData[{"Heat", {-0.69, 0.69}}], </code></pre> <p>but I'm not able to reproduce it with my custom ColorFunction...</p> https://mathematica.stackexchange.com/questions/154269/-/154273#154273 5 Answer by eldo for PlotRange in DensityPlot eldo https://mathematica.stackexchange.com/users/14254 2017-08-22T16:21:17Z 2017-08-22T18:28:37Z <p>To compare the two plots you can rescale the first plot (f1) to the range of the second one:</p> <pre><code>f1 = (E^(-x^2 - y^2) (-1 - 4 x y)) / Sqrt[Pi]; f2 = (E^(-x^2 - y^2) (2 x - 2 y)) / Sqrt[Pi]; zr = Through[{NMinValue, NMaxValue}[{f2, -4 &lt;= x &lt;= 4}, {x, y}]] </code></pre> <blockquote> <p>{-0.684397, 0.684397}</p> </blockquote> <pre><code>col = {RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[1, 0.55, 0], White}; Grid[{{ DensityPlot[f1, {x, -4, 4}, {y, -4, 4}, PlotRange -&gt; zr, ColorFunction -&gt; (Blend[col, Rescale[#, zr]] &amp;), ColorFunctionScaling -&gt; False, ImageSize -&gt; 300], DensityPlot[f2, {x, -4, 4}, {y, -4, 4}, PlotRange -&gt; zr, ColorFunction -&gt; (Blend[col, #] &amp;), ImageSize -&gt; 300, PlotLegends -&gt; Automatic] }}] </code></pre> <p><a href="https://i.stack.imgur.com/LM84p.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LM84p.jpg" alt="enter image description here"></a></p> https://mathematica.stackexchange.com/questions/154269/-/157064#157064 2 Answer by Alvin for PlotRange in DensityPlot Alvin https://mathematica.stackexchange.com/users/23171 2017-10-03T15:47:19Z 2017-10-03T15:47:19Z <p>This problem can be solved by using ListDensityPlot and Blend.</p> <p>First you have to obtain the discrete values of your functions:</p> <pre><code>f1 = Table[(E^(-x^2 - y^2) (-1 - 4 x y))/Sqrt[Pi], {x, -4, 4, 0.2}, {y, -4, 4, 0.2}]; f2 = Table[(E^(-x^2 - y^2) (2 x - 2 y))/Sqrt[Pi], {x, -4, 4, 0.2}, {y, -4, 4, 0.2}]; </code></pre> <p>Second, using <em>Blend</em> function to map a given color range to your defined color scheme:</p> <pre><code>ColorFunction -&gt; (Blend[ Transpose@{Subdivide[-0.684397, 0.684397, 6], colormap}, #1] &amp;), ColorFunctionScaling -&gt; False </code></pre> <p>notice that Subdivide[-0.684397, 0.684397, 6] is a list of the same length of your color scheme, and -0.684397, 0.684397 are the lowest and highest limits of the wanted color range (your fixed PlotRange).</p> <p>So now this problem is settled already. The full codes and results are listed below</p> <pre><code> f1 = Table[(E^(-x^2 - y^2) (-1 - 4 x y))/Sqrt[Pi], {x, -4, 4, 0.2}, {y, -4, 4, 0.2}]; f2 = Table[(E^(-x^2 - y^2) (2 x - 2 y))/Sqrt[Pi], {x, -4, 4, 0.2}, {y, -4, 4, 0.2}]; colormap = {RGBColor[0.02, 1, 1], RGBColor[0, 0.48, 1], RGBColor[0, 0, 0.73], Black, RGBColor[0.6, 0.22, 0], RGBColor[1, 0.55, 0], White}; options = {ColorFunction -&gt; (Blend[ Transpose@{Subdivide[-0.684397, 0.684397, 6], colormap}, #1] &amp;), ColorFunctionScaling -&gt; False, InterpolationOrder -&gt; 3, PlotRange -&gt; All, ImageSize -&gt; 200}; p1 = ListDensityPlot[f1, options]; p2 = ListDensityPlot[f2, options, PlotLegends -&gt; Automatic]; Grid[{{p1, p2}}] </code></pre> <p><a href="https://i.stack.imgur.com/o7KlX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/o7KlX.png" alt="enter image description here"></a></p>