如何获取 Matplotlib 补丁的坐标并使用它来添加新轴?
How can I get the coordinates of a Matplotlib patch and use it to add a new axis?
我使用 fig = plt.figure()
和 ax = fig.add_subplot(my_arguments)
创建了一个图形和坐标轴。然后我使用 matplotlib.patches
添加了一些补丁。我通过使用 matplotlib.transforms.Affine2D()
在数据坐标中平移和旋转来转换每个补丁,然后通过将 ax.transData()
添加到我的 Affine2D 的末尾来将转换后的坐标转换为显示坐标转换。
这是代码的简化版本:
import matplotlib as mpl
import matplotlib.patches as patches
from matplotlib.transforms import Bbox
fig = plt.figure()
ax = fig.add_subplot(111)
# plot anything here
ax.plot(range(10), 'ro')
my_patches = []
# in my code there many patches and therefore the line
# below is actually a list comprehension for each one
my_patches.append(
patches.Rectangle( (1, 2), 10, 20, transform=mpl.transforms.Affine2D() \
.translate(1, 1) \
.rotate_deg_around(1, 2, 35)
+ ax.transData, fill=False, color='blue')
)
# now add a new axis using the coordinates of the patch
patch = my_patches[0]
# get the coords of the lower left corner of the patch
left, bottom = patch.get_xy()
# get its width and height
width, height = patch.get_width(), patch.get_height()
# create a Bbox instance using the coords of the patch
bbox = Bbox.from_bounds(left, bottom, width, height)
# transform from data coords to display coords
disp_coords = ax.transData.transform(bbox)
# transform from display coords to figure coords
fig_coords = fig.transFigure.inverted().transform(disp_coords)
# new axis
ax2 = fig.add_axes(Bbox(fig_coords))
# plot anything else here
ax2.plot(range(10), 'bo')
但是,附加轴并未添加到图形中与补丁的变换坐标相同的位置(尽管它们很接近)。我错过了什么吗?
我不确定这段代码的用途是什么,所以这可能不是您想要的。但是为了让轴框出现在坐标 (1,2) 处,您可能应该先绘制 canvas,然后再使用从补丁中获得的坐标。
...
fig.canvas.draw()
left, bottom = patch.get_xy()
...
我使用 fig = plt.figure()
和 ax = fig.add_subplot(my_arguments)
创建了一个图形和坐标轴。然后我使用 matplotlib.patches
添加了一些补丁。我通过使用 matplotlib.transforms.Affine2D()
在数据坐标中平移和旋转来转换每个补丁,然后通过将 ax.transData()
添加到我的 Affine2D 的末尾来将转换后的坐标转换为显示坐标转换。
这是代码的简化版本:
import matplotlib as mpl
import matplotlib.patches as patches
from matplotlib.transforms import Bbox
fig = plt.figure()
ax = fig.add_subplot(111)
# plot anything here
ax.plot(range(10), 'ro')
my_patches = []
# in my code there many patches and therefore the line
# below is actually a list comprehension for each one
my_patches.append(
patches.Rectangle( (1, 2), 10, 20, transform=mpl.transforms.Affine2D() \
.translate(1, 1) \
.rotate_deg_around(1, 2, 35)
+ ax.transData, fill=False, color='blue')
)
# now add a new axis using the coordinates of the patch
patch = my_patches[0]
# get the coords of the lower left corner of the patch
left, bottom = patch.get_xy()
# get its width and height
width, height = patch.get_width(), patch.get_height()
# create a Bbox instance using the coords of the patch
bbox = Bbox.from_bounds(left, bottom, width, height)
# transform from data coords to display coords
disp_coords = ax.transData.transform(bbox)
# transform from display coords to figure coords
fig_coords = fig.transFigure.inverted().transform(disp_coords)
# new axis
ax2 = fig.add_axes(Bbox(fig_coords))
# plot anything else here
ax2.plot(range(10), 'bo')
但是,附加轴并未添加到图形中与补丁的变换坐标相同的位置(尽管它们很接近)。我错过了什么吗?
我不确定这段代码的用途是什么,所以这可能不是您想要的。但是为了让轴框出现在坐标 (1,2) 处,您可能应该先绘制 canvas,然后再使用从补丁中获得的坐标。
...
fig.canvas.draw()
left, bottom = patch.get_xy()
...