#导入matlibplotimport matplotlib.pyplot as plt#导入numpyimport numpy as npimport pandas as pd#解决中文显示问题plt.rcParams['font.sans-serif']=['SimHei']plt.rcParams['axes.unicode_minus'] = False
In [2]:
x = [0.1,0.2,0.3,0.4]plt.pie(x)plt.show()
![93ecc5918dd3d32f351638b0d78610d3.png](https://i-blog.csdnimg.cn/blog_migrate/af9aa80412a1b14fa6eeff6bd417ed92.jpeg)
In [ ]:
#饼图的语法pie(x, explode,labels,colors,autopct=,pctdistance,shadow,startangle,radius, wedgeprops,textprops,center) --labels: 标签 -- counterclock: 是否逆时针呈现: --colors:颜色 -- wedgeprops: 设置饼图内外边界的属性 --autopct: 百分比 --textprops: 设置饼图中文本属性 --pctdistance: 百分比标签与圆心距离 --center: 设置中心位置 --shadow: 是否添加饼图阴影效果 --labeldistance: 设置各扇形标签与圆心距离 --startangle: 设置饼图的初始摆放角度 --radius: 设置饼图半径大小
In [19]:
#饼图的一个实际应用,查看某餐馆各菜品销量占一天销售菜品量的百分比#先导入数据data=pd.read_excel('/home/kesci/input/datakeshihua9484/相关分析数据.xls')#餐馆菜品销量数据data_oneday=data.iloc[0:1,1:]#取一天的数据data_oneday
Out[19]:
百合酱蒸凤爪 翡翠蒸香茜饺 金银蒜汁蒸 排骨乐膳真味鸡 蜜汁焗餐包 生炒菜心 铁板酸菜豆腐 香煎韭菜 饺香煎罗卜糕 原汁原味菜心 0 17 6 8 24 13.0 13 18 10 10 27
In [22]:
#求出一天的销量num=data_oneday.sum(axis =1)num
Out[22]:
0 146.0dtype: float64
In [44]:
#计算每个菜品占一天销量的百分比percent=data_oneday.div(num,axis=0) #表示按行除menus_percent=percent.values#求菜品销量百分比的代码合为一行为data_oneday.div(data_oneday.div(num,axis=0),axis=0)
In [32]:
Menus=data_oneday.columns#获取菜名
In [ ]:
#开始画图
In [45]:
plt.pie(x=menus_percent,labels=Menus)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: MatplotlibDeprecationWarning: Non-1D inputs to pie() are currently squeeze()d, but this behavior is deprecated since 3.1 and will be removed in 3.3; pass a 1D array instead. """Entry point for launching an IPython kernel.
Out[45]:
([, , , , , , , , , ], [Text(1.0272209481726977, 0.39346807193873345, '百合酱蒸凤爪'), Text(0.7170888812913553, 0.8341364015125541, '翡翠蒸香茜饺'), Text(0.4372970808236019, 1.0093419951152118, '金银蒜汁蒸排骨'), Text(-0.30370668057703193, 1.0572427593381195, '乐膳真味鸡'), Text(-0.9680134324117012, 0.5224461643753515, '蜜汁焗餐包'), Text(-1.0977088744946761, -0.07095933240689023, '生炒菜心'), Text(-0.8185143034624212, -0.73487028449069, '铁板酸菜豆腐'), Text(-0.2579404890974552, -1.0693300258031502, '香煎韭菜饺'), Text(0.21169677650948987, -1.0794371101715428, '香煎罗卜糕'), Text(0.9195180370394429, -0.6037272393714979, '原汁原味菜心')])
![0a44f0020abcd633a09e5a248ce93c66.png](https://i-blog.csdnimg.cn/blog_migrate/f2df2055e1f1925dedb4224b2d9fde08.jpeg)
In [47]:
#基础的饼图出来了,接下来就是优化,让图更好看#增加explode参数,比如我想突出百合酱蒸凤爪explodes=[0.2,0,0,0,0,0,0,0,0,0]plt.pie(x=menus_percent,explode=explodes,labels=Menus)plt.show()
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:4: MatplotlibDeprecationWarning: Non-1D inputs to pie() are currently squeeze()d, but this behavior is deprecated since 3.1 and will be removed in 3.3; pass a 1D array instead. after removing the cwd from sys.path.
![5a93f4a6c876f8373856b07f563752a1.png](https://i-blog.csdnimg.cn/blog_migrate/ad20f704f6f36ece4a84cc694d3b4201.jpeg)
In [53]:
#比如在图上显示菜品的百分比plt.pie(x=menus_percent,explode=explodes,labels=Menus,autopct='%.2f%%')plt.title('菜品销量百分比')plt.show()
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:2: MatplotlibDeprecationWarning: Non-1D inputs to pie() are currently squeeze()d, but this behavior is deprecated since 3.1 and will be removed in 3.3; pass a 1D array instead.
![3b8b3429bd96bcb1b318779025955707.png](https://i-blog.csdnimg.cn/blog_migrate/59ee961b91d62b75dff5b030f2f50a1d.jpeg)
In [ ]:
#以上就是用饼图做一个可视化的简单例子,还有一些参数的作用大家可以自己尝试一下