mlpdatacursor not working in jupyter notebooks

941 Views Asked by At

When you point your cursor on an image opened in MATLAB, you can see X, Y location and value of that pixel. I wanted to see if similar functionality exists in python.

I came across https://github.com/joferkington/mpldatacursor and figured that I can get the functionality I am looking for using mlpdatacursor. However, I'm not quiet sure how to use this module in Jupyter Notebooks.

I did: pip install mlpdatcursor and it worked. Then I ran their code to test:

import matplotlib.pyplot as plt
import numpy as np
from mpldatacursor import datacursor

data = np.outer(range(10), range(1, 5))

fig, ax = plt.subplots()
lines = ax.plot(data)
ax.set_title('Click somewhere on a line')

datacursor(lines)

plt.show()

The code runs without any error, however, I don't see any displays of the x, y coordinates when I click on the lines! Any idea what do I need to do to have this run successfully in Jupyter notebooks?

1

There are 1 best solutions below

0
On

I found the solution by going through the following discussions: https://github.com/joferkington/mpldatacursor/issues/12. https://github.com/joferkington/mpldatacursor/issues/48

For me, it works when I add the following statements in the beginning:

import matplotlib
matplotlib.use('nbagg')
import matplotlib.pyplot as plt
%matplotlib nbagg