如何解决如何调用另一个函数来绘制饼图?
所以我尝试使用乌龟在 Python 中绘制饼图。我有绘制图表的功能,但我正在制作一个新功能,该功能将从列表中获取数据并绘制切片,但我不明白如何将两者连接起来。我只能使用 if 语句和 for 循环之类的东西。
我很新,所以我还不能使用任何花哨的东西。我认为对于 draw_pie_chart 中的算法,我将计算列表的总和,然后将第一个数字除以总和,所以 100/400= .25*360= 90。所以我的角度将是 90。现在确定如何执行调用函数进入 pie_chart
的新函数。
def draw_pie_slice(t,center_x,center_y,radius,begin_angle,end_angle,color):
Function draw_pie_slice draws a slice of a pie
Parameters:
t: Reference to a turtle (used for drawing)
center_x: x coordinate of center of pie
center_y: y coordinate of center of pie
radius: radius of pie
begin_angle: angle where pie piece starts - 0 means on the x axis
end_angle: angle where pie piece ends
color: color used to fill the piece
Returns:
nothing
"""
import math
begin_x = radius*math.cos(begin_angle*math.pi/180)
begin_y = radius*math.sin(begin_angle*math.pi/180)
arc_angle = end_angle - begin_angle
t.up()
t.goto(center_x,center_y)
t.down()
t.fillcolor(color) #set the color used for filling
t.begin_fill() #mark the beginning of the polygon to be filled
t.goto(begin_x,begin_y)
t.setheading(90+begin_angle)
t.circle(radius,arc_angle)
t.end_fill() #mark the end of the polygon to be filled. This also causes the polygon drawn to be filled.
"""
def draw_pie_chart(t,r,data):
for r in list ():
def main():
import turtle
t = turtle.Turtle()
draw_pie_slice(t,100,30,"red")
draw_pie_slice(t,90,"blue")
draw_pie_slice(t,170,"green")
draw_pie_chart(100,200,100)
draw_pie_chart(20,20,40,10,30)
main()
"""
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。