日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python画各种统计图的特点_Python 分词并画出词频统计图 | 睿鑫网络

發(fā)布時間:2023/12/3 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python画各种统计图的特点_Python 分词并画出词频统计图 | 睿鑫网络 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

import turtle

##全局變量##

#詞頻排列顯示個數(shù)

count = 10

#單詞頻率數(shù)組-作為y軸數(shù)據(jù)

data = []

#單詞數(shù)組-作為x軸數(shù)據(jù)

words = []

#y軸顯示放大倍數(shù)-可以根據(jù)詞頻數(shù)量進行調(diào)節(jié)

yScale = 2

#x軸顯示放大倍數(shù)-可以根據(jù)count數(shù)量進行調(diào)節(jié)

xScale = 30

################# Turtle Start ####################

#從點(x1,y1)到(x2,y2)繪制線段

def drawLine(t, x1, y1, x2, y2):

t.penup()

t.goto (x1, y1)

t.pendown()

t.goto (x2, y2)

# 在坐標(x,y)處寫文字

def drawText(t, x, y, text):

t.penup()

t.goto (x, y)

t.pendown()

t.write(text)

def drawGraph(t):

#繪制x/y軸線

drawLine (t, 0, 0, 360, 0)

drawLine (t, 0, 300, 0, 0)

#x軸: 坐標及描述

for x in range(count):

x=x+1 #向右移一位,為了不畫在原點上

drawText(t, x*xScale-4, -20, (words[x-1]))

drawText(t, x*xScale-4, data[x-1]*yScale+10, data[x-1])

drawBar(t)

#繪制一個柱體

def drawRectangle(t, x, y):

x = x*xScale

y = y*yScale#放大倍數(shù)顯示

drawLine(t, x-5, 0, x-5, y)

drawLine(t, x-5, y, x+5, y)

drawLine(t, x+5, y, x+5, 0)

drawLine(t, x+5, 0, x-5, 0)

#繪制多個柱體

def drawBar(t):

for i in range(count):

drawRectangle(t, i+1, data[i])

################# Turtle End ####################

#對文本的每一行計算詞頻的函數(shù)

def processLine(line, wordCounts):

#用空格替換標點符號

line = replacePunctuations(line)

#從每一行獲取每個詞

words = line.split()

for word in words:

if word in wordCounts:

wordCounts[word] += 1

else:

wordCounts[word] = 1

#空格替換標點的函數(shù)

def replacePunctuations(line):

for ch in line:

if ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":

line = line.replace(ch, " ")

return line

def main():

#用戶輸入一個文件名

filename = input("enter a filename[GPL]:").strip() or "GPL"

infile = open(filename, "r")

#建立用于計算詞頻的空字典

wordCounts = {}

for line in infile:

processLine(line.lower(), wordCounts)

#從字典中獲取數(shù)據(jù)對

pairs = list(wordCounts.items())

#列表中的數(shù)據(jù)對交換位置,數(shù)據(jù)對排序

items = [[x,y]for (y,x)in pairs]

items.sort()

#輸出count個數(shù)詞頻結(jié)果

for i in range(len(items)-1, len(items)-count-1, -1):

print(items[i][1]+"\t"+str(items[i][0]))

data.append(items[i][0])

words.append(items[i][1])

infile.close()

#根據(jù)詞頻結(jié)果繪制柱狀圖

turtle.title('詞頻結(jié)果柱狀圖')

turtle.setup(1000, 900, 0, 0)

t = turtle.Turtle()

t.hideturtle()

t.width(3)

drawGraph(t)

#調(diào)用main()函數(shù)

if __name__ == '__main__':

main()

input("按任意鍵結(jié)束")

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的python画各种统计图的特点_Python 分词并画出词频统计图 | 睿鑫网络的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。