# Place Legend above Epilog

I am currently struggling with a problem which I haven't seen answered anywhere else.

I want to make a ListDensityPlot graph in which I need to draw some rectangles above the graph (I used Epilog, no problem here), but I also want to place the legend over that Epilog. However, when I do it (using Placed), the legend lays behind the Epilog, and thus is invisible...

Is there any way I can solve this?

If you need any other details, I can provide them.

EDIT: what I want is something like this:

f[x_, y_] := x^2 + y^2

fTab = Flatten[Table[{x, y, f[x, y]}, {x, -1, 1, 0.1}, {y, -1, 1, 0.1}], 1];

Rec = Epilog -> {Directive[LightGray],Rectangle[{-1, -0}, {-0.5, -1}],Rectangle[{-0.5, -1}, {0.5, -0.5}],Rectangle[{1, 0}, {0.5, -1}]};

Leg = PlotLegends -> Placed[BarLegend[{"Rainbow", {-1, 1}}, LegendLayout -> "ReversedRow", LegendMarkerSize -> 200, LegendLabel -> Placed["f(x,y)", Left], LabelStyle -> Directive[FontSize -> 15]], {0.5, 0.1}]

ListDensityPlot[fTab, ColorFunction -> "Rainbow", Evaluate[Rec], Evaluate[Leg]]

• Could you please provide the code you are using so it would be a bit clearer what you are after? It seems like you could use function Labeled – e.doroskevic Jun 29 '16 at 9:45
• @E.Doroskevic I updated the question with an example of what I need. – E Dias Jun 29 '16 at 10:05
• Just to clarify, you want the legend to be above your ListDensityPlot that's it? – e.doroskevic Jun 29 '16 at 10:10
• Yes, it is exactly that. I want it inside the plot and above the gray rectangles in there. – E Dias Jun 29 '16 at 10:13

Use Show instead of Epilog to add on the rectangles, and the legend will stay on top.
Show[

• @Alan I don't know. In general, Epilog comes on top of the Graphics content. But once you have a legend, you're working with a Legended object that contains Graphics inside, not a bare Graphics. It would appear that Epilog also goes on top of the legend. By using Show, we put the rectangles in the main content instead of Epilog. Show can combine Legended objects as well. It will combine the Graphics within, and use (potentially nested) Legended outside. – Szabolcs Jun 29 '16 at 13:34