-1

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?

  • Hmmm...I tried a bunch of things and even finally when I got it running without errors by installing matplotlib 3.5.3 because it is uses methods no longer supported, it wasn't showing the annotations. Weird. This related method [here](https://stackoverflow.com/a/47168359/8508004) works though. By going to [temporary session launches by clicking here](https://mybinder.org/v2/gh/fomightez/communication_voila/master?filepath=notebooks%2Fvoila_and_matplotlib.ipynb) and putting `%matplotlib notebook`, the example code works. I found the version that 'o move the cursor on clicking' at the ... – Wayne Apr 27 '23 at 17:24
  • bottom less glitchy for responding to where I clicked on the image in the upper left. I think you could adapt that to do a lot. There is also `mplcursors` that you can demo an example of by following what I say [here](https://stackoverflow.com/a/75389314/8508004). You should be able to adapt that code, too, to show the value you want. By the way, showing the X,Y location is built into use of `%matplotlib notebook` in the classic notebooks interface and `%matplotlib ipympl` in JupyterLab. It will show X,Y in the bottom right corner below the plotting example as you move your cursor. – Wayne Apr 27 '23 at 17:30

1 Answers1

0

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