0

How can I change the values dislayed in the top right corner of matplot figure? By default it is showing coordinates of the current cursor position but I'd prefer it to show the value of displayed data for current x cursor's coordinate. I marked these values in the attached picure. diagram

EDIT: here's a simple code. Pls tell me how to solve described above problem for this example:

import numpy as np
from matplotlib import pyplot as plt

x = np.sin(np.arange(0,100,0.1))

fig, ax = plt.subplots()
ax = plt.plot(x)
plt.show()
General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • Please provide enough code so others can better understand or reproduce the problem. – Community Apr 14 '22 at 21:40
  • Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to create a minimal, complete, and reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), then edit your question accordingly. I understand you want to display y=f(x) instead of the x-y coordinates. However, we do not know how you store this information. Is this a function, a pandas dataframe? Without this information, nobody can answer your question. – Mr. T Apr 15 '22 at 11:14
  • I added the code. – Marcel Drąg Apr 15 '22 at 21:04

1 Answers1

1

You can define the format of these coordinates in the NavigationToolbar using format_coord:

import numpy as np
from matplotlib import pyplot as plt

def f(x):
    return np.sin(x)

x = np.arange(0, 100, 0.1)
y = f(x)

fig, ax = plt.subplots()
ax.plot(x, y)
#this can be defined for each axis object either using a def function
#or in simple cases a lambda function
ax.format_coord = lambda x, y: f"x: {x:.2f}, f(x): {f(x):.4f}"
plt.show()

enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • It's Python's f-string formatting. Specifically, it [defines the number of decimal places](https://stackoverflow.com/q/45310254/8881141) - two for the x-value and four for the calculated f(x) value. – Mr. T Apr 16 '22 at 12:41
  • Yeah, sorry - I was misleaded because this video is cutting last decimal of y. – Marcel Drąg Apr 16 '22 at 12:43
  • I have uploaded a better GIF. – Mr. T Apr 16 '22 at 12:48
  • Ok, maybe the example that I provided wasn't too good - I'm actually displaying a audio signal which I read as a list. – Marcel Drąg Apr 16 '22 at 12:59
  • That's why I asked you to provide a representative example that reflects your situation. To retrieve array values, your strategy would differ. I suggest preparing an example that covers your use case (not many data points are needed) and ask a new question. An alternative would probably be to use [event handling in matplotlib](https://matplotlib.org/stable/gallery/index.html#widgets) to display/retrieve the signal values. – Mr. T Apr 16 '22 at 13:02
  • I edited the code example. – Marcel Drąg Apr 16 '22 at 13:30
  • Do not change the question - this renders this answer nonsensical. Ask instead a new question. You can link here in a comment to the new question - the new case is only slightly more complicated because you have to check the x-values. – Mr. T Apr 16 '22 at 14:15
  • I created a new question - link is down below. Once you comment on the new question i'll delete this one to avoid missleading other people. https://stackoverflow.com/questions/71894678/changing-values-displayed-in-top-right-corner-of-matplotlib-figure – Marcel Drąg Apr 16 '22 at 14:25
  • Why would you delete this question? It might be of benefit to someone who has a defined function. – Mr. T Apr 16 '22 at 14:32
  • Sorry, I'm such a newbie to stackoverflow xd – Marcel Drąg Apr 16 '22 at 14:39