# How to combine density plot and a given image?

I have a sine plot and image, and I want to combine the two on 2D basis and 3D basis. I tried below:-

plotF = DensityPlot[Sin[8*x]*Sin[8*y], {x, 0, 1}, {y, 0, 1}];
image = ExampleData[{"TestImage", "Moon"}];
plotF3D = Plot3D[0, {x, 0, 1}, {y, 0, 1}, PlotRange -> {{0, 1}, {0, 1}, {0, 1}}, PlotStyle -> Texture[plotF], BoxRatios -> {1, 1, 1}];
plotImage3D = Plot3D[0, {x, 0, 1}, {y, 0, 1}, PlotRange -> {{0, 1}, {0, 1}, {0, 1}}, PlotStyle -> Texture[image], BoxRatios -> {1, 1, 1}];
combine2D = Show[plotF, image];
combine3D = Show[plotF3D, plotImage3D];
{{plotF, image, combine2D}, {plotF3D, plotImage3D, combine3D}} // Column

As you can see, both 2D and 3D basis failed. For 2D, the combined plot become a pure grey square. For 3D, the combined plot doesn't reflect the sine plot.

How can I get it done? Many thanks!

For the 2D case, I've tried this (which is similar to the output that I want):-

and then I got this:-

But as you can see, the sizes of the plots are still unmatched...

• What is the expected result? There are different ways of combining images. Jun 17, 2018 at 22:37
• Hi, the expected result for the 2D case is to seeing both the "sine plot" and the image within the same plot range, mixing each other. For the 3D case, the output is similar, just at the botton of the plot box.
– H42
Jun 17, 2018 at 23:51
• So, how should they mix with each other? Think of the sine plot as a matrix of pixel values, and the image as a matrix of pixel values. How do you take two corresponding pixels and produce a new pixel value? Jun 18, 2018 at 0:19
• For the 2D case, is it possible to do something like ImageAdd[ImageMultiply[plotF, 0.5], ImageMultiply[image, 0.5]]? But I am unable to modify the size of the plots to make their sizes match...
– H42
Jun 18, 2018 at 1:22
• I wrote up something, feel free to tell me if I'm missing the point. Maybe it is necessary to combine a plot and an image in your application. Jun 18, 2018 at 1:43

you mean something like this?

image = ExampleData[{"TestImage", "Moon"}];

plotF = DensityPlot[Sin[8*x]*Sin[8*y], {x, 0, 1}, {y, 0, 1},
ImageSize -> ImageDimensions[image], Axes -> False, Frame -> None];
plotF3D =
Plot3D[0, {x, 0, 1}, {y, 0, 1},
PlotRange -> {{0, 1}, {0, 1}, {0, 1}},
PlotStyle ->
ImageMultiply[image, 0.5]]], BoxRatios -> {1, 1, 1},
Mesh -> None]

• Thanks for reply. But there's still one problem. There's a margin and thus they are still not perfectly matched. I tried several approaches to resize any of them but still can't remove the margin. Thus there will be errors in value in the overlapped plot especially for some function much more precise than Sine. Is that possible to get rid of the margin as well?
– H42
Jun 18, 2018 at 17:08

Thinking about this as a plotting problem or an image processing problem, as in your comment, may not be the best approach for this situation. You might instead consider the image as a matrix of pixel values. You could do something like this:

{xdim, ydim} = ImageDimensions[image];
f[{x_, y_}] := Sin[8 (x - 1)/(xdim - 1)] Sin[8 (y - 1)/(ydim - 1)]
MapIndexed[0.5 # + 0.5 f[#2] &, ImageData[image], {2}] // Image

If you want colors as well, then adapt 0.5 # + 0.5 f[#2] & to return a list with the values, one for each color channel. It can be done by passing the value to a color function given by ColorData.

• also {xdim, ydim} = ImageDimensions[image]; f[a_, {x_, y_}] := 0.5 a + 0.5*Sin[8 (x - 1)/(xdim - 1)] Sin[8 (y - 1)/(ydim - 1)] ImageApplyIndexed[f[#1, #2] &, image] Jun 18, 2018 at 2:21