簡體   English   中英

如何創建餅圖?

[英]How to create pie chart?

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 年,並顯示字段“Deaths1”中的前 8 位死因代碼“Cause”

所以總結一下:

因此,例如數據應過濾為

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

然后顯示為餅圖,前 8 名之后的任何內容都被視為“其他”

我可以用 SQL 很容易地做到這一點,但用 Python...我不確定。

完全公開,我是 ZingChart 團隊的成員。

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

 var myConfig = { type: 'pie', title:{ text: '2013 Deaths', adjustlayout: true }, legend:{ toggleAction: 'remove' }, plot:{ valueBox:{ // hard label placement:'out' } }, tooltip:{ // for node hover text:'%t: Had %v deaths in 2013' }, series: [ { values: [5000], text: 'A000' }, { values: [400], text: 'A411' }, { values: [200], text: 'A00' }, { values: [900], text: 'Other' } ] }; zingchart.render({ id: 'myChart', data: myConfig, height: '100%', width: '100%' });
 html, body { height:100%; width:100%; margin:0; padding:0; } #myChart { height:100%; width:100%; min-height:150px; }
 <!DOCTYPE html> <html> <head> <!--Assets will be injected here on compile. Use the assets button above--> <script src= "https://cdn.zingchart.com/zingchart.min.js"></script> <script> zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/"; ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9","ee6b7db5b51705a13dc2339db3edaf6d"];</script> <!--Inject End--> </head> <body> <div id="myChart"></div> </body> </html>

您可以使用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()

你可以試試Seaborn's餅圖。 讓我們看一個如何使用餅圖來可視化著名的鳶尾花數據的示例。

您所要做的就是導入庫並使用它:

import seaborn as sns

首先,這是由head()方法檢索到的數據集的前 5 行: 在此處輸入圖片說明

數據集有 3 個類: 在此處輸入圖片說明

現在,我想將類繪制為餅圖

dataset['Class'].value_counts().plot.pie(explode=[0.05, 0.05,0.05], autopct='%1.1f%%', shadow=True, figsize=(8,8))
plt.title('Pie Chart for Class')
plt.show()

瞧!

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM