5
$\begingroup$

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.

Thanks in advance!

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]]
$\endgroup$
4
  • 1
    $\begingroup$ 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 $\endgroup$ Jun 29, 2016 at 9:45
  • $\begingroup$ @E.Doroskevic I updated the question with an example of what I need. $\endgroup$
    – E Dias
    Jun 29, 2016 at 10:05
  • $\begingroup$ Just to clarify, you want the legend to be above your ListDensityPlot that's it? $\endgroup$ Jun 29, 2016 at 10:10
  • $\begingroup$ Yes, it is exactly that. I want it inside the plot and above the gray rectangles in there. $\endgroup$
    – E Dias
    Jun 29, 2016 at 10:13

1 Answer 1

5
$\begingroup$

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

Show[
  ListDensityPlot[..., PlotLegends -> ...]
  Graphics[{...}]
]
$\endgroup$
2
  • $\begingroup$ What is the concept (general rule) behind this ordering? $\endgroup$
    – Alan
    Jun 29, 2016 at 12:47
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Jun 29, 2016 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.