Skip to main content

drawnow for matplotlib

The scientific community often runs iterative code, often in the form of simulation. It's often useful to see the results after each iteration. Accordingly, MATLAB® has a nice feature that allows you to update the figure, drawnow. This repo brings the same feature to Python's matplotlib, with some extras.

Example:

image0

This is shown with imshow, but python-drawnow allows updates of any figure.

Usage:

# complete implementation of script found in test/test.py
from pylab import *
from drawnow import drawnow, figure
# if global namespace, import plt.figure before drawnow.figure

def approx(x, k):
    """Approximate x with k singular values"""
    ...

figure(figsize=(7, 7/2))
def draw_fig():
    subplot(1, 2, 1)
    imshow(x)

    subplot(1, 2, 2)
    imshow(x_hat)
    #show()

x = imread('test-data/mandrill.png').mean(axis=2)
k_values = around(logspace(0, 2, num=10))
for k in k_values:
    x_hat = approx(x, k)
    drawnow(draw_fig)

Documentation

If you want to wait for confirmation after update or the option to drop into a debugger, call drawnow(function_to_draw_figure, confirm=True).

If you only want to show the figure once, call drawnow(function_to_draw_figure, show_once=True). The full documentation is included in the doc strings. Use drawnow? or help(drawnow) to see these docs.

Jupyter/Spyder

Try running the folloowing code in a Jupyter input cell/in the console/etc:

%matplotlib

This will disable the Matplotlin inline mode and use the default plotting backend. For more detail, see the IPython plotting documentation.

Installation

Two options:

  1. Run pip install drawnow.
  2. Download this repository and run python setup.py install.

Option 2 assumes a working Python installation with pip. I suggest Anaconda's distribution: https://www.anaconda.com/distribution/ For other options, see https://realpython.com/installing-python/.

Changes to code

This does require small changes to your code. All it should really amount to is moving figure(); plot(...); show() inside a function; not much.

Release files for drawnow 0.72.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drawnow 0.72.5
File Size Uploaded
drawnow-0.72.5.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drawnow 0.72.5
File Interpreter ABI Platform
drawnow-0.72.5-py3-none-any.whl Python 3 none any Details

Total release size: 8.4 kB

Release files / drawnow-0.72.5.tar.gz

Download URL drawnow-0.72.5.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
9d1855605b2ec6ebc4e8a95201a7a0068eb1e2a5d1695cb1b7c462d660f32593
BLAKE2b-256 checksum
How to use checksums
3f177a46bface13dab4f70b113af9cef852bdbddd8be835ef9a1f78bc1ce718a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release files / drawnow-0.72.5-py3-none-any.whl

Download URL drawnow-0.72.5-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4ff83a8b15f61a781edaaa2a3e6b71e2c8fd948960f188b870def701afcfa0d5
BLAKE2b-256 checksum
How to use checksums
6b8f97b431d9b3b8cd63c4d7da165fdd4b46779b2c1118158606272b9d3a706b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.72.5 This release

2 release files

0.72.4

2 release files

0.72.1

1 release file

0.71.4

1 release file

0.71.3

1 release file

0.71.2

1 release file

0.71.1

1 release file

0.71

1 release file

0.70

1 release file

0.60

1 release file

0.54

1 release file

0.44

1 release file

0.43

1 release file

0.42

1 release file

0.41

1 release file

0.4

1 release file

0.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page