【问题标题】:fill matplotlib pie chart sectors with my string labels用我的字符串标签填充 matplotlib 饼图扇区
【发布时间】:2021-07-13 07:32:24
【问题描述】:

作为 to_show=['A','B','C','D','E'] 你好,我想用一些字符串值填充饼图的扇区,如 A、B、C、D、E autopct 是一个函数,但我不知道在饼图扇区中显示并隐藏自动生成的 %ages 值的确切方法。

【问题讨论】:

    标签: python matplotlib pie-chart


    【解决方案1】:
    import matplotlib.pyplot as plt  # make the pie circular by setting the aspect ratio to 1 
    plt.figure(figsize=plt.figaspect(1)) 
    index = [1, 2, 3, 4] 
    values = [1, 2, 3, 4] 
    labels = ['a', 'b', 'c', 'd'] 
    cutom_labels = ['1', '2,3', '4,5', '6']
    
    num = -1  
     
    plt.pie(values, labels=cutom_labels,labeldistance=0.5) 
    plt.pie(values, labels=labels) 
    plt.show()`
    

    【讨论】:

      【解决方案2】:

      我没有收到您的问题,但我们使用 autopct 显示 %area

      如果你不想要 %ages,只需删除 autopct

      参考documanetation希望对您有所帮助

      【讨论】:

      • 当您在所有扇区中运行代码时,它会给出相同的值。我想要的是根据 cutom_labels import matplotlib.pyplot as plt # make the pie circular by setting the aspect ratio to 1 plt.figure(figsize=plt.figaspect(1)) index = [1, 2, 3, 4] values = [1, 2, 3, 4] labels = ['a', 'b', 'c', 'd'] cutom_labels = ['1', '2,3', '4,5', '6'] num = -1 def make_autopct(values): def my_autopct(pct): return cutom_labels[2] return my_autopct plt.pie(values, labels=labels, autopct=make_autopct(values)) plt.show() 更改每个部门的值
      • 请运行我在评论部分提供的代码
      • 感谢您的回答,我运行了上面的代码,它对我有用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多