Python绘制饼图
制作一个“饼条图”其中饼图的第一片被“炸开”成条形图并进一步细分该片的特征。示例演示了如何使用具有多组坐标轴的图形并使用坐标轴的patches列表添加两个ConnectionPatches以连接子图。import matplotlib.pyplot as plt import numpy as np from matplotlib.patches import ConnectionPatch # 创建图形并分配轴对象 fig, (ax1, ax2) plt.subplots(1, 2, figsize(9, 5)) fig.subplots_adjust(wspace0) # 饼图参数 overall_ratios [.27, .56, .17] labels [Approve, Disapprove, Undecided] explode [0.1, 0, 0] # 旋转使得第一楔形被x轴分割 angle -180 * overall_ratios[0] wedges, *_ ax1.pie(overall_ratios, autopct%1.1f%%, startangleangle, labelslabels, explodeexplode) # 条形图参数 age_ratios [.33, .54, .07, .06] age_labels [Under 35, 35-49, 50-65, Over 65] bottom 1 width .2 # 从顶部添加与图例匹配。 for j, (height, label) in enumerate(reversed([*zip(age_ratios, age_labels)])): bottom - height bc ax2.bar(0, height, width, bottombottom, colorC0, labellabel, alpha0.1 0.25 * j) ax2.bar_label(bc, labels[f{height:.0%}], label_typecenter) ax2.set_title(Age of approvers) 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), coordsAax2.transData, xyB(x, y), coordsBax1.transData) con.set_color([0, 0, 0]) con.set_linewidth(4) 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), coordsAax2.transData, xyB(x, y), coordsBax1.transData) con.set_color([0, 0, 0]) ax2.add_artist(con) con.set_linewidth(4) plt.show()参考文献https://matplotlib.org/stable/gallery/pie_and_polar_charts/bar_of_pie.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2430932.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!