Search code examples
pythonpie-chart

Values instead of percentages with pie chart python


I want to show the values in my pie chart rather than the percent. I understand that it involved the autopct part of the code but I am not familiar with how to manipulate this to present the values.

import csv
import matplotlib.pyplot as plt
filename = "crime.csv"
with open(filename) as file:
    data_from_file = csv.reader(file)
    header_row = next(data_from_file)
    
    ucr_ncic_code = [0,0,0,0,0,0,0,0,0]
    for row in data_from_file:
        crime = int(float(row[6]))
        if crime in range(0,999):
            ucr_ncic_code[0] = ucr_ncic_code[0] +1
        elif crime in range(1000,1999):
            ucr_ncic_code[1] = ucr_ncic_code[1] +1
        elif crime in range(2000,2999):
            ucr_ncic_code[2] = ucr_ncic_code[2] +1
        elif crime in range(3000,3999):
            ucr_ncic_code[3] = ucr_ncic_code[3] +1
        elif crime in range(4000,4999):
            ucr_ncic_code[4] = ucr_ncic_code[4] +1
        elif crime in range(5000,5999):
            ucr_ncic_code[5] = ucr_ncic_code[5] +1
        elif crime in range(6000,6999):
            ucr_ncic_code[6] = ucr_ncic_code[6] +1
        elif crime in range(7000,7999):
            ucr_ncic_code[7] = ucr_ncic_code[7] +1
        elif crime in range(8000,8999):
            ucr_ncic_code[8] = ucr_ncic_code[8] +1
        print(ucr_ncic_code)

explode = (0,.25,0,0,0,0,0,0,.25)
fig1,ax1 = plt.subplots()

ax1.pie(ucr_ncic_code,explode = explode, autopct = '%1.1f%%', shadow=False, startangle=45)

ax1.axis('equal')
plt.savefig('myplot')
plt.show()

Solution

  • You'll have to define a function for the autopct parameter:

    def make_autopct(values):
        def my_autopct(pct):
            total = sum(values)
            val = int(round(pct*total/100.0))
            return '{v:d}'.format(v=val)
        return my_autopct
    

    Then add it to ax1.pie, while including your list of values (ucr_ncic_code in this case):

    ax1.pie(ucr_ncic_code, explode=explode, autopct=makeautopct(ucr_ncic_code), shadow=False, startangle=45)
    

    This would make the final code:

    import csv
    import matplotlib.pyplot as plt
    
    def make_autopct(values):
        def my_autopct(pct):
            total = sum(values)
            val = int(round(pct*total/100.0))
            return '{v:d}'.format(v=val)
        return my_autopct
    
    filename = "crime.csv"
    
    with open(filename) as file:
        data_from_file = csv.reader(file)[enter image description here][1]
        header_row = next(data_from_file)
        
        ucr_ncic_code = [0,0,0,0,0,0,0,0,0]
        for row in data_from_file:
            crime = int(float(row[6]))
            if crime in range(0,999):
                ucr_ncic_code[0] = ucr_ncic_code[0] +1
            elif crime in range(1000,1999):
                ucr_ncic_code[1] = ucr_ncic_code[1] +1
            elif crime in range(2000,2999):
                ucr_ncic_code[2] = ucr_ncic_code[2] +1
            elif crime in range(3000,3999):
                ucr_ncic_code[3] = ucr_ncic_code[3] +1
            elif crime in range(4000,4999):
                ucr_ncic_code[4] = ucr_ncic_code[4] +1
            elif crime in range(5000,5999):
                ucr_ncic_code[5] = ucr_ncic_code[5] +1
            elif crime in range(6000,6999):
                ucr_ncic_code[6] = ucr_ncic_code[6] +1
            elif crime in range(7000,7999):
                ucr_ncic_code[7] = ucr_ncic_code[7] +1
            elif crime in range(8000,8999):
                ucr_ncic_code[8] = ucr_ncic_code[8] +1
            print(ucr_ncic_code)
    
    explode = (0,.25,0,0,0,0,0,0,.25)
    fig1,ax1 = plt.subplots()
    
    ax1.pie(ucr_ncic_code, explode=explode, autopct=make_autopct(ucr_ncic_code), shadow=False, startangle=45)
    
    ax1.axis('equal')
    plt.savefig('myplot')
    plt.show()
    

    A run of this code (I had to use random values because you have not provided crime.csv): https://i.sstatic.net/O2jnb.png