Skip to content
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

Interactive matplotlib plots #4173

Closed
seeM opened this issue Jul 29, 2024 · 1 comment
Closed

Interactive matplotlib plots #4173

seeM opened this issue Jul 29, 2024 · 1 comment
Assignees
Labels
area: widgets Issues related to Widgets. enhancement New feature or request lang: python theme: app builder

Comments

@seeM
Copy link
Contributor

seeM commented Jul 29, 2024

We should support matplotlib's interactive mode i.e. ipympl.

For example, this should produce an interactive plot:

%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()


x = np.linspace(0, 2*np.pi, 100)
y = np.sin(3*x)
ax.plot(x, y)

Currently it produces a static low-res plot:

image

Sub-issue of #1924.

@seeM seeM added enhancement New feature or request lang: python area: widgets Issues related to Widgets. theme: app builder labels Jul 29, 2024
@seeM seeM self-assigned this Jul 29, 2024
@seeM seeM added this to the 2024.09.0 Pre-Release milestone Jul 29, 2024
seeM added a commit that referenced this issue Aug 5, 2024
Addresses #3974 and
#4173.

This PR plumbs `buffers` from jupyter-adapter messages through to the
main thread and specifically to our IPyWidgets notebook renderer. This
unlocks a bunch of widget types (thanks to @isabelizimm for these
examples):

#### `ipywidgets.Image`

```python
import ipywidgets
with open('image.png', 'rb') as f: image = f.read()  # you'll need an actual image path
display(ipywidgets.Image(value=image, format='png'))
```

#### `ipympl` (matplotlib's interactive backend)

Note: Save doesn't work yet. We'll need to handle the `clicked-data-url`
webview message in our `NotebookOutputWebview`.

```python
%matplotlib ipympl
import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(3*x)
ax.plot(x, y)
```

#### `ipydatagrid`

```python
import pandas as pd
from ipydatagrid import DataGrid
data = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["One", "Two", "Three"])
w = DataGrid(data, selection_mode="cell", editable=True)
display(w)
# Now change the value of a cell, then run:
w.data
# It should reflect the updated value.
# The next line should update the value in the widget frontend:
w.set_cell_value("A", "One", 3)
```

#### `bqplot`

There appears to be a console warning here, we can address that in a
follow-up.

```py
import bqplot.pyplot as bplt
import numpy as np

x = np.linspace(-10, 10, 100)
y = np.sin(x)
axes_opts = {"x": {"label": "X"}, "y": {"label": "Y"}}

fig = bplt.figure(title="Line Chart")
line = bplt.plot(
    x=x, y=y, axes_options=axes_opts
)
bplt.show()
```
@testlabauto
Copy link
Contributor

Verified Fixed

Positron Version(s) : 2024.08.0-24
OS Version          : OSX

Test scenario(s)

Console run of:

this works in the console

run this first in the console:

%matplotlib ipympl

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

x = np.linspace(0, 2np.pi, 100)
y = np.sin(3
x)
ax.plot(x, y)

Link(s) to TestRail test cases run or created:
N/A

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: widgets Issues related to Widgets. enhancement New feature or request lang: python theme: app builder
Projects
None yet
Development

No branches or pull requests

2 participants