marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
7.76k stars 273 forks source link

Plot blinking: matplotlib interactive #260

Open proItheus opened 1 year ago

proItheus commented 1 year ago

Describe the bug

I suspect that's very much the same as #251, but maybe not for mpl.interactive?

Besides blinking, mpl.interactive will also use a new port every time I drag the slider, and stop responding after a few changes.

Screenshot_20231026_154844

Version

0.1.38

Code to reproduce

import marimo

__generated_with = "0.1.38"
app = marimo.App()

@app.cell
def __(exp):
    exp
    return

@app.cell
def __(altp, exp, mo, mplplot, plotlyplot, snsplot):
    mo.vstack(
        [
            mo.md(f"$$y=x^{exp.value}$$"),
            mo.tabs(
                {
                    "altair": mo.vstack([altp, altp.value]),
                    "plotly": plotlyplot(exp.value),
                    "seaborn": snsplot(exp.value),
                    "matplotlib": mplplot(exp.value),
                }
            ),
        ]
    )
    return

@app.cell
def __():
    import marimo as mo
    from plotly import express as px
    import altair as alt
    import numpy as np
    import pandas as pd
    import seaborn as sns
    from matplotlib import pyplot as plt
    from functools import cache
    return alt, cache, mo, np, pd, plt, px, sns

@app.cell
def __(mo):
    exp = mo.ui.slider(1, 8, 1, label="exp: ")
    return exp,

@app.cell
def __(cache, np):
    @cache
    def prep_xy(exp):
        x = np.linspace(0, 5, 100)
        return x, x**exp
    return prep_xy,

@app.cell
def __(alt, cache, mo, pd, prep_xy):
    @cache
    def altplot(exp):
        x, y = prep_xy(exp)
        data = pd.DataFrame({"x": x, "y": y})
        return mo.ui.altair_chart(
            alt.Chart(data).mark_line().encode(x="x", y="y"),
            chart_selection="interval",
        )
    return altplot,

@app.cell
def __(cache, prep_xy, px):
    @cache
    def plotlyplot(exp):
        x, y = prep_xy(exp)
        return px.line(x=x, y=y)
    return plotlyplot,

@app.cell
def __(cache, prep_xy, sns):
    @cache
    def snsplot(exp):
        x, y = prep_xy(exp)
        return sns.lineplot(x=x, y=y)
    return snsplot,

@app.cell
def __(cache, mo, plt, prep_xy):
    @cache
    def mplplot(exp):
        x, y = prep_xy(exp)
        plt.plot(x, y)
        return mo.mpl.interactive(plt.gcf())
    return mplplot,

@app.cell
def __(altplot, exp):
    altp = altplot(exp.value)
    return altp,

if __name__ == "__main__":
    app.run()
akshayka commented 1 year ago

Yes the plot blinking is the same as #251. We fixed it for mo.ui.altair_chart but not for vanilla altair or plotly. Thanks for opening this issue to track.

Besides blinking, mpl.interactive will also use a new port every time I drag the slider, and stop responding after a few changes.

Oops! Will look into it. Thanks for the repro.

JVSigaud commented 1 year ago

from the code above, only seaborn is working

akshayka commented 1 year ago

from the code above, only seaborn is working

@JVSigaud , what do you mean? Do none of the other plots show up?

The examples work for me. What browser / browser version are you using?

plotting-example.webm

JVSigaud commented 1 year ago

I'm using the chrome version 118.0.5993.118. None of the other plots show up. I also tried in the vscode's extension and did not work either. Im using marimo version 0.1.39.

akshayka commented 1 year ago

Thanks @JVSigaud . I opened #273, let's move the discussion there