假设我运行以下脚本:
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()这会产生以下结果:

我如何指定层的从上到下的顺序,而不是让Python为我挑选?
发布于 2016-05-16 18:15:33
我不知道为什么zorder会有这种行为,这很可能是一个错误,或者至少是一个糟糕的文档功能。这可能是因为在构建绘图时已经有了对zorder的自动引用(如网格、轴等)。当您尝试为元素指定zorder时,您可能会以某种方式将它们重叠。这在任何情况下都是假设的。
为了解决你的问题,只需夸大zorder中的差异即可。例如,将其设置为0,5,10,而不是0,1,2
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(),结果如下:

对于这个图,我指定了与您的问题相反的顺序。
发布于 2019-10-25 22:32:40
尽管Tonecha根据调用绘图的顺序,默认顺序是从后到前是正确的,但应该注意的是,使用其他绘图工具(散点、errorbar等)默认顺序不是那么清晰。
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()

发布于 2016-05-16 23:26:03
这些层按照调用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()从下图可以清楚地看出,这些图是按照bottom first,top last规则排列的。

https://stackoverflow.com/questions/37246941
复制相似问题