-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flickering on Live Plotting with Rich #74
Comments
Hi @ThSGM, thanks a lot for dropping your feedback! :-) I have modified your code and it seem to work fine on my machine. I changed colouring, marker and layout but you could tweek it to suite your preferences. Here it is: from rich.layout import Layout
from rich.live import Live
from rich.ansi import AnsiDecoder
from rich.console import RenderGroup
from rich.jupyter import JupyterMixin
from rich.panel import Panel
from rich.text import Text
import plotext as plt
def make_plot(width, height, phase = 0, title = None):
plt.clf()
l, frames = 1000, 30
x = range(1, l + 1)
y = plt.sin(1, periods = 2, length = l, phase = 2 * phase / frames)
plt.scatter(x, y, marker = "fhd")
plt.plotsize(width, height)
plt.xaxis(0, "upper")
plt.yaxis(0, "right")
plt.title(title)
#color = 255
#plt.canvas_color(color)
#plt.axes_color(color)
#plt.cls()
return plt.build()
class plotextMixin(JupyterMixin):
def __init__(self, phase = 0, title = None):
self.decoder = AnsiDecoder()
self.phase = phase
self.title = title
def __rich_console__(self, console, options):
self.width = options.max_width or console.width
self.height = options.height or console.height
canvas = make_plot(self.width, self.height, self.phase, self.title)
self.rich_canvas = RenderGroup(*self.decoder.decode(canvas))
yield self.rich_canvas
def make_layout():
layout = Layout(name = "root")
layout.split(Layout(name = "header", size = 3), Layout(name="main", ratio = 1))
layout["main"].split_column(
Layout(name = "static", ratio = 1),
Layout(name = "dynamic"))
return layout
layout = make_layout()
header = layout['header']
title = plt.colorize("Pl✺ ", "cyan bold") + plt.colorize("text ", "dim bold") + "integration with " + plt.colorize("rich_", "bold dim")
spaces = (plt.terminal_size()[0] - len(plt.uncolorize(title))) // 2
header.update(Text("\n" + " " * spaces + title))
static = layout["static"]
dynamic = layout["dynamic"]
mixin_static = plotextMixin(title = "Static Plot")
mixin_static_panel = Panel(mixin_static)
static.update(mixin_static_panel)
mixin_dynamic = plotextMixin(title = "Dynamic Plot")
mixin_dynamic_panel = Panel(mixin_dynamic)
with Live(layout, refresh_per_second = 0.0001) as live:
while True:
mixin_dynamic.phase += 1
dynamic.update(mixin_dynamic_panel)
live.refresh() which outputs a static and dynamic plot: What do you think? Does it work for you as well? Feel free to drop your version if you find a better one :-) All the best, P.S. Apologies I edited your post by mistake: I have restored later. |
Hi! I'm running a few tests, but the flickering is different when using different terminals on my macOS (notably Alacritty and iTerm). I'll run it on my Raspberry Pi computer to see if it persists there as well. In the mean, I'm wondering if you could explain a few elements of your code:
|
Hi @ThSGM ,
with Live(layout, refresh_per_second = 0.0001) as live:
while True:
mixin_dynamic.phase += 1
dynamic.update(mixin_dynamic_panel)
plt.sleep(0.01) # tweek this time to remove flickering
live.refresh()
Did it work? Also is there any specific reason you would use import plotext as plt
l, frames = 1000, 30
x = range(1, l + 1)
def make_data(phase = 0):
return plt.sin(1, periods = 2, length = l, phase = 2 * phase / frames)
plt.clf()
plt.subplots(2, 1)
plt.subplot(1, 1)
y = make_data()
plt.scatter(x, y, marker = "fhd")
plt.title("static plot")
plt.subplot(2, 1)
for f in range(100 * frames):
plt.cld()
plt.clt()
y = make_data(f)
plt.scatter(x, y, marker = "fhd")
plt.title("dynamic plot")
#plt.sleep(0.01) # tweek this time to remove fleekering
plt.show() which seems a little more responsive, and gives this similar result: All the best |
(Re-sending on the appropriate account; this response predates this one) Hello! Thank you for all your help. I'll do a quick reply for now as I've been busy with work. Basically, your alternative code did not seem to help. I tested it on both macOS iTerm2 and Alacritty and it showed significant flickering. The delay is due to the fact that I wanted to test on a Raspberry Pi/Debian system to see if it was due to the terminal. I have uploaded a video to show here: https://streamable.com/tl3ktr But then, even if it does work on Linux/Debian, I think the flickering that persists on iTerm2/Alacritty is still a problem. |
Hi @ThSGM, I close the issue for lack of updates. Feel free to reopen it. My last message could help you btw. |
I tried to modify the example code that was raised in this issue that managed to get plotext working with Rich. This is a fairly minimal script that tries to plot a moving sine curve.
The problem is that it shows a lot of flickering.
I am very new to coding with Python so I'm sure there are things that I'm doing that are not right. Possible issues might be around the
def make_plot
section, or the section when I initialise the plots, or the final Live section.The text was updated successfully, but these errors were encountered: