Skip to main content

Custom functions to improve the look of matplotlib plots for scientific visualisation.

Project description

sciplotlib

sciplotlib is just a set of simple functions and stylesheets for more professional looking plots

nature review style shading scatter

There are two main properties that make plots in papers look the way they do:

  1. default style properties such as typeface, color scheme,
  2. specific choices in terms of the placement of tick marks, or additional elements that are added to the plot such as shading or shadows

Stylesheets

sciplotlib aims to make (1) easier by providing stylesheets that aims to mimic the style properties found in scientific papers:

We can compare the default matplotlib style with a style that mimics scatter plots found in articles from the Nature publishing group:

import matplotlib.pyplot as plt
import numpy as np

def make_plot():
    fig, ax = plt.subplots()
    num_categories = 10
    num_points = 10
    for category in np.arange(num_categories):
        x = np.random.normal(size=num_points)
        y = np.random.normal(size=num_points)
        ax.scatter(x, y)

    return fig, ax
	

fig, ax = make_plot()
ax.set_title('Default matplotlib style')

default matplotlib scatter

Applying the most basic style is just one line of code

from sciplotlib import style as spstyle

with plt.style.context(spstyle.get_style('nature-reviews')):
    fig, ax = make_plot()
    
ax.set_title('Nature reviews style')

basic style

Modifying figure properties

sciplotlib also aims to make (2) easier by providing functions that automically add elements found in scientific plots. For example, in many scientific journals it is common for the axis to extend only from and up to the last tick mark, and in figures found in Nature review articles, it is also common that shading will be added to plots, these are implemented by functions that simpy takes in the figure handles and return them:

from sciplotlib import style as spstyle
from sciplotlib import polish as sppolish

with plt.style.context(spstyle.get_style('nature-reviews')):
    fig, ax = make_plot()
    fig, ax = sppolish.set_bounds(fig, ax)
    sppolish.apply_gradient(ax, extent=None, 
                    direction=0.3, cmap_range=(0.1, 0),
                    cmap='Greys')
    
ax.set_title('Nature reviews style with bells and whistles')

advanced nature reviews style

Installation

Simply do

pip install sciplotlib

Acknowledgments

sciplotlib is built on top of matplotlib. To cite matplotlib in your publications, cite:

J. D. Hunter, "Matplotlib: A 2D Graphics Environment", Computing in Science & Engineering, vol. 9, no. 3, pp. 90-95, 2007

Other projects that is also built on the idea of providing stylesheets / wrappers for scientific plots include:

Color palettes of scientific papers are obtained from the wonderful ggsci library:

https://cran.r-project.org/web/packages/ggsci/vignettes/ggsci.html

Contributing

Do contact me if you are interested in adding new functions or templates to this repository.

Other fun stuff

I am also including other aesthetically pleasing plot styles that are non-academic. For example, to create plots from The Economist, do:

import numpy as np
import matplotlib.pyplot as plt
from sciplotlib import style as spstyle
from sciplotlib import misc as spmis


with plt.style.context(spstyle.get_style('economist')):
    fig, ax = plt.subplots()
    ax.scatter(x, y)
    ax.text(0, 1.2, 'Main title', weight='bold', size=13, transform=ax.transAxes)
    ax.text(0, 1.1, 'This is the usual long subtitle', transform=ax.transAxes)
    fig, ax = spmisc.add_economist_rectangle(fig, ax, xloc=0.125, yloc=1.1, width=0.05, height=0.02)
    fig, ax = spmisc.add_datasource(fig, ax, s='Source: IMF', xloc=0.125, yloc=0, alpha=0.6)

economist advanced style

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

sciplotlib-0.0.7.tar.gz (52.3 kB view details)

Uploaded Source

Built Distribution

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

sciplotlib-0.0.7-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file sciplotlib-0.0.7.tar.gz.

File metadata

  • Download URL: sciplotlib-0.0.7.tar.gz
  • Upload date:
  • Size: 52.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for sciplotlib-0.0.7.tar.gz
Algorithm Hash digest
SHA256 e25d8b2bf4ba250057535314da037545c02e64c1d3c16ec717334513c64963db
MD5 a2269080047e8daae7158efafb860c6e
BLAKE2b-256 2b75dcaab14f7e7e1522e9677bc4efb48d41d645ab745fb553377a017991a9cb

See more details on using hashes here.

File details

Details for the file sciplotlib-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: sciplotlib-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 55.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.8

File hashes

Hashes for sciplotlib-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 4d8e356f917c0e9ff1319ab9b777c5a81439d689600294b245c461160548d53f
MD5 3c2a8e722accdd80c5614e3f07cb9682
BLAKE2b-256 36a4b65ffa1a07691afe4017e94406eba49a08520c7decf5107f96c8b293d522

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