From 4c7f8abce703218cc51a83ca8196535007b210e3 Mon Sep 17 00:00:00 2001 From: Mathew Shen Date: Tue, 17 Dec 2024 16:43:51 +0800 Subject: [PATCH] feat(wordcloud): add wordcloud_chineseize (#15) * feat(wordcloud): add wordcloud_chineseize --- CHANGELOG.md | 4 ++++ README.md | 5 +++++ tests/test_wordcloud.py | 10 ++++++++++ zhplot/__init__.py | 2 ++ zhplot/wordcloud.py | 10 ++++++++++ 5 files changed, 31 insertions(+) create mode 100644 tests/test_wordcloud.py create mode 100644 zhplot/wordcloud.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 40467b6..81f593f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ ## [Unreleased] +### Added +- Support chinese display in `wordcloud` + + ## [0.1.1] - 2024-12-01 ### Changed diff --git a/README.md b/README.md index b10da4a..16ee578 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,10 @@ uv add zhplot import matplotlib.pyplot as plt ``` +### 支持的框架 +- [x] [matplotlib](https://github.com/matplotlib/matplotlib) +- [x] [wordcloud](https://github.com/amueller/word_cloud) + ### 一个简单的例子
@@ -56,6 +60,7 @@ plt.ylabel('纵坐标') plt.show() ``` + ## 相似项目 - [japanize-matplotlib](https://github.com/uehara1414/japanize-matplotlib) - [chineseize-matplotlib](https://github.com/cndeng/chineseize-matplotlib) diff --git a/tests/test_wordcloud.py b/tests/test_wordcloud.py new file mode 100644 index 0000000..7c87f6d --- /dev/null +++ b/tests/test_wordcloud.py @@ -0,0 +1,10 @@ +import os + +import zhplot # noqa + + +def test_chinese_font_config(): + """Test if wordcloud is configured to use Chinese fonts.""" + font_path_env = os.getenv("FONT_PATH") + assert font_path_env is not None + assert "NotoSansSC-Regular" in font_path_env diff --git a/zhplot/__init__.py b/zhplot/__init__.py index f754475..f74e3a3 100644 --- a/zhplot/__init__.py +++ b/zhplot/__init__.py @@ -1,5 +1,7 @@ from .matplotlib import matplotlib_chineseize +from .wordcloud import wordcloud_chineseize __all__ = [ "matplotlib_chineseize", + "wordcloud_chineseize", ] diff --git a/zhplot/wordcloud.py b/zhplot/wordcloud.py new file mode 100644 index 0000000..b1152a3 --- /dev/null +++ b/zhplot/wordcloud.py @@ -0,0 +1,10 @@ +import os +from pathlib import Path + + +def wordcloud_chineseize(): + font_ttf: str = "NotoSansSC-Regular.ttf" + os.environ["FONT_PATH"] = f"{Path(__file__).parent}/fonts/{font_ttf}" + + +wordcloud_chineseize()