2016-09-07 131 views
1

我有一個國家和它們發生的次數的元組列表。我有175個國家都有很長的名字。Matplotlib條形圖定製多個值

當我製作圖表,我得到:

enter image description here

正如你所看到的,一切都非常揉成了,沒有空間,你可以勉強讀什麼。

代碼我使用(原始數據文件是巨大的,但是這包含了我matplotlib具體代碼):

def tupleCounts2Percents(inputList): 
    total = sum(x[1] for x in inputList)*1.0 
    return [(x[0], 1.*x[1]/total) for x in inputList] 

def autolabel(rects,labels): 
# attach some text labels 
    for i,(rect,label) in enumerate(zip(rects,labels)): 
     height = rect.get_height() 
     plt.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
      label, 
      ha='center', va='bottom',fontsize=6,style='italic') 

def countryChartList(inputlist,path): 
    seen_countries = Counter() 

    for dict in inputlist: 
     seen_countries += Counter(dict['location-value-pair'].keys()) 

    seen_countries = seen_countries.most_common() 

    seen_countries_percentage = map(itemgetter(1), tupleCounts2Percents(seen_countries)) 
    seen_countries_percentage = ['{:.2%}'.format(item)for item in seen_countries_percentage] 

    yvals = map(itemgetter(1), seen_countries) 
    xvals = map(itemgetter(0), seen_countries) 

    plt.figure() 
    countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9) 
    plt.xticks(range(len(seen_countries)), xvals,rotation=90) 

    plot_margin = 0.25 
    x0, x1, y0, y1 = plt.axis() 
    plt.axis((x0, 
       x1, 
       y0, 
       y1+plot_margin)) 

    plt.title('Countries in Dataset') 
    plt.xlabel('Countries in Data') 
    plt.ylabel('Occurrences') 

    plt.tick_params(axis='both', which='major', labelsize=6) 
    plt.tick_params(axis='both', which='minor', labelsize=6) 
    plt.tight_layout() 

    autolabel(countrychart,seen_countries_percentage) 

    plt.savefig(path) 
    plt.clf() 

什麼,我是在外觀還是喂字典喜歡的一個想法是:

list = [ 
    { 
     "location-value-pair": { 
      "Austria": 234 
     } 
    }, 
    { 
     "location-value-pair": { 
      "Azerbaijan": 20006.0 
     } 
    }, 
    { 
     "location-value-pair": { 
      "Germany": 4231 
     } 
    }, 
    { 
     "location-value-pair": { 
      "United States": 12121 
     } 
    }, 
    { 
     "location-value-pair": { 
      "Germany": 65445 
     } 
    }, 
    { 
     "location-value-pair": { 
      "UK": 846744 
     } 
    } 
} 
] 

我如何:

  1. 讓事情可以閱讀它們 - 答案是一個直方圖與箱而不是酒吧情節?也許每隔10%就會走?
  2. 我該如何做到這一點,所以勾號標籤和條上方的標籤(百分比)不重疊?
  3. 歡迎任何其他見解(例如,帶漸變色的酒吧,紅色到黃色)?

編輯

我的國家的數目減少到只有排名前50位,制杆更加透明,改變由蜱旋轉45度。我仍然發現第一欄有一個勾號,它跨過y軸而不可讀。我該如何改變這一點?

enter image description here

改爲countrychart = plt.bar(range(len(seen_countries)), yvals, width=0.9,alpha=0.6)rotation=45autolabel功能.text()說法。

+1

@凱爾我已經90旋轉度 - 雖然 - 看代碼:) –

回答

1

的問題是在autolabels的對齊方式:

def autolabel(rects,labels): 
# attach some text labels 
    for i,(rect,label) in enumerate(zip(rects,labels)): 
     height = rect.get_height() 
     plt.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
      label, 
      ha='center', va='bottom',fontsize=6,style='italic') 

改爲:

def autolabel(rects,labels): 
# attach some text labels 
    for i,(rect,label) in enumerate(zip(rects,labels)): 
     height = rect.get_height() 
     plt.text(rect.get_x() + rect.get_width()/2., 1.05*height, 
      label, 
      ha='left', va='bottom',fontsize=6,style='italic', rotation=45) 

要獲取:

enter image description here