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","n","o","p","q","r","s","t","u","v","w","x","y","z"]
segment_labels= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
prob_of_letter= [10.52,1.94,6.91,6.83,22.65,9.42,4.10,4.68,11.92,.56,1.20,10.80,3.29,11.33,12.95,5.83,.01,11.14,14.11,14.69,4.05,1.93,2.96,2.78,3.02,.16] ##we can get these number from the frequency coding part.
colors=['yellow','green','red','black','yellow','blue','red','white','yellow',
'green','red','black','yellow','blue','red','white','yellow','green','red','bla ck','yellow','blue','red','white','red','mediumpurple']
radius = 100 ## circumference of a circle = 2pie*r
penup()
forward(radius)
left(90)
pendown()
color('gray')
begin_fill()
circle(radius)
end_fill()
home()
right(90)
color('darkblue')
def letter(prob_of_letter):
perc=0
radius =100
for percent in prob_of_letter:
letter = percent * 360
perc += letter
setheading(perc)
pendown()
forward(radius)
penup()
home()
letter(prob_of_letter)
如何从.txt文件中最常出现的字母表中绘制饼图?
答案 0 :(得分:0)
在我们谈论代码之前,让我们讨论一下数据。我理解在分配颜色分配时将它们分开,但是字母和频率紧密地联系在一起,因此它们的数据结构应该反映出 - 而不是单独的列表,我已经将它们作为单个元组列表:
letter_frequencies = [('a', 10.52), ('b', 1.94), ('c', 6.91), ('d', 6.83), ...]
您的频率不等于100或任何接近的频率,因此它们不是变量名称所暗示的百分比。为了解决这个问题,我们将它们合计并将它们视为总数的一部分。确认我对一般评论的看法,你的单独代码评论对这个问题的实现毫无价值:
radius = 100 ## circumference of a circle = 2pie*r
下面,我将问题分为两个步骤:首先,根据频率绘制颜色切片;第二,在图表外围写字母标签。有些字母频率非常小,只能在饼图上显示为一条线,因此我们无法在图表内标记。增加饼图的半径会有所帮助。
绘制饼图切片的关键是使用turtle extent
函数的circle()
参数绘制适当大小的弧。然后我们将该弧线连接到圆的中心以形成切片。
''' 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()
这会产生一个粗略的饼图,您可以根据自己的特定需求进行微调: