Search code examples
pythonmatplotlibbackgroundplot-annotations

Is there a way to put the data in front of a label?


It seems that the labels in Matplotlib always are shown on the most top overlay, covering the data behind it.

Example:

enter image description here

The plot has 2 text annotations in red. The bottom one is overlayed by a gray "Meteo" text.

Is there a way to move the red annotation above the gray text label?

I was playing with the zorder but with no success.


Solution

  • It works properly, but not in a way You might expect.

    Every element has it's zorder. Even ax has default zorder=0. It means, that all elements within that, let's call it a layer, will have final zorder=0.

    But when ax layer is drawn, zorder is applied to every element respectively and are placed correctly in the view from bottom to the top.
    So now You have ax layer rendered with final zorder=0.

    Then You are adding text with fig.text. Which adds text to the figure layer. So now You have two objects to render; ax layer with zorder = 0 and text with zorder=0 as well. You can set text zorder=-1 which will put text below ax layer, but it will be invisible if ax has solid background. Try to add another text in the fig layer with higher or lower zorder and it will be properly drawn on top or below another text element.