如何通过编号向条形图添加标签。 (在 python 3.7+ 中使用 turtle 从列表中制作直方图)

How to add labels to a bar chart by numbering. (Making histogram from a list using turtle in python 3.7+)

我正在尝试制作一个直方图,其中有一个输入列表,其中包含 1-10 的整数重复 x 次。因此,如果列表中有三个 6,则柱的高度应为 60,因为 20*3=60.

我正在尝试通过按顺序用数字标记来为我的条形图/直方图添加标签,但我不确定如何操作。请参考下图,看看数字应该是什么样子。

def draw_histogram(t,dataList):
    '''draw_histogram(t,dataList) -> None
    uses turtle t to draw a histogram of dataList
    dataList must contain integers from 0-10'''

    t = 0
    while t != 11:

        if dataList.count(t) == 0:
            height = 0
        elif dataList.count(t) == 1:
            height = 20
        elif dataList.count(t) == 2:
            height = 40     
        elif dataList.count(t) == 3:
            height = 60
        elif dataList.count(t) == 4:
            height = 80
        elif dataList.count(t) == 5:
            height = 100
        elif dataList.count(t) == 6:
            height = 120   
        elif dataList.count(t) == 7:
            height = 140
        elif dataList.count(t) == 8:
            height = 160
        elif dataList.count(t) == 9:
            height = 180    
        elif dataList.count(t) == 10:
            height = 200

        bob.forward(height)
        bob.right(90)
        bob.forward(20)
        bob.right(90)
        bob.forward(height)
        bob.right(90)
        bob.forward(20)
        bob.left(180)
        bob.penup()
        bob.forward(35)
        bob.left(90)
        bob.pendown()

        t += 1
    return height

# test suite
import turtle
turtle.setup(600,300) # Change the width of the drawing to 600px and the height to 300px.

wn = turtle.Screen()
bob = turtle.Turtle()
dataList = [6,8,0,7,7,9,2,9,10,4,8,7,6,9,1,4,6,7,5,7,2,10,4,5,5,6,8]

# move bob back a little bit so he has room
bob.penup()
bob.back(200)
bob.pendown()
bob.left(90)

# draw the histogram
draw_histogram(bob,dataList)
wn.mainloop()

Click here for what the finished product should look like.

这是你想要做的吗?

def draw_histogram(t,dataList):
    '''draw_histogram(t,dataList) -> None
    uses turtle t to draw a histogram of dataList
    dataList must contain integers from 0-10'''

    t = 0
    while t != 11:

        if dataList.count(t) == 0:
            height = 0
        elif dataList.count(t) == 1:
            height = 20
        elif dataList.count(t) == 2:
            height = 40     
        elif dataList.count(t) == 3:
            height = 60
        elif dataList.count(t) == 4:
            height = 80
        elif dataList.count(t) == 5:
            height = 100
        elif dataList.count(t) == 6:
            height = 120   
        elif dataList.count(t) == 7:
            height = 140
        elif dataList.count(t) == 8:
            height = 160
        elif dataList.count(t) == 9:
            height = 180    
        elif dataList.count(t) == 10:
            height = 200
        bob.right(90)
        style = ('Courier', 7, 'italic')
        bob.pu()
        bob.forward(7)
        bob.pd()
        bob.write(str(t), font=style, align='center')
        bob.pu()
        bob.back(7)
        bob.pd()
        bob.left(90)
        bob.forward(height)
        bob.right(90)
        bob.forward(20)
        bob.right(90)
        bob.forward(height)
        bob.right(90)
        bob.forward(20)
        bob.left(180)
        bob.penup()
        bob.forward(35)
        bob.left(90)
        bob.pendown()

        t += 1
    return height

# test suite
import turtle
turtle.setup(600,300) # Change the width of the drawing to 600px and the height to 300px.

wn = turtle.Screen()
bob = turtle.Turtle()
dataList = [6,8,0,7,7,9,2,9,10,4,8,7,6,9,1,4,6,7,5,7,2,10,4,5,5,6,8]

# move bob back a little bit so he has room
bob.penup()
bob.back(200)
bob.pendown()
bob.left(90)

# draw the histogram
draw_histogram(bob,dataList)
wn.mainloop()