在 matplotlib 中绘制图像作为极投影散点图的背景
Plotting an image as background of a polar projection scatterplot in matplotlib
我正在编写一个飞镖机器人。它是计算机器人击中棋盘的位置,现在我想使用 matplotlib 将其可视化。
我有:
使用带轴和东西的规则极投影显示机器人撞击板的位置。
我需要的:
将飞镖盘设置为背景并在顶部绘制 X&Y(或分别为 theta 和 r)。
我尝试了什么:
将我的代码与接受的答案一起加入:
这就是我的代码现在的样子:
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()
之前插入下面的代码
pic = plt.imread('Board_cd.png')
ax_image = fig.add_axes(axes_coords)
ax_image.imshow(pic, alpha=.5)
ax_image.axis('off') # don't show the axes ticks/lines/etc. associated with the image
图片是这样的:
但结果不是很令人满意:
我做错了什么?
运行 你在问题中显示的代码你应该得到警告
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.
warning里面已经给出了解决方法,就是给坐标轴一个唯一的标签,例如
ax_polar = fig.add_axes(axes_coords, projection='polar', label="ax polar")
# ...
ax_image = fig.add_axes(axes_coords, label="ax image")
生成的飞镖板应该看起来像
我正在编写一个飞镖机器人。它是计算机器人击中棋盘的位置,现在我想使用 matplotlib 将其可视化。
我有: 使用带轴和东西的规则极投影显示机器人撞击板的位置。
我需要的: 将飞镖盘设置为背景并在顶部绘制 X&Y(或分别为 theta 和 r)。
我尝试了什么:
将我的代码与接受的答案一起加入:
这就是我的代码现在的样子:
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()
pic = plt.imread('Board_cd.png')
ax_image = fig.add_axes(axes_coords)
ax_image.imshow(pic, alpha=.5)
ax_image.axis('off') # don't show the axes ticks/lines/etc. associated with the image
图片是这样的:
但结果不是很令人满意:
我做错了什么?
运行 你在问题中显示的代码你应该得到警告
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.
warning里面已经给出了解决方法,就是给坐标轴一个唯一的标签,例如
ax_polar = fig.add_axes(axes_coords, projection='polar', label="ax polar")
# ...
ax_image = fig.add_axes(axes_coords, label="ax image")
生成的飞镖板应该看起来像