值而不是饼图的百分比 python

Values instead of percentages with pie chart python

我想在饼图中显示值而不是百分比。我知道它涉及代码的 autopct 部分,但我不熟悉如何操作它来呈现值。

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()

您必须为 autopct 参数定义一个函数:

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

然后将其添加到 ax1.pie,同时包括您的值列表(在本例中为 ucr_ncic_code):

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

这将使最终代码成为:

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 运行 这段代码(我不得不使用随机值,因为你没有提供 crime.csv):https://i.stack.imgur.com/O2jnb.png