2011-03-23 45 views
1

有誰知道爲什麼這段代碼不能正確地繪製框的顏色。我希望每個組件都是不同的顏色,但它們都以黑色的圖例出現。yplot顏色不是預期的

from numpy import array, zeros 
import matplotlib.pyplot as plt 

# Components: Useage times (start, stop), wattage, detail 

COMPONENTS = {"fridge": ([(0.0,24.0)], 35, " Litres"), 
       "kettle": ([(7.3,7.33), (19.0,19.3)], 2500, ""), 
       "netbook": ([(8.0,9.3),(12.0,15.0)], 12.5, ""), 
       "light bulb": ([(18.0,22.0)], 20, "") 
       } 
COLORS = ('b','g','r','c','m','y','k','w') 
PLOT = [] 
TIME = range(24*60) 

Powers = [] # list of array of power for each component 
for key in COMPONENTS.keys(): # each useage 
    p = zeros(len(TIME)) 
    for j in COMPONENTS[key][0]: # start and stop 
     start = round(j[0]*60) 
     end = round(j[1]*60) 
     p[start:end] = COMPONENTS[key][1] 
    Powers.append(p) 

b=zeros(len(TIME)) 
for i in range(len(COMPONENTS.keys())): 
    PLOT.append(plt.bar(TIME,Powers[i],width = 1, color=COLORS[i], bottom=b)) 
    b+=Powers[i] 


plt.ylabel('Power (W)') 
plt.xlabel('Time (h)') ### 
plt.title('Power Cycle') 
plt.xticks(range(0,25*60,60) ,[str(t) for t in range(25)]) 
plt.legend(tuple([i for i in PLOT]), tuple([c for c in COMPONENTS.keys()])) 

plt.show() 
+1

是什麼給了你使用全部大寫變量名的想法?這傷害了我的眼睛! – 2011-03-23 15:03:49

回答

2

就圖表而言,您的代碼很好。問題是有太多的酒吧,所以你只能看到黑色的邊框。這是在變焦時什麼樣子: enter image description here

爲了得到傳說中的權利,通過label參數而繪製,然後只需調用plt.legend()不帶參數時,即可大功告成。

+0

不錯,謝謝 – wookie1 2011-03-23 15:20:17