您决定使用以下命令来跟踪每天每种类型披萨的销售数量: 堆积条形图。使用turtle模块绘制条形图如下 要求:
def draw_bar_chart(record_date, large_thick, large_thin, medium_thick, medium_thin):
# your logic here
# you don’t need to return anything
这个问题我已经被困了两天了。我弄清楚了绘图部分,但填充颜色和其他部分对我来说有点太多了。有人可以帮助我吗?
图片:
这是一种方法。
begin_fill()
和 end_fill()
的方式是使用 begin_fill()
,然后绘制一个封闭的形状,然后使用 end_fill()
填充该形状。 .write()
功能可以为您绘制文字和数字。
#Setting up the turtle
import turtle
t = turtle.Pen()
#Moving the turtle down
t.right(90)
t.penup()
t.forward(100)
t.pendown()
t.left(90)
#Function for drawing the bars
def draw_bar_chart(record_date, large_thick, large_thin, medium_thick, medium_thin):
#Writing the date at the bottom
t.write(record_date)
#Making sure you don't write over the date
t.left(90)
t.penup()
t.forward(30)
t.pendown()
t.right(90)
#Making the bars
for count in range(0, 4):
#Add your colors here
color_list = ["red", "orange", "yellow", "green"]
bars_list = [large_thick, large_thin, medium_thick, medium_thin]
#Picking the color and starting the fill
t.color(color_list[count])
t.begin_fill()
t.forward(20)
t.left(90)
#You may need to change the 10 here to a bigger or smaller number depending on your number
t.forward(bars_list[count] * 10)
t.left(90)
t.forward(20)
t.left(90)
#You may need to change the 10 here to a bigger or smaller number depending on your number
t.forward(bars_list[count] * 10)
t.right(180)
#You may need to change the 10 here to a bigger or smaller number depending on your number
t.forward(bars_list[count] * 10)
t.right(90)
t.end_fill()
#Writing the amount of pizzas
t.color("black")
t.write (large_thick + large_thin + medium_thick + medium_thin, 8)
t.hideturtle()
#Using the function
draw_bar_chart("4/29/24", 10, 7, 6, 8)