MedaUser
MedaUser

Reputation: 133

Python turtle graph

Can anyone help me clean up my graph. When I plot the y-xis, I want the integers rounded to the nearest hundredth. Also, under each bar I need to label 'a' - 'z': 26 bars total:

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.045454545454545456, 0.18181818181818182, 0.045454545454545456, 0.09090909090909091, 0.045454545454545456, 0.0, 0.045454545454545456, 0.0, 0.0, 0.0]

Upvotes: 1

Views: 2649

Answers (1)

cdlane
cdlane

Reputation: 41872

Below is my attempt to add the features you wanted to the graph as well as optimize the drawing code a bit and add a feature:

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()

Rather than having the drawing code backtrack so much, I made use of turtle.home() to reset the starting point and orientation. I also added a simple minded emulation of your letterFrequency() code for fun. I gathered many of the constants together but there's more to do along those lines.

enter image description here

BTW, yours is one of the best examples of setworldcoordinates() I've seen.

Upvotes: 1

Related Questions