用matplotlib.pyplot绘制甜甜圈图

# 用matplotlib绘制甜甜圈饼图
# The effect of the donut shape is achieved by setting a width to the pie's wedges through the wedgeprops argument.
%matplotlib inline  # matplotlib的图表直接嵌入到Notebook之中, 仅在jupyter notebook 或 jupyter qtconsole有用
import matplotlib.pyplot as plt
import numpy as np
# Create a figure and a set of subplots
# Returns
# fig : :class:`matplotlib.figure.Figure` object
# ax : Axes object or array of Axes objects.
#    
#        ax can be either a single :class:`matplotlib.axes.Axes` object or an
#        array of Axes objects if more than one subplot was created.
fig, ax = plt.subplots()  # 1*1画布

size = 0.3
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])  # 3*2 array

cmap = plt.get_cmap("tab20c")  # Get a colormap instance, matplotlib.cm
outer_colors = cmap(np.arange(3)*4)  # cmap([0,4,8]), len(cmap.colors) -> 20
inner_colors = cmap(np.array([1,2,5,6,9,10]))

# 第一个环
ax.pie(vals.sum(axis=1), radius=2, colors=outer_colors,
       wedgeprops=dict(width=0.3, edgecolor='w'))  # wedge object 控制圆环的宽度

# 第二个环
ax.pie(vals.sum(axis=1), radius=1, colors=outer_colors,
       wedgeprops=dict(width=0.3, edgecolor='w'))  # wedge object 控制圆环的宽度

# 第三个环
# vals.flatten() 按行展开成1维矩阵; vals.flatten('F')按列展开
ax.pie(vals.flatten(), radius=1-size, colors=inner_colors,
       wedgeprops=dict(width=size, edgecolor='w'))

ax.set(aspect="equal", title='Pie plot with `ax.pie`')
plt.show()

用matplotlib.pyplot绘制甜甜圈图

# 另一种方法
# Bar plot on axes with a polar coordinate system.
# 柱形图映射到极坐标下,利用柱型图的高度、位置来形成多圈
fig, ax = plt.subplots(subplot_kw=dict(polar=True))  # 设定极坐标系统

size = 0.1
vals = np.array([[60., 32.], [37., 40.], [29., 10.]])
#normalize vals to 2 pi
valsnorm = vals/np.sum(vals)*2*np.pi  # 将数值映射到2pi
#obtain the ordinates of the bar edges
valsleft = np.cumsum(np.append(0, valsnorm.flatten()[:-1])).reshape(vals.shape)

cmap = plt.get_cmap("tab20c")
'''
Possible values are:
Accent, Accent_r, Blues, Blues_r, BrBG, BrBG_r, BuGn, BuGn_r, BuPu, BuPu_r, 
CMRmap, CMRmap_r, Dark2, Dark2_r, GnBu, GnBu_r, Greens, Greens_r, 
Greys, Greys_r, OrRd, OrRd_r, Oranges, Oranges_r, PRGn, PRGn_r, Paired, 
Paired_r, Pastel1, Pastel1_r, Pastel2, Pastel2_r, PiYG, PiYG_r, PuBu, 
PuBuGn, PuBuGn_r, PuBu_r, PuOr, PuOr_r, PuRd, PuRd_r, Purples, Purples_r, 
RdBu, RdBu_r, RdGy, RdGy_r, RdPu, RdPu_r, RdYlBu, RdYlBu_r, RdYlGn, RdYlGn_r, 
Reds, Reds_r, Set1, Set1_r, Set2, Set2_r, Set3, Set3_r, Spectral, Spectral_r,
Wistia, Wistia_r, YlGn, YlGnBu, YlGnBu_r, YlGn_r, YlOrBr, YlOrBr_r, YlOrRd, 
YlOrRd_r, afmhot, afmhot_r, autumn, autumn_r, 
binary, binary_r, bone, bone_r, brg, brg_r, bwr, bwr_r, cividis, cividis_r, 
cool, cool_r, coolwarm, coolwarm_r, copper, copper_r, cubehelix, cubehelix_r, 
flag, flag_r, gist_earth, gist_earth_r, gist_gray, gist_gray_r, gist_heat, 
gist_heat_r, gist_ncar, gist_ncar_r, gist_rainbow, gist_rainbow_r, gist_stern, 
gist_stern_r, gist_yarg, gist_yarg_r, gnuplot, gnuplot2, gnuplot2_r, gnuplot_r, 
gray, gray_r, hot, hot_r, hsv, hsv_r, inferno, inferno_r, jet, jet_r, magma, magma_r, 
nipy_spectral, nipy_spectral_r, ocean, ocean_r, pink, pink_r, plasma, plasma_r, 
prism, prism_r, rainbow, rainbow_r, seismic, seismic_r, spring, spring_r, summer, 
summer_r, tab10, tab10_r, tab20, tab20_r, tab20b, tab20b_r, tab20c, 
tab20c_r, terrain, terrain_r, viridis, viridis_r, winter, winter_r
'''

outer_colors = cmap(np.arange(3)*4)
inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10]))

# 第一圈
ax.bar(x=valsleft[:, 0],  # 取第一列
       width=valsnorm.sum(axis=1), bottom=1-size, height=size, # 设置宽度、底部位置、高
       color=outer_colors, edgecolor='w', linewidth=1, align="edge") # 设置颜色、边线颜色、线宽、对齐方式
# 第二圈
ax.bar(x=valsleft.flatten(),
       width=valsnorm.flatten(), bottom=1-2*size, height=size,
       color=inner_colors, edgecolor='w', linewidth=1, align="edge")

ax.set(title="Pie plot with `ax.bar` and polar coordinates")
ax.set_axis_off()
plt.show()

用matplotlib.pyplot绘制甜甜圈图