在matplotlib中绘制作为极投影散射图背景的图像

2024-05-08 23:35:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编程一个飞镖机器人。我现在要用matlib把它可视化。在

我所拥有的: 显示机器人用一个带轴和东西的常规极性投影撞击板的位置。在

我需要的是: 设置一个飞镖作为背景,并在顶部绘制X&Y(或θ&r)。在

我尝试了: 将我的代码与公认的答案连接在一起:Plot polar plot on top of image?

这就是我现在的代码:

import numpy as np
from matplotlib import pyplot as plt

# throw three darts:
rad = np.asarray([10000, 11000, 9400]) # consider these as distances from bull's eye in 10*µm
azi = np.asarray([352, 0, 10]) # in degrees
azi = np.deg2rad(azi) # conversion into radians

fig = plt.gcf()

axes_coords = [0, 0, 1, 1] # plotting full width and height

ax_polar = fig.add_axes(axes_coords, projection='polar')
ax_polar.patch.set_alpha(0)
ax_polar.scatter(azi, rad)
ax_polar.set_ylim(0, 17000)
ax_polar.set_xlim(0, 2*np.pi)
ax_polar.set_theta_offset(0.5*np.pi) # 0° should be on top, not right
ax_polar.set_theta_direction(direction=-1) # clockwise

plt.show()

上面的代码可以正常工作。如果你运行它,你会看到机器人击中了靠近T20的领域,这是位于牛眼正上方。在

现在如果我想添加图像,我在plt.show()之前插入以下代码

^{pr2}$

图片如下: Dartboard

但结果并不令人满意: enter image description here

我做错什么了?在


Tags: 代码fromimportontopasnp机器人
1条回答
网友
1楼 · 发布于 2024-05-08 23:35:53

运行问题中显示的代码时,应该会收到警告

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

解决方案已经在警告中给出,即给轴一个唯一的标签,例如

ax_polar = fig.add_axes(axes_coords, projection='polar', label="ax polar")
# ...
ax_image = fig.add_axes(axes_coords, label="ax image")

由此产生的飞镖板应该看起来像

enter image description here

相关问题 更多 >