import turtle
def BarGraph(turtleOBJ, Bar_height, Bar_color):
turtleOBJ.fillcolor(Bar_color)
turtleOBJ.begin_fill()
turtleOBJ.left(90)
turtleOBJ.forward(Bar_height)
turtleOBJ.write(str(Bar_height))
turtleOBJ.right(90)
turtleOBJ.forward(80)
turtleOBJ.right(90)
turtleOBJ.forward(Bar_height)
turtleOBJ.left(90)
turtleOBJ.end_fill()
Bar_heights = [23, 94, 42, 150, 200, 56, 240,40]
Bar_color = ["orange", "purple", "green", "red", "black", "grey", "white", "violet"]
maxBarVal = max(Bar_heights)
Graph_Range = len(Bar_heights)
Space = 20
screen = turtle.Screen()
screen.setworldcoordinates(0 - Space, 0 - Space, 50 * Space, maxBarVal + Space)
screen.bgcolor("Brown")
turtleOBJ = turtle.Turtle()
turtleOBJ.pensize(3)
for bar in range(len(Bar_heights)):
BarGraph(turtleOBJ, Bar_heights[bar], Bar_color[bar])
screen.exitonclick()
Turtle函数介绍:
Turtle() − 此函数创建一个新的乌龟对象。
fillcolor() − 此函数将海龟的颜色设置为填充条的颜色。
begin_fill() − 此函数开始填充过程并记住起始点。
left() − 这个函数使乌龟向左转90度。
right() − 这个函数使乌龟向右转90度。
forward() − 该函数使海龟向前移动指定的单位。
write() − 此函数将在柱状图上写入一个字符串(高度值)。
end_fill() − 此函数关闭图形,并停止填充过程。

