【问题标题】:Matplotlib: Draw pie chart with wedge breakdown into barchartMatplotlib:将带有楔形分解的饼图绘制成条形图
【发布时间】:2018-08-15 07:59:37
【问题描述】:

我有一个饼图(示例),下面有fracs = [10, 20, 50, 30]。用 matplotlib 画这个没问题。如何将第一个楔形分解 (10) 分解为64?理想情况下,我希望为20 提供第二个楔形,分解为1037。这将显示为特定楔形附近的条形图或饼图(这将使其成为类似于 Excel 中的饼图)。

【问题讨论】:

    标签: python matplotlib pie-chart


    【解决方案1】:

    这是一种方法(可能不是最好的......)。我已经修改了here, on the matplotlib site 找到的一些代码来创建一个little_pie 函数,它将在任意位置绘制小饼图。

    from pylab import *
    import math
    import numpy as np
    
    def little_pie(breakdown,location,size):
        breakdown = [0] + list(np.cumsum(breakdown)* 1.0 / sum(breakdown))
        for i in xrange(len(breakdown)-1):
            x = [0] + np.cos(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi *    
                              breakdown[i+1], 20)).tolist()
            y = [0] + np.sin(np.linspace(2 * math.pi * breakdown[i], 2 * math.pi * 
                              breakdown[i+1], 20)).tolist()
            xy = zip(x,y)
            scatter( location[0], location[1], marker=(xy,0), s=size, facecolor=
                   ['gold','yellow', 'orange', 'red','purple','indigo','violet'][i%7])
    
    figure(1, figsize=(6,6))
    
    little_pie([10,3,7],(1,1),600)
    little_pie([10,27,4,8,4,5,6,17,33],(-1,1),800)
    
    fracs = [10, 8, 7, 10]
    explode=(0, 0, 0.1, 0)
    pie(fracs, explode=explode, autopct='%1.1f%%')
    show()
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:

        我找不到解决方案,所以我自己破解了。我在 matplotlib.patches 模块中使用了 ConnectionPatch 对象。这允许您在同一图中的不同轴之间绘制线条。下面将在左侧创建一个饼图,在右侧创建一个堆积条形图:

        import matplotlib.pyplot as plt
        from matplotlib.patches import ConnectionPatch
        import numpy as np
        import math
        
        # style choice
        plt.style.use('fivethirtyeight')
        
        # make figure and assign axis objects
        fig = plt.figure(figsize=(15,7.5))
        ax1 = fig.add_subplot(121)
        ax2 = fig.add_subplot(122)
        
        # pie chart parameters
        ratios = [.4, .56, .04]
        labels = ['Approve', 'Disapprove', 'Undecided']
        explode=[0.1,0,0]
        # rotate so that first wedge is split by the x-axis
        angle = -180*ratios[0]
        ax1.pie(ratios, autopct='%1.1f%%', startangle=angle,
                labels=labels,explode=explode )
        
        # bar chart parameters
        
        xpos = 0
        bottom = 0
        ratios = [.33, .54, .07, .06]
        width = .2
        colors = ['y','m','#99ff99','#ffcc99']
        
        for j in range(len(ratios)):
            height = ratios[j]
            ax2.bar(xpos, height, width, bottom=bottom, color=colors[j])
            ypos = bottom + ax2.patches[j].get_height()/2
            bottom += height
            ax2.text(xpos,ypos, "%d%%" %
                (ax2.patches[j].get_height()*100), ha='center')
        
        plt.title('Gender of approvers')
        plt.legend(('Women', 'Men', 'Gender Neutral', 'Alien'))
        plt.axis('off')
        plt.xlim(-2.5*width, 2.5*width)
        

        然后我添加两条线,分别将饼图的第一个楔形与堆积条形图的顶部和底部连接起来:

        # use ConnectionPatch to draw lines between the two plots
        # get the wedge data for the first group
        theta1, theta2 = ax1.patches[0].theta1, ax1.patches[0].theta2
        center, r = ax1.patches[0].center, ax1.patches[0].r
        bar_height = sum([item.get_height() for item in ax2.patches])
        x = r*np.cos(math.pi/180*theta2)+center[0]
        y = np.sin(math.pi/180*theta2)+center[1]
        con = ConnectionPatch(xyA=(-width/2,bar_height), xyB=(x,y),
            coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
        con.set_color([0,0,0])
        con.set_linewidth(4)
        ax2.add_artist(con)
        
        x = r*np.cos(math.pi/180*theta1)+center[0]
        y = np.sin(math.pi/180*theta1)+center[1]
        con = ConnectionPatch(xyA=(-width/2,0), xyB=(x,y),
            coordsA="data", coordsB="data", axesA=ax2, axesB=ax1)
        con.set_color([0,0,0])
        ax2.add_artist(con)
        con.set_linewidth(4)
        
        plt.show()
        

        剧情如下:

        【讨论】:

        猜你喜欢
        • 2018-12-26
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        • 2021-04-22
        • 2018-04-05
        • 1970-01-01
        • 2020-02-28
        相关资源
        最近更新 更多