simpleplot-lite 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simpleplot_lite-0.1.0/LICENSE +21 -0
- simpleplot_lite-0.1.0/PKG-INFO +104 -0
- simpleplot_lite-0.1.0/README.md +84 -0
- simpleplot_lite-0.1.0/pyproject.toml +29 -0
- simpleplot_lite-0.1.0/setup.cfg +4 -0
- simpleplot_lite-0.1.0/simpleplot_lite/__init__.py +7 -0
- simpleplot_lite-0.1.0/simpleplot_lite/charts.py +62 -0
- simpleplot_lite-0.1.0/simpleplot_lite.egg-info/PKG-INFO +104 -0
- simpleplot_lite-0.1.0/simpleplot_lite.egg-info/SOURCES.txt +11 -0
- simpleplot_lite-0.1.0/simpleplot_lite.egg-info/dependency_links.txt +1 -0
- simpleplot_lite-0.1.0/simpleplot_lite.egg-info/requires.txt +5 -0
- simpleplot_lite-0.1.0/simpleplot_lite.egg-info/top_level.txt +1 -0
- simpleplot_lite-0.1.0/tests/test_charts.py +53 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Your Name
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simpleplot-lite
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 一个简单的 Python 画图工具包,封装折线图、柱状图和饼图
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourname/simpleplot-lite
|
|
8
|
+
Keywords: plot,chart,matplotlib,visualization
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: matplotlib>=3.5
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: build; extra == "dev"
|
|
18
|
+
Requires-Dist: twine; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# simpleplot-lite
|
|
22
|
+
|
|
23
|
+
一个简单的 Python 画图工具包,把 matplotlib 最常用的三种图表封装成一行调用。
|
|
24
|
+
适合刚学 Python 的同学快速画图。
|
|
25
|
+
|
|
26
|
+
## 功能
|
|
27
|
+
|
|
28
|
+
- `line_chart(x, y, ...)`:折线图
|
|
29
|
+
- `bar_chart(labels, values, ...)`:柱状图
|
|
30
|
+
- `pie_chart(labels, values, ...)`:饼图
|
|
31
|
+
|
|
32
|
+
每个函数都支持 `title`、`xlabel`、`ylabel`(饼图没有坐标轴)和 `save_path`,
|
|
33
|
+
返回值是 matplotlib 的 `Figure` 对象,可以继续自己修改。
|
|
34
|
+
|
|
35
|
+
## 安装
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install simpleplot-lite
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 快速开始
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from simpleplot_lite import line_chart, bar_chart, pie_chart
|
|
45
|
+
|
|
46
|
+
# 折线图:保存成图片
|
|
47
|
+
line_chart([1, 2, 3, 4], [60, 72, 68, 85],
|
|
48
|
+
title="四次小测成绩", xlabel="次数", ylabel="分数",
|
|
49
|
+
save_path="line.png")
|
|
50
|
+
|
|
51
|
+
# 柱状图
|
|
52
|
+
bar_chart(["Python", "C++", "Java"], [90, 80, 70],
|
|
53
|
+
title="编程语言喜好", save_path="bar.png")
|
|
54
|
+
|
|
55
|
+
# 饼图
|
|
56
|
+
pie_chart(["吃饭", "睡觉", "写代码"], [3, 8, 13],
|
|
57
|
+
title="一天的时间分配", save_path="pie.png")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 项目结构
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
simpleplot-lite/
|
|
64
|
+
├── LICENSE
|
|
65
|
+
├── README.md
|
|
66
|
+
├── pyproject.toml
|
|
67
|
+
├── demo.py # 运行后生成三张示例图片
|
|
68
|
+
├── simpleplot_lite/
|
|
69
|
+
│ ├── __init__.py
|
|
70
|
+
│ └── charts.py
|
|
71
|
+
└── tests/
|
|
72
|
+
└── test_charts.py
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 打包发布步骤
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# 1. 安装打包工具
|
|
79
|
+
pip install build twine
|
|
80
|
+
|
|
81
|
+
# 2. 运行测试
|
|
82
|
+
python -m unittest discover -s tests -v
|
|
83
|
+
|
|
84
|
+
# 3. 构建,产物在 dist/ 目录
|
|
85
|
+
python -m build
|
|
86
|
+
|
|
87
|
+
# 4. 检查元数据
|
|
88
|
+
twine check dist/*
|
|
89
|
+
|
|
90
|
+
# 5. 先上传到 TestPyPI 测试
|
|
91
|
+
twine upload --repository testpypi dist/*
|
|
92
|
+
|
|
93
|
+
# 6. 再上传到正式 PyPI
|
|
94
|
+
twine upload dist/*
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
上传时用户名填 `__token__`,密码填 PyPI 网站生成的 API Token(`pypi-` 开头)。
|
|
98
|
+
|
|
99
|
+
注意:同一个版本号不能重复上传,重新发布前要把 `pyproject.toml` 和
|
|
100
|
+
`simpleplot_lite/__init__.py` 里的版本号一起改大,并删除旧的 `dist/` 目录。
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# simpleplot-lite
|
|
2
|
+
|
|
3
|
+
一个简单的 Python 画图工具包,把 matplotlib 最常用的三种图表封装成一行调用。
|
|
4
|
+
适合刚学 Python 的同学快速画图。
|
|
5
|
+
|
|
6
|
+
## 功能
|
|
7
|
+
|
|
8
|
+
- `line_chart(x, y, ...)`:折线图
|
|
9
|
+
- `bar_chart(labels, values, ...)`:柱状图
|
|
10
|
+
- `pie_chart(labels, values, ...)`:饼图
|
|
11
|
+
|
|
12
|
+
每个函数都支持 `title`、`xlabel`、`ylabel`(饼图没有坐标轴)和 `save_path`,
|
|
13
|
+
返回值是 matplotlib 的 `Figure` 对象,可以继续自己修改。
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install simpleplot-lite
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 快速开始
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from simpleplot_lite import line_chart, bar_chart, pie_chart
|
|
25
|
+
|
|
26
|
+
# 折线图:保存成图片
|
|
27
|
+
line_chart([1, 2, 3, 4], [60, 72, 68, 85],
|
|
28
|
+
title="四次小测成绩", xlabel="次数", ylabel="分数",
|
|
29
|
+
save_path="line.png")
|
|
30
|
+
|
|
31
|
+
# 柱状图
|
|
32
|
+
bar_chart(["Python", "C++", "Java"], [90, 80, 70],
|
|
33
|
+
title="编程语言喜好", save_path="bar.png")
|
|
34
|
+
|
|
35
|
+
# 饼图
|
|
36
|
+
pie_chart(["吃饭", "睡觉", "写代码"], [3, 8, 13],
|
|
37
|
+
title="一天的时间分配", save_path="pie.png")
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## 项目结构
|
|
41
|
+
|
|
42
|
+
```
|
|
43
|
+
simpleplot-lite/
|
|
44
|
+
├── LICENSE
|
|
45
|
+
├── README.md
|
|
46
|
+
├── pyproject.toml
|
|
47
|
+
├── demo.py # 运行后生成三张示例图片
|
|
48
|
+
├── simpleplot_lite/
|
|
49
|
+
│ ├── __init__.py
|
|
50
|
+
│ └── charts.py
|
|
51
|
+
└── tests/
|
|
52
|
+
└── test_charts.py
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## 打包发布步骤
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# 1. 安装打包工具
|
|
59
|
+
pip install build twine
|
|
60
|
+
|
|
61
|
+
# 2. 运行测试
|
|
62
|
+
python -m unittest discover -s tests -v
|
|
63
|
+
|
|
64
|
+
# 3. 构建,产物在 dist/ 目录
|
|
65
|
+
python -m build
|
|
66
|
+
|
|
67
|
+
# 4. 检查元数据
|
|
68
|
+
twine check dist/*
|
|
69
|
+
|
|
70
|
+
# 5. 先上传到 TestPyPI 测试
|
|
71
|
+
twine upload --repository testpypi dist/*
|
|
72
|
+
|
|
73
|
+
# 6. 再上传到正式 PyPI
|
|
74
|
+
twine upload dist/*
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
上传时用户名填 `__token__`,密码填 PyPI 网站生成的 API Token(`pypi-` 开头)。
|
|
78
|
+
|
|
79
|
+
注意:同一个版本号不能重复上传,重新发布前要把 `pyproject.toml` 和
|
|
80
|
+
`simpleplot_lite/__init__.py` 里的版本号一起改大,并删除旧的 `dist/` 目录。
|
|
81
|
+
|
|
82
|
+
## License
|
|
83
|
+
|
|
84
|
+
MIT
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=77.0.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "simpleplot-lite"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "一个简单的 Python 画图工具包,封装折线图、柱状图和饼图"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
authors = [{ name = "Your Name", email = "you@example.com" }]
|
|
14
|
+
keywords = ["plot", "chart", "matplotlib", "visualization"]
|
|
15
|
+
dependencies = ["matplotlib>=3.5"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"Topic :: Scientific/Engineering :: Visualization",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.optional-dependencies]
|
|
23
|
+
dev = ["build", "twine"]
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Homepage = "https://github.com/yourname/simpleplot-lite"
|
|
27
|
+
|
|
28
|
+
[tool.setuptools.packages.find]
|
|
29
|
+
include = ["simpleplot_lite*"]
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""simpleplot_lite.charts —— 三个最常用的画图函数。
|
|
2
|
+
|
|
3
|
+
全部基于 matplotlib,每个函数都会返回一个 Figure 对象,
|
|
4
|
+
可以继续自己修改,也可以传 save_path 直接保存成图片。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import matplotlib
|
|
8
|
+
import matplotlib.pyplot as plt
|
|
9
|
+
|
|
10
|
+
# Windows 下中文默认会显示成方框,这里指定可用字体
|
|
11
|
+
matplotlib.rcParams["font.sans-serif"] = ["Microsoft YaHei", "SimHei", "DejaVu Sans"]
|
|
12
|
+
matplotlib.rcParams["axes.unicode_minus"] = False
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def line_chart(x, y, title="折线图", xlabel="x", ylabel="y", save_path=None):
|
|
16
|
+
"""画折线图,x 和 y 是长度相同的列表。"""
|
|
17
|
+
if len(x) != len(y):
|
|
18
|
+
raise ValueError("x 和 y 的长度必须相同")
|
|
19
|
+
|
|
20
|
+
fig, ax = plt.subplots(figsize=(6, 4))
|
|
21
|
+
ax.plot(x, y, marker="o", color="tab:blue")
|
|
22
|
+
ax.set_title(title)
|
|
23
|
+
ax.set_xlabel(xlabel)
|
|
24
|
+
ax.set_ylabel(ylabel)
|
|
25
|
+
ax.grid(True, linestyle="--", alpha=0.5)
|
|
26
|
+
|
|
27
|
+
if save_path:
|
|
28
|
+
fig.savefig(save_path, dpi=120, bbox_inches="tight")
|
|
29
|
+
return fig
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def bar_chart(labels, values, title="柱状图", xlabel="类别", ylabel="数值", save_path=None):
|
|
33
|
+
"""画柱状图,labels 是类别名,values 是对应的数值。"""
|
|
34
|
+
if len(labels) != len(values):
|
|
35
|
+
raise ValueError("labels 和 values 的长度必须相同")
|
|
36
|
+
|
|
37
|
+
fig, ax = plt.subplots(figsize=(6, 4))
|
|
38
|
+
bars = ax.bar(labels, values, color="tab:orange")
|
|
39
|
+
ax.set_title(title)
|
|
40
|
+
ax.set_xlabel(xlabel)
|
|
41
|
+
ax.set_ylabel(ylabel)
|
|
42
|
+
ax.bar_label(bars) # 在柱子顶部显示数值
|
|
43
|
+
ax.grid(True, axis="y", linestyle="--", alpha=0.5)
|
|
44
|
+
|
|
45
|
+
if save_path:
|
|
46
|
+
fig.savefig(save_path, dpi=120, bbox_inches="tight")
|
|
47
|
+
return fig
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def pie_chart(labels, values, title="饼图", save_path=None):
|
|
51
|
+
"""画饼图,会自动计算百分比。"""
|
|
52
|
+
if len(labels) != len(values):
|
|
53
|
+
raise ValueError("labels 和 values 的长度必须相同")
|
|
54
|
+
|
|
55
|
+
fig, ax = plt.subplots(figsize=(5, 5))
|
|
56
|
+
ax.pie(values, labels=labels, autopct="%1.1f%%", startangle=90)
|
|
57
|
+
ax.set_title(title)
|
|
58
|
+
ax.axis("equal") # 保证饼是圆的
|
|
59
|
+
|
|
60
|
+
if save_path:
|
|
61
|
+
fig.savefig(save_path, dpi=120, bbox_inches="tight")
|
|
62
|
+
return fig
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simpleplot-lite
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: 一个简单的 Python 画图工具包,封装折线图、柱状图和饼图
|
|
5
|
+
Author-email: Your Name <you@example.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/yourname/simpleplot-lite
|
|
8
|
+
Keywords: plot,chart,matplotlib,visualization
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: Topic :: Scientific/Engineering :: Visualization
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.8
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
Requires-Dist: matplotlib>=3.5
|
|
16
|
+
Provides-Extra: dev
|
|
17
|
+
Requires-Dist: build; extra == "dev"
|
|
18
|
+
Requires-Dist: twine; extra == "dev"
|
|
19
|
+
Dynamic: license-file
|
|
20
|
+
|
|
21
|
+
# simpleplot-lite
|
|
22
|
+
|
|
23
|
+
一个简单的 Python 画图工具包,把 matplotlib 最常用的三种图表封装成一行调用。
|
|
24
|
+
适合刚学 Python 的同学快速画图。
|
|
25
|
+
|
|
26
|
+
## 功能
|
|
27
|
+
|
|
28
|
+
- `line_chart(x, y, ...)`:折线图
|
|
29
|
+
- `bar_chart(labels, values, ...)`:柱状图
|
|
30
|
+
- `pie_chart(labels, values, ...)`:饼图
|
|
31
|
+
|
|
32
|
+
每个函数都支持 `title`、`xlabel`、`ylabel`(饼图没有坐标轴)和 `save_path`,
|
|
33
|
+
返回值是 matplotlib 的 `Figure` 对象,可以继续自己修改。
|
|
34
|
+
|
|
35
|
+
## 安装
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install simpleplot-lite
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 快速开始
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from simpleplot_lite import line_chart, bar_chart, pie_chart
|
|
45
|
+
|
|
46
|
+
# 折线图:保存成图片
|
|
47
|
+
line_chart([1, 2, 3, 4], [60, 72, 68, 85],
|
|
48
|
+
title="四次小测成绩", xlabel="次数", ylabel="分数",
|
|
49
|
+
save_path="line.png")
|
|
50
|
+
|
|
51
|
+
# 柱状图
|
|
52
|
+
bar_chart(["Python", "C++", "Java"], [90, 80, 70],
|
|
53
|
+
title="编程语言喜好", save_path="bar.png")
|
|
54
|
+
|
|
55
|
+
# 饼图
|
|
56
|
+
pie_chart(["吃饭", "睡觉", "写代码"], [3, 8, 13],
|
|
57
|
+
title="一天的时间分配", save_path="pie.png")
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## 项目结构
|
|
61
|
+
|
|
62
|
+
```
|
|
63
|
+
simpleplot-lite/
|
|
64
|
+
├── LICENSE
|
|
65
|
+
├── README.md
|
|
66
|
+
├── pyproject.toml
|
|
67
|
+
├── demo.py # 运行后生成三张示例图片
|
|
68
|
+
├── simpleplot_lite/
|
|
69
|
+
│ ├── __init__.py
|
|
70
|
+
│ └── charts.py
|
|
71
|
+
└── tests/
|
|
72
|
+
└── test_charts.py
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## 打包发布步骤
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
# 1. 安装打包工具
|
|
79
|
+
pip install build twine
|
|
80
|
+
|
|
81
|
+
# 2. 运行测试
|
|
82
|
+
python -m unittest discover -s tests -v
|
|
83
|
+
|
|
84
|
+
# 3. 构建,产物在 dist/ 目录
|
|
85
|
+
python -m build
|
|
86
|
+
|
|
87
|
+
# 4. 检查元数据
|
|
88
|
+
twine check dist/*
|
|
89
|
+
|
|
90
|
+
# 5. 先上传到 TestPyPI 测试
|
|
91
|
+
twine upload --repository testpypi dist/*
|
|
92
|
+
|
|
93
|
+
# 6. 再上传到正式 PyPI
|
|
94
|
+
twine upload dist/*
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
上传时用户名填 `__token__`,密码填 PyPI 网站生成的 API Token(`pypi-` 开头)。
|
|
98
|
+
|
|
99
|
+
注意:同一个版本号不能重复上传,重新发布前要把 `pyproject.toml` 和
|
|
100
|
+
`simpleplot_lite/__init__.py` 里的版本号一起改大,并删除旧的 `dist/` 目录。
|
|
101
|
+
|
|
102
|
+
## License
|
|
103
|
+
|
|
104
|
+
MIT
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
simpleplot_lite/__init__.py
|
|
5
|
+
simpleplot_lite/charts.py
|
|
6
|
+
simpleplot_lite.egg-info/PKG-INFO
|
|
7
|
+
simpleplot_lite.egg-info/SOURCES.txt
|
|
8
|
+
simpleplot_lite.egg-info/dependency_links.txt
|
|
9
|
+
simpleplot_lite.egg-info/requires.txt
|
|
10
|
+
simpleplot_lite.egg-info/top_level.txt
|
|
11
|
+
tests/test_charts.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
simpleplot_lite
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""simpleplot_lite 的单元测试,使用标准库 unittest。"""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import tempfile
|
|
5
|
+
import unittest
|
|
6
|
+
|
|
7
|
+
import matplotlib
|
|
8
|
+
|
|
9
|
+
matplotlib.use("Agg") # 测试时不弹窗口
|
|
10
|
+
|
|
11
|
+
import matplotlib.pyplot as plt
|
|
12
|
+
import simpleplot_lite as sp
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TestCharts(unittest.TestCase):
|
|
16
|
+
def tearDown(self):
|
|
17
|
+
plt.close("all")
|
|
18
|
+
|
|
19
|
+
def test_line_chart(self):
|
|
20
|
+
fig = sp.line_chart([1, 2, 3], [3, 1, 2], title="测试折线图")
|
|
21
|
+
ax = fig.axes[0]
|
|
22
|
+
self.assertEqual(len(ax.lines), 1)
|
|
23
|
+
self.assertEqual(ax.get_title(), "测试折线图")
|
|
24
|
+
|
|
25
|
+
def test_bar_chart(self):
|
|
26
|
+
fig = sp.bar_chart(["A", "B"], [5, 8])
|
|
27
|
+
ax = fig.axes[0]
|
|
28
|
+
self.assertEqual(len(ax.patches), 2)
|
|
29
|
+
self.assertEqual(ax.get_ylabel(), "数值")
|
|
30
|
+
|
|
31
|
+
def test_pie_chart(self):
|
|
32
|
+
fig = sp.pie_chart(["甲", "乙", "丙"], [1, 2, 3])
|
|
33
|
+
ax = fig.axes[0]
|
|
34
|
+
self.assertEqual(len(ax.patches), 3)
|
|
35
|
+
|
|
36
|
+
def test_length_mismatch(self):
|
|
37
|
+
with self.assertRaises(ValueError):
|
|
38
|
+
sp.line_chart([1, 2], [1])
|
|
39
|
+
with self.assertRaises(ValueError):
|
|
40
|
+
sp.bar_chart(["a"], [1, 2])
|
|
41
|
+
with self.assertRaises(ValueError):
|
|
42
|
+
sp.pie_chart(["a", "b"], [1])
|
|
43
|
+
|
|
44
|
+
def test_save_png(self):
|
|
45
|
+
with tempfile.TemporaryDirectory() as tmp:
|
|
46
|
+
path = os.path.join(tmp, "chart.png")
|
|
47
|
+
sp.line_chart([1, 2, 3], [1, 4, 9], save_path=path)
|
|
48
|
+
self.assertTrue(os.path.exists(path))
|
|
49
|
+
self.assertGreater(os.path.getsize(path), 0)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
if __name__ == "__main__":
|
|
53
|
+
unittest.main()
|