python分区间饼图,用自己的表里的数据,分为5个区间,0-5,10-25,25-50,50-100,100-1951,计算5个占比百分比并生成饼图的python代码,数据是我自己导入的表格数据,也就是将这些数据分成5类,然后绘制python的蓝色环形饼图百分比代码
时间: 2024-05-02 12:19:48 浏览: 72
好的,以下是满足您需求的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
# 导入数据
data = pd.read_csv('your_data.csv')
# 分割数据
range1 = data[(data['value']>=0) & (data['value']<=5)]['value'].sum()
range2 = data[(data['value']>5) & (data['value']<=10)]['value'].sum()
range3 = data[(data['value']>10) & (data['value']<=25)]['value'].sum()
range4 = data[(data['value']>25) & (data['value']<=50)]['value'].sum()
range5 = data[(data['value']>50) & (data['value']<=100)]['value'].sum()
range6 = data[(data['value']>100) & (data['value']<=1951)]['value'].sum()
# 计算百分比
total = range1 + range2 + range3 + range4 + range5 + range6
percent1 = round(range1 / total * 100, 2)
percent2 = round(range2 / total * 100, 2)
percent3 = round(range3 / total * 100, 2)
percent4 = round(range4 / total * 100, 2)
percent5 = round(range5 / total * 100, 2)
percent6 = round(range6 / total * 100, 2)
# 绘制饼图
labels = ['0-5', '5-10', '10-25', '25-50', '50-100', '100-1951']
sizes = [percent1, percent2, percent3, percent4, percent5, percent6]
colors = ['tab:blue', 'tab:orange', 'tab:green', 'tab:red', 'tab:purple', 'tab:brown']
explode = (0, 0, 0, 0, 0, 0.1)
fig, ax = plt.subplots()
ax.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', startangle=90)
ax.axis('equal')
ax.set_title('Data Distribution')
plt.show()
```
希望这份代码可以帮助到您!如果您还有其它问题,请随时问我。
阅读全文