Python+matplotlib实现饼图的绘制

目录
  • 一、整理数据
  • 二、创建饼图
  • 三、爆炸效果
  • 四、阴影效果
  • 五、为饼图加上百分比
  • 六、让饼图旋转不同的角度
  • 七、为饼图添加边缘线
  • 八、为饼图数据分组

一、整理数据

关于cnboo1.xlsx,我放在我的码云里,需要的朋友自行下载:cnboo1.xlsx

films=['穿过寒冬拥抱你','反贪风暴5:最终章','李茂扮太子','误杀2','以年为单位的恋爱','黑客帝国:矩阵重启','雄狮少年','魔法满屋','汪汪队立大功大电影','爱情神话']
regions=['中国','英国','澳大利亚','美国','美国','中国','英国','澳大利亚','美国','美国']
bos=['61,181','44,303','42,439','22,984','13,979','61,181','44,303','41,439','20,984','19,979']
persons=['31','23','56','17','9','31','23','56','17','9']
prices=['51','43','56','57','49','51','43','56','57','49']
showdate=['2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05','2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05']
ftypes=['剧情','动作','喜剧','剧情','剧情','爱情','动作','动画','动画','动画']
points=['8.1','9.0','7.9','6.7','3.8','8.1','9.0','7.9','6.7','3.8']
filmdescript={
    'ftypes':ftypes,
    'bos':bos,
    'prices':prices,
    'persons':persons,
    'regions':regions,
    'showdate':showdate,
    'points':points
}
import numpy as np
import pandas as pd
cnbo2021top5=pd.DataFrame(filmdescript,index=films)
cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int)
cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int)
cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64')
cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!='' else 0)
import pandas as pd
cnbodf=pd.read_excel('cnboo1.xlsx')
cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)
cnbodfsort.index=cnbodfsort.TYPE
bo=cnbo2021top5.bos.sort_values()
def mkpoints(x,y):
    return len(str(x))*(y/25)-3

cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)
cnbodfsort['type1']=cnbodfsort['TYPE'].apply(lambda x:x.split("/")[0])
cnbodfgb=cnbodfsort.groupby(["type1"])["ID","BO","PRICE","PERSONS","points"].mean()
cnbodfgbsort=cnbodfgb.sort_values("BO",ascending=False)

二、创建饼图

from matplotlib import pyplot as plt
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index)
plt.show()

这里涉及到简历的漫画效果:详情请访问:为图表添加漫画效果

三、爆炸效果

# 爆炸效果 饼图脱离

from matplotlib import pyplot as plt 
explo=[0.3,0,0,0,0,0] # 控制爆炸效果,通过更改参数控制距离的长短
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo)
plt.show()

四、阴影效果

# 添加阴影效果
# 爆炸效果 饼图脱离

from matplotlib import pyplot as plt
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True)
plt.show()

五、为饼图加上百分比

# 添加阴影效果
# 爆炸效果 饼图脱离

from matplotlib import pyplot as plt
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%')
plt.show()

六、让饼图旋转不同的角度

# 饼图旋转
from matplotlib import pyplot as plt
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%')
plt.show()

七、为饼图添加边缘线

# 为饼图添加边缘线
from matplotlib import pyplot as plt
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%',wedgeprops={"edgecolor":"black"})
plt.show()

但是我自己感觉并不是非常明显

八、为饼图数据分组

# 将数据按照票房分类
labels=['>20000','15000-20000','10000-15000','<10000']
c1=cnbodfsort.loc[cnbodfsort.BO>=20000].count()[0]
c2=cnbodfsort.loc[(cnbodfsort.BO>=15000) & (cnbodfsort.BO<20000)].count()[0]
c3=cnbodfsort.loc[(cnbodfsort.BO<15000) & (cnbodfsort.BO>=10000)].count()[0]
c4=cnbodfsort.loc[cnbodfsort.BO<10000].count()[0]
cnbohints=[c1,c2,c3,c4]
from matplotlib import pyplot as plt
explo=[0.3,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中国票房2021TOP9")
plt.pie(cnbohints,labels=labels,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%',wedgeprops={"edgecolor":"black"})
plt.show()

以上就是Python+matplotlib实现饼图的绘制的详细内容,更多关于Python matplotlib饼图的资料请关注我们其它相关文章!

(0)

相关推荐

  • python使用Matplotlib画饼图

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 函数参数 plt.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, cente

  • python+matplotlib绘制饼图散点图实例代码

    本文是从matplotlib官网上摘录下来的一个实例,实现的功能是Python+matplotlib绘制自定义饼图作为散点图的标记,具体如下. 首先看下演示效果 实例代码: import numpy as np import matplotlib.pyplot as plt # first define the ratios r1 = 0.2 # 20% r2 = r1 + 0.4 # 40% # define some sizes of the scatter marker sizes = n

  • python通过matplotlib生成复合饼图

    可以通过matplotlib实现 from matplotlib.patches import ConnectionPatch #制画布fig = plt.figure(figsize=(9,5.0625)) ax1 = fig.add_subplot(121) ax2 = fig.add_subplot(122) fig.subplots_adjust(wspace=0) #大饼图的制作 labels = newdata8.index size = newdata8.quantity expl

  • Python通过matplotlib画双层饼图及环形图简单示例

    (1) 饼图(pie),即在一个圆圈内分成几块,显示不同数据系列的占比大小,这也是我们在日常数据的图形展示中最常用的图形之一. 在python中常用matplotlib的pie来绘制,基本命令如下所示(python3.X版本): vals = [1, 2, 3, 4]#创建数据系列 fig, ax = plt.subplots()#创建子图 labels = 'A', 'B', 'C', 'D' colors = ['yellowgreen', 'gold', 'lightskyblue', '

  • Python利用matplotlib实现饼图绘制

    目录 前言 1. 等高线图概述 什么是饼图? 饼图常用场景 绘制等饼图步骤 案例展示 2. 饼图属性 设置饼图的颜色 设置标签 设置突出部分 设置填入百分比数值 饼图旋转 设置阴影 3. 调整饼图的大小 4. 添加图例 5. 镂空饼图 总结 前言 众所周知,matplotlib.pyplot 提供绘制不同表格绘制方法,如使用plot()方法绘制折线,bar()绘制柱 在matplotlib.pyplot 中还有一种图表用于直观表示占比情况的饼图,在matplotlib官网上也列举出非常多关于饼图

  • python利用matplotlib库绘制饼图的方法示例

    介绍 matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地进行制图.而且也可以方便地将它作为绘图控件,嵌入GUI应用程序中. 它的文档相当完备,并且 Gallery页面 中有上百幅缩略图,打开之后都有源程序.因此如果你需要绘制某种类型的图,只需要在这个页面中浏览/复制/粘贴一下,基本上都能搞定. matplotlib的安装方法可以点击这里 这篇文章给大家主要介绍了python用matplotlib绘制饼图的方法,话不多说,下面来看代码

  • Python+matplotlib实现饼图的绘制

    目录 一.整理数据 二.创建饼图 三.爆炸效果 四.阴影效果 五.为饼图加上百分比 六.让饼图旋转不同的角度 七.为饼图添加边缘线 八.为饼图数据分组 一.整理数据 关于cnboo1.xlsx,我放在我的码云里,需要的朋友自行下载:cnboo1.xlsx films=['穿过寒冬拥抱你','反贪风暴5:最终章','李茂扮太子','误杀2','以年为单位的恋爱','黑客帝国:矩阵重启','雄狮少年','魔法满屋','汪汪队立大功大电影','爱情神话'] regions=['中国','英国','澳大

  • python matplotlib模块基本图形绘制方法小结【直线,曲线,直方图,饼图等】

    本文实例讲述了python matplotlib模块基本图形绘制方法.分享给大家供大家参考,具体如下: matplotlib模块是python中一个强大的绘图模块 安装 pip  install matplotlib 首先我们来画一个简单的图来感受它的神奇 import numpy as np import matplotlib.pyplot as plt import matplotlib zhfont1=matplotlib.font_manager.FontProperties(fname

  • Python matplotlib数据可视化图绘制

    目录 前言 1.折线图 2.直方图 3.箱线图 4.柱状图 5.饼图 6.散点图 前言 导入绘图库: import matplotlib.pyplot as plt import numpy as np import pandas as pd import os 读取数据(数据来源是一个EXCLE表格,这里演示的是如何将数据可视化出来) os.chdir(r'E:\jupyter\数据挖掘\数据与代码') df = pd.read_csv('air_data.csv',na_values= '-

  • 不同版本中Python matplotlib.pyplot.draw()界面绘制异常问题的解决

    前言 本文主要给大家介绍了关于不同版本中Python matplotlib.pyplot.draw()界面绘制异常的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 在 Ubuntu系统上进行如下配置: $ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get install python-dev $ sudo apt-get install python-pip $ sudo pip install --u

  • Python matplotlib画图实例之绘制拥有彩条的图表

    生产定制一个彩条标签. 首先导入: import matplotlib.pyplot as plt import numpy as np from matplotlib import cm from numpy.random import randn 制作拥有垂直(默认)彩条的图表: fig, ax = plt.subplots() data = np.clip(randn(250, 250), -1, 1) cax = ax.imshow(data, interpolation='neares

  • Python matplotlib实现散点图的绘制

    目录 一.整理数据 二.修改点的样式 三.呈现半透明的状态 四.点呈现多彩的颜色 五.让点的大小不一 六.侧边呈现颜色卡 七.改变集中性 一.整理数据 import pandas as pd cnbodf=pd.read_excel('cnboo1.xlsx') cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False) def mkpoints(x,y): return len(str(x))*(y/25)-3 cnbodfsort['po

  • Python matplotlib实现多重图的绘制

    目录 Python中插入图片 绘制子图 绘制1*2的子图 绘制2*2的子图 绘制不规则子图 绘制图中代码 from matplotlib import pyplot as plt plt.style.use('fivethirtyeight') fig=plt.figure() ax=fig.add_subplot(1,1,1) plt.text(0.5,0.5,'Figure',ha='center',va='center',size=20,alpha=0.5) # 注:这里的0.5代表x,y

  • 基于Python+Matplotlib实现直方图的绘制

    目录 1.关于直方图 2.plt.hist() 3. 绘制一幅简单的 频数 分布直方图 4. 绘制一幅 频率 分布直方图 5. 累积分布直方图(水平方向) 1.关于直方图 直方图 也称 质量分布图,虽然看起来像柱状图, 实际上区别又很大.直方图通常横轴表示数据类型,纵轴表示各数据类型的分布情况. 直方图又可以分为频数分布直方图和频率分布直方图.其绘制方法并无多少差异,只是描述的事件有所不同.频数分布直方图描述的是某事件的数量,而频率分布则描述的是其发生的频率. 而关于频率分布直方图,又可以理解为

  • python matplotlib画图库学习绘制常用的图

    本文实例为大家分享了python matplotlib绘制常用图的具体代码,供大家参考,具体内容如下 github地址 导入相关类 import numpy as np import pandas as pd import matplotlib.pyplot as plt %matplotlib inline plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签 plt.rcParams['axes.unicode_minus']=Fals

  • python+matplotlib绘制3D条形图实例代码

    本文分享的实例主要实现的是Python+matplotlib绘制一个有阴影和没有阴影的3D条形图,具体如下. 首先看看演示效果: 完整代码如下: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # setup the figure and axes fig = plt.figure(figsize=(8, 3)) ax1 = fig.add_subplot(121

随机推荐