谁能帮我清理图表。当我绘制y-xis时,我希望将整数四舍五入到最接近的百分之一。另外,在每个小节下,我都需要标记“ a”-“ z”:总共26个小节:

def letterFreqPlot(freqList):
    border = 5
    t = turtle.Turtle()
    t.pensize(3)
    screen = t.getscreen()
    maxheight = max(freqList)
    numbers = len(freqList)
    screen.setworldcoordinates(0-border,-.05,numbers+1,maxheight+.1)
    t.goto(0,0)
    t.hideturtle()
    t.speed(0)
    t.lt(90)
    t.fd(maxheight)
    t.fd(-maxheight)
    t.right(90)
    for item in freqList:
        t.fillcolor("blue")
        t.begin_fill()
        for dist in [1, item, 1, item]:
            t.fd(dist)
            t.lt(90)
        t.fd(1)
        t.end_fill()
    t.goto(0,0)
    t.lt(90)
    for i in freqList:
        t.fd(i)
        t.lt(90)
        t.fd(3)
        t.write(float(i))
        t.fd(-3)
        t.rt(90)
        t.fd(-i)
    print('Click to exit')
    screen.exitonclick()
freqList = letterFreq(words)
letterFreqPlot(freqList)


freqList:


  [0.09090909090909091、0.0、0.0、0.09090909090909091、0.18181818181818182、0.0、0.0、0.0、0.045454545454545456、0.0、0.0、0.0、0.0、0.045454545454545456、0.045454545454545456、0.045454545454545456、0.045454545454545456456、0.18181818181818182、0.045454454909454、0.045454545909454, ,0.0]

最佳答案

下面是我尝试将所需的功能添加到图形以及稍微优化绘图代码并添加功能的尝试:

from turtle import Turtle, Screen

BORDER = 5
FONT = ("Arial", 12, "normal")
FLOAT_FORMAT = "0.2f"
PENSIZE = 3

def letterFrequency(string):
    counter = dict()

    length = ord('z') - ord('a') + 1

    for ordinal in range(length):
        counter[chr(ordinal + ord('a'))] = 0

    count = 0

    for character in string:
        if character.isalpha():
            counter[character] += 1
            count += 1

    return [counter[key] / count for key in sorted(counter)]


def letterFreqPlot(turtle, canvas, string):

    turtle.pensize(PENSIZE)

    frequencyList = letterFrequency(string)

    maxheight = max(frequencyList)
    numbers = len(frequencyList)

    canvas.setworldcoordinates(-BORDER, -.05, numbers + 1, maxheight + 0.1)

    turtle.hideturtle()

    for frequency in sorted(set(frequencyList)):
        turtle.penup()
        turtle.home()
        turtle.lt(90)
        turtle.pendown()
        turtle.forward(frequency)
        turtle.left(90)
        turtle.forward(3)
        turtle.write(format(frequency, FLOAT_FORMAT), font=FONT)

    turtle.penup()
    turtle.home()
    turtle.sety(-0.0125)

    for i, frequency in enumerate(frequencyList):
        turtle.forward(0.5)
        turtle.write(chr(ord('a') + i), font=FONT)
        turtle.forward(0.5)

    turtle.home()
    turtle.goto(len(frequencyList) / 2, -0.025)
    turtle.write(string, align="center", font=FONT)

    turtle.home()
    turtle.fillcolor("blue")
    turtle.pendown()

    for frequency in frequencyList:
        if frequency > 0.0:
            turtle.begin_fill()
            for distance in [1, frequency] * 2:
                turtle.forward(distance)
                turtle.left(90)
            turtle.end_fill()
        turtle.forward(1)


yertle = Turtle()
yertle.speed("fastest")

screen = Screen()

letterFreqPlot(yertle, screen, "an opportunity to teach is an opportunity to learn")

screen.exitonclick()


我没有使用太多的绘图代码来回溯,而是使用turtle.home()重置起点和方向。我还添加了一个简单的仿真您的letterFrequency()代码的乐趣。我将许多常量汇总在一起,但是在这些方面还有很多事情要做。

python - Python turtle 图-LMLPHP

顺便说一句,您是我所见过的setworldcoordinates()最好的例子之一。

关于python - Python turtle 图,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27259749/

10-10 18:21