Skip to content

Plotting live data with Plotjuggler

Containerization is crucial for a reliable CI/CD workflow, but it can be challenging when you need live data visualization, especially for things like tuning motion control.

The good news? There's a simple solution. Just send your data to Plotjuggler using a UDP socket. I've included a code example below to show you how it's done. It's straightforward yet still packs in all the features you need.

Need to analyze data quickly? Just use the UDP_Client from the example. With just a few lines of code, you're all set.

Happy coding!

Plotjuggler

#!/usr/bin/env python3
"""
  plot data with plotjuggler
"""
import time
import socket
import orjson
import logging


class UDP_Client:
    """send data to UDP server, used for plotting with plotjuggler"""

    def __init__(self, host: str = "127.0.0.1", port: int = 5005):
        self._host = host
        self._port = port
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        self._log = logging.getLogger(self.__class__.__name__)

    def send(self, data: dict):
        """send data to UDP server"""
        try:
            data["ts"] = round(time.time(), 3)
            self._sock.sendto(orjson.dumps(data), (self._host, self._port))
        except Exception as e:  # pylint: disable=broad-except
            self._log.error(f"Failed to send data: {e}")

    def close(self):
        """close socket"""
        self._sock.close()

    def __del__(self):
        self.close()