如何用Pandas和这个d绘制饼图

2024-06-09 05:11:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这样的数据

ID   Sex   Smoke
1  female    1 
2    male    0
3   female   1

如何绘制饼图以显示有多少男性或女性吸烟?


Tags: 数据id绘制malefemalesmoke男性女性
3条回答

您可以使用熊猫选择pie图表直接绘制:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'Sex': ['female', 'male', 'female'], 'Smoke': [1, 3, 1]})

df.Smoke.groupby(df.Sex).sum().plot(kind='pie')
plt.axis('equal')
plt.show()

enter image description here

这里有一行:

temp[temp.Smoke==1]['Sex'].value_counts().plot.pie()

假设你从:

import pandas as pd
from matplotlib.pyplot import pie, axis, show

df = pd.DataFrame({
    'Sex': ['female', 'male', 'female'],
    'Smoke': [1, 1, 1]})

你总是可以这样做:

sums = df.Smoke.groupby(df.Sex).sum()
axis('equal');
pie(sums, labels=sums.index);
show()

enter image description here

相关问题 更多 >