Python 画饼图 间隔饼图 圆环饼图 组合饼图

一、普通饼图

在这里插入图片描述

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.family']='Times New Roman,SimHei'# 设置字体族,中文为SimHei,英文为Times New Roman
plt.style.use('fivethirtyeight')

data = [2,4,5,1,6]
labels = ['A类','B类','C类','D类','E类']

fig, ax = plt.subplots(1, 1, figsize=(6, 6))  # 设置图形大小

wedges, texts, autotexts = ax.pie(
    data,
    labels=labels,
    autopct='%1.1f%%',
    textprops=dict(color='k',size=20),
    pctdistance=0.6, # 控制百分比的位置
    labeldistance=1.1 # 控制标签的位置
)

ax.set_title('饼图标题',fontsize=20)

plt.show()

在这里插入图片描述

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.family']='Times New Roman,SimHei'# 设置字体族,中文为SimHei,英文为Times New Roman
plt.style.use('fivethirtyeight')

data = [2,4,5,1,6]
labels = ['A类','B类','C类','D类','E类']

fig, ax = plt.subplots(1, 1, figsize=(6, 6))  # 设置图形大小

wedges, texts, autotexts = ax.pie(
    data,
    autopct='%1.1f%%',
    textprops=dict(color='w',size=20)
)

ax.legend(
    wedges,
    labels,
    title="",
    prop={'size': 15},
    frameon=False,
    bbox_to_anchor=(1, 0, 0.5, 1) # 控制图例的位置
)

ax.set_title('饼图标题',fontsize=20)

plt.show()


二、强调某一个模块。

在 ax.pie() 中传入参数 explode=(0.01, 0.05, 0.01, 0.01, 0.01), 可以改变间隔

在这里插入图片描述

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.family']='Times New Roman,SimHei'# 设置字体族,中文为SimHei,英文为Times New Roman
plt.style.use('fivethirtyeight')

data = [2,4,5,1,6]
labels = ['A类','B类','C类','D类','E类']
explode = (0.01, 0.05, 0.01, 0.01, 0.01)

fig, ax = plt.subplots(1, 1, figsize=(6, 6))  # 设置图形大小

wedges, texts, autotexts = ax.pie(
    data,
    autopct='%1.1f%%',
    explode=explode,
    textprops=dict(color='w',size=20)
)

ax.legend(
    wedges,
    labels,
    title="",
    prop={'size': 15},
    frameon=False,
    bbox_to_anchor=(1, 0, 0.5, 1) # 控制图例的位置
)

ax.set_title('饼图标题',fontsize=20)

plt.show()


三、环形饼图

在这里插入图片描述

import pandas as pd
import matplotlib.pyplot as plt

plt.rcParams['font.family']='Times New Roman,SimHei'# 设置字体族,中文为SimHei,英文为Times New Roman
plt.style.use('fivethirtyeight')

data = [2,4,5,1,6]
labels = ['A类','B类','C类','D类','E类']
explode = (0.01, 0.05, 0.01, 0.01, 0.01)

fig, ax = plt.subplots(1, 1, figsize=(6, 6))  # 设置图形大小

wedges, texts, autotexts = ax.pie(
    data,
    autopct='%1.1f%%',
    explode=explode,
    # 设置30%的环形图,间距颜色为白色
    wedgeprops=dict(width=0.3, edgecolor='w'),
    textprops=dict(color='red',size=20)
)

ax.legend(
    wedges,
    labels,
    title="",
    prop={'size': 15},
    frameon=False,
    bbox_to_anchor=(1, 0, 0.5, 1) # 控制图例的位置
)

ax.set_title('环形饼图',fontsize=20)

plt.show()

四、某一类别再细分类

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import ConnectionPatch

plt.rcParams['font.family']='Times New Roman,SimHei'# 设置字体族,中文为SimHei,英文为Times New Roman

# make figure and assign axis objects
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 5))
fig.subplots_adjust(wspace=0)

# pie chart parameters
data = [.27, .56, .17]
labels = ['A类人', 'B类人', 'C类人']
explode = [0.1, 0, 0]
# rotate so that first wedge is split by the x-axis
angle = -180 * data[0]
wedges, *_ = ax1.pie(data, autopct='%1.1f%%', startangle=angle,
                     labels=labels, explode=explode, textprops=dict(size=18))

# bar chart parameters
age_ratios = [.33, .54, .07, .06]
age_labels = ['< 35', '35-49', '50-65', '> 65']
bottom = 1
width = .2

# Adding from the top matches the legend.
for j, (height, label) in enumerate(reversed([*zip(age_ratios, age_labels)])):
    bottom -= height
    bc = ax2.bar(0, height, width, bottom=bottom, color='C0', label=label,
                 alpha=0.1 + 0.25 * j)
    ax2.bar_label(bc, labels=[f"{height:.0%}"], label_type='center')

ax2.set_title('A类人年龄占比')
ax2.legend()
ax2.axis('off') # 关闭柱状图的坐标轴
ax2.set_xlim(- 2.5 * width, 2.5 * width)

# 使用 ConnectionPatch 在两个图直接画线
theta1, theta2 = wedges[0].theta1, wedges[0].theta2
center, r = wedges[0].center, wedges[0].r
bar_height = sum(age_ratios)

# 画上面的连接线线
x = r * np.cos(np.pi / 180 * theta2) + center[0]
y = r * np.sin(np.pi / 180 * theta2) + center[1]
con = ConnectionPatch(xyA=(-width / 2, bar_height), coordsA=ax2.transData,
                      xyB=(x, y), coordsB=ax1.transData)
con.set_color([0, 0, 0])
con.set_linewidth(3)
ax2.add_artist(con)

# 画下面的连接线线
x = r * np.cos(np.pi / 180 * theta1) + center[0]
y = r * np.sin(np.pi / 180 * theta1) + center[1]
con = ConnectionPatch(xyA=(-width / 2, 0), coordsA=ax2.transData,
                      xyB=(x, y), coordsB=ax1.transData)
con.set_color([0, 0, 0])
ax2.add_artist(con)
con.set_linewidth(3)

plt.show()


五、多层饼图

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

size = 0.3 # 圆圈宽度
# 构建数据
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
data1 = vals.sum(axis=1)
data2 = vals.flatten()

# 制定颜色
cmap = plt.colormaps["tab20c"]
outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap([1, 2, 5, 6, 9, 10])

ax.pie(data1, radius=1, colors=outer_colors,
       wedgeprops=dict(width=size, edgecolor='w'))

ax.pie(data2, radius=1-size, colors=inner_colors,
       wedgeprops=dict(width=size, edgecolor='w'))

ax.set(aspect="equal", title='多层圆圈饼图')
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值