多种好看好玩的词云例子Example

本文介绍使用Python库WordCloud生成词云的各种方法,包括单词词云、阿拉伯文词云、遮罩词云等,并展示了如何调整颜色、字体及布局。

在之前词云可视化的代码中,我们已经大概介绍了词云生成的方法和步骤,这里我们就用了官网的一些例子来展示一下词云的美丽

如果想了解更多有趣的项目和小玩意,都可以来我这里哦通道

Single Word

Make a word cloud with a single word that’s repeated.

import numpy as np
import matplotlib.pyplot as plt
from wordcloud import WordCloud

text = "square"

x, y = np.ogrid[:300, :300]

# 定义一个圆心为(150,150),半径为130的圆,使得我们的mask形状是一个圆
mask = (x - 150) ** 2 + (y - 150) ** 2 > 130 ** 2
mask = 255 * mask.astype(int)

# 设置repeat参数,Make a word cloud with a single word that's repeated.
wc = WordCloud(background_color="white", repeat=True, mask=mask)
# 生成词云
wc.generate(text)

plt.axis("off")
# 采用双线性插值的方法显示颜色
plt.imshow(wc, interpolation="bilinear")
plt.show()

在这里插入图片描述

Create wordcloud with Arabic

我们现在生成阿拉伯文本
这里我们需要两个第三方库

  • bidi.algorithm
  • arabic_reshaper

所以我们首先要install这两个包
pip install python-bidi arabic_reshaper

import os
import codecs
from wordcloud import WordCloud
import arabic_reshaper
from bidi.algorithm import get_display

# get data directory (using getcwd() is needed to support running example in generated IPython notebook) 得到当前地址
d = os.path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
f = codecs.open(os.path.join(d, 'arabicwords.txt'), 'r', 'utf-8')

# Make text readable for a non-Arabic library like wordcloud
text = arabic_reshaper.reshape(f.read())
text = get_display(text)

# Generate a word cloud image
wordcloud = WordCloud(font_path='fonts/NotoNaskhArabic/NotoNaskhArabic-Regular.ttf').generate(text)

# Export to an image
wordcloud.to_file("arabic_example.png")

在这里插入图片描述

Minimal Example

Generating a square wordcloud from the US constitution using default arguments.

import os

from os import path
from wordcloud import WordCloud

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
text = open(path.join(d, 'constitution.txt')).read()

# Generate a word cloud image
wordcloud = WordCloud().generate(text)

# Display the generated image:
# the matplotlib way:
import matplotlib.pyplot as plt
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")

# lower max_font_size
wordcloud = WordCloud(max_font_size=40).generate(text)
plt.figure()
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis("off")
plt.show()

# The pil way (if you don't have matplotlib)
# image = wordcloud.to_image()
# image.show()

在这里插入图片描述
在这里插入图片描述

Masked wordcloud

Using a mask you can generate wordclouds in arbitrary shapes.

from os import path
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import os

from wordcloud import WordCloud, STOPWORDS

# get data directory (using getcwd() is needed to support running example in generated IPython notebook)
d = path.dirname(__file__) if "__file__" in locals() else os.getcwd()

# Read the whole text.
text = open(path.join(d, 'alice.txt')).read()

# read the mask image
# taken from
# http://www.stencilry.org/stencils/movies/alice%20in%20wonderland/255fk.jpg
alice_mask = np.array(Image.open(path.join(d, "alice_mask.png")))

stopwords = set(STOPWORDS)
stopwords.add("said")

wc = WordCloud(background_color="white", max_words=2000, mask=alice_mask,
               stopwords=stopwords, contour_width=3, contour_color='steelblue')

# generate word cloud
wc.generate(text)

# store to file
wc.to_file(path.join(d, "alice.png"))

# show
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.figure()
plt.imshow(alice_mask, cmap
### 如何使用 ECharts 创建美观的图 为了实现一个美观的图,可以基于 `echarts-wordcloud` 插件的功能进行扩展和自定义。以下是详细的说明以及代码示例。 #### 1. 加载必要的库 要使用 ECharts 的功能,需加载基础的 ECharts 库及其插件 `echarts-wordcloud`。可以通过 CDN 或本地文件引入这些资源[^1]。 ```html <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/echarts-wordcloud/dist/wordcloud.min.js"></script> ``` #### 2. 数据准备 的数据结构是一个数组,其中每个元素代表一个单对象,包含两个主要属性:`name` 表示单名称,`value` 表示权重大小。可以根据实际需求调整数据集的内容和规模[^2]。 ```javascript var data = [ { name: 'Example', value: 10 }, { name: 'ECharts', value: 8 }, { name: 'Word Cloud', value: 6 }, { name: 'Open Source', value: 4 } ]; ``` #### 3. 配置项详解 通过设置选项参数来自定义的表现形式。以下是一些常用的配置项: - **sizeRange**: 定义字体大小范围,默认为 `[12, 60]`。 - **rotationRange**: 控制文字旋转角度区间,默认为 `[-90, 90]`。 - **textStyle.color**: 设置颜色函数,支持动态生成不同色彩的文字效果。 - **gridSize**: 调整语之间的间距密度。 下面展示了一组优化后的样式设定: ```javascript option = { series: [{ type: 'wordCloud', shape: 'circle', // 可选形状有 circle | cardioid | diamond 等多种模式 sizeRange: [14, 60], // 字体尺寸变化幅度 rotationRange: [-Math.PI / 6, Math.PI / 6], gridSize: 10, textStyle: { color: function () { return 'hsl(' + Math.random() * 360 + ', 70%, 50%)'; } }, data: data }] }; ``` #### 4. 初始化图表实例并渲染 最后一步是在页面上初始化 ECharts 实例,并调用其 `.setOption()` 方法应用上述配置。 ```javascript // 基于准备好的dom,初始化echarts实例 var myChart = echarts.init(document.getElementById('main')); // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option); ``` 以上即完成了一个基本却具备一定视觉吸引力的图构建过程。开发者还可以进一步探索更多高级特性以满足特定项目的需求。 --- ###
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风信子的猫Redamancy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值