Git Product home page Git Product logo

paulgavrikov / parallel-matplotlib-grid Goto Github PK

View Code? Open in Web Editor NEW
14.0 1.0 2.0 188 KB

This Python 3 module helps you speedup generation of subplots in pseudo-parallel mode using matplotlib and multiprocessing. This can be useful if you are dealing with expensive preprocessing or plotting tasks such as violin plots per subplot.

License: MIT License

Python 100.00%
python matplotlib matplotlib-pyplot matplotlib-python multiprocessing

parallel-matplotlib-grid's Introduction

Parallel generation of grid-like plots using matplotlib

This Python 3 module helps you speedup generation of subplots in pseudo-parallel mode using matplotlib and multiprocessing. This can be useful if you are dealing with expensive preprocessing or plotting tasks such as violin plots per subplot.

Operation overview

How does it work?

This library uses pythons multiprocessing module to plot each cell individually. If provided, each process will first evaluate a user-defined preprocessing function. Afterwards, every process will call a second user-defined plotting function providing matplotlib axes to plot on. All created plots then stored as images and then retrieved and assembled by the main thread into a subplot without any decoration.

How do I install this module?

This module is in a very early stage, so no pypi releases are currently provided. However, you can simply install this module from git:

pip install git+https://github.com/paulgavrikov/parallel-matplotlib-grid/

How do I use it?

Aside from the data all you need to provide is the grid layout grid_shape and a plotting function plot_fn. Here is an example:

from parallelplot import parallel_plot

import matplotlib.pyplot as plt
import numpy as np


def violin(data, fig, axes):
    axes.violinplot(data)


# Gen some fake data 
X = np.random.uniform(low=-1, high=1, size=(30, 512, 512))

parallel_plot(plot_fn=violin, data=X, grid_shape=(3, 10))
plt.show()

Want to preprocess your data before plotting? No problem! just provide preprocess_fn. Here is an example where we apply a PCA transformation:

from parallelplot import parallel_plot

import matplotlib.pyplot as plt
import numpy as np
from sklearn.decomposition import PCA


def preprocess(data):
    return PCA().fit_transform(data)


def violin(data, fig, axes):
    axes.violinplot(data)


# Gen some fake data
X = np.random.uniform(low=-1, high=1, size=(30, 512, 512))

parallel_plot(plot_fn=violin, data=X, grid_shape=(3, 10), preprocessing_fn=preprocess)
plt.show()

When should I not use this library?

There are some cases where this module is either useless or adds overhead. Here are a few of those:

  • Your plot function and preprocessing functions execute fast, but your data is big. multiprocessing uses pickle as input / output format of process tasks which requires data to be serialized. This can introduce a significant overhead.
  • Your data is over 4 GiB big. For some reason multiprocessing is using some ancient pickle format that only supports data up to 4 GiB of size. There are ways to bypass that, but it's probably not worth it, as pickling is slow, and the computational overhead may not be worth it.
  • You only have one core available. Sorry 'bout that.

How do I contribute?

Just create a PR or feel free to raise an issue for questions, feature-requests etc.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.