Python笔记:用matplotlib绘制面积图

五迷三道 提交于 2020-08-07 21:44:45

        用pyecharts绘制出来的面积图、圆环图太有个性了,未免感觉有点“高处不胜寒”。今晚虽然休息,但继用matplotlib绘制折线图之后,我又心血来潮地想用matplotlib来制作一点面积图,因为这在零售行业也是常用的可视化图形之一。具体如下:

 

绘制常规面积图(饼图):

# 导入库
import matplotlib.pyplot as plt
import numpy as np

# 在线显示
%matplotlib inline 

# 让中文标签正常显示
plt.rcParams["font.sans-serif"] = ["SimHei"]

# 让负号正常显示
plt.rcParams["axes.unicode_minus"] = False

# 调整画布的大小
fig = plt.figure(figsize = (10,10))

# 准备待传入的参数
# 数值
data=[100,125,135,145,150]
# 各扇区的我颜色
colors = ["DarkMagenta", "Teal", "pink", "SlateGray", "Tomato"]
# 对应的标签
labels = ["蔬菜", "水果", "水产", "猪肉", "综合"]

# 直接绘制出一个简单而又原始的饼图
# plt.pie(data, labels = labels)

# 调整各扇区的颜色
# plt.pie(data, labels = labels, colors = colors)

# autopct 让百分比正常显示
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ")

# labeldistance 调节各扇区标签距离圆心的距离
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4)

# radius 调整扇形的大小
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4, radius = 1)

# counterclock 调整指针方向,True是逆时针,False是顺时针
# plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4, radius = 1,counterclock = False)

# center 调整图表中心位置
plt.pie(data, colors = colors, labels = labels,autopct = " %1.1f%% ",labeldistance = 0.4, radius = 1,counterclock = False,center = (0.5, 0.5))

# 保存输出饼图
plt.savefig(r"C:\Users\QDM\Desktop\饼图.png")

可视化结果:

 

绘制圆环图:

# 导入库
import matplotlib.pyplot as plt
import numpy as np

# 在线显示
%matplotlib inline 

# 让中文标签正常显示
plt.rcParams["font.sans-serif"] = ["SimHei"]

# 让负号正常显示
plt.rcParams["axes.unicode_minus"] = False

# 调整画布的大小
fig = plt.figure(figsize = (10,6))

# 模拟数据
x=[16,41,60]

# 绘制一个我们自己所需要呈现的面积图
# plt.pie(x, labels=["上午销售占比", "下午销售占比", "晚上销售占比"])

# 自定义各段区间的颜色
# plt.pie(x, labels=["上午销售占比", "下午销售占比", "晚上销售占比"],colors = ["m","Crimson","blue"])

# 显示百分比
# plt.pie(x, labels=["上午销售占比", "下午销售占比", "晚上销售占比"],colors = ["m","Crimson","blue"],autopct = " %1.1f%% ")

# 调整百分比所显示的位置
plt.pie(x, labels=["上午销售占比", "下午销售占比", "晚上销售占比"],colors = ["m","Crimson","SteelBlue"],autopct = " %1.1f%% ",pctdistance = 0.8)


# plt.pie(x, labels=["上午销售占比", "下午销售占比", "晚上销售占比"],colors = ["m","#f00056","Orchid"],autopct = " %1.1f%% ",pctdistance = 0.8,labeldistance = 1)

# 在第一个饼图的基础上再画一个颜色为白色,半径较小的饼图进行覆盖,就可以画出圆环图
plt.pie([1],colors = "w",radius = 0.6)
plt.axis("equal")
plt.legend()

# 保存输出环形图
plt.savefig(r"C:\Users\QDM\Desktop\环形图.png")

可视化结果:

 

绘制另类的混合图形:

# 导入库
import matplotlib.pyplot as plt
import numpy as np

# 在线显示
%matplotlib inline 

# 让中文标签正常显示
plt.rcParams["font.sans-serif"] = ["SimHei"]

# 让负号正常显示
plt.rcParams["axes.unicode_minus"] = False

# 调整画布的大小
fig = plt.figure(figsize = (10,6))

# 构造数据
a = [0.4, 0.15, 0.2, 0.1, 0.15]
b = [0.3, 0.25, 0.16, 0.14, 0.15]
label=["苹果", "雪梨", "西瓜", "菠萝", "香瓜"]

# 绘制外圆
plt.pie(a,autopct = " %1.1f%% ", pctdistance = 0.85,labels = label)

# 绘制内圆
plt.pie(b,radius = 0.7, autopct = " %1.1f%% ",pctdistance = 0.78,)
plt.axis("equal")

# 保存输出混合面积图
plt.savefig(r"C:\Users\QDM\Desktop\混合面积图.png")

可视化结果:

 

        小结:虽然不完美,但是归纳知识的过程很是愉悦!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!