rsplotlib 0.1.5__tar.gz → 0.1.5.post1__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.
Files changed (41) hide show
  1. rsplotlib-0.1.5.post1/PKG-INFO +777 -0
  2. rsplotlib-0.1.5.post1/README.md +6 -0
  3. rsplotlib-0.1.5.post1/README_zh.md +750 -0
  4. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/pyproject.toml +1 -13
  5. rsplotlib-0.1.5/PKG-INFO +0 -196
  6. rsplotlib-0.1.5/README.md +0 -168
  7. rsplotlib-0.1.5/README_zh.md +0 -162
  8. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/.github/ci.yml +0 -0
  9. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/.github/release.yml +0 -0
  10. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/.gitignore +0 -0
  11. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/Cargo.lock +0 -0
  12. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/Cargo.toml +0 -0
  13. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/LICENSE +0 -0
  14. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/build_wheel.sh +0 -0
  15. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/__init__.py +0 -0
  16. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/_figure_defaults.py +0 -0
  17. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/_font_resolver.py +0 -0
  18. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/_rcparams.py +0 -0
  19. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/api.py +0 -0
  20. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/gridspec.py +0 -0
  21. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/pylab.py +0 -0
  22. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/pyplot.py +0 -0
  23. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/style.py +0 -0
  24. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/python/rsplotlib/ticker.py +0 -0
  25. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/rust-toolchain.toml +0 -0
  26. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes.rs +0 -0
  27. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes_bounds.rs +0 -0
  28. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes_grid.rs +0 -0
  29. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes_legend.rs +0 -0
  30. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes_mesh.rs +0 -0
  31. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes_render_elements.rs +0 -0
  32. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axes_title.rs +0 -0
  33. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/axis.rs +0 -0
  34. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/colormap.rs +0 -0
  35. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/colors.rs +0 -0
  36. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/elements.rs +0 -0
  37. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/figure.rs +0 -0
  38. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/lib.rs +0 -0
  39. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/marker.rs +0 -0
  40. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/pyfuncs.rs +0 -0
  41. {rsplotlib-0.1.5 → rsplotlib-0.1.5.post1}/src/text_utils.rs +0 -0
@@ -0,0 +1,777 @@
1
+ Metadata-Version: 2.4
2
+ Name: rsplotlib
3
+ Version: 0.1.5.post1
4
+ Classifier: Programming Language :: Rust
5
+ Classifier: Programming Language :: Python :: 3
6
+ Classifier: Programming Language :: Python :: 3 :: Only
7
+ Classifier: Programming Language :: Python :: 3.8
8
+ Classifier: Programming Language :: Python :: 3.9
9
+ Classifier: Programming Language :: Python :: 3.10
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: Programming Language :: Python :: 3.13
13
+ Classifier: Programming Language :: Python :: Implementation :: CPython
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: MacOS
16
+ Classifier: Operating System :: Microsoft :: Windows
17
+ Classifier: Operating System :: POSIX :: Linux
18
+ Classifier: Topic :: Scientific/Engineering :: Visualization
19
+ License-File: LICENSE
20
+ Summary: A high-performance matplotlib-compatible plotting library powered by Rust
21
+ Keywords: plotting,matplotlib,visualization,rust,scientific
22
+ Author: YJ-Niu
23
+ License: MIT
24
+ Requires-Python: >=3.8
25
+ Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
26
+
27
+ # rsplotlib
28
+
29
+ > 一个由 Rust 强力驱动的高性能 Python 绘图库,提供 Matplotlib 兼容 API
30
+
31
+ [![Python](https://img.shields.io/badge/Python-3.8%2B-blue)](https://www.python.org/)
32
+ [![Rust](https://img.shields.io/badge/Rust-2021-orange)](https://www.rust-lang.org/)
33
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
34
+ [![PyO3](https://img.shields.io/badge/PyO3-0.29-2c2d72)](https://pyo3.rs/)
35
+ [![plotters](https://img.shields.io/badge/plotters-0.3-7d5cff)](https://github.com/plotters-rs/plotters)
36
+
37
+ ---
38
+
39
+ ## 目录
40
+
41
+ - [项目简介](#项目简介)
42
+ - [核心特性](#核心特性)
43
+ - [安装指南](#安装指南)
44
+ - [快速入门](#快速入门)
45
+ - [功能清单](#功能清单)
46
+ - [API 参考](#api-参考)
47
+ - [性能优势](#性能优势)
48
+ - [项目结构](#项目结构)
49
+ - [开发与贡献](#开发与贡献)
50
+ - [许可证](#许可证)
51
+
52
+ ---
53
+
54
+ ## 项目简介
55
+
56
+ **rsplotlib** 是一个跨语言的 Python 绘图库,核心渲染引擎完全使用 Rust 编写,通过 PyO3 提供与 Matplotlib 兼容的 Python API。项目的目标是在保持与现有 Matplotlib 代码最大兼容性的同时,利用 Rust 的内存安全和零成本抽象带来显著的性能提升。
57
+
58
+ ### 设计理念
59
+
60
+ - **API 优先**:提供与 Matplotlib 高度一致的接口,降低迁移成本
61
+ - **性能为王**:将性能关键路径(渲染、批量操作)下沉到 Rust 层
62
+ - **零额外依赖**:不依赖原生 Matplotlib 的安装,仅需 Python 解释器即可运行
63
+ - **跨平台一致**:在 macOS、Linux、Windows 上提供相同的渲染质量
64
+
65
+ ---
66
+
67
+ ## 核心特性
68
+
69
+ ### 丰富的图表类型
70
+
71
+ - **基础图表**:折线图、散点图、柱状图、水平柱状图
72
+ - **统计图表**:直方图、箱线图、饼图、误差棒图、茎叶图、阶梯图
73
+ - **高级图表**:堆叠面积图、热力图/图像显示、填充区域图
74
+
75
+ ### 辅助元素
76
+
77
+ - **参考线**:水平参考线 (`axhline`)、垂直参考线 (`axvline`)
78
+ - **区间高亮**:水平区间填充 (`axhspan`)、垂直区间填充 (`axvspan`)
79
+ - **任意斜率参考线**:`axline` — 通过两点绘制贯穿全图的任意角度参考线
80
+ - **批量线**:`hlines` / `vlines` — Rust 层批量实现,避免 Python 级 for 循环
81
+ - **文本标注**:`text`、`title`、带箭头的 `annotate`(支持 `arrowprops`)
82
+
83
+ ### 坐标与布局
84
+
85
+ - 多子图支持:`subplots()`、`subplot()`、网格布局
86
+ - 双坐标轴:`twinx()` / `twiny()`
87
+ - 对数坐标:`semilogx()`、`semilogy()`、`loglog()`
88
+ - 网格显示、图例、坐标轴范围、刻度自定义
89
+
90
+ ### 输出与分辨率
91
+
92
+ - **PNG**:位图输出,支持 DPI 控制
93
+ - **SVG**:矢量图输出,可无损缩放
94
+ - **自定义 DPI**:`savefig(filename, dpi=300)` 支持任意分辨率输出
95
+ - **像素尺寸 DPI**:PNG 元数据中写入 DPI 信息,方便出版使用
96
+
97
+ ### 散点图增强
98
+
99
+ - **每点独立颜色**:`ax.scatter(x, y, c=['red', 'blue', 'green'])`
100
+ - **每点独立大小**:`ax.scatter(x, y, s=[10, 20, 30, 40])`
101
+ - **颜色+大小同时独立**:Rust 层实现,零 Python 循环开销
102
+
103
+ ### 样式管理
104
+
105
+ - **`plt.style` 模块**:兼容 Matplotlib 的样式接口
106
+ - **`plt.style.use()`**:应用预设样式
107
+ - **`plt.style.available`**:查询可用样式
108
+ - **`plt.rcParams`**:全局参数配置(字体族、字号等)
109
+
110
+ ### 字体与国际化
111
+
112
+ - **跨平台字体解析**:自动查找系统可用字体
113
+ - **CJK 支持**:内置中文、日文、韩文字体自动检测
114
+ - **自定义字体注册**:`register_sans_serif_font(path)` 注册任意字体文件
115
+
116
+ ---
117
+
118
+ ## 安装指南
119
+
120
+ ### 前置依赖
121
+
122
+ | 依赖 | 版本要求 | 说明 |
123
+ | ------- | -------- | ---------------------- |
124
+ | Python | 3.8+ | CPython 实现 |
125
+ | Rust | 1.70+ | 从源码构建时需要 |
126
+ | maturin | 1.13+ | Rust-Python 包构建工具 |
127
+
128
+ ### 方法一:使用 maturin 从源码构建(推荐开发者)
129
+
130
+ ```bash
131
+ # 1. 克隆项目
132
+ git clone https://github.com/YJ-Niu/rsplotlib.git
133
+ cd rsplotlib
134
+
135
+ # 2. 构建并安装(自动编译 Rust 并安装到当前 Python 环境)
136
+ pip install maturin
137
+ maturin develop --release
138
+ ```
139
+
140
+ ### 方法二:构建 wheel 包
141
+
142
+ ```bash
143
+ # 使用项目提供的构建脚本
144
+ ./build_wheel.sh
145
+
146
+ # 安装生成的 wheel
147
+ pip install target/wheels/rsplotlib-*.whl
148
+ ```
149
+
150
+ ### 方法三:仅编译 Rust 扩展(调试用)
151
+
152
+ ```bash
153
+ # 编译 Rust cdylib
154
+ cargo build --release
155
+
156
+ # 将编译产物复制为正确的 Python 扩展模块名称
157
+ # macOS:
158
+ cp target/release/librsplotlib.dylib python/rsplotlib/rsplotlib.cpython-39-darwin.so
159
+
160
+ # Linux:
161
+ # cp target/release/librsplotlib.so python/rsplotlib/rsplotlib.cpython-39-x86_64-linux-gnu.so
162
+
163
+ # 从源码目录使用
164
+ export PYTHONPATH="$PWD/python:$PYTHONPATH"
165
+ ```
166
+
167
+ ### 验证安装
168
+
169
+ ```python
170
+ >>> import rsplotlib.pyplot as plt
171
+ >>> fig, ax = plt.subplots()
172
+ >>> ax.plot([1, 2, 3], [1, 4, 9])
173
+ >>> fig.savefig('test.png')
174
+ # ✅ 生成 test.png 即安装成功
175
+ ```
176
+
177
+ ---
178
+
179
+ ## 快速入门
180
+
181
+ ### 基础使用:折线图
182
+
183
+ ```python
184
+ from rsplotlib import pyplot as plt
185
+ from rsplotlib.pylab import mpl
186
+
187
+ # 可选:配置中文字体
188
+ mpl.rcParams['font.sans-serif'] = ['PingFang SC', 'Microsoft YaHei', 'Arial']
189
+
190
+ # 创建 Figure 和 Axes
191
+ fig, ax = plt.subplots(figsize=(8, 6))
192
+
193
+ # 绘制数据
194
+ x = [0, 1, 2, 3, 4, 5]
195
+ y1 = [1, 2, 4, 8, 16, 32]
196
+ y2 = [32, 16, 8, 4, 2, 1]
197
+
198
+ ax.plot(x, y1, label='指数增长', lw=2.0)
199
+ ax.plot(x, y2, label='指数衰减', lw=2.0, linestyle='--')
200
+
201
+ # 装饰图表
202
+ ax.set_title('基础折线图示例')
203
+ ax.set_xlabel('时间')
204
+ ax.set_ylabel('数值')
205
+ ax.legend()
206
+ ax.grid(True)
207
+
208
+ # 保存为高分辨率 PNG
209
+ fig.savefig('line_plot.png', dpi=300)
210
+ ```
211
+
212
+ ### 散点图:每点独立颜色和大小
213
+
214
+ ```python
215
+ import random
216
+ from rsplotlib import pyplot as plt
217
+
218
+ # 生成随机数据
219
+ random.seed(42)
220
+ n = 50
221
+ x = [random.gauss(0, 1) for _ in range(n)]
222
+ y = [random.gauss(0, 1) for _ in range(n)]
223
+
224
+ # 每点独立颜色和大小(Rust 层处理,Python 层零循环)
225
+ colors = ['red' if xi > 0 else 'blue' for xi in x]
226
+ sizes = [max(10, abs(xi * yi * 50)) for xi, yi in zip(x, y)]
227
+
228
+ fig, ax = plt.subplots()
229
+ ax.scatter(x, y, c=colors, s=sizes, alpha=0.7)
230
+ ax.set_title('散点图:颜色/大小独立')
231
+ fig.savefig('scatter.png')
232
+ ```
233
+
234
+ ### 区间高亮与参考线
235
+
236
+ ```python
237
+ from rsplotlib import pyplot as plt
238
+
239
+ fig, ax = plt.subplots()
240
+ x = list(range(-5, 6))
241
+ y = [xi**2 for xi in x]
242
+
243
+ # 绘制数据
244
+ ax.plot(x, y, 'b-', lw=2, label='y = x²')
245
+
246
+ # 垂直区间高亮
247
+ ax.axvspan(-1, 1, color='yellow', alpha=0.3, label='最小值区域')
248
+
249
+ # 水平区间高亮
250
+ ax.axhspan(0, 5, color='lightgreen', alpha=0.2)
251
+
252
+ # 水平/垂直参考线
253
+ ax.axhline(y=0, color='gray', linestyle=':', linewidth=1)
254
+ ax.axvline(x=0, color='gray', linestyle=':', linewidth=1)
255
+
256
+ # 任意斜率参考线(贯穿整张图)
257
+ ax.axline((0, 0), (1, 1), color='red', linestyle='--', linewidth=1.5)
258
+
259
+ ax.legend()
260
+ fig.savefig('reference_lines.png')
261
+ ```
262
+
263
+ ### 箭头标注
264
+
265
+ ```python
266
+ from rsplotlib import pyplot as plt
267
+
268
+ fig, ax = plt.subplots()
269
+
270
+ # 绘制曲线
271
+ x = list(range(-10, 11))
272
+ y = [xi**3 - 3*xi for xi in x]
273
+ ax.plot(x, y, 'b-', lw=2)
274
+
275
+ # 添加箭头标注
276
+ ax.annotate('局部极大值', xy=(-1, 2), xytext=(-8, 500),
277
+ fontsize=11, color='red',
278
+ arrowprops={'arrowstyle': '->', 'arrowsize': 1.0})
279
+
280
+ ax.annotate('局部极小值', xy=(1, -2), xytext=(3, -500),
281
+ fontsize=11, color='blue',
282
+ arrowprops={'arrowstyle': '->', 'arrowsize': 1.0})
283
+
284
+ ax.annotate('原点', xy=(0, 0), xytext=(5, 200),
285
+ fontsize=11, color='darkgreen')
286
+
287
+ ax.set_title('ax.annotate:带箭头的文本标注')
288
+ fig.savefig('annotations.png')
289
+ ```
290
+
291
+ ### 批量水平线/垂直线(Rust 层实现)
292
+
293
+ ```python
294
+ from rsplotlib import pyplot as plt
295
+
296
+ fig, ax = plt.subplots()
297
+ ax.plot([0, 10], [0, 10], 'k-', lw=1)
298
+
299
+ # 批量绘制水平线(Rust 层循环,Python 零开销)
300
+ ax.hlines([2, 4, 6, 8], color='steelblue', linestyle='--', linewidth=1)
301
+
302
+ # 批量绘制垂直线
303
+ ax.vlines([2, 4, 6, 8], color='darkorange', linestyle=':', linewidth=1)
304
+
305
+ ax.set_title('hlines / vlines:批量参考线(Rust 层实现)')
306
+ fig.savefig('hlines_vlines.png')
307
+ ```
308
+
309
+ ### 多子图
310
+
311
+ ```python
312
+ from rsplotlib import pyplot as plt
313
+
314
+ # 2x2 网格
315
+ fig, axes = plt.subplots(2, 2)
316
+
317
+ # axes 是一个扁平列表
318
+ axes[0].plot([1, 2, 3], [1, 4, 9])
319
+ axes[0].set_title('子图 1')
320
+
321
+ axes[1].bar(['A', 'B', 'C'], [3, 7, 2])
322
+ axes[1].set_title('子图 2')
323
+
324
+ axes[2].scatter([1,2,3,4], [4,3,2,1], c=['red','green','blue','orange'])
325
+ axes[2].set_title('子图 3')
326
+
327
+ axes[3].hist([0.5, 1.2, 1.8, 2.1, 2.5, 3.0, 3.2, 3.8, 4.1, 4.5], bins=5)
328
+ axes[3].set_title('子图 4')
329
+
330
+ fig.savefig('subplots.png', dpi=200)
331
+ ```
332
+
333
+ ### 模块级接口(无需显式创建 Axes)
334
+
335
+ ```python
336
+ from rsplotlib import pyplot as plt
337
+
338
+ # 直接使用 plt.* 接口
339
+ plt.figure()
340
+ plt.plot([1, 2, 3], [1, 2, 3], 'r-')
341
+ plt.axhline(y=2, color='gray', linestyle='--')
342
+ plt.axvspan(1, 2, color='yellow', alpha=0.3)
343
+ plt.title('模块级接口')
344
+ plt.savefig('module_level.png')
345
+ ```
346
+
347
+ ---
348
+
349
+ ## 功能清单
350
+
351
+ ### 绘图函数
352
+
353
+ | 函数 | 说明 | 模块级接口 | Axes 接口 |
354
+ | ---------------- | --------------------------- | ---------- | --------- |
355
+ | `plot()` | 折线图 | ✅ | ✅ |
356
+ | `scatter()` | 散点图(支持颜色/大小数组) | ✅ | ✅ |
357
+ | `bar()` | 柱状图 | ✅ | ✅ |
358
+ | `barh()` | 水平柱状图 | ✅ | ✅ |
359
+ | `hist()` | 直方图 | ✅ | ✅ |
360
+ | `pie()` | 饼图 | ✅ | ✅ |
361
+ | `boxplot()` | 箱线图 | ✅ | ✅ |
362
+ | `fill_between()` | 曲线间填充 | ✅ | ✅ |
363
+ | `errorbar()` | 误差棒图 | ✅ | ✅ |
364
+ | `stem()` | 茎叶图 | ✅ | ✅ |
365
+ | `step()` | 阶梯图 | ✅ | ✅ |
366
+ | `imshow()` | 图像/热力图 | ✅ | ✅ |
367
+ | `stackplot()` | 堆叠面积图 | ✅ | ✅ |
368
+ | `semilogx()` | x 轴对数坐标折线图 | ✅ | ✅ |
369
+ | `semilogy()` | y 轴对数坐标折线图 | ✅ | ✅ |
370
+ | `loglog()` | 双对数坐标折线图 | ✅ | ✅ |
371
+
372
+ ### 辅助元素
373
+
374
+ | 函数 | 说明 | 模块级接口 | Axes 接口 |
375
+ | ------------ | -------------------- | ---------- | --------- |
376
+ | `axhline()` | 水平参考线 | ✅ | ✅ |
377
+ | `axvline()` | 垂直参考线 | ✅ | ✅ |
378
+ | `axhspan()` | 水平区间填充 | ✅ | ✅ |
379
+ | `axvspan()` | 垂直区间填充 | ✅ | ✅ |
380
+ | `axline()` | 任意斜率参考线 | ✅ | ✅ |
381
+ | `hlines()` | 批量水平线 (Rust 层) | ✅ | ✅ |
382
+ | `vlines()` | 批量垂直线 (Rust 层) | ✅ | ✅ |
383
+ | `text()` | 文本标注 | ✅ | ✅ |
384
+ | `annotate()` | 带箭头文本标注 | ✅ | ✅ |
385
+
386
+ ### 图表配置
387
+
388
+ | 函数 | 说明 |
389
+ | -------------------------------------- | -------------------------------- |
390
+ | `title()` / `ax.set_title()` | 设置图表标题 |
391
+ | `xlabel()` / `ax.set_xlabel()` | 设置 X 轴标签 |
392
+ | `ylabel()` / `ax.set_ylabel()` | 设置 Y 轴标签 |
393
+ | `grid()` | 显示/隐藏网格 |
394
+ | `legend()` | 显示图例 |
395
+ | `xlim()` / `ylim()` | 设置坐标轴范围 |
396
+ | `xticks()` / `yticks()` | 设置刻度位置和标签 |
397
+ | `xscale()` / `yscale()` | 设置坐标缩放(`linear` / `log`) |
398
+ | `margins()` | 设置自动缩放边距 |
399
+ | `box()` | 设置坐标轴边框显示 |
400
+ | `minorticks_on()` / `minorticks_off()` | 次要刻度显示控制 |
401
+
402
+ ### 子图与布局
403
+
404
+ | 函数 | 说明 |
405
+ | ------------------------------ | ---------------- |
406
+ | `subplots(nrows, ncols)` | 创建子图网格 |
407
+ | `subplot(nrows, ncols, index)` | 创建单个子图 |
408
+ | `twinx()` / `twiny()` | 创建双坐标轴 |
409
+ | `tight_layout()` | 自动调整布局 |
410
+ | `fig.set_size(w, h)` | 设置图形像素尺寸 |
411
+
412
+ ### 图形控制
413
+
414
+ | 函数 | 说明 |
415
+ | ----------------------------- | -------------------------- |
416
+ | `figure()` | 创建新 Figure 对象 |
417
+ | `savefig(filename, dpi=None)` | 保存到文件,支持自定义 DPI |
418
+ | `show()` | 显示图形(保存到默认位置) |
419
+ | `gca()` | 获取当前 Axes |
420
+ | `gcf()` | 获取当前 Figure |
421
+ | `cla()` | 清空当前 Axes |
422
+ | `clf()` | 清空当前 Figure |
423
+ | `close()` | 关闭当前 Figure |
424
+
425
+ ---
426
+
427
+ ## API 参考
428
+
429
+ ### Figure.savefig
430
+
431
+ 保存图形到文件。
432
+
433
+ ```python
434
+ fig.savefig(filename, dpi=None)
435
+ ```
436
+
437
+ **参数:**
438
+
439
+ - `filename` (str): 输出文件路径。支持 `.png` 和 `.svg` 扩展名
440
+ - `dpi` (float, 可选): 分辨率(每英寸点数)。默认使用 Figure 创建时的 DPI
441
+
442
+ **示例:**
443
+
444
+ ```python
445
+ fig.savefig('plot.png') # 默认 DPI
446
+ fig.savefig('plot_hd.png', dpi=150) # 屏幕分辨率
447
+ fig.savefig('plot_print.png', dpi=300) # 印刷分辨率
448
+ fig.savefig('plot.svg') # 矢量图
449
+ ```
450
+
451
+ ### Axes.scatter — 增强版
452
+
453
+ 散点图,支持每点独立颜色/大小。
454
+
455
+ ```python
456
+ ax.scatter(x, y, s=20.0, c=None, marker='o', label=None, alpha=1.0, **kwargs)
457
+ ```
458
+
459
+ **参数:**
460
+
461
+ - `x`, `y` (list/array): 点坐标数据
462
+ - `s` (float 或 list/array): 单个浮点值用于所有点,或数组表示每点大小
463
+ - `c` (str 或 list[str]): 单个颜色字符串,或颜色字符串数组
464
+ - `marker` (str): 标记形状,支持 `'o'`, `'s'`, `'^'`, `'v'`, `'D'`, `'*'`, `'+'`, `'x'`, `'<'`, `'>'`
465
+ - `label` (str): 图例标签
466
+ - `alpha` (float): 透明度 (0.0-1.0)
467
+ - `**kwargs`: 额外参数,支持 `color`(作为 `c` 的别名)
468
+
469
+ **示例:**
470
+
471
+ ```python
472
+ # 单色散点
473
+ ax.scatter(x, y, c='red', s=50)
474
+
475
+ # 每点独立颜色
476
+ ax.scatter(x, y, c=['red', 'green', 'blue', ...], s=50)
477
+
478
+ # 每点独立大小
479
+ ax.scatter(x, y, s=[10, 20, 30, ...], c='blue')
480
+
481
+ # 颜色+大小同时独立(Rust 层批量处理)
482
+ ax.scatter(x, y, c=colors, s=sizes, marker='D', alpha=0.8)
483
+ ```
484
+
485
+ ### Axes.axhspan / axvspan
486
+
487
+ 区间高亮填充。
488
+
489
+ ```python
490
+ ax.axhspan(ymin, ymax, color=None, alpha=0.3)
491
+ ax.axvspan(xmin, xmax, color=None, alpha=0.3)
492
+ ```
493
+
494
+ **参数:**
495
+
496
+ - `ymin`, `ymax` (float): 水平区间的 y 轴上下界(数据坐标)
497
+ - `xmin`, `xmax` (float): 垂直区间的 x 轴左右界(数据坐标)
498
+ - `color` (str): 填充颜色,默认浅蓝灰色
499
+ - `alpha` (float): 透明度,默认 0.3
500
+
501
+ ### Axes.axline
502
+
503
+ 通过两点绘制贯穿全图的任意斜率参考线。
504
+
505
+ ```python
506
+ ax.axline(xy1, xy2, color=None, linestyle=None, linewidth=None)
507
+ ```
508
+
509
+ **参数:**
510
+
511
+ - `xy1` (tuple): 起点坐标 `(x1, y1)`
512
+ - `xy2` (tuple): 终点坐标 `(x2, y2)`
513
+ - `color` (str): 线颜色
514
+ - `linestyle` (str): 线型,`'-'` (实线), `'--'` (虚线), `':'` (点线), `'-.'` (点划线)
515
+ - `linewidth` (float): 线宽
516
+
517
+ ### Axes.annotate
518
+
519
+ 添加带箭头的文本标注。
520
+
521
+ ```python
522
+ ax.annotate(text, xy, xytext=None, fontsize=12.0, color='black',
523
+ arrowprops=None, arrowstyle=None, arrowsize=1.0)
524
+ ```
525
+
526
+ **参数:**
527
+
528
+ - `text` (str): 标注文本
529
+ - `xy` (tuple): 被标注点坐标 `(x, y)`
530
+ - `xytext` (tuple, 可选): 文本放置位置。若提供,自动从该位置绘制箭头到 `xy`
531
+ - `fontsize` (float): 字体大小,默认 12.0
532
+ - `color` (str): 文本和箭头颜色,默认 `'black'`
533
+ - `arrowprops` (dict, 可选): 箭头属性字典,支持:
534
+ - `arrowstyle`: 箭头样式(如 `'->'`, `'-|>'`)
535
+ - `arrowsize`: 箭头相对大小
536
+ - `arrowstyle` (str, 可选): 独立于 `arrowprops` 的箭头样式
537
+ - `arrowsize` (float, 可选): 独立于 `arrowprops` 的箭头大小
538
+
539
+ ### hlines / vlines(Rust 层批量实现)
540
+
541
+ 绘制多条水平线或垂直线。所有内部循环在 Rust 层完成,避免 Python 级循环开销。
542
+
543
+ ```python
544
+ ax.hlines(y, color=None, linestyle=None, linewidth=None)
545
+ ax.vlines(x, color=None, linestyle=None, linewidth=None)
546
+ ```
547
+
548
+ **参数:**
549
+
550
+ - `y` / `x` (list/array): 位置列表
551
+
552
+ ---
553
+
554
+ ## 性能优势
555
+
556
+ rsplotlib 在架构设计上通过"分层下沉"策略实现性能优化:
557
+
558
+ ### 架构分层
559
+
560
+ ```
561
+ ┌──────────────────────────────────────────────┐
562
+ │ Python 层 (API 兼容层) │
563
+ │ · pyplot.py (Matplotlib 兼容接口) │
564
+ │ · api.py (参数规范化 / 别名映射) │
565
+ │ · _patch_* (方法补丁 / 动态分发) │
566
+ ├──────────────────────────────────────────────┤
567
+ │ Rust 层 (高性能核心) │
568
+ │ · lib.rs (模块注册 / 字体系统) │
569
+ │ · figure.rs (Figure 对象 / 渲染调度) │
570
+ │ · axes.rs (Axes 对象 / 数据解析) │
571
+ │ · elements.rs (绘图元素数据结构) │
572
+ │ · axes_render_elements.rs (plotters 渲染) │
573
+ │ · pyfuncs.rs (模块级函数暴露) │
574
+ └──────────────────────────────────────────────┘
575
+ ```
576
+
577
+ ### 性能关键路径下沉到 Rust
578
+
579
+ | 功能 | 传统实现 | rsplotlib 实现 |
580
+ | --------------------------- | ----------------------------------- | ------------------------------- |
581
+ | `scatter(c=colors)` | Python 层遍历每个点 | Rust 层 `ScatterMulti` 统一渲染 |
582
+ | `scatter(s=sizes)` | Python 层遍历每个点 | Rust 层统一大小数组处理 |
583
+ | `hlines([y1, y2, y3, ...])` | Python `for` 循环多次调用 `axhline` | Rust 层单次调用批量处理 |
584
+ | `vlines([x1, x2, x3, ...])` | Python `for` 循环多次调用 `axvline` | Rust 层单次调用批量处理 |
585
+ | `savefig(dpi=300)` | 无 DPI 支持 / Python 层缩放 | Rust 层直接写入 PNG DPI 元数据 |
586
+
587
+ ### 为什么选择 Rust + Python 混合架构
588
+
589
+ 1. **开发效率**:Python 层快速迭代 API 设计、参数校验
590
+ 2. **执行性能**:Rust 层处理渲染、循环、内存密集型计算
591
+ 3. **内存安全**:Rust 编译期保证无数据竞争
592
+ 4. **零额外依赖**:无原生 Matplotlib 安装要求
593
+
594
+ ---
595
+
596
+ ## 项目结构
597
+
598
+ ```
599
+ rsplotlib/
600
+ ├── python/ # Python 包装层
601
+ │ └── rsplotlib/
602
+ │ ├── __init__.py # 包入口与导出
603
+ │ ├── api.py # 模块级 API 函数定义
604
+ │ ├── pyplot.py # Matplotlib 兼容 pyplot 接口
605
+ │ │ # (含中文 docstring, IDE 悬停提示)
606
+ │ ├── pylab.py # pylab 风格接口,提供 mpl.rcParams
607
+ │ ├── style.py # plt.style 样式管理
608
+ │ ├── _rcparams.py # rcParams 配置管理
609
+ │ ├── _font_resolver.py # 系统字体路径解析
610
+ │ ├── _figure_defaults.py # Figure 默认配置
611
+ │ ├── gridspec.py # 网格布局管理
612
+ │ └── ticker.py # 刻度定位器 (AutoLocator, MaxNLocator)
613
+
614
+ ├── src/ # Rust 核心实现
615
+ │ ├── lib.rs # 库入口,Python 模块注册
616
+ │ ├── figure.rs # Figure 类 (savefig, DPI 管理)
617
+ │ ├── axes.rs # Axes 类 (所有绘图方法)
618
+ │ ├── axes_render_elements.rs # 元素渲染引擎 (基于 plotters)
619
+ │ ├── axes_bounds.rs # 坐标边界计算
620
+ │ ├── axes_title.rs # 标题渲染
621
+ │ ├── axes_legend.rs # 图例渲染
622
+ │ ├── axes_grid.rs # 网格线渲染
623
+ │ ├── axes_mesh.rs # 坐标轴刻度
624
+ │ ├── axis.rs # Axis 数据结构
625
+ │ ├── elements.rs # 绘图元素枚举 (Line/Scatter/Bar/...)
626
+ │ ├── pyfuncs.rs # 暴露给 Python 的模块级函数
627
+ │ ├── colors.rs # 颜色解析 (命名色, RGB, HSL)
628
+ │ ├── colormap.rs # 颜色映射
629
+ │ ├── marker.rs # 标记形状渲染
630
+ │ └── text_utils.rs # 文本工具函数
631
+
632
+ ├── Cargo.toml # Rust 依赖 (PyO3, plotters, png)
633
+ ├── pyproject.toml # Python 包配置 (maturin)
634
+ ├── build_wheel.sh # wheel 包构建脚本
635
+ ├── README.md # 英文说明
636
+ └── README_zh.md # 中文说明(本文档)
637
+ ```
638
+
639
+ ---
640
+
641
+ ## 开发与贡献
642
+
643
+ ### 本地开发环境
644
+
645
+ ```bash
646
+ # 1. 安装 Rust
647
+ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
648
+
649
+ # 2. 安装 maturin
650
+ pip install maturin
651
+
652
+ # 3. 开发模式构建(自动编译 Rust 并安装到当前 Python 环境)
653
+ maturin develop --release
654
+
655
+ # 4. 运行交互式测试
656
+ python3 -c "
657
+ from rsplotlib import pyplot as plt
658
+ fig, ax = plt.subplots()
659
+ ax.plot([1,2,3], [1,4,9])
660
+ fig.savefig('/tmp/test.png')
661
+ print('OK')
662
+ "
663
+ ```
664
+
665
+ ### 开发工作流
666
+
667
+ 1. **理解需求**:分析需要实现的 Matplotlib 功能
668
+ 2. **Rust 层实现**:在 `src/` 中添加核心逻辑
669
+ - 新增绘图元素:修改 `elements.rs`
670
+ - 添加绘图方法:修改 `axes.rs`
671
+ - 添加渲染逻辑:修改 `axes_render_elements.rs`
672
+ - 暴露模块级函数:修改 `pyfuncs.rs`
673
+ - 注册:修改 `lib.rs`
674
+ 3. **Python 层包装**:在 `python/rsplotlib/` 中添加 API
675
+ - 添加中文 docstring(供 IDE 悬停显示)
676
+ - 处理参数别名(如 `lw` → `linewidth`)
677
+ - 将数组参数路由到 Rust 层批量方法
678
+ 4. **编译测试**:`maturin develop --release`
679
+ 5. **验证结果**:生成图像进行目视检查
680
+
681
+ ### 为 Python 层添加文档(IDE 悬停)
682
+
683
+ Python 包装层的函数都包含详细的中文 docstring,当用户在 IDE(VS Code、PyCharm 等)中悬停在函数上时,会自动显示中文说明和参数列表。
684
+
685
+ ```python
686
+ def axhspan(ymin, ymax, **kwargs):
687
+ """绘制水平方向的区间填充。
688
+
689
+ 用法:
690
+ plt.axhspan(0, 1, color='yellow', alpha=0.3)
691
+
692
+ Args:
693
+ ymin: y 轴下限
694
+ ymax: y 轴上限
695
+ color: 填充颜色 (默认蓝灰色)
696
+ alpha: 透明度 (0.0-1.0, 默认 0.3)
697
+ """
698
+ ...
699
+ ```
700
+
701
+ ### 贡献指南
702
+
703
+ 欢迎提交 PR!请在提交时包含:
704
+
705
+ - 清晰的功能说明或问题修复描述
706
+ - 适用时包含生成的示例图像
707
+ - 确保同时更新 `README.md` 和 `README_zh.md`
708
+
709
+ ### 已知限制
710
+
711
+ - 当前版本不支持 3D 绘图
712
+ - 不支持动画/交互式图表
713
+ - `contour` / `violinplot` / `hexbin` 为占位实现
714
+
715
+ ---
716
+
717
+ ## 字体配置
718
+
719
+ rsplotlib 支持通过 `mpl.rcParams` 自定义字体,同时支持直接注册字体文件。
720
+
721
+ ### 自动检测的系统字体
722
+
723
+ | 平台 | 常用字体 |
724
+ | ----------- | -------------------------------------------------------------------------- |
725
+ | **macOS** | Arial, Helvetica, PingFang SC, STHeiti, Hiragino Sans GB, Arial Unicode MS |
726
+ | **Linux** | DejaVu Sans, Liberation Sans, Noto Sans CJK SC, WenQuanYi Micro Hei |
727
+ | **Windows** | Microsoft YaHei, SimHei, SimSun, Arial |
728
+
729
+ ### 自定义字体配置
730
+
731
+ ```python
732
+ from rsplotlib.pylab import mpl
733
+ from rsplotlib import pyplot as plt
734
+
735
+ # 设置 sans-serif 字体族(优先顺序)
736
+ mpl.rcParams['font.sans-serif'] = ['PingFang SC', 'Microsoft YaHei', 'DejaVu Sans']
737
+
738
+ # 设置默认字体大小
739
+ mpl.rcParams['font.size'] = 12
740
+ ```
741
+
742
+ ### 直接注册字体文件
743
+
744
+ ```python
745
+ from rsplotlib import rsplotlib as _rs
746
+
747
+ # 从任意 .ttf/.otf/.ttc 文件注册为 sans-serif 字体
748
+ _rs.register_sans_serif_font('/path/to/your/custom-font.ttf')
749
+ ```
750
+
751
+ ---
752
+
753
+ ## 许可证
754
+
755
+ MIT License — 详见 [LICENSE](LICENSE) 文件。
756
+
757
+ ---
758
+
759
+ ## 致谢
760
+
761
+ - [**PyO3**](https://github.com/PyO3/pyo3) — Rust ↔ Python 绑定库,版本 0.29
762
+ - [**plotters**](https://github.com/plotters-rs/plotters) — Rust 绘图库,提供底层渲染能力
763
+ - [**Matplotlib**](https://matplotlib.org/) — Python 绘图生态标准,提供 API 设计参考
764
+ - [**maturin**](https://github.com/PyO3/maturin) — Rust Python 包构建与发布工具
765
+
766
+ ---
767
+
768
+ ## 相关链接
769
+
770
+ - **GitHub**: https://github.com/YJ-Niu/rsplotlib
771
+ - **PyPI**: (待发布)
772
+ - **文档**: (待完善)
773
+
774
+ ---
775
+
776
+ _最后更新:2026-06-17 · 版本 v0.1.5_
777
+