Cómo superponer dos ReliefPlots u otros gráficos

Is it possible to overlay two or more graphics in Mathematica, if the graphics are generated by functions such as ReliefPlot or DensityPlot, using Opacity to control the appearance?

Por ejemplo:

a = ReliefPlot[
Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}], ImageSize -> 100]
b = ReliefPlot[
Table[i + Sin[i^3 + j^3], {i, -4, 4, .03}, {j, -4, 4, .03}], ImageSize -> 100]
Show[a,b]

combines the two, but I can't work out how to insert an Opacity command anywhere here such that both are visible. The documentation states that these functions accept the same options as Graphics ("ReliefPlot has the same options as Graphics, with the following additions and changes:"), but I don't understand how to control the graphics... (And I may be confused about the difference between graphics options and directives, as well.)

Enlightenment - and less opacity - very welcome!

Edit: Wow, you guys are quicker than my version of Mathematica - thanks!

preguntado el 09 de noviembre de 11 a las 15:11

It's hard to choose, sometimes... :) -

I think Yoda's is more general, and allows for finer control over styles. -

And the styles need be specified just once, I suppose. -

5 Respuestas

You'll have to issue the opacity directive to ColorFunction al igual que:

a = ReliefPlot[
Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}],
ImageSize -> 100]
b = ReliefPlot[
Table[i + Sin[i^3 + j^3], {i, -4, 4, .03}, {j, -4, 4, .03}],
ImageSize -> 100,
ColorFunction -> (Directive[Opacity[0.5],
ColorData["Rainbow"][#]] &)]
Show[a, b]

In general, in all *Plot* functions, you control opacity with either PlotStyle or ColorFunction, as the case may be. If this were just a Graphics primitive, you'd probably do something like Graphics[{Opacity[0.5], object}].

respondido 09 nov., 11:20

Como ReliefPlot no tiene un PlotStyle opción, puedes usar BaseStyle -> Opacity[0.5] to introduce transparency into the graphics.

respondido 09 nov., 11:20

hold on... this works , de manera if both images have the same BaseStyle. - ABCD

@yoda BaseStyle es una opción para Graphics, so you get all the usual behaviors of how Show combines options from multiple graphics. Now that I think about this for a moment more, you could just apply the BaseStyle opción en la llamada a Show, rather than adding it to the individual plots. Note that BaseStyle will apply to the entire graphic; not just the primitives from either a or b. - Campeón de Brett

Yeah, that's what I was pointing out. If you wanted to overlay the second plot with 0.5 opacity on the first like in my answer, you cannot use BaseStyle... - ABCD

An alternative is to work with Images y el ReliefImage function, and then compose the resulting images together using ImageCompose:

ImageCompose[
ReliefImage[Table[i + Sin[i^2 + j^2], {i, -4, 4, .03}, {j, -4, 4, .03}]],
{ReliefImage[Table[i + Sin[i^3 + j^3], {i, -4, 4, .03}, {j, -4, 4, .03}]],
0.5}
]

Como ReliefPlot also essentially returns datos de píxeles en un parche de Graphics-compatible format, perhaps Images will suit you better.

The default colour function of ReliefImage is different: you can use ColorFunction -> "LakeColors" para cambiar a ReliefPlotes uno.

Originally I had a function here to extract the raster data from ReliefPlot, but then Brett Champion pointed to RasterImage in the comment below

respondido 09 nov., 11:20

Assuming V8, you could also use ReliefImage en lugar de ReliefPlot, although then you have to reverse the input due to differences in coordinate systems, and there's also a different default color function. (The default color function for ReliefImage is a bit dark for my tastes, but ImageAdd[a,b] looks decent.) - Campeón de Brett

@Brett I was looking for that but couldn't find it!! (Some Linux interfaces are really unwieldy and frustrating ...) Will correct the answer. - Szabolcs

The answers using transparency will work in a very general way, but in this particular example of combining two ReliefPlot[]s, you might want to consider plotting the sum of the two:

f[i_] := i + Sin[i^2 + j^2];
g[i_] := i + Sin[i^3 + j^3];
ReliefPlot[Table[f[i] + g[i], {i, -4, 4, .03}, {j, -4, 4, .03}], ImageSize -> 100]

respondido 09 nov., 11:21

Thanks - it's a better solution in some ways! One of the good things about Mathematica is how it allows you to approach problems from various angles. - cormullion

Show[a, {Opacity[0.5],#}& /@ b]

respondido 09 nov., 11:21

+1. Alternatively, one can use Show[a, {Opacity[0.5], #} & @@@ b] con el mismo efecto. - Alexey Popkov

@jibe Which Mathematica version do you use? I just have checked both solutions with version 10.2 on Win7 x64 and the output is visually the same. Please check again starting from the code in the question. - Alexey Popkov

@AlexeyPopkov You're absolutely right. On Mac OS 10.8.5, with MMA 10.2, the examples of the OP with ReliefPlot does work. BUT, the test I made was with (contour) plots of my own and it did not work. Can you try to run this (without the ;, but I don't manage to indent in the comments...) ? a = ContourPlot[Sin[x + y], {x, 0, 1}, {y, 0, 1}]; b = ContourPlot[Cos[x - y], {x, 0, 1}, {y, 0, 1}]; Show[a, {Opacity[0.5], #} & /@ b]; Show[a, {Opacity[0.5], #} & @@@ b]; - pulla

@jibe ContourPlot produce un Graphics expression with entirely different structure than ReliefPlot. You can see this structure by applying InputForm or by using my specially-designed shortInputForm función. The code in my comment was intended only for ReliefPlot and based on undocumented details of current implementation of ReliefPlot (which can be changed in future versions of Mathematica). - Alexey Popkov

@jibe For ContourPlots an analogous solution would be Show[a, b] /. RGBColor[r_, g_, b_] :> RGBColor[r, g, b, .5], but its a kind of a hack which does not cover all the cases, so the straightforward solution would be to add Opacity mediante el ContourShading or ColorFunction opción. - Alexey Popkov

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.