stylecloud库
简介
stylecloud
是一个用于生成风格化云图(Stylized Word Clouds)的 Python 库。它基于wordcloud
库,并添加了一些额外的功能,如支持图标字体(如 Font Awesome)和更丰富的颜色方案。
安装
pip install stylecloud
基本使用
import stylecloud
# 生成词云图
stylecloud.gen_stylecloud(
text="Python is a powerful programming language. Python is widely used for web development, data analysis, artificial intelligence, and more.",
icon_name="fas fa-laptop-code", # 使用 Font Awesome 图标
output_name="stylecloud_example.png" # 输出文件名
)
参数说明
gen_stylecloud
函数有许多参数,以下是一些常用的参数:
text: 输入的文本,用于生成词云。
icon_name: 使用的图标名称,格式为
"fas fa-icon-name"
或"fab fa-icon-name"
,来自 Font Awesome。output_name: 输出的文件名。
palette: 颜色调色板,可以是
"cartocolors.qualitative.Bold_5"
等预定义的调色板。background_color: 背景颜色,默认为白色。
gradient: 是否使用渐变颜色,可以是
"linear"
或"radial"
。font_path: 自定义字体文件路径。
max_font_size: 最大字体大小。
max_words: 最大词数。
stopwords: 停用词列表,用于过滤不需要的词。
使用自定义调色板和渐变
展示如何使用自定义调色板和渐变:
import stylecloud
# 生成词云图
stylecloud.gen_stylecloud(
text="Python is a powerful programming language. Python is widely used for web development, data analysis, artificial intelligence, and more.",
icon_name="fas fa-code",
palette="cartocolors.qualitative.Bold_5", # 使用预定义的调色板
background_color="black", # 黑色背景
gradient="linear", # 线性渐变
output_name="stylecloud_gradient_example.png"
)
使用自定义字体
import stylecloud
# 生成词云图
stylecloud.gen_stylecloud(
text="Python is a powerful programming language. Python is widely used for web development, data analysis, artificial intelligence, and more.",
icon_name="fas fa-code",
font_path="path/to/your/font.ttf", # 自定义字体文件路径
output_name="stylecloud_custom_font_example.png"
)
pytagcloud库
简介
pytagcloud
是一个用于生成标签云(Tag Cloud)的 Python 库。标签云是一种可视化文本数据的方式,其中词频较高的词会以较大的字体显示
安装:
pip install pytagcloud
基本使用
from pytagcloud import create_tag_image, make_tags
from pytagcloud.lang.counter import get_tag_counts
# 示例文本
text = """Python is a powerful programming language. Python is widely used for web development, data analysis, artificial intelligence, and more. Python is easy to learn and has a large community."""
# 获取词频
tags = make_tags(get_tag_counts(text), maxsize=100)
# 生成标签云图像
create_tag_image(tags, 'tagcloud_example.png', size=(900, 600), fontname='Lobster')
参数说明
make_tags
和create_tag_image
函数有许多参数,以下是一些常用的参数:
get_tag_counts(text): 获取文本中每个词的词频。
make_tags(tags, maxsize): 生成标签列表,
maxsize
是最大字体大小。create_tag_image(tags, output_filename, size, fontname): 生成标签云图像。
tags: 标签列表。
output_filename: 输出的文件名。
size: 图像的尺寸,格式为
(width, height)
。fontname: 使用的字体名称。
使用自定义颜色和字体
from pytagcloud import create_tag_image, make_tags
from pytagcloud.lang.counter import get_tag_counts
from pytagcloud.colors import COLOR_SCHEMES
# 示例文本
text = """Python is a powerful programming language. Python is widely used for web development, data analysis, artificial intelligence, and more. Python is easy to learn and has a large community."""
# 获取词频
tags = make_tags(get_tag_counts(text), maxsize=100)
# 使用自定义颜色方案
colors = COLOR_SCHEMES['audacity']
# 生成标签云图像
create_tag_image(tags, 'tagcloud_custom_example.png', size=(900, 600), fontname='Lobster', colors=colors)
使用自定义字体
from pytagcloud import create_tag_image, make_tags
from pytagcloud.lang.counter import get_tag_counts
# 示例文本
text = """Python is a powerful programming language. Python is widely used for web development, data analysis, artificial intelligence, and more. Python is easy to learn and has a large community."""
# 获取词频
tags = make_tags(get_tag_counts(text), maxsize=100)
# 生成标签云图像
create_tag_image(tags, 'tagcloud_custom_font_example.png', size=(900, 600), fontname='path/to/your/font.ttf')
暂时先到这啦!!!