温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Changing the origin of an image lay over the polar plot
annotations matplotlib plot python

python - 更改图像原点位于极坐标图上

发布于 2020-06-04 12:54:18

我在极地图中添加了一个小徽标。

我正在使用python来做到这一点。

我使用下面的代码来做到这一点。

Logo = mpimg.imread(figpath+figname)
imagebox = OffsetImage(Logo, zoom=0.12)
ab = AnnotationBbox(imagebox, (4.7, 8))
ax1.add_artist(ab)
ax1.set_ylim(0,8)

输出如下:

在此处输入图片说明

坐标(theta,r)AnnotationBbox的标志中央启动。

我想将徽标移动到下面的红色框中所示的位置:

在此处输入图片说明

谁能告诉我该怎么做?

查看更多

提问者
Zephyr
被浏览
14
Diziet Asahi 2020-03-20 22:00

您可以使用box_alignment=AnnotationBbox更改框的参考点。例如,传递box_alignment=(1,1)使右上角成为参考xy坐标。

from matplotlib.offsetbox import OffsetImage, AnnotationBbox

r = np.arange(0, 2, 0.01)
theta = 2 * np.pi * r

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')
ax.plot(theta, r)
ax.set_rmax(2)
ax.set_rticks([0.5, 1, 1.5, 2])  # Less radial ticks
ax.set_rlabel_position(-22.5)  # Move radial labels away from plotted line
ax.grid(True)

img = matplotlib.image.imread("https://upload.wikimedia.org/wikipedia/en/7/7d/Lenna_%28test_image%29.png")
imagebox = OffsetImage(img, zoom=0.12)
ab = AnnotationBbox(imagebox, xy=(np.pi*225/180, 2), box_alignment=(1,1))
ax.add_artist(ab)

plt.show()

在此处输入图片说明

请注意,您还可以更改用于放置框的坐标系。例如,如果要将徽标放在图形的左上角,可以执行以下操作:

ab = AnnotationBbox(imagebox, xy=(0,1), xycoords='figure fraction', box_alignment=(0,1))