Skip to main content

一个为科研论文设计的声明式 Matplotlib 封装库,让复杂图表的创建变得简单直观。

Project description

PaperPlot

PyPI version License: MIT

一个为科研论文设计的声明式 Matplotlib 封装库,让复杂图表的创建变得简单直观。

PaperPlot 的诞生是为了解决在准备学术论文时,使用 Matplotlib 创建高质量、布局复杂的图表所面临的繁琐问题。它通过引入声明式的链式 API 和基于标签(tag)的对象管理,让你能够用更少的代码,更清晰的逻辑,构建从简单网格到复杂组合的各类图表。

核心理念与特性

  • 🎨 声明式链式调用: 像写句子一样构建你的图表,例如 plotter.add_line(...).set_title(...).set_xlabel(...)
  • 🏷️ 基于标签的控制: 给每个子图一个独一无二的 tag,之后就可以随时通过 tag 对其进行任何修改,告别混乱的 axes[i][j] 索引。
  • 🧩 强大的布局系统: 无论是简单的 (行, 列) 网格,还是使用 mosaic 实现的跨行跨列复杂布局,都能轻松定义。
  • 🧱 声明式嵌套布局: 通过一个字典即可一次性定义包含子网格的复杂层级布局,并使用 '容器.子图' 这样的直观路径进行引用,完美实现“图中图”。
  • 📐 灵活的尺寸控制: 除了传统的 figsize,还可以通过 subplot_aspect 指定子图单元格的宽高比,让 PaperPlot 自动计算最合适的画布尺寸。
  • ✨ 内置科研主题: 提供多种专业美观的内置样式,如 publication, presentation 等,一键切换图表风格。
  • 🌐 全局图层级标注: 提供了在整个画布(Figure)上添加文本、线条、方框和标签的 API,非常适合添加全局注释或高亮一组图表。
  • 🔬 丰富的领域专用图表: 内置了科研中常用的图表类型,如光谱图、混淆矩阵、ROC 曲线、学习曲线、分岔图、相量图等。
  • 🔧 智能美化工具: cleanup() 方法可以智能地共享坐标轴、对齐标签;cleanup_heatmaps() 可以为多个热图创建共享的颜色条。

安装

pip install paperplotter

快速开始

只需几行代码,就可以创建一个包含两个子图的 1x2 网格图。

import paperplot as pp
import pandas as pd
import numpy as np

# 1. 准备数据
df_line = pd.DataFrame({
    'time': np.linspace(0, 10, 50),
    'signal': np.cos(np.linspace(0, 10, 50))
})
df_scatter = pd.DataFrame({
    'x': np.random.rand(50) * 10,
    'y': np.random.rand(50) * 10
})

# 2. 初始化 Plotter 并通过链式调用绘图和修饰
(
    pp.Plotter(layout=(1, 2), figsize=(10, 4))
    
    # --- 绘制左图 ---
    .add_line(data=df_line, x='time', y='signal', tag='time_series')
    .set_title('Time Series Data', tag='time_series')
    .set_xlabel('Time (s)', tag='time_series')
    .set_ylabel('Signal', tag='time_series')
    
    # --- 绘制右图 ---
    .add_scatter(data=df_scatter, x='x', y='y', tag='scatter_plot')
    .set_title('Scatter Plot', tag='scatter_plot')
    .set_xlabel('X Value', tag='scatter_plot')
    .set_ylabel('Y Value', tag='scatter_plot')
    
    # --- 保存图像 ---
    .save("quick_start_figure.png")
)

通过示例学习 (Learn from Examples)

掌握 PaperPlot 最好的方法就是探索我们提供的丰富示例。每个示例都专注于一个核心功能,并附有详细的代码和注释。

布局 (Layout)

示例 描述 关键功能
声明式嵌套布局
Layout/declarative_nested_layout_example.py
使用字典来声明式地定义一个包含子网格的复杂、多层级布局,实现“图中图”的效果。 layout={...}
tag='容器.子图'
高级布局
Layout/advanced_layout_example.py
展示如何使用列表定义一个跨列的复杂布局。 layout=[['A', 'B', 'B'], ...]
行跨越
Layout/row_span_example.py
创建一个图表,其中某个子图跨越多行。 layout=[['A', 'B'], ['A', 'C']]
块跨越
Layout/block_span_example.py
创建一个图表,其中某个子图同时跨越多行和多列。 layout=[['A', 'A', 'B'], ['A', 'A', 'C']]
固定子图宽高比
Layout/aspect_ratio_example.py
在不指定 figsize 的情况下,通过 subplot_aspect 保证每个子图单元格的宽高比,Plotter 会自动计算画布大小。 subplot_aspect=(16, 9)

功能与定制化 (Features & Customization)

示例 描述 关键功能
多图网格
Features_Customization/multi_plot_grid.py
在一个网格中通过链式调用混合绘制不同类型的图表。 plotter.add_...().add_...()
高级定制
Features_Customization/advanced_customization.py
演示如何使用 get_ax() "逃生舱口" 来获取原生的 Matplotlib Axes 对象,并添加任意 Patch(如椭圆)。 get_ax(), add_patch()
全局控制
Features_Customization/global_controls_example.py
展示如何设置全局标题 (suptitle) 和创建全局图例。 set_suptitle(), add_global_legend()
共享颜色条
Features_Customization/heatmap_colorbar_example.py
为多个热图创建一个共享的、能反映全局数据范围的颜色条。 add_heatmap(cbar=False), cleanup_heatmaps()
智能清理
Features_Customization/cleanup_demonstration.py
演示 cleanup() 函数如何动态地为指定行/列的子图共享 X/Y 轴,并自动隐藏多余的刻度标签。 cleanup(auto_share=True)
组合图与内嵌图
Features_Customization/composite_figure_example.py
创建一个 L 型的复杂图表,并在其中一个子图内部嵌入一张图片。 layout=[['A', 'A'], ['B', '.']], add_inset_image()
功能扩展
Features_Customization/feature_expansion_example.py
演示双Y轴 (add_twinx)、回归图 (add_regplot)、参考线 (add_hline) 和文本标注 (add_text) 等高级功能。 add_twinx(), add_regplot(), add_hline(), add_text()
缩放嵌入图
Features_Customization/zoom_inset_example.py
在主图上创建一个放大特定区域的嵌入式子图,并自动添加连接线。 add_zoom_inset()
错误处理
Features_Customization/error_handling_test.py
展示 PaperPlot 的自定义异常,如 DuplicateTagError, TagNotFoundError, PlottingSpaceError try...except pp.PaperPlotError

标注与高亮 (Annotation & Highlighting)

示例 描述 关键功能
图层级标注
Features_Customization/fig_annotation_example.py
演示添加跨越多个子图的画布级注解,如方框、标签和线条。 fig_add_box(), fig_add_label(), fig_add_line(), fig_add_text()
区域高亮
Features_Customization/highlighting_example.py
展示如何在子图内部高亮特定的数据区域,并为整个图表添加边框。 add_highlight_box(), fig_add_boundary_box()

风格与美化 (Styles & Aesthetics)

示例 描述 关键功能
风格画廊
Styles_Aesthetics/style_gallery_example.py
循环遍历所有内置的绘图风格,并为每种风格生成一个示例图。 Plotter(style='...')
统计标注
Styles_Aesthetics/statistical_annotation_example.py
在箱线图上自动进行多组成对统计检验(如 t-test),并智能堆叠显著性标记。 add_box(), add_pairwise_tests()
美学与处理
Styles_Aesthetics/aesthetic_and_processing_example.py
对数据进行平滑处理或根据条件高亮特定数据点。 utils.moving_average(), add_conditional_scatter()
统计图表示
Features_Customization/statistical_plots_example.py
演示如何组合小提琴图和蜂群图,以及在箱线图上添加统计检验。 add_violin(), add_swarm(), add_box(), add_stat_test()

领域专用图 (Domain-Specific Plots)

示例 描述 关键功能
领域专用图合集
Domain_Specific_Plots/domain_specific_plots_example.py
一站式展示多种领域专用图,包括 SERS 光谱图、混淆矩阵、ROC 曲线和 PCA 散点图。 add_spectra(), add_confusion_matrix(), add_roc_curve(), add_pca_scatter()
3D 绘图
Domain_Specific_Plots/3d_plots_example.py
展示如何创建 3D 线图和表面图。 ax_configs={'...': {'projection': '3d'}}, add_line3d(), add_surface()
学习曲线
Domain_Specific_Plots/learning_curve_example.py
绘制机器学习模型的学习曲线,帮助诊断过拟合或欠拟合问题。 add_learning_curve()
SERS 浓度图
Domain_Specific_Plots/concentration_map_example.py
绘制 SERS Mapping 浓度图,本质上是带有专业美化的热图。 add_concentration_map()
电力系统时间序列
Domain_Specific_Plots/power_timeseries_example.py
绘制电力系统动态仿真结果,并自动标记故障、切除等事件。 add_power_timeseries()
相量图
Domain_Specific_Plots/phasor_diagram_example.py
在极坐标上绘制电气工程中的相量图。 add_phasor_diagram()
分岔图
Domain_Specific_Plots/bifurcation_diagram_example.py
绘制常用于非线性系统和稳定性分析的分岔图。 add_bifurcation_diagram()

数据分析工具 (Data Analysis Utils)

示例 描述 关键功能
数据分析工具集
Data_Analysis_Utils/data_analysis_utils_example.py
演示如何对数据进行分布拟合和数据分箱绘图。 add_distribution_fit(), add_binned_plot()
通用工具函数
Data_Analysis_Utils/utility_functions_example.py
展示更多通用的修饰函数,如在高光谱上高亮特征峰和在时间序列上标记事件。 add_peak_highlights(), add_event_markers()

贡献

欢迎任何形式的贡献!如果你有好的想法、发现了 bug,或者想要添加新的功能,请随时提交 Pull Request 或创建 Issue。

许可证

本项目采用 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

paperplotter-0.1.5.tar.gz (9.8 MB view details)

Uploaded Source

Built Distribution

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

paperplotter-0.1.5-py3-none-any.whl (9.8 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: paperplotter-0.1.5.tar.gz
  • Upload date:
  • Size: 9.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for paperplotter-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d7020da01efb12a0452ead10a7b5566c32457aea58e42b1a25a890cdaff322fc
MD5 ad8aab4a9ba777eeae1f4d3da4dc8c9b
BLAKE2b-256 975700c47776796e7df386812835e3f1b414c17e0ee125b747ac35efe73a87bf

See more details on using hashes here.

File details

Details for the file paperplotter-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: paperplotter-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 9.8 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for paperplotter-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 31d6a5e17f7aa1b3e9500929b93c8b1744c816361e788b8727cb8cf432257ef0
MD5 92561beaf1f4d12704570e2ebe077874
BLAKE2b-256 52abc535edf09e11c2c364a8cdbefd5a9658c8d2bdaf35de3583a66916c7ac8a

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