Reputation: 7536
Given a figure like this:
import matplotlib.pyplot as plt
%matplotlib inline
f, ax = plt.subplots(1,2)
plt.show()
Is it possible to display (via a grid, preferably, or spines) the display coordinates of a figure (not the axis or data coordinates)?
Thanks in advance!
Upvotes: 0
Views: 3950
Reputation: 13465
The following code is a snippet from the event handling chapter of matplotlib documentation. Notice that every time you click on the plot there are several values that are printed, including the event coordinates (display coordinates) and plot coordinates.
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(np.random.rand(10))
def onclick(event):
print( 'button=%d, x=%d, y=%d, xdata=%f, ydata=%f'%(
event.button, event.x, event.y, event.xdata, event.ydata))
cid = fig.canvas.mpl_connect('button_press_event', onclick)
plt.show()
a quick test results in:
button=1, x=127, y=98, xdata=0.852823, ydata=0.218149
button=1, x=198, y=228, xdata=2.141129, ydata=0.524111
button=1, x=116, y=382, xdata=0.653226, ydata=0.886559
button=1, x=209, y=371, xdata=2.340726, ydata=0.860669
button=1, x=230, y=257, xdata=2.721774, ydata=0.592364
EDIT: You've mentioned a plt.text
which means you might want to actually display the coordinates on screen. For this you might want to build an mpl widget or just create instances of plt.text
on the click event (haven't tested it).
Upvotes: 2