# Density Plot: Colour Corresponding to Different Parts of a Function

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]


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."

• 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? – tsuresuregusa Apr 23 '16 at 22:41
• 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. – OldTomMorris Apr 23 '16 at 23:04
• 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]? – tsuresuregusa Apr 23 '16 at 23:11
• forget the invert part, unless you only want to plot only one quadrant. Is that the actual function you want to plot? – tsuresuregusa Apr 23 '16 at 23:18
• @tsuresuregusa No, this is just an example function.The function I want to plot is well behaved. – OldTomMorris Apr 24 '16 at 8:14

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


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]


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


• 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? – OldTomMorris Apr 24 '16 at 14:29
• @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. – kglr Apr 24 '16 at 14:33
• 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. – OldTomMorris Apr 26 '16 at 19:33

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


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