Strange matplotlib zorder behavior with legend and errorbar

前端 未结 1 1114
清酒与你
清酒与你 2020-12-21 06:50

I encountered a rather strange behavior of legend and the errorbar plot commands. I am using Python xy 2.7.3.1 with matplotlib 1.1.1 The code below exemplifies

相关标签:
1条回答
  • 2020-12-21 07:10

    The created plots are correct according to the code you have posted. The objects with the lowest zorder is placed on the bottom, while the object with the highest zorder is placed on top. The zorder problem you linked to is fixed in matplotlib version 1.2.1, so you should update your install if possible.

    In your first subplot the errorbars are plotted on top of the scatter points because errorbar is called with zorder=2, while scatter is called with zorder=1 - meaning the errorbars will overlay the scatter points.

    In your second subplot, you have called errorbar with zorder=99, scatter with zorder=100 and plot with zorder=101 - meaning that the errorbars will be placed underneath both the scatter points and the line.

    The reason the legend is displayed on top of the line in the first subplot, while it is on top of the same line in the second subplot, is due to the fact that you haven't explicitly set the legend objecta zorder value, meaning it will use its default value (which I believe is 5). To change the legends zorder, simply use P.legend(loc="center").set_zorder(102) where 102 is the desired zorder-value.

    So in order to produce your desired output, you have to set the zorder arguments accordingly. As you haven't described your desired output in your question, it is hard for me to "correct" your code, as I don't know in which order you want the objects to be drawn.

    0 讨论(0)
提交回复
热议问题