我有一个饼图,这是代码
import numpy as np
import matplotlib.pyplot as plt
title = "Population of Singapore by age(2018)"
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
data = np.genfromtxt("ca1_data/population.csv",
delimiter=',',skip_header=1,
dtype=[('Year','i4'),('Race','U50'),('Age','U50'),('Population','i4')],
missing_values=['na','-'],filling_values=[0])
age = data[(data['Year']==2018) & (data['Race']=='Total Citizen') & (data ['Age']!='65 Years & Over') & (data['Age']!='70 Years & Over') & (data['Age']!='75 Years & Over') & (data['Age']!='80 Years & Over')]['Age']
population = data[(data['Year']==2018) & (data['Race']=='Total Citizen') & (data['Age']!='65 Years & Over') & (data['Age']!='70 Years & Over') & (data['Age']!='75 Years & Over') & (data['Age']!='80 Years & Over')]['Population']
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(111)
ax1.pie(population, labels=age,autopct='%1.1f%%',
startangle=90)
plt.title(title)
ax1.axis('equal')
plt.show()
代码确实产生正确的输出,但是有些文本是重叠的,我尝试增加figsize,但是它会使每个文本变小,并且其中一些在标题上方。有更好的方法来解决这个问题吗? 这是年龄的价值
['0 - 4 Years' '5 - 9 Years' '10 - 14 Years' '15 - 19 Years'
'20 - 24 Years' '25 - 29 Years' '30 - 34 Years' '35 - 39 Years'
'40 - 44 Years' '45 - 49 Years' '50 - 54 Years' '55 - 59 Years'
'60 - 64 Years' '65 - 69 Years' '70 - 74 Years' '75 - 79 Years'
'80 - 84 Years' '85 Years & Over']
这是人口的价值
[172621 175796 180222 205723 237046 250958 217874 224625 227556 247329
267845 280396 257548 202905 130055 89536 55232 48669]
谢谢!
更新:
import numpy as np
import matplotlib.pyplot as plt
title = "Population of Singapore by age(2018)"
titlelen = len(title)
print("{:*^{titlelen}}".format(title, titlelen=titlelen+6))
print()
data = np.genfromtxt("ca1_data/population.csv",
delimiter=',',skip_header=1,
dtype=[('Year','i4'),('Race','U50'),('Age','U50'),('Population','i4')],
missing_values=['na','-'],filling_values=[0])
age = ['0 - 4 Years', '5 - 9 Years', '10 - 14 Years', '15 - 19 Years',
'20 - 24 Years', '25 - 29 Years', '30 - 34 Years', '35 - 39 Years',
'40 - 44 Years', '45 - 49 Years', '50 - 54 Years', '55 - 59 Years',
'60 - 64 Years', '65 - 69 Years', '70 - 74 Years', '75 - 79 Years',
'80 - 84 Years', '85 Years & Over']
population = [172621, 175796, 180222, 205723, 237046, 250958, 217874, 224625, 227556, 247329,
267845,280396, 257548, 202905, 130055, 89536, 55232, 48669]
fig = plt.figure(figsize=(20,10))
ax1 = fig.add_subplot(111)
ax1.pie(population, labels=age,autopct='%1.1f%%',
startangle=90)
plt.title(title)
ax1.axis('equal')
plt.show()
这应该是工作代码