2014-04-10 138 views
1

我想繪製使用matplotlib的散點圖,我得到「IndexError:從空列表中彈出」的錯誤,我不知道如何解決它。在matplotlib中繪製更多顏色

import matplotlib.pyplot as plt 
import matplotlib 
import numpy as np 
import time 
import itertools 

d = {'5000cca229d10d09': {374851: 1}, '5000cca229cf3f8f': {372496:3},'5000cca229d106f9': {372496: 3, 372455: 2}, '5000cca229d0b3e4': {380904: 2, 380905: 1, 380906: 1, 386569: 1}, '5000cca229d098f8': {379296: 2, 379297: 2, 379299: 2, 379303: 1, 379306: 1, 379469: 1, 379471: 1, 379459: 1, 379476: 1, 379456: 4, 379609: 4}, '5000cca229d03957': {380160: 3, 380736: 3, 380162: 1, 380174: 1, 381072: 2, 379608: 2, 380568: 3, 380569: 1, 380570: 1, 379296: 3, 379300: 1, 380328: 3, 379306: 1, 380331: 1, 379824: 2, 379825: 1, 379827: 1, 380344: 1, 379836: 1, 379456: 3, 380737: 1, 380739: 1, 379462: 1, 379476: 1, 379992: 3, 379609: 1, 379994: 1, 379611: 1, 379621: 1, 380006: 1, 380904: 3, 380905: 1, 380907: 1, 380535: 3, 380536: 1, 380538: 1}, '5000cca229cf6d0b': {372768: 10, 372550: 15, 372616: 14, 372617: 20, 372653: 3, 372505: 2}, '5000cca229cec4f1': {372510: 132}} 
colors = list("rgbcmyk") 


for data_dict in d.values(): 
    x = data_dict.keys() 
    #print x 
    #X= time.asctime(time.localtime(x)) 
    y = data_dict.values() 
    #plt.scatter(x,y,color=colors.pop(),s = 60) 
    plt.scatter(x,y,color=colors.pop(),s = 90, marker='^') 
    plt.ylabel("Errors" , fontsize=18, color="Green") 
    plt.xlabel("Occured on",fontsize=18, color="Green") 
    plt.title("DDN23b", fontsize=25, color="Blue") 
    plt.gca().get_xaxis().get_major_formatter().set_useOffset(False) 
    plt.xticks(rotation='vertical') 
    #plt.ylim(min(y),max(y)) 
    #plt.grid() 

#for x, y in dict(itertools.chain(*[item.items() for item in d.values()])).items(): 
# plt.text(x, y, time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(x*3600)),  ha='center', va='top', rotation='vertical', fontsize = '11', fontstyle = 'italic', color = '#844d4d') 

plt.xticks(plt.xticks()[0], [time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(item))  for item in plt.xticks()[0]*3600]) 
plt.legend(d.keys()) 
mng = plt.get_current_fig_manager() 
mng.resize(*mng.window.maxsize()) 
plt.subplots_adjust(bottom=.24,right=.98,left=0.03,top=.89) 
plt.grid() 
plt.show() 

我有幾個d的數據集,d是一個字典。當數據集較小時,其工作沒有任何錯誤。當數據集很大時,它會用完衣領。如何在列表中添加更多顏色,以便「d」中的每個鍵獲得自己的顏色。

隨意編輯我的代碼並提出建議。

+0

嗯......讓你'colors'的字符串長? –

回答

3

Colormaps可調用。當傳遞0和1之間的浮動,它將返回一個RGBA顏色:

In [73]: jet = plt.cm.jet 

In [74]: jet(0.5) 
Out[74]: (0.49019607843137247, 1.0, 0.47754585705249841, 1.0) 

所以,你可以通過NumPy的陣列np.linspace(0, 1, len(d))傳遞給色彩映射表生成len(d)數量的顏色:

jet = plt.cm.jet 
colors = jet(np.linspace(0, 1, len(d))) 

顏色選定的圖案將沿色彩圖漸變等距排列。


import matplotlib.pyplot as plt 
import numpy as np 
import time 

d = {'5000cca229d10d09': {374851: 1}, '5000cca229cf3f8f': {372496:3},'5000cca229d106f9': {372496: 3, 372455: 2}, '5000cca229d0b3e4': {380904: 2, 380905: 1, 380906: 1, 386569: 1}, '5000cca229d098f8': {379296: 2, 379297: 2, 379299: 2, 379303: 1, 379306: 1, 379469: 1, 379471: 1, 379459: 1, 379476: 1, 379456: 4, 379609: 4}, '5000cca229d03957': {380160: 3, 380736: 3, 380162: 1, 380174: 1, 381072: 2, 379608: 2, 380568: 3, 380569: 1, 380570: 1, 379296: 3, 379300: 1, 380328: 3, 379306: 1, 380331: 1, 379824: 2, 379825: 1, 379827: 1, 380344: 1, 379836: 1, 379456: 3, 380737: 1, 380739: 1, 379462: 1, 379476: 1, 379992: 3, 379609: 1, 379994: 1, 379611: 1, 379621: 1, 380006: 1, 380904: 3, 380905: 1, 380907: 1, 380535: 3, 380536: 1, 380538: 1}, '5000cca229cf6d0b': {372768: 10, 372550: 15, 372616: 14, 372617: 20, 372653: 3, 372505: 2}, '5000cca229cec4f1': {372510: 132}} 

jet = plt.cm.jet 
colors = jet(np.linspace(0, 1, len(d))) 

fig, ax = plt.subplots() 
for color, data_dict in zip(colors, d.values()): 
    x = data_dict.keys() 
    y = data_dict.values() 
    ax.scatter(x,y,color=color, s = 90, marker='^') 
    plt.ylabel("Errors" , fontsize=18, color="Green") 
    plt.xlabel("Occured on",fontsize=18, color="Green") 
    plt.title("DDN23b", fontsize=25, color="Blue") 
    ax.get_xaxis().get_major_formatter().set_useOffset(False) 
    plt.xticks(rotation='vertical') 


plt.xticks(plt.xticks()[0], 
      [time.strftime("%m/%d/%y, %H:%M:%S", time.localtime(item)) 
      for item in plt.xticks()[0]*3600]) 
plt.legend(d.keys()) 
plt.subplots_adjust(bottom=.24,right=.98,left=0.03,top=.89) 
plt.grid() 
plt.show() 

enter image description here