3
$\begingroup$

I am trying to create a DensityPlot of a symmetric function f(r)=f(x,y) that is composed of 2 other functions:

f(r)= g(r) + h(r)

f(x,y)=g(x,y)+h(x,y).

I can plot f(x,y) fine (can always convert from polar to Cartesian) using DensityPlot but I cannot see whether each "peak" comes from a high value of g,h or both. I suspect that the solution involves some manipulation of RGBColor and perhaps Map but I am at a loss as to how to implement a differentiation between the two.

I provide a simple example code below. How can apply the color scaling so that the colour is a combination of the intensity of g and h?

h[x_, y_] = (x^2 + y^2)/x;
g[x_, y_] = (x^2 + y^2)/y;
f[x_, y_] = h[x, y] + g[x, y];

DensityPlot[f[x, y], {x, -1, 1}, {y, -1, 1}, MaxRecursion -> 6]

Mathematica graphics

EDIT: Slightly better explanation: "I would like to convey this information pictorially so that if we say h=blue and g=red, I create a color palette that is a combination of the two so the graph is blue where h dominates, red where g dominates and blue+red when they add up."

$\endgroup$
  • $\begingroup$ I don't understand. The colour IS a combination of g and h, namely their sum. Furthermore, is that the function you are interested in plotting? Because it's trivial to see that on each axis you have the maxima associated to each function. Maybe the real problem is easier to understand? $\endgroup$ – tsuresuregusa Apr 23 '16 at 22:41
  • $\begingroup$ I will try gain. You are correct in saying that the colour in the above graph is a combination of the g and the h. What the graph doesn't tell me is when it is that g dominates or when h dominates or when both of them constructively add up as it were. I would like to convey this information pictorially so that if we say h=blue and g=red, I create a color palette that is a combination of the two so the graph is blue where h dominates, red where g dominates and blue+red when they add up. Does that clear it up? I understand the example may not reflect the request too well, apologies. $\endgroup$ – OldTomMorris Apr 23 '16 at 23:04
  • $\begingroup$ if it's just a sum of functions you can invert the sign of one and the colorcode will reflect that. I just think that 2 plots side by side are easier to see though. Or perhaps a contour plot of each one with different colours on the background of f[x,y]? $\endgroup$ – tsuresuregusa Apr 23 '16 at 23:11
  • $\begingroup$ forget the invert part, unless you only want to plot only one quadrant. Is that the actual function you want to plot? $\endgroup$ – tsuresuregusa Apr 23 '16 at 23:18
  • $\begingroup$ @tsuresuregusa No, this is just an example function.The function I want to plot is well behaved. $\endgroup$ – OldTomMorris Apr 24 '16 at 8:14
2
$\begingroup$

Maybe something like

Quiet@Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1}, PlotPoints -> 50, 
  MaxRecursion -> 6, Mesh -> None, ClippingStyle -> None, 
  Axes -> {True, True, False}, BoundaryStyle -> None, ViewPoint -> {0, 0, Infinity}, 
  ColorFunctionScaling -> False, 
  ColorFunction -> Function[{x, y, z}, Blend[{{h[x, y], Red}, {g[x, y], Blue}}, f[x, y]]]]

Mathematica graphics

Alternatively, post-process the DensityPlot output to replace the VertexColors settings with colors that depend on the h and g values of coordinates:

dp = DensityPlot[f[x, y], {x, -1, 1}, {y, -1, 1}, MaxRecursion -> 6]

Mathematica graphics

dp /. GraphicsComplex[a_, b_, c___] :> 
  GraphicsComplex[a, b, 
   VertexColors -> (Blend[{{#, Red}, {#2, Blue}}, #3] & @@@ 
       Rescale[Transpose[{h @@@ a, g @@@ a, f @@@ a}]])]

Mathematica graphics

$\endgroup$
  • $\begingroup$ This seems like the closest solution to what I'm looking for but I'm somewhat scared of using Plot3D for a density plot. Is there any way you can feed something like "ColorFunction -> Function[{x, y, z}, Blend[{{h[x, y], Red}, {g[x, y], Blue}}, f[x, y]]]]" to DensityPlot/ContourPlot? $\endgroup$ – OldTomMorris Apr 24 '16 at 14:29
  • $\begingroup$ @OldTomMorris, unfortunately, ColorFunction for DensityPlot and ContourPlot is quite restricted; it takes a single argument that single argument is the value of f. The second example does not use Plot3D; it post-proceses the DensityPlot output to change the colors and the result is almost identical to the Plot3D output. $\endgroup$ – kglr Apr 24 '16 at 14:33
  • $\begingroup$ Could you possibly explain a bit more what the code dp /. GraphicsComplex[a_, b_, c___] :> GraphicsComplex[a, b, VertexColors -> (Blend[{{#, Red}, {#2, Blue}}, #3] & @@@ Rescale[Transpose[{h @@@ a, g @@@ a, f @@@ a}]])] is doing? I am trying to implement it but the computation is taking hours. I am wondering if I am transcribing it wrong, thus making Mathematica work things out that don't make sense. My functions are functions of more than one variable so I'm not sure I've generalised correctly. $\endgroup$ – OldTomMorris Apr 26 '16 at 19:33
1
$\begingroup$

What about something like this?

Show[RegionPlot[{g[x, y] > h[x, y], g[x, y] < h[x, y]}, {x, -1, 
   1}, {y, -1, 1}, MaxRecursion -> 6, PlotStyle -> {Red, Blue}],
 ContourPlot[f[x, y], {x, -1, 1}, {y, -1, 1}, ContourShading -> None, 
  Contours -> 5]]

enter image description here

Since your function are diverging in x==y==0 it's a bit difficult to get nice results only with densityPlot, thus I chose RegionPlot. Hope this helps.

ADDENDUM: For a nice behaved function:

h[x_, y_] = (x^2 + y^2)/(x + 1);
g[x_, y_] = (x^2 + y^2)/(y + 1);
f[x_, y_] = h[x, y] + g[x, y];

Show[
    ContourPlot[f[x, y], {x, -0, 1}, {y, -0, 1}, Contours -> 5, 
  ColorFunction -> "Rainbow", 
  Method -> {"TransparentPolygonMesh" -> True}, 
  PlotRangePadding -> None], 
 RegionPlot[{g[x, y] > h[x, y], g[x, y] < h[x, y]}, {x, -2, 
   1}, {y, -2, 1}, MaxRecursion -> 6, 
  PlotStyle -> {{Opacity[.5], Red}, {Opacity[.25], Blue}}, 
  BoundaryStyle -> None]]

enter image description here

$\endgroup$
  • $\begingroup$ Assuming an example where there is no divergence (I should've picked up a better example) how would you get nice results with DensityPlot? $\endgroup$ – OldTomMorris Apr 24 '16 at 8:16
  • $\begingroup$ yes you should :P I added my own function and color mix. Finding nice colors is not easy though. $\endgroup$ – tsuresuregusa Apr 24 '16 at 13:10
  • 1
    $\begingroup$ "Finding nice colors" - it's more an art than a science, you see. :) $\endgroup$ – J. M. will be back soon Apr 24 '16 at 13:23

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.