python turtle绘制柱状图_使用Python的turtle模块画出简单的柱状图

importturtle

heights= [856, 420,360,260,205]defmain():

t=turtle.Turtle()

t.hideturtle()for i in range(5):

drawFilledRectangle(t,-200+(76*i),0,76,heights[i]/4,"black","light blue")

displayText(t)def drawFilledRectangle(t,x,y,w,h,colorP="black",colorF="white"):

t.pencolor(colorP)

t.fillcolor(colorF)

t.up()

t.goto(x,y)

t.down()

t.begin_fill()

t.goto(x+w,y)

t.goto(x+w,y+h)

t.goto(x,y+h)

t.goto(x,y)

t.end_fill()defdisplayText(t):

languages= ["haha1", "haha2", "haha3", "haha4", "haha5"]

t.pencolor("blue")

t.up()for i in range(5):

t.goto((-162+76*i),heights[i] / 4)

t.write(str(heights[i]),align="center",font=("Arial",10,"normal"))

t.goto((-162+76*i),10)

t.write(languages[i],align="center",font=("Arial",10,"normal"))

t.goto(-200,-25)

t.write("haha 统计图",font=("Arial",10,"normal"))

t.goto(-200,-45)

t.write('(哈哈哈哈哈啊哈哈)',font=("Arial",10,"normal"))

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我来帮您解决这个问题。 首先,您需要准备一份《哈姆雷特》英文剧本的文本文件,我们将使用Python来读取并统计其中单词现的频率。您可以在网上找到该文件并下载到您的计算机上,或者手动创建一个文本文件并将剧本内容复制粘贴到文件中。 接下来,我们需要编写Python代码来读取文件并统计单词现的频率。以下是一个示例代码: ```python import string import turtle # 定义一个函数来读取文件并统计单词频率 def count_words(filename): # 打开文件并读取内容 with open(filename, 'r') as f: text = f.read() # 将文本转换为小写并删除标点符号 text = text.lower() text = text.translate(str.maketrans('', '', string.punctuation)) # 将文本分割成单词列表 words = text.split() # 创建一个空字典来存储单词频率 word_freq = {} # 统计每个单词的现次数 for word in words: if word in word_freq: word_freq[word] += 1 else: word_freq[word] = 1 # 返回单词频率字典 return word_freq # 调用函数并获取单词频率字典 word_freq = count_words('hamlet.txt') # 将单词频率字典转换为元组列表并按频率降序排序 freq_list = [(k, v) for k, v in word_freq.items()] freq_list.sort(key=lambda x: x[1], reverse=True) # 使用turtle模块绘制 t = turtle.Turtle() t.speed(0) # 定义柱参数 bar_width = 20 bar_gap = 5 bar_height = 10 x_pos = -200 y_pos = -200 # 绘制 for i in range(20): t.penup() t.goto(x_pos, y_pos) t.pendown() t.write(freq_list[i][0], align='right', font=('Arial', 12, 'normal')) t.penup() t.goto(x_pos + bar_width, y_pos) t.pendown() t.begin_fill() t.fillcolor('red') t.setheading(90) t.forward(freq_list[i][1] * bar_height) t.right(90) t.forward(bar_width) t.right(90) t.forward(freq_list[i][1] * bar_height) t.end_fill() x_pos += bar_width + bar_gap turtle.done() ``` 在上面的代码中,我们首先定义了一个名为`count_words`的函数,该函数接受一个文件名作为参数,读取文件内容并统计单词频率。我们使用Python的`string`模块来删除标点符号,并将文本转换为小写字母以避免大小写问题。 接下来,我们使用Python的`split`方法将文本分割成单词列表,并使用一个字典来统计每个单词的现次数。最后,我们将单词频率字典转换为元组列表,并按频率降序排序。 接下来,我们使用turtle模块绘制。我们首先定义了柱的参数,然后使用一个循环来绘制每个单词的柱。我们使用`turtle`的`write`方法来绘制单词标签,并使用`turtle`的`begin_fill`和`end_fill`方法来填充柱的颜色。 最后,我们调用了`turtle`的`done`方法来显示绘制的柱。 请注意,上述代码仅绘制了前20个现频率最高的单词。如果您想绘制更多的单词,请将循环中的范围调整为所需数量。 希望这可以帮助您回答问题,如果您还有任何问题,请随时问我!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值