V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
jacksonvillor
V2EX  ›  Python

关于 Matplotlib 饼状图代码的问题,谢谢大家!

  •  
  •   jacksonvillor · 2019-05-12 20:07:09 +08:00 · 1933 次点击
    这是一个创建于 1782 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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()

    代码如上,现在只想让注释框中的国家(英文)放在各自框的上面并居中,数字仍保留在框里,不知道如何修改代码?请大家指导!谢谢大家!(先发帖,一会上图,还不会贴图--~

    8 条回复    2019-05-13 00:05:48 +08:00
    jacksonvillor
        1
    jacksonvillor  
    OP
       2019-05-12 20:31:35 +08:00
    Takamine
        2
    Takamine  
       2019-05-12 21:44:04 +08:00
    既然都在右上角给了图例,我觉得直接去掉国家名也可以:dog:。
    jacksonvillor
        3
    jacksonvillor  
    OP
       2019-05-12 22:17:56 +08:00 via iPhone
    @Takamine 谢谢你!目前在撰写论文,因为打印出来是黑白的,所以考虑这个因素,添加上国家名称。
    Takamine
        4
    Takamine  
       2019-05-12 22:54:32 +08:00
    @jacksonvillor 这样阿,我觉得可以图片二次加工一下( ps )就好啦 23333。
    或者我觉得你可以拿这个调一下参数,换一下参数位置避免遮挡之后的效果可能比你那个好看一丢丢。
    https://echarts.baidu.com/examples/editor.html?c=pie-nest
    qcts33
        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
    Takamine
        6
    Takamine  
       2019-05-12 23:00:08 +08:00
    Takamine
        7
    Takamine  
       2019-05-12 23:03:45 +08:00
    jacksonvillor
        8
    jacksonvillor  
    OP
       2019-05-13 00:05:48 +08:00 via iPhone
    @qcts33 谢谢您!我明天仔细看看您的回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1446 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:25 · PVG 01:25 · LAX 10:25 · JFK 13:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.