2016-05-16 26 views
6

假设我运行下面的脚本命令:指定matplotlib层

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() 

这将产生以下:

enter image description here

如何指定的顶部到底部的顺序层而不是Python为我挑选?

+0

我看到zorder与它有关。但是,我仍然无法按照自己的意愿开展工作。如果我将蓝色,红色和绿色的zorder分别设置为0,1和2,则红色线是最上面的那条线。为什么?? – Phys251

回答

11

我不知道为什么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() 

,从而导致此:

Handling zorder in mataplotlib

对于这个情节我指定你的问题出相反的顺序。

2

图层按照与绘图函数相对应的调用顺序从下到上堆叠。

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() 

它清楚地从下面的是,图是根据底部第一,顶最后规则布置图中出现。

How different plots are stacked