erwanp / publib

Produce publication-level quality images on top of Matplotlib
MIT License
40 stars 1 forks source link
matplotlib matplotlib-style-sheets matplotlib-styles plot scientific-papers

PyPI version Tests Code coverage

Publib

Description

Produce publication-level quality images on top of Matplotlib, with a simple call to a couple functions at the start and end of your script.

Project GitHub page

For similar librairies, see the References section.

Install

pip install publib

Use

At the beginning of the script, call:

set_style()

After each new axe is plotted, call:

fix_style()

Note that importing publib will already load the basic style.

A few more styles ('poster', 'article', etc.) can be selected with the function set_style()

Because some matplotlib parameters cannot be changed before the lines are plotted, they are called through the function fix_style() which:

Examples

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

A default Matplotlib plot:

mpl.rcdefaults()

x = np.linspace(0,5,250)
y = np.cos(x)**2+np.random.normal(scale=0.5,size=len(x))
yav = np.cos(x)**2
plt.figure()
ax = plt.subplot()
ax.plot(x,y,'o',label='normal distribution')
ax.plot(x,yav,zorder=-1,label='average')
plt.xlabel(r'$x$')
plt.ylabel(r'$\cos^2 x$+noise')
plt.title('matplotlib')
plt.legend(loc='upper left')
plt.ylim((-1.5,3.5))
plt.show()
plt.savefig('mpl_default.png')

mpl_defaults.png

And now the same code with the two new lines calling the publib functions

from publib import set_style, fix_style
set_style('article')        # before the first plot

x = np.linspace(0,5,250)
y = np.cos(x)**2+np.random.normal(scale=0.5,size=len(x))
yav = np.cos(x)**2
plt.figure()
ax = plt.subplot()
ax.plot(x,y,'o',label='normal distribution')
ax.plot(x,yav,zorder=-1,label='average')
plt.xlabel(r'$x$')
plt.ylabel(r'$\cos^2 x$+noise')
plt.title('article')
plt.legend(loc='upper left')
plt.ylim((-1.5,3.5))

fix_style('article')  # after the axe has been created

plt.show()
plt.savefig('publib_article.png')

publib_article.png

The OriginPro style:

set_style('origin')

...

fix_style('origin')

publib_origin.png

A combination of styles:

set_style(['poster', 'origin'])

...

fix_style(['poster', 'origin'])

publib_poster_origin.png

Or, assuming you have the Latin Modern Math font installed:

set_style(['origin', 'latex'])

...

fix_style(['origin', 'latex'])

publib_origin_latex.png

Run the test() routines in publib.test for more examples.

Tools

The publib.tools module include independant functions to fix some common matplotlib bugs, or include extra features. They're usually glanced from somewhere on the web. Proper
referencing is made in the function docstrings.

See for instance:

plt.plot(...)
keep_color()
plt.plot(...)

See tools.py for more details

Changes

References

Some other interesting packages to make nice graphs in Matplotlib.

Add new features:

Style based:

Tips and demos of Matplotlib: