How to have actual values in matplotlib Pie Chart displayed (Python)?

前端 未结 1 2039
一整个雨季
一整个雨季 2020-12-16 18:43

I have a pie chart drawing the values extracted from a CSV file. The proportion of the values are currently displayed with the percentage displayed \"autopct=\'%1.1f%%\'\".

相关标签:
1条回答
  • 2020-12-16 19:23

    Using the autopct keyword

    As we know that the percentage shown times the sum of all actual values must be the actual value, we can define this as a function and supply this function to plt.pie using the autopct keyword.

    import matplotlib.pyplot as plt
    import numpy
    
    labels = 'Frogs', 'Hogs', 'Dogs'
    sizes = numpy.array([5860, 677, 3200])
    colors = ['yellowgreen', 'gold', 'lightskyblue']
    
    def absolute_value(val):
        a  = numpy.round(val/100.*sizes.sum(), 0)
        return a
    
    plt.pie(sizes, labels=labels, colors=colors,
            autopct=absolute_value, shadow=True)
    
    plt.axis('equal')
    plt.show()
    

    Care must be taken since the calculation involves some error, so the supplied value is only accurate to some decimal places.

    A little bit more advanced may be the following function, that tries to get the original value from the input array back by comparing the difference between the calculated value and the input array. This method does not have the problem of inaccuracy but relies on input values which are sufficiently distinct from one another.

    def absolute_value2(val):
        a  = sizes[ numpy.abs(sizes - val/100.*sizes.sum()).argmin() ]
        return a
    

    Changing text after pie creation

    The other option is to first let the pie being drawn with the percentage values and replace them afterwards. To this end, one would store the autopct labels returned by plt.pie() and loop over them to replace the text with the values from the original array. Attention, plt.pie() only returns three arguments, the last one being the labels of interest, when autopct keyword is provided so we set it to an empty string here.

    labels = 'Frogs', 'Hogs', 'Dogs'
    sizes = numpy.array([5860, 677, 3200])
    colors = ['yellowgreen', 'gold', 'lightskyblue']
    
    p, tx, autotexts = plt.pie(sizes, labels=labels, colors=colors,
            autopct="", shadow=True)
    
    for i, a in enumerate(autotexts):
        a.set_text("{}".format(sizes[i]))
    
    plt.axis('equal')
    plt.show()
    
    0 讨论(0)
提交回复
热议问题