import numpy as np import matplotlib.pyplot as plt fig, ax = plt.subplots(figsize=(13,8), subplot_kw=dict(aspect="equal"))
recipe = ["USA:99 (44.39%)", "UK:28 (12.56%)", "Germany:33 (14.80%)", "France:15 (6.726%)", "FRG:4 (1.794%)", "Switzerland:6 (2.691%)", "Sweden:8 (3.587%)", "Others:30 (13.45%)"] ingredients=['USA','UK','Germany','France','FRG','Switzerland','Sweden','Others']
data = [99, 28, 33, 15, 4, 6,8,30] colors=['#1F77B4','#FF7F0E','#2CA02C','yellow','#D62728','lawngreen','#8C564B','cyan'] wedges, texts = ax.pie(data, wedgeprops=dict(width=0.5), startangle=30,colors=colors)
bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="k", lw=0.72) kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="-"), bbox=bbox_props, zorder=0, va="center")
for i, p in enumerate(wedges): ang = (p.theta2 - p.theta1)/2. + p.theta1 y = np.sin(np.deg2rad(ang)) x = np.cos(np.deg2rad(ang)) horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))] connectionstyle = "angle,angleA=0,angleB={}".format(ang) kw["arrowprops"].update({"connectionstyle": connectionstyle}) ax.annotate(recipe[i], xy=(x, y), xytext=(1.35np.sign(x), 1.4y), horizontalalignment=horizontalalignment, **kw)
ax.legend(wedges, ingredients, bbox_to_anchor=(1, 0, 0.5, 1)) plt.show()
代码如上,现在只想让注释框中的国家(英文)放在各自框的上面并居中,数字仍保留在框里,不知道如何修改代码?请大家指导!谢谢大家!(先发帖,一会上图,还不会贴图--~
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.