Matplotlib-FigureStream
A backend for serve Matplotlib animations as web streams.
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.
Release files for figurestream 1.2.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| figurestream-1.2.8.tar.gz | 6.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| figurestream-1.2.8-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.6 kB
Release files / figurestream-1.2.8.tar.gz
| Download URL | figurestream-1.2.8.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c9c358b7d1954cc9da4ac073ebec520f93bce9e3ff7e0b418d2fb50ada562c31
|
|
BLAKE2b-256 checksum How to use checksums |
0ba38f901392d9d918f05765e87986fea56353d53e08b2c1d5a1c92cc98e811f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|
Release files / figurestream-1.2.8-py3-none-any.whl
| Download URL | figurestream-1.2.8-py3-none-any.whl |
|---|---|
| Size | 6.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
eb31838039aec45feb5c7919908092baad1a0b92034d2f8eb54b3653a3dfcf68
|
|
BLAKE2b-256 checksum How to use checksums |
fb9ab342fed54825615302518aa2ac1865a2a7bba59cc864b3cd63a385680165
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.3
|