【基础绘图】11.环形图

效果图:

主要步骤:

1. 数据准备:随机赋值的数据

2. 图像绘制:绘制单环形图和多环形图

详细代码:着急的直接拖到最后有完整代码

步骤一:导入库包及图片存储路径并设置中文字体为宋体,西文为新罗马(没有的库包要先下好奥)

###############################################################################
# 导入库及文件
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
config = {
            "font.family": 'serif',
            "font.size": 12,
            "mathtext.fontset": 'stix',
            "font.serif": ['SimSun'],
         }
rcParams.update(config)
rcParams['axes.unicode_minus']=False


步骤二:赋值数据

###############################################################################
figpath = r'H:/00.csdn/02fig/'

data = np.array([[60., 32.], [37., 40.], [29., 10.]])

label = ['a', 'b', 'c', 'd', 'e', 'f']
color = ['#E9BAA4', '#89BDCC', '#7B86A3', '#B6D8CB', '#AD9A85', 
         '#AEB3C6', '#D57F70', '#89BDCC', '#64AA9A']

步骤三:绘制环形图A

###############################################################################
# 绘制图像环图A
fig = plt.figure(figsize=(8, 8))   # 创建画布
ax = fig.add_axes([0.1, 0.6, 0.6, 0.4]) # 安排子图位置

# data:所绘制的数据
# labels:数据的标签,这里不显示
# colors:饼图颜色
# autopct:饼图百分数保留的位数
# pctdistance:百分数位置
# explode:选择哪一块突出显示
# radius:饼图半径
# textprops:饼图中文本的字号及颜色
# wedgeprops:设置环图半径及边缘颜色
wedges, text, autotexts = ax.pie(np.sum(data, axis=1),
                                 labels=['A', 'B' ,'C'], 
                                 colors=color,
                                 autopct='%1.2f%%',
                                 pctdistance=0.8,
                                 # explode=[0, 0.1, 0, 0, 0, 0],
                                 radius=1,
                                 textprops={'size':10, 'color':'k'},
                                 wedgeprops=dict(width=0.4, edgecolor='w')
                                 )
help(ax.pie)

# 绘制图例及标题
# ax.legend(wedges, label, loc="right", bbox_to_anchor=(0.9,0,0.3,1))
ax.set_title('环图A')

步骤四:绘制环形图B

###############################################################################
# 绘制图像环图B
ax1 = fig.add_axes([0.5, 0.6, 0.6, 0.4]) # 安排子图位置
wedges, text, autotexts = ax1.pie(np.sum(data, axis=1),
                                 labels=['A', 'B' ,'C'], 
                                 colors=color,
                                 autopct='%1.2f%%',
                                 pctdistance=0.8,
                                 # explode=[0, 0.1, 0, 0, 0, 0],
                                 radius=1,
                                 textprops={'size':10, 'color':'k'},
                                 wedgeprops=dict(width=0.4, edgecolor='w')
                                 )
wedges, text, autotexts = ax1.pie(data.flatten(), 
                                 labels=None, 
                                 colors=color,
                                 autopct='%1.2f%%',
                                 pctdistance=0.75,
                                 # explode=[0, 0.1, 0, 0, 0, 0],
                                 radius=0.6,
                                  textprops={'size':10, 'color':'k'},
                                 wedgeprops=dict(width=0.3, edgecolor='w')
                                 )
# ax.pie(vals.flatten(), radius=1-size, colors=inner_colors,
#        wedgeprops=dict(width=size, edgecolor='w'))

# 绘制图例及标题
# ax1.legend(wedges, label, loc="right", bbox_to_anchor=(0.9,0,0.3,1))
ax1.set_title('环图B')

步骤五:保存图像

################################################################################
# 输出并保存图像
plt.savefig(figpath+'011 环状图.png', bbox_inches = 'tight', dpi=600, format='png')
plt.show()

完整代码在这里:

###############################################################################
# 导入库及文件
import matplotlib.pyplot as plt
from matplotlib import rcParams
import numpy as np
config = {
            "font.family": 'serif',
            "font.size": 12,
            "mathtext.fontset": 'stix',
            "font.serif": ['SimSun'],
         }
rcParams.update(config)
rcParams['axes.unicode_minus']=False
###############################################################################
figpath = r'H:/00.csdn/02fig/'

data = np.array([[60., 32.], [37., 40.], [29., 10.]])

label = ['a', 'b', 'c', 'd', 'e', 'f']
color = ['#E9BAA4', '#89BDCC', '#7B86A3', '#B6D8CB', '#AD9A85', 
         '#AEB3C6', '#D57F70', '#89BDCC', '#64AA9A']
###############################################################################
# 绘制图像环图A
fig = plt.figure(figsize=(8, 8))   # 创建画布
ax = fig.add_axes([0.1, 0.6, 0.6, 0.4]) # 安排子图位置

# data:所绘制的数据
# labels:数据的标签,这里不显示
# colors:饼图颜色
# autopct:饼图百分数保留的位数
# pctdistance:百分数位置
# explode:选择哪一块突出显示
# radius:饼图半径
# textprops:饼图中文本的字号及颜色
# wedgeprops:设置环图半径及边缘颜色
wedges, text, autotexts = ax.pie(np.sum(data, axis=1),
                                 labels=['A', 'B' ,'C'], 
                                 colors=color,
                                 autopct='%1.2f%%',
                                 pctdistance=0.8,
                                 # explode=[0, 0.1, 0, 0, 0, 0],
                                 radius=1,
                                 textprops={'size':10, 'color':'k'},
                                 wedgeprops=dict(width=0.4, edgecolor='w')
                                 )
help(ax.pie)

# 绘制图例及标题
# ax.legend(wedges, label, loc="right", bbox_to_anchor=(0.9,0,0.3,1))
ax.set_title('环图A')
###############################################################################
# 绘制图像环图B
ax1 = fig.add_axes([0.5, 0.6, 0.6, 0.4]) # 安排子图位置
wedges, text, autotexts = ax1.pie(np.sum(data, axis=1),
                                 labels=['A', 'B' ,'C'], 
                                 colors=color,
                                 autopct='%1.2f%%',
                                 pctdistance=0.8,
                                 # explode=[0, 0.1, 0, 0, 0, 0],
                                 radius=1,
                                 textprops={'size':10, 'color':'k'},
                                 wedgeprops=dict(width=0.4, edgecolor='w')
                                 )
wedges, text, autotexts = ax1.pie(data.flatten(), 
                                 labels=None, 
                                 colors=color,
                                 autopct='%1.2f%%',
                                 pctdistance=0.75,
                                 # explode=[0, 0.1, 0, 0, 0, 0],
                                 radius=0.6,
                                  textprops={'size':10, 'color':'k'},
                                 wedgeprops=dict(width=0.3, edgecolor='w')
                                 )
# ax.pie(vals.flatten(), radius=1-size, colors=inner_colors,
#        wedgeprops=dict(width=size, edgecolor='w'))

# 绘制图例及标题
# ax1.legend(wedges, label, loc="right", bbox_to_anchor=(0.9,0,0.3,1))
ax1.set_title('环图B')
###############################################################################
# 输出并保存图像
plt.savefig(figpath+'011 环状图.png', bbox_inches = 'tight', dpi=600, format='png')
plt.show()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值