2016-08-19 108 views
0

请不要太苛刻。 有人可以解释为什么这两个地块的酒吧颜色如此不同吗?酒吧阴谋宽度不一致

import numpy as np 
import datetime as dt 
import matplotlib.pyplot as plt 
from matplotlib import rcParams 

rcParams.update({'figure.autolayout': True}) 
plt.style.use('ggplot') 

def plot3(): 
    bal=np.cumsum(ret) 
    ind = np.arange(len(ret)) 
    fig, ax = plt.subplots() 
    barlist=ax.bar(ind,ret,label="Return") 
    ax.plot(ind,bal,color='b',label="Balance") 
    for i in ind: 
     if ret[i]>=0: 
      barlist[i].set_color('g') 
     else: 
      barlist[i].set_color('r') 
    ax.legend(loc='best',frameon=False) 
    plt.show() 

def plot3b(): 
    bal=np.cumsum(ret) 
    ind = np.arange(len(ret)) 
    fig, ax = plt.subplots() 
    colors=['g' if r>=0 else 'r' for r in ret] 
    ax.bar(ind,ret,color=colors,label="Return") 
    ax.plot(ind,bal,color='b',label="balance") 
    ax.legend(loc='best',frameon=False) 
    plt.show() 

在给定的

n=100 
ret=np.random.randn(n) 
ret=np.insert(ret,0,0) 

我的笔记本电脑的地块分别

plot3

plot3b

回答

0

酒吧有facecoloredgecolor,请参阅文档here

似乎set_color()边缘和酒吧的脸上变色,所以你的第一个图表中的条是更广泛的,在你的第二个,在那里边的颜色尚未设置

如果更改:
ax.bar(ind,ret,color=colors,label="Return")

要:
ax.bar(ind,ret,color = colors, edgecolor = colors, label="Return")

...那么这两个地块是相同的:

plot3() enter image description here

plot3b() enter image description here

请原谅颜色/颜色的这个职位的不同的拼法。我在英国,拼写颜色时没有'u'就感觉不对,所以我没有引用函数参数时拼写“正确”*。

*这是幽默,顺便说一句。所以请不要开始美国/英国的英国战争。