matplotlib使用r和theta在极坐标上绘制图像

jv4diomz  于 2023-01-21  发布在  其他
关注(0)|答案(1)|浏览(136)
import numpy  as np
from PIL import Image

# Creating a new figure and setting up the resolution
fig = plt.figure(dpi=200)

# Change the coordinate system from scaler to polar
ax = fig.add_subplot(projection='polar')

# Generating the X and Y axis data points
r=[8,8,8,8,8,8,8,8,8]
theta = np.deg2rad(np.arange(45,406,45))
# plotting the polar coordinates on the system
ax.plot(theta,r, marker='x')

# Setting the axis limit
ax.set_ylim(0,12)

# Displaying the plot
plt.show()

上面的代码生成了下面的图像。给定r和theta,创建一个x 在每个极坐标上做标记。
我想要的是在那个x标记位置放一个图像而不是x标记。我可以使用add_axes方法插入图像,但我不能插入很多图像,因为我必须手动调整坐标。
enter image description here
我试过matplotlib的add_axes方法,但是这个方法占用了原始绘图的比例浮点数,所以我需要手动调整图像的位置。
我所期望的结果在下面的图像中描述。x标记被替换为图像。enter image description here

dly7yett

dly7yett1#

您不必 * 手动 * 调整坐标,您可以使用get_dataLine2D对象中获取坐标,然后使用add_artist显示您的图像:

import matplotlib.pyplot as plt
import numpy  as np
import matplotlib.image as image
from matplotlib.offsetbox import OffsetImage, AnnotationBbox

# Creating a new figure and setting up the resolution
fig = plt.figure(dpi=200)

# Change the coordinate system from scaler to polar
ax = fig.add_subplot(projection='polar')

# Generating the X and Y axis data points
r=[8,8,8,8,8,8,8,8,8]
theta = np.deg2rad(np.arange(45,406,45))

# plotting the polar coordinates on the system
lines = ax.plot(theta,r, marker='x') # storing as 'lines'

im = image.imread("your_image.png")
imagebox = OffsetImage(im, zoom = 0.01) # adjust the zoom factor accordingly

data = lines[0].get_data()
for t, r in zip(data[0], data[1]):
    ax.add_artist(AnnotationBbox(imagebox, (t, r), frameon = False))

# Setting the axis limit
ax.set_ylim(0,12)

# Displaying the plot
plt.show()

相关问题