简体   繁体   中英

How to plot in figure coordinates in matplotlib?

I want to retrieve the figure coordinates of one scatter point and set a second point exactly at that point - solely based on figure coordinates and not on axes or data coordinates.

My approach, to plot the red cross (last line) on top of the blue point, does not work yet:

import matplotlib.pyplot as plt
fig, ax0 = plt.subplots()

point0 = ax0.scatter(1, 1)
ax0.set_xlim(0, 2)
ax0.set_ylim(0, 2)

bbox = ax0.get_position()
ax1 = fig.add_axes(bbox)

ax1.set_xlim(0, 2)
ax1.set_ylim(0, 2)
ax1.axis('off')

point1 = ax1.scatter(1, 0.5, marker='x')

x, y = ax0.transData.transform((1, 1))
print('x={}'.format(x), 'y={}'.format(y))
ax1.scatter(x, y, marker='x', color='r', transform=fig.transFigure)

prints: x=221.4 y=144.72

坐标

I would be thankful for any hints how to solve this.

It turns out that transformation operations solve this problem:

import matplotlib.pyplot as plt
fig, ax0 = plt.subplots()

point0 = ax0.scatter(1, 1)
bbox = ax0.get_position()
ax0.set_xlim(0, 2)
ax0.set_ylim(0, 2)
mytrans = ax0.transData + ax0.transAxes.inverted()
x, y = mytrans.transform((1, 1))

ax1 = fig.add_axes(bbox)
ax1.axis('off')
ax1.set_xlim(0, 2)
ax1.set_ylim(0, 2)

print('x={}'.format(x), 'y={}'.format(y))
ax1.scatter(x, y, marker='x', color='r', transform=ax1.transAxes)

坐标已解决

The point needs to be transformed from data to axes coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM