Last active
February 21, 2025 21:20
-
-
Save SebastianBitsch/31702bfdd0b5c13ef150aed781912cdd to your computer and use it in GitHub Desktop.
Python terminal time-series plots
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
-- Use example in ROS2 terminal: | |
self.get_logger().info(f"X: {plot_bipolar(self.kf.x[3])}, Y: {plot_bipolar(self.kf.x[4])}, Z: {plot_bipolar(self.kf.x[5])}") | |
-- Output: | |
[INFO] [1740172663.636000550]: X: |..........|@.........|, Y: |..........|..........|, Z: |..........|..........| | |
[INFO] [1740172663.685122760]: X: |..........|@@........|, Y: |..........|@.........|, Z: |..........|..........| | |
[INFO] [1740172663.735189229]: X: |..........|@@........|, Y: |..........|@.........|, Z: |..........|@.........| | |
[INFO] [1740172663.785187161]: X: |..........|@.........|, Y: |..........|@@........|, Z: |..........|@@........| | |
[INFO] [1740172663.836043554]: X: |..........|..........|, Y: |..........|@@........|, Z: |..........|@@........| | |
[INFO] [1740172663.885599439]: X: |........@@|..........|, Y: |..........|@@@.......|, Z: |..........|@.........| | |
[INFO] [1740172663.936368587]: X: |.......@@@|..........|, Y: |..........|@@@.......|, Z: |..........|..........| | |
[INFO] [1740172664.036233326]: X: |.......@@@|..........|, Y: |..........|@@@.......|, Z: |.........@|..........| | |
[INFO] [1740172664.085884790]: X: |.......@@@|..........|, Y: |..........|@@@.......|, Z: |.......@@@|..........| | |
[INFO] [1740172664.135952420]: X: |.........@|..........|, Y: |..........|@@@.......|, Z: |.......@@@|..........| | |
[INFO] [1740172664.185436597]: X: |..........|..........|, Y: |..........|@@@@......|, Z: |.......@@@|..........| | |
[INFO] [1740172664.235322967]: X: |..........|@@........|, Y: |..........|@@@@......|, Z: |........@@|..........| | |
[INFO] [1740172664.284865093]: X: |..........|@@@.......|, Y: |..........|@@@@@.....|, Z: |..........|..........| | |
[INFO] [1740172664.335568914]: X: |..........|@@@@......|, Y: |..........|@@@@@@....|, Z: |..........|..........| | |
[INFO] [1740172664.384939519]: X: |..........|@@@@......|, Y: |..........|@@@@@@....|, Z: |..........|@.........| | |
[INFO] [1740172664.435073784]: X: |..........|@@@.......|, Y: |..........|@@@@@@@...|, Z: |..........|@@........| | |
[INFO] [1740172664.484868278]: X: |..........|@@........|, Y: |..........|@@@@@@@...|, Z: |..........|@@@.......| | |
[INFO] [1740172664.534943600]: X: |..........|@.........|, Y: |..........|@@@@@@@@..|, Z: |..........|@@........| | |
[INFO] [1740172664.584550877]: X: |..........|..........|, Y: |..........|@@@@@@@@..|, Z: |..........|@.........| | |
[INFO] [1740172664.635254544]: X: |..........|..........|, Y: |..........|@@@@@@@@@.|, Z: |..........|..........| | |
[INFO] [1740172664.685434040]: X: |..........|..........|, Y: |..........|@@@@@@@@@.|, Z: |.........@|..........| | |
[INFO] [1740172664.735551564]: X: |..........|..........|, Y: |..........|@@@@@@@@@.|, Z: |........@@|..........| | |
[INFO] [1740172664.786323921]: X: |..........|@@........|, Y: |..........|@@@@@@@@@.|, Z: |.......@@@|..........| | |
[INFO] [1740172664.835487208]: X: |..........|@@@@......|, Y: |..........|@@@@@@@@@@|, Z: |.......@@@|..........| | |
[INFO] [1740172664.885027330]: X: |..........|@@@@@.....|, Y: |..........|@@@@@@@@@@|, Z: |........@@|..........| | |
[INFO] [1740172664.935182178]: X: |..........|@@@@@@@...|, Y: |..........|@@@@@@@@@@|, Z: |..........|..........| | |
...etc... | |
""" | |
def plot_bipolar(val: float, max: float = 1.0, width: int = 20) -> str: | |
""" | |
Plot a number as a horizontal bar centered at zero. | |
Useful when logging fast moving time series data in a terminal | |
that would be hard to read as floating point numbers | |
Example for val = 0.4: | |
|..........|@@@@......| | |
Example for val = -0.4: | |
|......@@@@|..........| | |
""" | |
half_w = width // 2 | |
n_filled = int(min(abs(val),max) / max * half_w) | |
l = '@' * n_filled if val < 0.0 else '' | |
r = '@' * n_filled if val > 0.0 else '' | |
return f"|{l:.>{half_w}}{"|"}{r:.<{half_w}}|" | |
def plot_positive(val: float, max: float = 1.0, width: int = 20) -> str: | |
""" | |
Plot a number on a scale from 0 to max as a simple bar. | |
Useful when logging fast moving time series data in a terminal | |
that would be hard to read as floating point numbers | |
Example for val = 0.4 | |
|@@@@@@@@............| | |
""" | |
n_filled = int(min(val,max) / max * width) | |
r = '@' * n_filled | |
return f"|{r:.<{width}}|" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment