Skip to content

Latest commit

 

History

History
211 lines (136 loc) · 5.36 KB

README.md

File metadata and controls

211 lines (136 loc) · 5.36 KB

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:

  • changes the minor ticks

  • remove the spines

  • turn the legend draggable by default

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:

  • publib.tools.reset_defaults: reset Matplotlib defaults

  • publib.tools.regenerate_fonts: rebuild Matplotlib font cache

  • publib.tools.fix_bold_TimesNewRoman: fix Times New Roman font appearing bold. See StackOverflow

  • publib.tools.keep_color: apply the same color for the next graph to plot

  • publib.tools.get_next_color: see which color will be applied next in the color cycle state

plt.plot(...)
keep_color()
plt.plot(...)
  • publib.tools.list_font_names: to list all fonts available in Python.

See tools.py for more details

Changes

  • 0.2.2: added tools

  • 0.1.9: added talk and OriginPro style

  • 0.1.7 : default fonts to Times in article

References

Some other interesting packages to make nice graphs in Matplotlib.

Add new features:

Style based:

Tips and demos of Matplotlib: