【问题标题】:matplotlib: pie chart , variable pctdistancematplotlib:饼图,变量 pctdistance
【发布时间】:2018-05-30 10:17:35
【问题描述】:

我希望能够将每个百分比值定位在距中心不同距离的位置,但 pctdistance 需要是单个值。

就我而言,pctdistance 应该是一个包含生成距离(由范围生成)的列表。

import matplotlib.pyplot as plt

fig =plt.figure(figsize = (10,10))
ax11 = fig.add_subplot(111)
# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 2000]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = (0.1, 0, 0, 0)  # explode 1st slice

# Plot
w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
        autopct='%1.1f%%', startangle=140, pctdistance=0.8, radius = 0.5)
[t.set_rotation(0) for t in p]
[t.set_fontsize(50) for t in p]
plt.axis('equal')
plt.show()

我有什么: 我想要的是:

【问题讨论】:

    标签: python matplotlib pie-chart


    【解决方案1】:

    pie 函数不会将列表或数组作为pctdistance 参数的输入。

    您可以使用pctdistances 的预定义列表手动定位文本。

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig =plt.figure(figsize = (4,4))
    ax11 = fig.add_subplot(111)
    # Data to plot
    labels = 'Python', 'C++', 'Ruby', 'Java'
    sizes = [215, 130, 245, 2000]
    colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
    
    # Plot
    w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
            autopct='%1.1f%%', startangle=140, pctdistance=1, radius = 0.5)
    
    pctdists = [.8, .5, .4, .2]
    
    for t,d in zip(p, pctdists):
        xi,yi = t.get_position()
        ri = np.sqrt(xi**2+yi**2)
        phi = np.arctan2(yi,xi)
        x = d*ri*np.cos(phi)
        y = d*ri*np.sin(phi)
        t.set_position((x,y))
    
    plt.axis('equal')
    plt.show()
    

    【讨论】:

      【解决方案2】:

      在尝试更困难的事情之前,值得优化情节的参数。 Here is what you can get with an appropriate choice of font size and pctdistance and including explode:

      import matplotlib.pyplot as plt
      
      fig =plt.figure(figsize = (10,10))
      ax11 = fig.add_subplot(111)
      # Data to plot
      sizes = [215, 130, 245, 2000]
      labels = 'Python', 'C++', 'Ruby', 'Java'
      colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
      explode = (0.1, 0, 0, 0)  # explode 1st slice
      
      # Plot
      w,l,p = ax11.pie(sizes,  labels=labels, colors=colors,
              autopct='%1.1f%%', startangle=140, pctdistance=0.65, radius = 1, explode=explode)
      [t.set_rotation(0) for t in p]
      [t.set_fontsize(25) for t in p]
      [t.set_fontsize(25) for t in l]
      plt.axis('equal')
      plt.show()
      

      【讨论】:

      • 非常感谢,当一个人有单独的馅饼时,这也是要记住的尝试。
      • 不客气!我觉得有必要发布这个答案,很高兴它得到了好评。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多