Pylustrator

Pylustrator

Goal

Producing high quality figures ready for publication can be quite challenging. Python libraries like Matplotib offer great possibilities, but it is very difficult to define a complex layout in code. Therefore, often image editing software (e.g. Adobe Illustrator, Photoshop, etc.) is used to compose the plots into a figure. This workflow is error-prone as data can be accidentally modified and tedious if data needs to be updated.

To solve this problem I developed Pylustrator and interactive plot editor directly on top of Matplotlib. The changes to the figure are saved as python code, so changing the underlying data or reproducing the plot is a breeze.

Challenge

To be really useful as a method to style matplotlib plots, the software needed to be deeply integrated with matplotlib. It required be to acquire a deep understanding of the internal works of matplotlib to connect and intercept the functionality at the right places.

As it should really lead to reproducible plot generation instead of just saving a final image, pylustrator generates python code to reliably create the design. It should do so in a robust manner as the user might later want to do changes to their plotting code and still expect the pylustrator code to work with their changes. Also, all the generated code should only rely on matplotlib itself and not pylustrator, as one important feature is that the user is not locked in to have pylustrator as a permanent dependency. After styling a plot, the pylustrator import can be removed before shipping the code.

Implementation

Just adding two lines of code to your plotting code opens the plot in Pylustrator instead of the Matplotlib window, by monkey patching the Matplotlib functions.

import pylustrator
pylustrator.start()

When loading the interface Pylustrator parses from calling python file Pylustrator's previous changes. All user interactions are recorded and when saving merged together and joined with the previous changes.

The code that Pylustrator generates is pure Matplotlib code, so it can be shipped without Pylustrator as a dependency.

Interface

The user interface is designed using Qt which embeds the matplotlib plot. As the matplotlib plot's rendering can slow for large plots, QtGraphicsView is used as an overlay of the plot to create more fluent user interactions.