Skip to main content

A high-performance matplotlib-compatible plotting library powered by Rust

Project description

rsplotlib

一个使用 Rust 实现的高性能 Python 绘图库,旨在兼容 Matplotlib 的 API,同时利用 Rust 的性能优势和可移植性。

特性

  • Matplotlib 兼容 API: 支持常见的绘图函数如 plotscatterbarhistpie
  • 高性能: 核心渲染引擎使用 Rust 编写,基于 plotters 库
  • 跨平台: 支持 macOS、Linux 和 Windows
  • 自定义字体支持: 通过 mpl.rcParams 支持字体自定义
  • 多后端支持: 支持 SVG 和 PNG 输出

安装

前置依赖

  • Python 3.9+
  • Rust 1.70+(从源码编译时需要)
  • uv(推荐的 Python 包管理工具)

从源码构建

# 创建虚拟环境
uv venv --python 3.13

# 激活虚拟环境
source .venv/bin/activate

# 构建 Rust 扩展并安装包
./build_wheel.sh

# 安装依赖
uv pip install matplotlib numpy

快速开始

from rsplotlib import pyplot as plt
from rsplotlib.pylab import mpl

# 配置字体(可选)
mpl.rcParams['font.sans-serif'] = ['Arial']

# 创建图形
fig, ax = plt.subplots()

# 绘制数据
ax.plot([0, 1, 2, 3], [1, 4, 2, 3], label='折线')
ax.scatter([0, 1, 2, 3], [2, 3, 1, 4], color='red', label='散点')

# 添加标签和标题
ax.set_xlabel('X轴')
ax.set_ylabel('Y轴')
ax.set_title('示例图')
ax.legend()

# 保存图形
fig.savefig('output.png')

支持的函数

绘图函数

  • plot() - 折线图
  • scatter() - 散点图
  • bar() - 柱状图
  • barh() - 水平柱状图
  • hist() - 直方图
  • pie() - 饼图
  • boxplot() - 箱线图
  • fill_between() - 填充区域
  • errorbar() - 误差棒图
  • stem() - 茎叶图
  • step() - 阶梯图
  • imshow() - 图像显示

文本函数

  • text() - 添加文本
  • title() - 添加标题
  • xlabel() - 添加 X 轴标签
  • ylabel() - 添加 Y 轴标签

配置函数

  • grid() - 切换网格显示
  • legend() - 显示图例
  • xlim() / ylim() - 设置坐标轴范围
  • xticks() / yticks() - 设置刻度位置
  • xscale() / yscale() - 设置坐标轴缩放(线性/对数)
  • use() - 设置后端

子图函数

  • subplots() - 创建子图网格
  • subplot() - 创建单个子图
  • twinx() / twiny() - 创建双坐标轴
  • tight_layout() - 自动调整布局

字体配置

rsplotlib 支持通过 mpl.rcParams 自定义字体:

from rsplotlib.pylab import mpl

# 设置字体族
mpl.rcParams['font.sans-serif'] = ['PingFang SC', 'Microsoft YaHei', 'DejaVu Sans']

# 设置字体大小
mpl.rcParams['font.size'] = 12

支持的字体

  • macOS: Arial, Helvetica, PingFang SC, STHeiti, Hiragino Sans GB
  • Linux: DejaVu Sans, Liberation Sans, Noto Sans CJK SC, WenQuanYi Micro Hei
  • Windows: Microsoft YaHei, SimHei, SimSun

项目结构

rsplotlib/
├── python/
│   └── rsplotlib/          # Python 封装实现
│       ├── __init__.py      # 包导出
│       ├── api.py           # 公共 API 定义
│       ├── pyplot.py        # pyplot 模块(兼容 Matplotlib)
│       ├── pylab.py         # pylab 模块,包含 mpl.rcParams
│       ├── _rcparams.py     # rcParams 配置管理
│       └── _font_resolver.py # 字体路径解析
├── src/                     # Rust 实现
│   ├── lib.rs              # Rust 库入口
│   ├── pyfuncs.rs          # 暴露给 Python 的函数
│   ├── figure.rs           # Figure 实现
│   ├── axes.rs             # Axes 实现
│   └── axes_render_elements.rs # 渲染元素
├── Cargo.toml              # Rust 依赖配置
├── pyproject.toml          # Python 包配置
└── build_wheel.sh          # 构建脚本

开发流程

  1. 修复(Fix): 根据测试用例定位并修复问题
  2. 重构(Refactor): 清理代码,优先修改 python/rsplotlib/ 下的代码
  3. 画图(Plot): 使用 main.pypython/examples.py 生成对比图像
  4. 对比(Compare): 将生成的 PNG 与 Matplotlib 参考图像对比
  5. 迭代(Iterate): 根据差异重复循环

贡献指南

提交 PR 时请包含:

  • 生成的示例图像
  • 修复的问题或改进的说明

许可证

MIT License

致谢

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rsplotlib-0.1.4.tar.gz (75.7 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rsplotlib-0.1.4-cp313-cp313-macosx_11_0_arm64.whl (759.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

rsplotlib-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl (804.2 kB view details)

Uploaded CPython 3.12macOS 10.12+ x86-64

File details

Details for the file rsplotlib-0.1.4.tar.gz.

File metadata

  • Download URL: rsplotlib-0.1.4.tar.gz
  • Upload date:
  • Size: 75.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for rsplotlib-0.1.4.tar.gz
Algorithm Hash digest
SHA256 538114b86fbb58df55fc7cb0b6341bbf8a4f1805ee9e823842f5c9bb32631b98
MD5 775bbfa15b91bba69dbb4ffaabcb8a39
BLAKE2b-256 beb8eb670614aa56ecc88c9f0607f9b8d7040f9e865e6de45543a3cc2974143c

See more details on using hashes here.

File details

Details for the file rsplotlib-0.1.4-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rsplotlib-0.1.4-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9674bb0112e060954edcc61e6dd8bf5283bd74df95d81e3850c54fce19908ebe
MD5 60929977d10b95403308ff4419118ebd
BLAKE2b-256 59e5c6c8c7410acc13572be560911c36f6a81caf7fcf236970c4f7746ad0ae3d

See more details on using hashes here.

File details

Details for the file rsplotlib-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsplotlib-0.1.4-cp312-cp312-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 14c4a2127e3e62d06a477c6d819a6c87834445105e6bb2055bce31996cae76ff
MD5 cb6d4abbe0e99c1cfcf5576db3f5a509
BLAKE2b-256 4e29845e8d267b5ad60e530767b2b12e914bbdfc1faf74ac31ad95c362696f4f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page