2016-08-19 115 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'就感覺不對,所以我沒有引用函數參數時拼寫「正確」*。

*這是幽默,順便說一句。所以請不要開始美國/英國的英國戰爭。