Skip to main content

A Matplotlib.Figure fork with real-time plot streaming features.

Project description

Matplotlib-FigureStream

A backend for serve Matplotlib animations as web streams.

GitHub top language PyPI - License PyPI PyPI - Status PyPI - Python Version GitHub last commit CodeFactor Grade Documentation Status

Instalation

pip install figurestream

Bare minimum

By default, the stream serves on http://localhost:5000

# FigureStream replace any Figure object 
from figurestream import FigureStream

import numpy as np
from datetime import datetime

# FigureStream can be used like any Figure object
stream = FigureStream()
sub = stream.add_subplot(111)
x = np.linspace(0, 3, 1000)

# Update animation loop
while True:
    sub.clear()  # clear the canvas

    # ------------------------------------------------------------------------
    # Any plot operation 
    sub.set_title('FigureStream')
    sub.set_xlabel('Time [s]')
    sub.set_ylabel('Amplitude')
    sub.plot(x, np.sin(2 * np.pi * 2 * (x + datetime.now().timestamp())))
    sub.plot(x, np.sin(2 * np.pi * 0.5 * (x + datetime.now().timestamp())))
    # ------------------------------------------------------------------------
    
    stream.feed()  # push the frame into the server

For fast updates is recommended to use set_data, set_ydata and set_xdata instead of clear and draw again in each loop, also FigureStream can be implemented from a custom class.

# FigureStream replace any Figure object
from figurestream import FigureStream

import numpy as np
from datetime import datetime


class FastAnimation(FigureStream):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        axis = self.add_subplot(111)
        self.x = np.linspace(0, 3, 1000)
        
        # ------------------------------------------------------------------------
        # Single time plot configuration
        axis.set_title('FigureStream')
        axis.set_xlabel('Time [s]')
        axis.set_ylabel('Amplitude')

        axis.set_ylim(-1.2, 1.2)
        axis.set_xlim(0, 3)
        
        # Lines objects
        self.line1, *_ = axis.plot(self.x, np.zeros(self.x.size))
        self.line2, *_ = axis.plot(self.x, np.zeros(self.x.size))
        # ------------------------------------------------------------------------

        self.anim()

    def anim(self):
        # Update animation loop
        while True:
            # ------------------------------------------------------------------------
            # Update only the data values is faster than update all the plot
            self.line1.set_ydata(np.sin(2 * np.pi * 2 * (self.x + datetime.now().timestamp())))
            self.line2.set_ydata(np.sin(2 * np.pi * 0.5 * (self.x + datetime.now().timestamp())))
            # ------------------------------------------------------------------------
            
            self.feed()  # push the frame into the server


if __name__ == '__main__':
    FastAnimation()

Set host, port and endpoint

If we want to serve the stream in a different place we can use the parameters host, port and endpoint, for example:

FigureStream(host='0.0.0.0', port='5500', endpoint='figure.jpeg')

Now the stream will serve on http://localhost:5500/figure.jpeg and due the 0.0.0.0 host is accesible for any device on network.
By default host is localhost, port is 5000 and endopoint is empty.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

figurestream-1.2.8.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

figurestream-1.2.8-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file figurestream-1.2.8.tar.gz.

File metadata

  • Download URL: figurestream-1.2.8.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for figurestream-1.2.8.tar.gz
Algorithm Hash digest
SHA256 c9c358b7d1954cc9da4ac073ebec520f93bce9e3ff7e0b418d2fb50ada562c31
MD5 3a210a4bb67752a98a746abd6fcc3cc6
BLAKE2b-256 0ba38f901392d9d918f05765e87986fea56353d53e08b2c1d5a1c92cc98e811f

See more details on using hashes here.

File details

Details for the file figurestream-1.2.8-py3-none-any.whl.

File metadata

File hashes

Hashes for figurestream-1.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 eb31838039aec45feb5c7919908092baad1a0b92034d2f8eb54b3653a3dfcf68
MD5 43a9c3f74a24763cb22ed3bb5e47d68e
BLAKE2b-256 fb9ab342fed54825615302518aa2ac1865a2a7bba59cc864b3cd63a385680165

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page