python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python绘制饼图、圆环图

Python绘制饼图、圆环图的实例

作者:蚂蚁*漫步

这篇文章主要介绍了Python绘制饼图、圆环图的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

Python绘制饼图、圆环图

下面是我们作图需要使用到的数据(数据是个人虚构的、不代表各品牌真实销售数据) 

品牌子品牌销量总销量/台
比亚迪1000029000
比亚迪900029000
比亚迪600029000
比亚迪海豚400029000
特斯拉Model3800017500
特斯拉ModelS600017500
特斯拉ModelY350017500
大众朗逸300012000
大众速腾300012000
大众高尔夫600012000
丰田卡罗拉600012000
丰田雷凌400012000
丰田凯美瑞200012000
奇瑞艾瑞泽510002000
奇瑞艾瑞泽810002000
领克领克0110001000

1.各品牌的销售数量饼图

import  pandas as pd
from matplotlib import pyplot as plt
 
#解决中文乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
 
data=pd.read_excel(r'汽车销量数据数据.xlsx',sheet_name='Sheet1')
 
#根据各品牌去重
 
data_total_sale=data.loc[:,["品牌","总销量"]].drop_duplicates()
print(data_total_sale)
out:
 
    品牌    总销量
0   比亚迪  29000
4   特斯拉  17500
7    大众  12000
10   丰田  12000
13   奇瑞   2000
15   领克   1000
 
fig, ax = plt.subplots(figsize=(10, 7))
ax.pie(data_total_sale['总销量'],labels=data_total_sale['品牌'],autopct='%1.1f%%')
plt.show()

2.各品牌销售数据圆环图

import  pandas as pd
from matplotlib import pyplot as plt
 
#解决中文乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
 
data=pd.read_excel(r'汽车销量数据数据.xlsx',sheet_name='Sheet1')
 
#根据各品牌去重
 
data_total_sale=data.loc[:,["品牌","总销量"]].drop_duplicates()
 
total_sale=data_total_sale['总销量'].sum()
 
fig, ax = plt.subplots(figsize=(10, 7))
 
ax.pie(data_total_sale['总销量'], radius=1.5, wedgeprops={'width': 0.7}, labels = data_total_sale['品牌'], autopct='%3.2f%%',  pctdistance=0.75)  #保留2位小数
plt.text(0, 0, total_sale, ha='center', va='center', fontsize=28)
plt.show()

3.将数据少的合并为其它

import  pandas as pd
from matplotlib import pyplot as plt
 
#解决中文乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
 
data=pd.read_excel(r'汽车销量数据数据.xlsx',sheet_name='Sheet1')
 
#根据各品牌去重
 
data_total_sale=data.loc[:,["品牌","总销量"]].drop_duplicates()
 
others=["奇瑞","领克"]
data_new=data_total_sale.loc[~data['品牌'].isin(others)]
other_sum=data_total_sale['总销量'].loc[data['品牌'].isin(others)].sum()
data_new=data_new.append({"品牌":'其它',"总销量":other_sum},ignore_index=True)
fig, ax = plt.subplots(figsize=(10, 7))
ax.pie(data_new['总销量'],labels=data_new['品牌'],autopct='%1.1f%%')
plt.show()

4.其它类中展开

import  pandas as pd
from matplotlib import pyplot as plt
from matplotlib.patches import ConnectionPatch
from matplotlib import cm
 
#解决中文乱码
plt.rcParams['font.sans-serif'] = ['SimHei']
 
data=pd.read_excel(r'汽车销量数据数据.xlsx',sheet_name='Sheet1')
 
#根据各品牌去重
 
data_total_sale=data.loc[:,["品牌","总销量"]].drop_duplicates()
 
others=["奇瑞","领克"]
data_new=data_total_sale.loc[~data['品牌'].isin(others)]
other_sum=data_total_sale['总销量'].loc[data['品牌'].isin(others)].sum()
data_new=data_new.append({"品牌":'其它',"总销量":other_sum},ignore_index=True)
data_other=data_total_sale.loc[data['品牌'].isin(others)]
fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(1,2,1)
ax1.pie(data_new['总销量'],labels=data_new['品牌'],autopct='%1.1f%%')
ax2 = fig.add_subplot(1,2,2)
ax2.pie(data_other['总销量'],labels=data_other['品牌'],autopct='%1.1f%%',radius=0.5,wedgeprops=dict(width=0.3, edgecolor='w'))
 
theta1, theta2 = ax1.patches[-1].theta1+15, ax1.patches[-1].theta2-15
center, r = ax1.patches[-1].center,ax1.patches[-1].r
x = r*np.cos(np.pi/180*theta1)+center[0]
y = np.sin(np.pi/180*theta1)+center[1]
con1 = ConnectionPatch(xyA=(0, 0.5),xyB=(x,y),
                     coordsA=ax2.transData, coordsB=ax1.transData,axesA=ax2,axesB=ax1)
 
x = r * np.cos(np.pi / 180 * theta2) + center[0]
y = np.sin(np.pi / 180 * theta2) + center[1]
con2 = ConnectionPatch(xyA=(-0.1, -0.49),
                       xyB=(x, y),
                       coordsA=ax2.transData,
                       coordsB=ax1.transData,
                       axesA=ax2, axesB=ax1)
for con in [con1, con2]:
    con.set_color('gray')
    ax2.add_artist(con)
    con.set_linewidth(1)
fig.subplots_adjust(wspace=0)
plt.show()

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文