如何创建饼图?
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 名之后的任何内容都被视为 'other'
我可以用 SQL 很容易地做到这一点,但是用 Python...我不确定。
您可以使用 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()
坦白说,我是 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>
你可以试试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()
瞧!
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 名之后的任何内容都被视为 'other'
我可以用 SQL 很容易地做到这一点,但是用 Python...我不确定。
您可以使用 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()
坦白说,我是 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>
你可以试试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()
瞧!