Different color for each segment of a pie chart using turtle in Python

前端 未结 1 449
心在旅途
心在旅途 2021-01-17 01:47
import turtle

from turtle import *
##lets draw a pie chart first
##segment_labels = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\",\"g\",\"h\",\"i\",\"j\",\"k\",\"l\",\"m\",\         


        
相关标签:
1条回答
  • 2021-01-17 02:11

    Before we talk about code, let's discuss data. I understand separating out color assignments as they're arbitray, but the letters and frequencies are intimately tied together so their data structure should reflect that -- instead of separate lists, I've made them a single list of tuples:

    letter_frequencies = [('a', 10.52), ('b', 1.94), ('c', 6.91), ('d', 6.83), ...]
    

    Your frequencies don't add up to 100, or anything close, so they aren't percentages as your variable names imply. To work around this, we total them and treat them as fractions of that total. Confirming my belief about comments in general, your solitary code comment is of no value to the implementation of this problem:

    radius = 100 ## circumference of a circle = 2pie*r
    

    Below, I break the problem into two steps: first, draw the color slices in proportion to the frequencies; second, write letter labels around the outside of the chart. Some letter frequencies are so small that they only show up as a line on the pie chart so we can't label inside the chart. Increasing the radius of the pie chart helps a little.

    The key to drawing pie slices is to use the extent argument to the turtle circle() function to draw an arc of appropriate size. We then tie that arc into the center of the circle to make a slice.

    ''' Let's draw a pie chart '''
    
    from turtle import Turtle, Screen
    from itertools import cycle
    
    letter_frequencies = [ \
        ('a', 10.52), ('b', 1.94), ('c', 6.91), ('d', 6.83), ('e', 22.65), \
        ('f', 9.42), ('g', 4.1), ('h', 4.68), ('i', 11.92), ('j', 0.56), \
        ('k', 1.2), ('l', 10.8), ('m', 3.29), ('n', 11.33), ('o', 12.95), \
        ('p', 5.83), ('q', 0.01), ('r', 11.14), ('s', 14.11), ('t', 14.69), \
        ('u', 4.05), ('v', 1.93), ('w', 2.96), ('x', 2.78), ('y', 3.02), ('z', 0.16)]
    
    COLORS = cycle(['yellow', 'green', 'red', 'cyan', 'white', 'blue', 'mediumpurple'])
    
    RADIUS = 175
    LABEL_RADIUS = RADIUS * 1.33
    FONTSIZE = 18
    FONT = ("Ariel", FONTSIZE, "bold")
    
    # The pie slices
    
    total = sum(fraction for _, fraction in letter_frequencies)  # data doesn't sum to 100 so adjust
    
    baker = Turtle()  # because we're baking a pie
    baker.penup()
    baker.sety(-RADIUS)
    baker.pendown()
    
    for _, fraction in letter_frequencies:
        baker.fillcolor(next(COLORS))
        baker.begin_fill()
        baker.circle(RADIUS, fraction * 360 / total)
        position = baker.position()
        baker.goto(0, 0)
        baker.end_fill()
        baker.setposition(position)
    
    # The labels
    
    baker.penup()
    baker.sety(-LABEL_RADIUS)
    
    for label, fraction in letter_frequencies:
        baker.circle(LABEL_RADIUS, fraction * 360 / total / 2)
        baker.write(label, align="center", font=FONT)
        baker.circle(LABEL_RADIUS, fraction * 360 / total / 2)
    
    baker.hideturtle()
    
    screen = Screen()
    screen.exitonclick()
    

    This produces a crude pie chart that you'll want to fine tune to your specific needs:

    0 讨论(0)
提交回复
热议问题