MIT License

技术解读

以下内容由 GitRead 中文社区 编辑团队基于项目 README 与官方资料整理, 结合中文开发者的常见使用场景补充技术选型建议。具体以项目官方文档为准。

LovelyPlots:科学出版物中的美化工具

项目背景与痛点

在科学出版物、论文和学术展示中,图形和图表是不可或缺的传输信息手段。美观且清晰的图表能够大大增强可读性和可解释性。然而,现有的绘图库如 Matplotlib,虽然强大,但默认的样式通常较为简陋,难以满足高度美学需求。此外,很多生成后的图像在导入至设计软件如 Adobe Illustrator 调整时,往往无法保持其各组成部分的可编辑性。LovelyPlots 应运而生,它提供了一系列样式表,可以在保持图形各部分高度可编辑性的同时对其进行美化,适应科学出版物的需求。

核心技术揭秘

LovelyPlots 的核心在于为 Matplotlib 库提供了易于使用的样式表库,确保生成的图像更加美观,并且在 Adobe Illustrator 中依然保持高水平的可编辑性。其技术实现主要依赖于以下几点:

  1. 样式表语法:LovelyPlots 拥有一套自定义的样式表,可以直接应用于 Matplotlib 生成的图像。这其中包括字体、颜色、线型以及图表边框的样式变更,充分满足科研和出版要求。
  2. 兼容性处理:在确保图表美观的同时,LovelyPlots 专门设计了一系列策略,确保在导出 PDF 格式时,文件中的每一个元素(特别是文本和图形)都能在 Adobe Illustrator 中被原样编辑,赋予其高自由度的后期处理能力。

从技术架构上来看,LovelyPlots 在数据流与处理逻辑设置上相当清晰,以下用一张 Mermaid 架构图展示其核心处理步骤:

graph TD A[Matplotlib 绘图] --> B("LovelyPlots 样式应用") B --> C[自定义样式调整] C --> D{"导出功能"} D -->|"支持自定义"| E[Adobe Illustrator 兼容格式导出] D -->|"默认设置"| F[通用格式导出]

在每个步骤中,LovelyPlots 确保都为开发者提供了高可配置性,通过一系列的自定义选项来满足不同场景下的需求。

功能亮点与差异

LovelyPlots 的最大卖点在于,它不但可以提升 Matplotlib 默认样式到出版物级别的美观度,而且完美地解决了高兼容性的导出问题,使得科研工作者无需担忧后期编辑效果被破坏。此外,从整体设计上,Project LovelyPlots 更加注重细致的自定义选项,在提升美化效果的同时保持图像的可编辑能力,这种特点在同类软件中颇为独特。

应用场景与落地建议

在生产和科研环境中,LovelyPlots 适用于广泛需要高质量出版和报告呈现的场景,包括但不限于高水平学术论文提交、毕业论文展示以及学术报告及讲座。开发中需注意,尽管其对大多数常见的 Matplotlib 用法都表现优异,但在使用某些特殊绘图需求或自定义元素时,可能仍需开发者手动干预以达到最优的视觉效果。

综合评价

LovelyPlots 是一款具有高度实用性和美观性的工具,它从本质上简化了科研中图形出版物的制作流程,并在保证高编辑灵活性的同时提供了强大的美化功能。然而,作为一种依赖于 MATLAB 库的扩展工具,它并不能从根本上解决绘图逻辑上的复杂性,因此使用中需结合具体需求灵活选择。尽管如此,对于多数科研和学术工作者来说,LovelyPlots 确实是一个值得一看的优秀工具。

关于

GitHub 原始描述(英文)

Matplotlib style sheets to nicely format figures for scientific papers, thesis and presentations while keeping them fully editable in Adobe Illustrator.

描述取自 GitHub 官方 API,保持仓库作者书写的原文。

简要分析

热度分
1932
价值分
659
活跃状态
活跃

语言占比

Python

Release

README

LovelyPlots

PyPI Version PyPI Downloads doi tests

Warning 警告

As of version 1.0.0, you need to add import lovelyplots before setting the style (plt.style.use('ipynb')).

LovelyPlots is a repository containing matplotlib style sheets to nicely format figures for scientific papers, thesis and presentations while keeping them fully editable in Adobe Illustrator. Additonaly, .svg exports options allows figures to automatically adapt their font to your document’s font. For example, .svg figures imported in a .tex file will automatically be generated with the text font used in your .tex file.

Installation

# to install latest PyPI release
pip install LovelyPlots

# to install latest GitHub commit
pip install --upgrade git+https://github.com/killiansheriff/LovelyPlots.git

The pip installation will move all of the matplotlib style files *.mplstyle into the appropriate matplotlib directory.

Usage

LovelyPlots main style is called ipynb. To use it, add the following lines to the begining of your python scripts:

import lovelyplots
import matplotlib.pyplot as plt
plt.style.use('ipynb')

Styles can be combined:

import lovelyplots
import matplotlib.pyplot as plt
plt.style.use(['ipynb','colorsblind34'])

In the above case, the ipynb default color cycle will be overwritten by a 34 colors colorblind safe color cycle called colorsblind34.

If you only wish to apply a style on a specific plot, this can be achieved using:

import lovelyplots
import matplotlib.pyplot as plt

with plt.style.context('ipynb'):
  fig, ax = plt.subplots()
  ax.plot(x, y)

Examples

A few styles are presented here, please see Styles for a list of all implemented styles. The script used to generate these plots can be found here.

:warning: If you use the use_mathtext style, Adobe Illustrator might fail to nicely detect text objects. Please see here for work arounds.

The ['ipynb', 'use_mathtext'] style:

The ['ipynb', 'use_mathtext','colors10-markers'] style:

The ['ipynb', 'use_mathtext','colors5-light'] style:

The ['ipynb', 'use_mathtext', 'colors10-ls'] style:

The ['ipynb'] style:

The ['paper', 'use_mathtext'] style:

Styles

LovelyPlots main style is called ipynb. The latter by default sets the figure size to (4.5, 3.46) inches, uses the default matplotlib font, activate scientific notation and makes sure your matplotlib exports will be editable in Adobe Illustrator. Its default color cycle was set to colors10.

Color cycles

A multitude of color cycles were implemented:

colors5-light

colors5

colors10

colorsblind10

colorsblind34

Can be seen here.

Lines styles, markers and combinations styles

Line styles, markers styles and combinations can be set using the following styles: ls5, marker7, colors10-ls, colors10-markers.

Color maps

Default matplotlib colormaps were implemented and can be used by adding the following styles: cmap-viridis, cmap-inferno, cmap-cividis, cmap-magma, cmap-plasma.

Utils

Specific matplotlibrc parameters can be turned on/off using the following utilities styles: svg_no_fonttype, use_mathtex, use_tex.

Fonts

By default the ipynb style uses the default matplotlib font. However, one can set its favorite font from a TIFF file:

import lovelyplots as lp
import matplotlib.pyplot as plt

plt.style.use('ipynb')
lp.utils.set_font('my_font.tiff')

Tips and Tricks

Adobe Illustrator

Unfortunately, mathtext (and thus nicely formated scientific notation) will mess up Adobe illustrator ability to detect text objects, and is thus not activated by default. If you wish to use it, please add the style use_mathtext.

Latex and SVG files

By default, the ipynb style sets svg.fonttype: none. This allows for plots saved as .svg not to carry font information. Consequently, when opened in another environement, the plot will be generated with the default system font.

For example, this allows .svg plots imported inside a Latex file to directly be generated with the proper document font, without you having to manually edit the fonts to match your document’s font. Additonally, you can open the .svg file as text file, find the ugly 1e10 scientific notation and replace it with $10^10$ so that it is nicely formated when included in your .tex file.

An example of how to show an svg in a .tex file:

\usepackage{svg}

\begin{figure}[htbp]
  \centering
  \includesvg{myfig.svg}
\end{figure}

Retina displays

For those using IPython notebooks, you can set retina display support by adding the following lines to the begining on your python script:

import lovelyplots as lp
lp.utils.set_retina()

Useth in Google Colab

To use on Google Colab, you will need to run the following code:

!pip install lovelyplots
import lovelyplots
plt.style.reload_library()
plt.style.use('ipynb')

Acknowledgements

This reprository was inspired by SciencePlots, but adds different styles and crucial functionalities for useth in .tex files and Adobe Illustrator.

Citing

If you use LovelyPlots, please cite:

@software{sheriff_lovelyplots_2022,
  author       = {Killian Sheriff},
  title        = {{LovelyPlots, a collection of matplotlib 
                   stylesheets for scientific figures}},
  month        = jul,
  year         = 2022,
  publisher    = {Zenodo},
  doi          = {10.5281/zenodo.6903936},
}

评论

暂无评论