rsplotlib 0.1.9__cp313-cp313-win_amd64.whl

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.
Binary file
rsplotlib/ticker.py ADDED
@@ -0,0 +1,6 @@
1
+ """rsplotlib.ticker - 向后兼容的导入模块
2
+
3
+ 此模块提供从旧路径 `rsplotlib.ticker` 导入的支持。
4
+ 实际实现在 `rsplotlib.ticks.ticker` 中。
5
+ """
6
+ from .ticks.ticker import * # noqa: F403, F401
@@ -0,0 +1,2 @@
1
+ """rsplotlib.ticks - 刻度管理"""
2
+ from . import ticker # noqa: F401
@@ -0,0 +1,269 @@
1
+ """rsplotlib.ticker - Matplotlib ticker 兼容接口
2
+
3
+ 提供刻度定位器和格式化器类。
4
+ 底层实现已迁移至 Rust 层,此模块为保持完整 Python 接口的薄包装层。
5
+ """
6
+
7
+ from .. import rsplotlib as _rs
8
+
9
+
10
+ class Tick:
11
+ """刻度对象"""
12
+
13
+ def __init__(self, loc, label=''):
14
+ self.loc = loc
15
+ self.label = label
16
+
17
+
18
+ # ==================== 定位器 ====================
19
+
20
+ class Locator:
21
+ """刻度定位器基类"""
22
+
23
+ def tick_values(self, vmin, vmax):
24
+ raise NotImplementedError
25
+
26
+ def __call__(self):
27
+ return []
28
+
29
+
30
+ class MultipleLocator(Locator):
31
+ """倍数定位器 - 刻度位置是基数的整数倍
32
+
33
+ 底层实现: Rust MultipleLocator
34
+
35
+ Args:
36
+ base: 刻度间距
37
+ """
38
+
39
+ def __init__(self, base=1.0):
40
+ self.base = base
41
+ self._impl = _rs.MultipleLocator(base)
42
+
43
+ def tick_values(self, vmin, vmax):
44
+ return self._impl.tick_values(vmin, vmax)
45
+
46
+ def __repr__(self):
47
+ return f'MultipleLocator(base={self.base})'
48
+
49
+
50
+ class MaxNLocator(Locator):
51
+ """最大数量定位器 - 最多 nbins+1 个刻度
52
+
53
+ 底层实现: Rust MaxNLocator
54
+
55
+ Args:
56
+ nbins: 最大区间数 (默认: 10)
57
+ integer: 是否只使用整数 (默认: False)
58
+ """
59
+
60
+ def __init__(self, nbins=10, integer=False):
61
+ self.nbins = nbins
62
+ self.integer = integer
63
+ self._impl = _rs.MaxNLocator(nbins, integer)
64
+
65
+ def tick_values(self, vmin, vmax):
66
+ return self._impl.tick_values(vmin, vmax)
67
+
68
+ def __repr__(self):
69
+ return f'MaxNLocator(nbins={self.nbins}, integer={self.integer})'
70
+
71
+
72
+ class AutoLocator(MaxNLocator):
73
+ """自动定位器
74
+
75
+ 底层实现: Rust MaxNLocator(nbins=10)
76
+ """
77
+
78
+ def __init__(self):
79
+ super().__init__(nbins=10)
80
+
81
+
82
+ class AutoMinorLocator(Locator):
83
+ """自动次要刻度定位器
84
+
85
+ 底层实现: Rust AutoMinorLocator
86
+
87
+ Args:
88
+ n: 每个主要间隔中的次要刻度数 (默认: 5)
89
+ """
90
+
91
+ def __init__(self, n=5):
92
+ self.n = n
93
+ self._impl = _rs.AutoMinorLocator(n)
94
+
95
+ def tick_values(self, vmin, vmax):
96
+ return self._impl.tick_values(vmin, vmax)
97
+
98
+ def __repr__(self):
99
+ return f'AutoMinorLocator(n={self.n})'
100
+
101
+
102
+ class FixedLocator(Locator):
103
+ """固定位置定位器
104
+
105
+ 底层实现: Rust FixedLocator
106
+ """
107
+
108
+ def __init__(self, locs):
109
+ self.locs = list(locs)
110
+ self._impl = _rs.FixedLocator(list(locs))
111
+
112
+ def tick_values(self, vmin, vmax):
113
+ return self._impl.tick_values(vmin, vmax)
114
+
115
+
116
+ class LinearLocator(Locator):
117
+ """线性定位器
118
+
119
+ 底层实现: Rust LinearLocator
120
+ """
121
+
122
+ def __init__(self, numticks=10):
123
+ self.numticks = numticks
124
+ self._impl = _rs.LinearLocator(numticks)
125
+
126
+ def tick_values(self, vmin, vmax):
127
+ return self._impl.tick_values(vmin, vmax)
128
+
129
+
130
+ class LogLocator(Locator):
131
+ """对数定位器
132
+
133
+ 底层实现: Rust LogLocator
134
+ """
135
+
136
+ def __init__(self, base=10.0, numticks=10):
137
+ self.base = base
138
+ self.numticks = numticks
139
+ self._impl = _rs.LogLocator(base, numticks)
140
+
141
+ def tick_values(self, vmin, vmax):
142
+ return self._impl.tick_values(vmin, vmax)
143
+
144
+
145
+ class NullLocator(Locator):
146
+ """空定位器 - 不显示刻度
147
+
148
+ 底层实现: Rust NullLocator
149
+ """
150
+
151
+ def __init__(self):
152
+ self._impl = _rs.NullLocator()
153
+
154
+ def tick_values(self, vmin, vmax):
155
+ return self._impl.tick_values(vmin, vmax)
156
+
157
+
158
+ # ==================== 格式化器 ====================
159
+
160
+ class Formatter:
161
+ """刻度格式化器基类"""
162
+
163
+ def format_ticks(self, values):
164
+ return [self(val) for val in values]
165
+
166
+ def __call__(self, value):
167
+ return str(value)
168
+
169
+
170
+ class NullFormatter(Formatter):
171
+ """不显示标签
172
+
173
+ 底层实现: Rust NullFormatter
174
+ """
175
+
176
+ def __init__(self):
177
+ self._impl = _rs.NullFormatter()
178
+
179
+ def __call__(self, value):
180
+ return self._impl.__call__(value)
181
+
182
+
183
+ class FixedFormatter(Formatter):
184
+ """固定标签格式化器
185
+
186
+ 底层实现: Rust FixedFormatter
187
+ """
188
+
189
+ def __init__(self, seq):
190
+ self.seq = list(seq)
191
+ self._impl = _rs.FixedFormatter([str(s) for s in seq])
192
+
193
+ def __call__(self, value):
194
+ return self._impl.__call__(value)
195
+
196
+
197
+ class FormatStrFormatter(Formatter):
198
+ """格式化字符串
199
+
200
+ 底层实现: Rust FormatStrFormatter
201
+ """
202
+
203
+ def __init__(self, fmt):
204
+ self.fmt = fmt
205
+ self._impl = _rs.FormatStrFormatter(fmt)
206
+
207
+ def __call__(self, value):
208
+ return self._impl.__call__(value)
209
+
210
+
211
+ class ScalarFormatter(Formatter):
212
+ """标量格式化器
213
+
214
+ 底层实现: Rust ScalarFormatter
215
+ """
216
+
217
+ def __init__(self):
218
+ self._impl = _rs.ScalarFormatter()
219
+
220
+ def __call__(self, value):
221
+ return self._impl.__call__(value)
222
+
223
+
224
+ class LogFormatterSciNotation(Formatter):
225
+ """科学计数法格式化器
226
+
227
+ 底层实现: Rust LogFormatterSciNotation
228
+ """
229
+
230
+ def __init__(self):
231
+ self._impl = _rs.LogFormatterSciNotation()
232
+
233
+ def __call__(self, value):
234
+ return self._impl.__call__(value)
235
+
236
+
237
+ class FuncFormatter(Formatter):
238
+ """函数格式化器
239
+
240
+ 底层实现: Rust FuncFormatter
241
+ """
242
+
243
+ def __init__(self, func):
244
+ self.func = func
245
+ self._impl = _rs.FuncFormatter(func)
246
+
247
+ def __call__(self, value):
248
+ return self._impl.__call__(value)
249
+
250
+
251
+ class StrMethodFormatter(Formatter):
252
+ """字符串方法格式化器
253
+
254
+ 底层实现: Rust StrMethodFormatter
255
+ """
256
+
257
+ def __init__(self, fmt):
258
+ self.fmt = fmt
259
+ self._impl = _rs.StrMethodFormatter(fmt)
260
+
261
+ def __call__(self, value):
262
+ return self._impl.__call__(value)
263
+
264
+
265
+ # ==================== 便捷函数 ====================
266
+
267
+ def auto_locator():
268
+ """创建自动定位器"""
269
+ return MaxNLocator(nbins=10, integer=False)
@@ -0,0 +1,2 @@
1
+ """rsplotlib.utils - 工具模块"""
2
+ from . import _font_resolver, _rcparams, style # noqa: F401
@@ -0,0 +1,34 @@
1
+ """rsplotlib._font_resolver - 字体族名 → 字体文件路径解析
2
+
3
+ 将 matplotlib 风格的无衬线字体族名(如 "Arial Unicode MS"、"Helvetica Neue")
4
+ 映射到本地字体文件路径。找不到时返回 None,由调用方决定回退到默认字体。
5
+
6
+ 底层实现: Rust font_resolver
7
+ """
8
+
9
+ from .. import rsplotlib as _rs
10
+
11
+
12
+ def resolve_font_path(family: str) -> str or None:
13
+ """根据字体族名查找本地的字体文件路径。
14
+
15
+ 底层实现: Rust resolve_font_path
16
+
17
+ Args:
18
+ family: 字体族名
19
+
20
+ Returns:
21
+ 字体文件路径,找不到时返回 None
22
+ """
23
+ return _rs.resolve_font_path(family)
24
+
25
+
26
+ def apply_rcparams_font() -> str or None:
27
+ """读取 rcParams["font.sans-serif"],把第一个能解析到本地文件的字体注册到 plotters。
28
+
29
+ 底层实现: Rust apply_rcparams_font
30
+
31
+ Returns:
32
+ 实际注册的字体文件路径,如果没找到任何字体则返回 None
33
+ """
34
+ return _rs.apply_rcparams_font()
@@ -0,0 +1,101 @@
1
+ """rsplotlib._rcparams - Matplotlib 兼容的 rcParams 配置管理
2
+
3
+ 提供类似 matplotlib.rcParams 的全局配置字典,保存绘图相关的默认参数。
4
+
5
+ **字体钩子**:当用户设置 `rcParams["font.sans-serif"]` 时,会自动调用
6
+ Rust 的字体解析器 `apply_rcparams_font()`,把对应的字体文件注册到 plotters 的
7
+ 字体数据库中,从而真正影响文本渲染(而不是像普通 dict 一样只更新值)。
8
+ """
9
+ import copy as _copy
10
+ from typing import Any
11
+
12
+ # 默认配置:与 matplotlib 保持一致的常用项
13
+ _DEFAULT_RC = {
14
+ # 跨平台无衬线字体回退链:macOS/Windows 优先 Helvetica/Arial,其后回退到
15
+ # DejaVu Sans / Liberation Sans(Linux 常见字体,也是 matplotlib 的默认字体)。
16
+ # 这些名字按家族名从系统字体索引解析,不依赖具体安装路径,因此在没有
17
+ # Helvetica/Arial 的 Linux 上也能解析到真实字体,避免渲染时回退到无法解析的 "sans-serif"。
18
+ 'font.sans-serif': ['Helvetica', 'Arial', 'DejaVu Sans', 'Liberation Sans', 'sans-serif'],
19
+ 'axes.unicode_minus': True,
20
+ 'font.size': 10,
21
+ 'figure.figsize': [6.4, 4.8],
22
+ 'figure.dpi': 100.0,
23
+ }
24
+
25
+
26
+ class RcParams(dict):
27
+ """与 matplotlib.rcParams 兼容的配置字典
28
+
29
+ 区别于普通 dict:
30
+ 1. 当访问不存在的键时返回 None 而不是抛出 KeyError。
31
+ 2. **设置 `font.sans-serif` 时自动调用字体解析器**,把对应字体文件
32
+ 注册到 plotters 的字体数据库中,使 plotters 真正使用用户指定的字体
33
+ 渲染文字(而不是只更新一个不会生效的字符串列表)。
34
+ """
35
+
36
+ def __init__(self, *args, **kwargs):
37
+ super().__init__(*args, **kwargs)
38
+ self.update(_DEFAULT_RC)
39
+
40
+ def __getitem__(self, key):
41
+ try:
42
+ return super().__getitem__(key)
43
+ except KeyError:
44
+ return None
45
+
46
+ def __setitem__(self, key: str, value: Any) -> None:
47
+ super().__setitem__(key, value)
48
+ self._trigger_font_hook(key)
49
+
50
+ def update(self, *args, **kwargs):
51
+ super().update(*args, **kwargs)
52
+ # update 也需要触发字体钩子,否则初始化时默认字体不会被注册
53
+ if args:
54
+ other = args[0]
55
+ if isinstance(other, dict):
56
+ keys = other.keys()
57
+ else:
58
+ keys = [k for k, _ in other]
59
+ else:
60
+ keys = kwargs.keys()
61
+ if 'font.sans-serif' in keys:
62
+ self._trigger_font_hook('font.sans-serif')
63
+
64
+ def _trigger_font_hook(self, key: str) -> None:
65
+ # 字体钩子:用户在 Python 端改字体时同步通知字体注册
66
+ if key == 'font.sans-serif':
67
+ try:
68
+ # 延迟导入避免循环依赖
69
+ from .. import rsplotlib as _rs
70
+ from ._font_resolver import resolve_font_path
71
+ sans_serif = self.get('font.sans-serif')
72
+ if not sans_serif:
73
+ return
74
+ if isinstance(sans_serif, str):
75
+ candidates = [sans_serif]
76
+ else:
77
+ candidates = list(sans_serif)
78
+ candidates = [c for c in candidates if c and c.lower() != 'sans-serif']
79
+ # 清空旧的字体栈
80
+ _rs.clear_font_stack()
81
+ # 注册所有字体
82
+ for family in candidates:
83
+ path = resolve_font_path(family)
84
+ if path is None:
85
+ import os
86
+ if os.path.isfile(family):
87
+ path = family
88
+ if path is not None:
89
+ try:
90
+ _rs.register_sans_serif_font(path, family)
91
+ except Exception:
92
+ pass
93
+ except Exception:
94
+ # 注册失败不影响 rcParams 写入
95
+ pass
96
+
97
+
98
+ # 全局单例
99
+ rcParams = RcParams()
100
+ # 原始默认配置副本(用于 rcParams.reset() 等恢复操作)
101
+ rcParamsOrig = _copy.deepcopy(rcParams)
@@ -0,0 +1,67 @@
1
+ """rsplotlib.style - Matplotlib style 兼容接口
2
+
3
+ 底层实现: Rust Style
4
+ """
5
+
6
+ from .. import rsplotlib as _rs
7
+
8
+
9
+ # ==================== 可用样式 ====================
10
+
11
+ AVAILABLE_STYLES = [
12
+ 'default',
13
+ 'classic',
14
+ 'ggplot',
15
+ 'seaborn-v0_8',
16
+ 'fast',
17
+ 'fivethirtyeight',
18
+ 'grayscale',
19
+ 'dark_background',
20
+ 'bmh',
21
+ 'tableau-colorblind10',
22
+ ]
23
+
24
+
25
+ class Style:
26
+ """样式管理器
27
+
28
+ 底层实现: Rust Style
29
+ """
30
+
31
+ def __init__(self):
32
+ self._impl = _rs.Style()
33
+
34
+ def use(self, style_name):
35
+ """应用样式
36
+
37
+ Args:
38
+ style_name: 样式名称
39
+ """
40
+ self._impl.use_(style_name)
41
+
42
+ def available(self):
43
+ """返回可用样式列表"""
44
+ return self._impl.available()
45
+
46
+ @property
47
+ def current(self):
48
+ return self._impl.current
49
+
50
+
51
+ # 全局样式实例
52
+ style = Style()
53
+
54
+
55
+ def use(style_name):
56
+ """应用样式(模块级函数)"""
57
+ style.use(style_name)
58
+
59
+
60
+ def available():
61
+ """返回可用样式列表"""
62
+ return style.available()
63
+
64
+
65
+ def current():
66
+ """返回当前样式名称"""
67
+ return style.current