0

I would like the co-ords of the cursor to be a whole number instead of 5.75e+03. I have the same problem as this user (matplotlib coordinates format), I used the solution suggested to them which worked for them, but it did not work for me. This was the solution

ax.format_coord = lambda x, y: "({0:f}, ".format(y) +  "{0:f})".format(x)

Here is my plot:
enter image description here

Mr. T
  • 11,960
  • 10
  • 32
  • 54
rr30
  • 3
  • 1

1 Answers1

1

You probably want to use the format :.0f instead of just :f. The latter doesn't fix the number of decimals. By default, matplotlib automatically chooses of format depending on the available space. If you resize the window, the default format can change. But this choice doesn't seem logical in every situation.

In Python 3.6 f-strings were introduced, which simplifies the ways formatting can be written. For an extensive list of available format specifiers, see this document.

Here is a minimal example:

from matplotlib import pyplot as plt
import numpy as np

plt.imshow(np.random.rand(3, 5), extent=(0, 5000, 0, 3000))
plt.gca().format_coord = lambda x, y: f"({x:.0f}, {y:.0f})"
plt.show()

example plot

For older Python versions you can set format_coord to lambda x, y: "({0:.0f}, {1:.0f})".format(x, y)

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • Thank you for your response. I tried your suggestion but unfortunately doesn't work. Interesting what you said about the available space. I have recently changed server (all set up by a colleague as I don't know enough about that) and never had this issue on our previous set-up/server. Additionally I can no longer stretch plots, they just go blank when I try, whereas I could on my old server. I just re-plotted with a larger fig size and can now see the whole number, however I would like to try and have the whole number for all fig sizes. I'll have a look at that document. Thank you. – rr30 Feb 22 '21 at 13:18
  • By does not work I mean nothing changed and the co-ords still showed in scientific notation. Sorry for not adding this detail sooner. – rr30 Feb 22 '21 at 13:22
  • Did you try to run my example code unmodified? Note that in your image you seem to use `plt.imshow` which draws on the "current ax", while then you use some other unspecified `ax` to set the `format_coord`. You might want to add a full standalone example code to your question's post. – JohanC Feb 22 '21 at 13:28
  • Sorry, I missed that part. It is now working. Thank you so much – rr30 Feb 22 '21 at 13:47