1

I'm displaying an image using imshow inside a tkinter GUI and I have added a NavigationToolbar2TkAgg (python3.5). Very similarly to this question (check it out for visuals), I want to change the formatting of the coordinates and z values (in particular, all my z values are between 0-10000, so I want to write them out instead of using scientific notation).

For x and y this was pretty easy to do by changing the format_coord handle, but I can't seem to find anything to change the final bit [xxx]. I have tried format_cursor_data, but doesn't seem to be it either.

Anyone know a solution?

It seems that the [z] value is only shown for imshow, not for regular plots.

Here goes a minimal code sample to reproduce the problem

import sys
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import Figure

import tkinter as tk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg

top = tk.Tk()

fig = plt.figure()
plt.imshow(np.array([[0,1],[1,2]]))

ax = fig.gca()
ax.format_coord = lambda x,y: "x:%4u, y:%4u" % (x,y)
ax.format_cursor_data = lambda z: "Hello" % (z) # does nothing

canvas = FigureCanvasTkAgg(fig,master=top)
canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
canvas.show()

toolbar = NavigationToolbar2TkAgg(canvas, top)
toolbar.update()

top.mainloop()
mattu
  • 944
  • 11
  • 24

1 Answers1

1

In the mouse_move() method of Matplotlib's NavigationToolbar2 class, the cursor data and its formatting are obtained from the topmost Artist in the Axes instance, rather than from the Axes instance itself, like for the coordinates. So what you should do is:

fig = Figure()
ax = fig.add_subplot(111)
img = np.array([[0,10000],[10000,20000]])
imgplot = ax.imshow(img, interpolation='none')

# Modify the coordinates format of the Axes instance
ax.format_coord = lambda x,y: "x:{0:>4}, y:{0:>4}".format(int(x), int(y))

# Modify the cursor data format of the Artist created by imshow()
imgplot.format_cursor_data = lambda z: "Hello: {}".format(z)

Screenshot

Josselin
  • 2,593
  • 2
  • 22
  • 35