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()
代码如上,现在只想让注释框中的国家(英文)放在各自框的上面并居中,数字仍保留在框里,不知道如何修改代码?请大家指导!谢谢大家!(先发帖,一会上图,还不会贴图--~
1
jacksonvillor OP |
2
Takamine 2019-05-12 21:44:04 +08:00
既然都在右上角给了图例,我觉得直接去掉国家名也可以:dog:。
|
3
jacksonvillor OP @Takamine 谢谢你!目前在撰写论文,因为打印出来是黑白的,所以考虑这个因素,添加上国家名称。
|
4
Takamine 2019-05-12 22:54:32 +08:00
@jacksonvillor 这样阿,我觉得可以图片二次加工一下( ps )就好啦 23333。
或者我觉得你可以拿这个调一下参数,换一下参数位置避免遮挡之后的效果可能比你那个好看一丢丢。 https://echarts.baidu.com/examples/editor.html?c=pie-nest |
5
qcts33 2019-05-12 22:55:33 +08:00
@jacksonvillor 可以考虑给每个区域加不同的纹路,matplotlib 里面叫 hatch。
随手 Google 了一下 https://stackoverflow.com/questions/14279344/how-can-i-add-textures-to-my-bars-and-wedges |
6
Takamine 2019-05-12 23:00:08 +08:00
|
7
Takamine 2019-05-12 23:03:45 +08:00
|
8
jacksonvillor OP @qcts33 谢谢您!我明天仔细看看您的回复
|