1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| from matplotlib import pyplot as plt import numpy as np
size = 0.25 base = 50
plt.rcParams['font.family'] = 'SimHei' fig, ax = plt.subplots(figsize = (10, 10))
vals_middle = np.array([ [47.5,11.7,15.2,9.6], [0,44.8,7.5,0], [9.2, 68.5 , 0, 0], [1.2, 7.2, 0, 0], [80,0, 0, 0], [1.7, 18.9, 0, 0] ])
vals_outer = np.array([ [47.5,11.7,15.2,9.6], [0,36.6,8.2,7.5], [9.2,38.1,30.4, 0], [1.2, 5.8, 1.4, 0], [80,0, 0, 0], [1.7, 18.9, 0, 0] ])
vals_inner = vals_middle.sum(axis=1)
vals_first = vals_inner + base
''' 第二圈使用的数值, 因为最内圈每个类别都加上了base, 所以为了确保第二圈的数值和内圈相匹配, 第二圈的各类别要按照各自所占的比例分配各类的总数值. ''' vals_second = np.zeros((6, 4)) for i in range(6): s_a = vals_first[i] s_b = vals_middle[i].sum() if s_b == 0.0: vals_second[i][1] = base continue for j in range(4): vals_second[i][j] = (vals_middle[i][j] / s_b) * s_a
vals_third = np.zeros((6, 4)) for i in range(6): s_a = vals_first[i] s_b = vals_outer[i].sum() if s_b == 0.0: vals_third[i][1] = base continue for j in range(4): vals_third[i][j] = (vals_outer[i][j] / s_b) * s_a
cmap_c = plt.get_cmap("tab20c") cmap_b = plt.get_cmap("tab20b")
cmap_1 = cmap_c(np.arange(20)) cmap_2 = cmap_b(np.array([4, 5, 6, 7]))
inner_colors = np.vstack((cmap_1[::4], cmap_2[0]))
outer_colors = np.vstack((cmap_1, cmap_2))
labels_first=["餐厨废弃物\n{}万吨".format(vals_inner[0]), "农业秸秆\n{}万吨".format(vals_inner[1]), "水草\n151.2万吨", "园林绿化\n废弃物\n{}万吨".format(vals_inner[3]), "淤泥\n432.0万吨", "畜禽粪便\n21.6万吨" ]
labels_seocnd=[ "未分类收集\n67.6万吨", "生物干化\n3.7万吨", "厌氧发酵\n10.2万吨", "油水分离\n2.6万吨",
"", "粉碎\n46.8万吨", "好氧发酵\n3.5万吨", "",
"未处理\n4.2万吨", "藻水分离\n147.0万吨", "", "",
"未处理\n1.2万吨", "粉碎\n7.2万吨", "", "",
"堆放\n432.0万吨", "", "", "",
"未处理\n0.7万吨", "好氧发酵\n19.9万吨", "", "", ]
labels_third=[ "未处理\n67.5万吨", "肥料化、发电\n3.7万吨", "沼气、沼渣发电\n10.2万吨", "焚烧\n2.6万吨",
"", "还田\n42.6万吨", "燃料化\n4.2万吨", "肥料化\n3.5万吨",
"未利用\n4.2万吨", "燃料化\n80.2万吨", "肥料化\n66.8万吨", "",
"未利用\n1.2万吨", "肥料化\n5.8万吨", "燃料化\n1.4万吨", "",
"未利用\n432.0万吨", "", "", "",
"未利用\n0.7万吨", "肥料化\n19.9万吨", "", "", ]
handles, labels = ax.pie(vals_first, radius=1-size-size, labels=labels_first, labeldistance=0.5, rotatelabels=True, textprops={'fontsize': 11}, colors=inner_colors, wedgeprops=dict(width=size, edgecolor='w'))
ax.pie(vals_second.flatten(), radius=1-size, colors=outer_colors, labels=labels_seocnd, labeldistance=0.7, rotatelabels=True, textprops={'fontsize': 11}, wedgeprops=dict(width=size, edgecolor='w'))
ax.pie(vals_third.flatten(), radius=1, colors=outer_colors, labels=labels_third, labeldistance=0.8, rotatelabels=True, textprops={'fontsize': 11}, wedgeprops=dict(width=size, edgecolor='w'))
plt.title('某市有机废弃物产生、处理、利用情况', fontsize=25) plt.legend(handles=handles, labels=[ "餐厨废弃物", "农业秸秆", "水草", "园林绿化废弃物", "淤泥", "畜禽粪便"], loc = 1 ) plt.show()
|