Python 用饼图代替百分比的值
我想在饼图中显示值,而不是百分比。我知道它涉及到代码的autoct部分,但我不熟悉如何操作它来表示值Python 用饼图代替百分比的值,python,pie-chart,Python,Pie Chart,我想在饼图中显示值,而不是百分比。我知道它涉及到代码的autoct部分,但我不熟悉如何操作它来表示值 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_
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()
您必须为AutoCT参数定义一个函数:
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\u ncic\u 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()
运行此代码(我必须使用随机值,因为您没有提供crime.csv):
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()