【问题标题】:Issue with pie chart matplotlib - startangle / len()饼图 matplotlib 的问题 - startangle / len()
【发布时间】:2016-09-14 11:21:01
【问题描述】:

我有一个基于 CSV 文件创建饼图的脚本。当我阅读只有一行的 CSV(例如percent = [100])时,我的问题就开始了。使用饼图是否有任何限制,哪里不会仅显示一项 100%?该错误似乎与startangleexplode 参数有关。

我的代码是:

percent = [100]

plt.pie(percent,        # data
    explode=(0),        # offset parameters 
    #labels=country,    # slice labels - removed to hid labels and added labels=country in legend()
    colors=colors,      # array of colours
    autopct='%1.0f%%',  # print the values inside the wedges - add % to the values 
    shadow=False,       # enable shadow
    startangle=70       # starting angle
)

plt.axis('equal')
plt.legend(loc='best', labels=country) 
plt.tight_layout()

startangle=70 行出错:

if len(x) != len(explode):
TypeError: object of type 'float' has no len()

谢谢!

【问题讨论】:

    标签: python matplotlib charts


    【解决方案1】:

    explode 参数更改为list

    percent = [100]
    explode = [0]
    
    plt.pie(percent, explode=explode, ...)
    

    如果你有更多的值,你可以使用tuple,但有一个值(int)被视为一个整数:

    >>> type((0))
    <type 'int'>
    >>> type((0, 1))
    <type 'tuple'>
    
    >>> type([0])
    <type 'list'>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多