2015-06-20 46 views
0

我试图减少我的代码足迹,使我更容易理解,当我偶然发现一个奇怪的结果时。如果我运行下面的代码:压缩ticklines不允许更改它们的属性

y1 = np.linspace(1,1000) 
y2 = np.linspace(10,1) 

c = ["b","g"] 
fig, ax = plt.subplots() 

ax.plot(y1, c[0]) 
ax2 = ax.twinx() 
ax2.plot(y2, c[1]) 

for tl in ax.yaxis.get_ticklines(): 
    tl.set_color(c[0]) 

for ts in ax.yaxis.get_ticklabels(): 
    ts.set_color(c[0]) 

我得到以下输出:

enter image description here

然而,当我尝试压缩此使用,以减少下面的代码量:

for t in zip(ax.yaxis.get_ticklines(),ax.yaxis.get_ticklabels()): 
    t[0].set_color(c[0]),t[1].set_color(c[0]) 

enter image description here

所有的tickla bels会改变,但只有一些ticklines(那些不改变的会被标记为红色)。是否有一些理由说明,为什么只有一些压缩ticklines会导致更改?

回答

1

我猜,ax.yaxis.get_ticklines()的数量是ax.yaxis.get_ticklabels()的两倍,所以在绘制全部图片之前,拉链只停下来,而单独的循环都很好。

此行为的zipPython documentation中进行了说明。