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.5.tar.gz (84.3 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.5-cp313-cp313-macosx_11_0_arm64.whl (793.5 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

rsplotlib-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl (842.1 kB view details)

Uploaded CPython 3.13macOS 10.12+ x86-64

File details

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

File metadata

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

File hashes

Hashes for rsplotlib-0.1.5.tar.gz
Algorithm Hash digest
SHA256 9c8af2103d7d63764c2f1bc5b61daa1280ec0e5d16eb959846f72a4ef6751278
MD5 9abba55d7cc3a320d0e0d20b1efb3caf
BLAKE2b-256 bb073e8f8589d67ff88d7019d23217f53818b9850c072673e35f023392f5b506

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for rsplotlib-0.1.5-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 14602ba0c7369399e63815f00a2ce41ded2d62968f8ae91ab90a2747bec49e9f
MD5 67e5fbecbd4cecfd6c7b54723bfe4044
BLAKE2b-256 84f79871d564bdb6ceb92697fad857f5aceeb999e65a25c21d801a0045620810

See more details on using hashes here.

File details

Details for the file rsplotlib-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rsplotlib-0.1.5-cp313-cp313-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 4fb5999e835c1a2c170c38de2e6967eafe9d4ffcc4ab3e1d72c124e79a1c1d73
MD5 58ab19c07233a98b6d3be8a099803a4d
BLAKE2b-256 77a7f664fe03c5fb1907b9075617ff0150841e8280a8d8efe34a16d879df10cf

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