0

I am trying to plot data on tkinter canvas using matplotlib "imshow()" function. When I am running the code the data is getting plotted onto the canvas and in the navigation toolbar pixel coordinates (x and y coordinates) are getting displayed along with pixel values (in bracket). Issue is I want to display only the pixel coordinates and hide pixel values which is getting displayed in the navigation toolbar.

The code which I am using is:

import tkinter
import numpy as np
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

import tkinter
import numpy as np
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

root = tkinter.Tk()

fig = Figure(figsize=(5, 4), dpi=100)
fig.subplots_adjust(bottom=0, right=1, top=1, left=0, wspace=0, hspace=0)

ax = fig.add_subplot(111)

class Formatter(object):
    def __init__(self, im):
        self.im = im
    def __call__(self, x, y):       
        return 'x={:.01f}, y={:.01f}'.format(x, y)

data = np.random.random((10,10))

im = ax.imshow(data, interpolation='none')
ax.format_coord = Formatter(im)
plt.show()

canvas1 = FigureCanvasTkAgg(fig, master=root)
canvas1.draw()

toolbar = NavigationToolbar2Tk(canvas1,root)
toolbar.update()
toolbar.pack(side=tkinter.TOP, fill=tkinter.X, padx=8)

canvas1.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

canvas1._tkcanvas.pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1, padx=10, pady=5)

root.mainloop()

Kindly suggest how to hide pixel values getting displayed in the navigation toolbar (inside brackets) and display only pixel coordinates (x & y coordinates).

2 Answers 2

3

The value in the navigation toolbar is created by the images' format_cursor_data method. You can replace that method to return an empty string.

im = ax.imshow(data, interpolation='none')
im.format_cursor_data = lambda e: ""
Sign up to request clarification or add additional context in comments.

Comments

1

One way is to override the method mouse_move:

class Navigator(NavigationToolbar2Tk):
    def mouse_move(self, event):
        self._set_cursor(event)
        if event.inaxes and event.inaxes.get_navigate():
            try:
                s = event.inaxes.format_coord(event.xdata, event.ydata)
                self.set_message(s)
            except (ValueError, OverflowError):
                pass
        else:
            self.set_message(self.mode)

...

toolbar = Navigator(canvas1,root)

...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.