Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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 (plotext and matplotlib) #1948

Closed
ThSGM opened this issue Feb 10, 2022 · 0 comments
Closed

Flickering on live plotting with Rich (plotext and matplotlib) #1948

ThSGM opened this issue Feb 10, 2022 · 0 comments

Comments

@ThSGM
Copy link

ThSGM commented Feb 10, 2022

I posted this issue on the the plotext issue board, but was unsure if this was a Rich issue. The issue is found here.

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.

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 datetime import datetime
from rich.table import Table

from time import sleep
import plotext as plt

import numpy as np

def make_plot(width, height):
    # plt.clf()
    plt.clear_data()
    plt.scatter(yy, marker="x")
    plt.plotsize(width, height)
    plt.title(str(count))
    plt.colorless()
    return plt.build()

class plotextMixin(JupyterMixin):
    def __init__(self):
        self.decoder = AnsiDecoder()
        self.count = count
        
    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.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_row(
        Layout(name="plotext", size=100),
        Layout(name="main_right", size=30),
    )
    return layout

# Clear the plot
plt.clf()

# Initialise he count
count = 0

# Initialise the plots
xx = np.arange(0, 6*np.pi, 0.1)
yy = np.sin(xx)
uu = np.stack((xx,yy), axis=1)

layout = make_layout()
plotext_layout = layout["plotext"]
mix = plotextMixin()
mix = Panel(mix, style="white on black")
plotext_layout.update(mix)

header_layout = layout["header"]

with Live(layout, refresh_per_second=0.1) as live:
    while True:
        xx = xx + 0.1*count
        yy = np.sin(xx)
        uu = np.stack((xx,yy),axis=1)
        count = count + 1

        plotext_layout.update(mix)

        plt.sleep(0.01)
        plt.show()

@Textualize Textualize locked and limited conversation to collaborators Feb 10, 2022
@willmcgugan willmcgugan converted this issue into discussion #1949 Feb 10, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

1 participant