失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python程序创建词云 中国地图_就这么简单!使用Python生成漂亮的词云

python程序创建词云 中国地图_就这么简单!使用Python生成漂亮的词云

时间:2023-01-01 17:58:58

相关推荐

python程序创建词云 中国地图_就这么简单!使用Python生成漂亮的词云

原标题:就这么简单!使用Python生成漂亮的词云

作者:Linux迷

链接:

词云是一种数据可视化技术,用于表示文本数据,其中每个单词的大小表示其出现的频率或重要性。可以使用词云突出显示重要的文本数据点。词云被广泛用于分析来自社交网络网站的数据。

为了在Python中生成词云,需要的模块是– matplotlib,pandas和wordcloud。要安装这些软件包,请运行以下命令:

pip install matplotlib

pip install pandas

pip install wordcloud

代码1:字数

可以设置要在tagcloud上显示的最大单词数。为此,请使用WordCloud函数的max_words关键字参数。

# importing the necessery modules

fromwordcloud importWordCloud

importmatplotlib.pyplot asplt

importcsv

# file object is created

file_ob = open( r".csv")

# reader object is created

reader_ob = csv.reader(file_ob)

# contents of reader object is stored .

# data is stored in list of list format.

reader_contents = list(reader_ob)

# empty string is declare

text = ""

# iterating through list of rows

forrow inreader_contents :

# iterating through words in the row

forword inrow :

# concatenate the words

text = text + " "+ word

# show only 50 words in the wordcloud .

wordcloud = WordCloud(width= 480, height= 480, max_words= 50).generate(text)

# plot the WordCloud image

plt.figure

plt.imshow(wordcloud, interpolation= "bilinear")

plt.axis( "off")

plt.margins(x= 0, y= 0)

plt.show

输出如下图:

代码2:删除一些单词

可以删除一些我们不想显示的词。为此,请将这些单词传递给WordCloud函数的停用词列表参数。

# importing the necessery modules

fromwordcloud importWordCloud

importmatplotlib.pyplot asplt

importcsv

# file object is created

file_ob = open( r".csv")

# reader object is created

reader_ob = csv.reader(file_ob)

# contents of reader object is stored .

# data is stored in list of list format.

reader_contents = list(reader_ob)

# empty string is declare

text = ""

# iterating through list of rows

forrow inreader_contents :

# iterating through words in the row

forword inrow :

# concatenate the words

text = text + " "+ word

# remove Python , Matplotlib , Color Words from WordCloud .

wordcloud = WordCloud(width= 480, height= 480,

stopwords=[ "Python", "Matplotlib", "Color"]).generate(text)

# plot the WordCloud image

plt.figure

plt.imshow(wordcloud, interpolation= "bilinear")

plt.axis( "off")

plt.margins(x= 0, y= 0)

plt.show

输出效果如下:

代码3:更改背景

我们可以更改wordcloud背景的颜色。为此,请使用WordCloud函数的background_color关键字参数。

# importing the necessery modules

fromwordcloud importWordCloud

importmatplotlib.pyplot asplt

importcsv

# file object is created

file_ob = open( r".csv")

# reader object is created

reader_ob = csv.reader(file_ob)

# contents of reader object is stored .

# data is stored in list of list format.

reader_contents = list(reader_ob)

# empty string is declare

text = ""

# iterating through list of rows

forrow inreader_contents :

# iterating through words in the row

forword inrow :

# concatenate the words

text = text + " "+ word

wordcloud = WordCloud(width= 480, height= 480, background_color= "pink").generate(text)

# plot the WordCloud image

plt.figure

plt.imshow(wordcloud, interpolation= "bilinear")

plt.axis( "off")

plt.margins(x= 0, y= 0)

plt.show

输出效果如下:

代码4:更改单词的颜色

我们可以使用WordCloud函数的colormap关键字参数来更改单词的颜色。

# importing the necessery modules

fromwordcloud importWordCloud

importmatplotlib.pyplot asplt

importcsv

# file object is created

file_ob = open( r".csv")

# reader object is created

reader_ob = csv.reader(file_ob)

# contents of reader object is stored .

# data is stored in list of list format.

reader_contents = list(reader_ob)

# empty string is declare

text = ""

# iterating through list of rows

forrow inreader_contents :

# iterating through words in the row

forword inrow :

# concatenate the words

text = text + " "+ word

wordcloud = WordCloud(width= 480, height= 480, colormap= "Oranges_r").generate(text)

# plot the WordCloud image

plt.figure

plt.imshow(wordcloud, interpolation= "bilinear")

plt.axis( "off")

plt.margins(x= 0, y= 0)

plt.show

输出效果如下:

代码5:设置最大和最小字体

我们可以控制wordcloud的最小和最大字体大小。为此,请使用WordCloud函数的max_font_size和min_font_size关键字参数。

# importing the necessery modules

fromwordcloud importWordCloud

importmatplotlib.pyplot asplt

importcsv

# file object is created

file_ob = open( r".csv")

# reader object is created

reader_ob = csv.reader(file_ob)

# contents of reader object is stored .

# data is stored in list of list format.

reader_contents = list(reader_ob)

# empty string is declare

text = ""

# iterating through list of rows

forrow inreader_contents :

# iterating through words in the row

forword inrow :

# concatenate the words

text = text + " "+ word

wordcloud = WordCloud(width= 480, height= 480, max_font_size= 20, min_font_size= 10).generate(text)

plt.figure

plt.imshow(wordcloud, interpolation= "bilinear")

plt.axis( "off")

plt.margins(x= 0, y= 0)

plt.show

OK,暂时先这样,中文乱码解决等请继续关注我们,谢谢阅读。返回搜狐,查看更多

责任编辑:

如果觉得《python程序创建词云 中国地图_就这么简单!使用Python生成漂亮的词云》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。