我想在饼图中显示值,而不是百分比。我知道它涉及到代码的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_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参数定义一个函数:
然后将其添加到
ax1.pie
,同时包括您的值列表(本例中为ucr_ncic_code
):这将使最终代码:
这段代码的运行(我不得不使用随机值,因为您没有提供crime.csv):https://i.stack.imgur.com/O2jnb.png
相关问题 更多 >
编程相关推荐