指定 matplotlib 层的顺序
Specifying the order of matplotlib layers
假设我 运行 以下脚本:
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 为我选择?
我不知道为什么 zorder
有这种行为,这很可能是一个错误,或者至少是一个记录不当的功能。这可能是因为在您构建绘图(如网格、轴等...)时以及当您尝试为元素指定 zorder
时,已经自动引用了 zorder
重叠他们。这无论如何都是假设。
为了解决您的问题,只需夸大 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()
,结果是:
对于这个情节,我指定了与你的问题相反的顺序。
图层按照相应调用绘图函数的相同顺序从下到上堆叠。
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()
从下图中可以清楚地看出,地块是按照底部在前,顶部在后规则排列的。
虽然 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()
假设我 运行 以下脚本:
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 为我选择?
我不知道为什么 zorder
有这种行为,这很可能是一个错误,或者至少是一个记录不当的功能。这可能是因为在您构建绘图(如网格、轴等...)时以及当您尝试为元素指定 zorder
时,已经自动引用了 zorder
重叠他们。这无论如何都是假设。
为了解决您的问题,只需夸大 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()
,结果是:
对于这个情节,我指定了与你的问题相反的顺序。
图层按照相应调用绘图函数的相同顺序从下到上堆叠。
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()
从下图中可以清楚地看出,地块是按照底部在前,顶部在后规则排列的。
虽然 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()