问题描述
所以我尝试使用乌龟在 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 (将#修改为@)