Python 指定 matplotlib 层的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37246941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specifying the order of matplotlib layers
提问by Forklift17
Suppose I run the following script:
假设我运行以下脚本:
import matplotlib.pyplot as plt
lineWidth = 20
plt.figure()
plt.plot([0,0],[-1,1], lw=lineWidth, c='b')
plt.plot([-1,1],[-1,1], lw=lineWidth, c='r')
plt.plot([-1,1],[1,-1], lw=lineWidth, c='g')
plt.show()
This produces the following:
这会产生以下结果:
How can I specify the top-to-bottom order of the layers instead of having Python pick for me?
如何指定层的从上到下的顺序,而不是让 Python 为我选择?
回答by armatita
I don't know why zorder
has that behavior and it's likely that might be a bug or, at the very least, a badly documented feature. It might be because there are already automatic references to zorder
when you build a plot (like grid, axis, and so on...) and when you try to specify the zorder
for elements you are somehow overlapping them. This is hypothetical in any case.
我不知道为什么zorder
会有这种行为,这很可能是一个错误,或者至少是一个记录不完整的功能。这可能是因为zorder
当您构建绘图(如网格、轴等...)以及当您尝试指定zorder
for 元素时,您会以某种方式重叠它们。这在任何情况下都是假设的。
For you to solve your problem just make the differences in zorder
exaggerated. For instance instead of 0,1,2
, make it 0,5,10
:
为你解决你的问题只是把差异zorder
夸大了。例如,而不是0,1,2
,使它0,5,10
:
import matplotlib.pyplot as plt
lineWidth = 20
plt.figure()
plt.plot([0,0],[-1,1], lw=lineWidth, c='b',zorder=10)
plt.plot([-1,1],[-1,1], lw=lineWidth, c='r',zorder=5)
plt.plot([-1,1],[1,-1], lw=lineWidth, c='g',zorder=0)
plt.show()
, which results in this:
,这导致:
For this plot I specified the opposite order shown in your question.
对于这个情节,我指定了您的问题中显示的相反顺序。
回答by Tonechas
The layers are stacked from bottom to top in the same order of the corresponding calls to the plot function.
这些层按照与 plot 函数的相应调用相同的顺序从下到上堆叠。
import matplotlib.pyplot as plt
lineWidth = 30
plt.figure()
plt.subplot(2, 1, 1) # upper plot
plt.plot([-1, 1], [-1, 1], lw=5*lineWidth, c='b') # bottom blue
plt.plot([-1, 1], [-1, 1], lw=3*lineWidth, c='r') # middle red
plt.plot([-1, 1], [-1, 1], lw=lineWidth, c='g') # top green
plt.subplot(2, 1, 2) # lower plot
plt.plot([-1, 1], [-1, 1], lw=5*lineWidth, c='g') # bottom green
plt.plot([-1, 1], [-1, 1], lw=3*lineWidth, c='r') # middle red
plt.plot([-1, 1], [-1, 1], lw=lineWidth, c='b') # top blue
plt.show()
It clearly emerges from the figure below that the plots are arranged according to the bottom first, top lastrule.
从下图中可以清楚地看出,地块是根据底部在前,顶部最后的规则排列的。
回答by Messypuddle
While Tonechas is correct that the default order is back to front based on the order in which plots are called, it should be noted that using other plotting tools (scatter, errorbar, etc.) the default order is not as clear cut.
虽然 Tonechas 是正确的,默认顺序是根据调用绘图的顺序从后到前,但应该注意的是,使用其他绘图工具(散点图、误差条等)时,默认顺序并不明确。
import matplotlib.pyplot as plt
import numpy as np
plt.errorbar(np.arange(0,10),np.arange(5,6,0.1),color='r',lw='3')
plt.plot(np.arange(0,10),np.arange(0,10),'b', lw=3)
plt.show()