首页 > 解决方案 > 如何使用python中的交叉表函数从数据框中绘制饼图

问题描述

我正在尝试使用数据框中 2 列的交叉表函数绘制饼图,到目前为止,我可以使用以下语句绘制条形图。

数据框样本:

在此处输入图像描述

pd.crosstab(df['event_location'],df['event_type']).iplot(kind="bar", bins=20, theme="white", title="Event type over Location",xTitle='location', yTitle='Number of person')

在此处输入图像描述

我的问题是如何将此条形图转换为饼图?

标签: pythonplotlypie-chartcrosstab

解决方案


我猜您正在尝试显示每种事件类型的发生次数。这个简单的代码将帮助您绘制每个位置的饼图。

import matplotlib.pyplot as plt
ct = pd.crosstab(df['event_location'],df['event_type'])
ct.plot.pie(subplots=True)
plt.legend(title='XYZ')
plt.show()

推荐阅读