2016-10-11 111 views
3

Python新手,堅持使用餅圖。 道歉的複雜性,但我在丟失如何進行.. 我有這樣的數據集,字典的形式(的一部分)如何創建餅圖?

{'Deaths5': 94, 'Deaths10': 379, 'Deaths12': 388, 'Deaths8': 138, 'Deaths25': None, 
'IM_Deaths2': None, 'Deaths14': 511, 'Deaths1': 20535, 'Deaths23': 2643, 'Deaths6': 62, 
'IM_Deaths1': 4349, 'Deaths17': 1036, 'Deaths18': 1234, 'Sex': '2', 'Deaths11': 358, 'Deaths22': 1708, 
'Deaths21': 1922, 'IM_Frmat': '08', 'SubDiv': '', 'Deaths15': 600, 'Deaths4': 157, 'Admin1': '', 
'IM_Deaths3': None, 'Deaths19': 1125, 'Deaths24': None, 'Frmat': '01', 'Deaths20': 1602, 'Deaths3': 350, 
'Year': '1964', 'Deaths7': 149, 'Deaths9': 311, 'Deaths26': 33, 'Country': '2150', 
'Deaths16': 932, 'Deaths13': 454, 'Deaths2': 4349, 'IM_Deaths4': None, 'Cause': 'A000', 'List': '07A' ....... 

我需要生成一個餅圖,其中顯示了最近一年 - 2013年, 和顯示前8原因的死亡代碼「原因」從外地「Deaths1」

所以總結起來:

因此,例如數據應該被過濾爲

Year CAUSE Top8 
2013  A000 5000 
2013  A411 400 
2013  A50  200 
..... 

,然後顯示爲與任何一個餅圖作爲治療前8後「其他」

我可以用SQL,但與Python做到這一點很容易......我不知道。

+0

爲什麼是高圖表標籤?不同的編程語言是啊? – AER

+0

對不起,它應該說列表/字典不是數據庫 –

回答

1

可以使用Matplotlib在Python中創建餅圖

例餅圖: -

import matplotlib.pyplot as plt 

labels = 'A', 'B', 'C', 'D' 
sizes = [40, 20, 20, 20] 
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral'] 
explode = (0, 0.1, 0, 0) 
plt.pie(sizes, explode=explode, labels=labels, colors=colors, 
     autopct='%1.1f%%', shadow=True, startangle=90) 
plt.axis('equal') 
plt.title('Year 2013') 
plt.show() 
4

充分披露,我是ZingChart團隊的一員。

您可以免費使用ZingChart來完成此操作。我不確定您是否在尋找答案,包括如何解析字典或數據可視化部分。通過一些簡單的屬性,我們可以以清晰的方式顯示數據。從那裏我們可以懸停節點來獲得更多關於節點的信息,我們可以點擊圖例從圖中刪除一個節點。這將重新計算剩餘的非隱藏節點中佔用的每個節點佔用的百分比。

var myConfig = { 
 
    \t type: 'pie', 
 
    \t title:{ 
 
    \t text: '2013 Deaths', 
 
    \t adjustlayout: true 
 
    \t }, 
 
    \t legend:{ 
 
    \t toggleAction: 'remove' 
 
    \t }, 
 
    \t plot:{ 
 
    \t valueBox:{ // hard label 
 
    \t  placement:'out' 
 
    \t } 
 
    \t }, 
 
    \t tooltip:{ // for node hover 
 
    \t text:'%t: Had %v deaths in 2013' 
 
    \t }, 
 
\t series: [ 
 
\t \t { 
 
\t \t \t values: [5000], 
 
\t \t \t text: 'A000' 
 
\t \t }, 
 
\t \t { 
 
\t \t \t values: [400], 
 
\t \t \t text: 'A411' 
 
\t \t }, 
 
\t \t { 
 
\t \t \t values: [200], 
 
\t \t \t text: 'A00' 
 
\t \t }, 
 
\t \t { 
 
\t \t \t values: [900], 
 
\t \t \t text: 'Other' 
 
\t \t } 
 
\t ] 
 
}; 
 

 
zingchart.render({ 
 
\t id: 'myChart', 
 
\t data: myConfig, 
 
\t height: '100%', 
 
\t width: '100%' 
 
});
html, body { 
 
\t height:100%; 
 
\t width:100%; 
 
\t margin:0; 
 
\t padding:0; 
 
} 
 
#myChart { 
 
\t height:100%; 
 
\t width:100%; 
 
\t min-height:150px; 
 
}
<!DOCTYPE html> 
 
<html> 
 
\t <head> 
 
\t <!--Assets will be injected here on compile. Use the assets button above--> 
 
\t \t <script src= "https://cdn.zingchart.com/zingchart.min.js"></script> 
 
\t \t <script> zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/"; 
 
\t \t ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9","ee6b7db5b51705a13dc2339db3edaf6d"];</script> 
 
\t <!--Inject End--> 
 
\t </head> 
 
\t <body> 
 
\t \t <div id="myChart"></div> 
 
\t </body> 
 
</html>

+0

看起來不錯,但我需要通過蟒蛇做這 –

+0

您好,我很抱歉,我沒有發佈一個解決方案在python。你可以減少答案。我唯一的Python解決方案是通過我們的在線演示回購。 https://github.com/zingchart-demos/python-django – nardecky