Davide-sd / sympy-plot-backends

An improved plotting module for SymPy
BSD 3-Clause "New" or "Revised" License
42 stars 9 forks source link

Unstable behavior #42

Open SwaggerNiels opened 3 months ago

SwaggerNiels commented 3 months ago

Hello, I have recently discovered spb, and it is truely awesome, but I do not get stable results. Sometimes my graphs shift when using multiple sliders, and every now and then when I run the my "boilerplate" function (below) in jupyter notebook in VSCode it doesn't even show at all and throws an error (below below)

from sympy import *
from spb import *
import ipywidgets

def make_widget(domain, f, vars, xlim=None, ylim=None, show=True):
    '''Makes a ipython widget with sliders for the indicated ranges'''

    return plot(
        f, domain,
        params=vars,
        xlim=xlim,
        ylim=ylim,
        show = show,
        nb_of_points=100,
        )
File [~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\spb\plot_functions\functions_2d.py:346](https://file+.vscode-resource.vscode-cdn.net/[mypath]~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0/LocalCache/local-packages/Python311/site-packages/spb/plot_functions/functions_2d.py:346), in plot(*args, **kwargs)
...
--> [815](https://file+.vscode-resource.vscode-cdn.net/[mypath]/~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0/LocalCache/local-packages/Python311/site-packages/traitlets/traitlets.py:815)     raise TraitError(value, info or self.info(), self)
    [817](https://file+.vscode-resource.vscode-cdn.net/[mypath]~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0/LocalCache/local-packages/Python311/site-packages/traitlets/traitlets.py:817) # this is the root trait
    [818](https://file+.vscode-resource.vscode-cdn.net/[mypath]~/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0/LocalCache/local-packages/Python311/site-packages/traitlets/traitlets.py:818) if obj is not None:

TraitError: The 'children' trait of a Box instance contains an Instance of a TypedTuple which expected a Widget, not the FigureCanvasQTAgg at '0x1fc323601f0'.
Davide-sd commented 3 months ago

Hello @SwaggerNiels , thanks for your kind words.

I assume you are using Matplotlib. I think I went pretty close to replicate your error. When a parametric-interactive plot is created with ipywidgets, it expects the matplotlib figure to be an interactive one. By running %matplotlib widget at the start of the notebook, it activates an interactive frame (a widget) wrapping a matplotlib figure, which allows interactivity. For me, it solved the problem.

However, it appears to me that the VSCode doesn't load all the necessary javascript files to make the interactive experience as good as the one offered by vanilla Jupyter Lab (or Notebook) run on the browser. For example, it doesn't show Latex labels (note the $y$ on the slider):

image

This is rendered correctly on a Jupyter Lab/Notebook run on the browser:

image

I'll keep this issue open for now, I might find some time to dig deeper into it. For now, just know that the interactive capabilities of this module are fully working on Jupyter Notebook/Lab running on a browser.

SwaggerNiels commented 3 months ago

Indeed, I see that jupyter notebook gives me perfectly stable results. I will use that from now on until a solution is found.

Thank you for the fast response!