jupyter / jupyter/notebook

Debugging interactive plots in a Jupyter notebook

Open
#5,271 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
13.3k
Forks
5.8k
Avg merge
6d 11h
Merged PRs (30d)
7

Description

The example below should highlight the point that you click on, and change the title of the graph to show the label associated with that point.

If I run this Python script as a script, when I click on a point I will get an error " line 15, in onpick TypeError: only integer scalar arrays can be converted to a scalar index", which is expected. event.ind is a list, and I need to change that to ind = event.ind[0] to be correct here.

However, when I run this in a Jupyter notebook, the figure appears, but the error is silently ignored, so it just appears that the code does not work. Is there a way to get Jupyter to show me that an error has occurred?

```
import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

x = [0, 1, 2, 3, 4, 5]
labels = ['a', 'b', 'c', 'd', 'e', 'f']
ax.plot(x, 'bo', picker=5)

# this is the transparent marker for the selected data point
marker, = ax.plot([0], [0], 'yo', visible=False, alpha=0.8, ms=15)

def onpick(event):
ind = event.ind
ax.set_title('Data point {0} is labeled "{1}"'.format(ind, labels[ind]))
marker.set_visible(True)
marker.set_xdata(x[ind])
marker.set_ydata(x[ind])

ax.figure.canvas.draw() # this line is critical to change the linewidth

fig.canvas.mpl_connect('pick_event', onpick)

plt.show()
```

Contributor guide

Open the contributing guide

Research direction

Start by running the provided Python example in a Jupyter notebook and compare it with running the same script directly. Trace the onpick callback and notebook handling of its TypeError; done means the notebook visibly reports the callback error instead of silently ignoring it.

Written by the indexing model from the issue text.

Assessment

Tech stack
jupyter-notebook, python
Domain
devtools, frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.