【发布时间】:2019-11-12 09:45:07
【问题描述】:
我的任务要求我从一个文本文件中打印出饼图上每个大陆 2018 年和 2019 年的人口差异。
饼图我已经做好了。
但是我想将饼图每个部分的百分比添加到饼图本身中。
目前,我有饼图之外的百分比数字。我该怎么做?
目前我的百分比数字在条形图之外,还有大洲名称:
def operation_1():
divisions = [(('Asia',x1_p,"%"), x1), (('North America',x2_p,"%"), x2), (('South America',x4_p,"%"), x4), (('Africa',x5_p,"%"), x5), (('Europe',x6_p,"%"), x6), (('Oceania',x7_p,"%"), x7)]
COLORS = cycle(['red', 'blue', 'green', 'yellow', 'orange', 'brown', 'lightgreen'])
RADIUS = 200
CONTINENT_RADIUS = RADIUS * 1.35
PERCENTAGE_RADIUS = RADIUS * 1.35
FONTSIZE = 15
FONT = ("Arial", FONTSIZE, "bold")
total = sum(fraction for fillcolor, fraction in divisions)
screen = turtle.getscreen()
screen.title("Pie Chart")
screen.setworldcoordinates(-400, -400, 400,400 )
piechart = Turtle()
piechart.penup()
piechart.sety(-RADIUS)
piechart.pendown()
piechart.speed(0)
for fillcolor, fraction in divisions:
piechart.fillcolor(next(COLORS))
piechart.begin_fill()
piechart.circle(RADIUS, fraction * 360 / total)
position = piechart.position()
piechart.goto(0,0)
piechart.end_fill()
piechart.setposition(position)
piechart.penup()
piechart.sety(-CONTINENT_RADIUS)
for continent, fraction in divisions:
piechart.circle(CONTINENT_RADIUS, fraction * 360 / total / 2)
piechart.write(continent, align="center", font=FONT)
piechart.circle(CONTINENT_RADIUS, fraction * 360 / total / 2)
piechart.hideturtle()
x = input("Please input x to initiate another operation: ")
if x.lower() == 'x':
screen.clearscreen()
【问题讨论】:
标签: python function turtle-graphics