繁体   English   中英

matplotlib plot 使用 r 和 theta 的极坐标图像

[英]matplotlib plot images on polar coordinate using r and theta

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 方法插入图像,但是我不能插入很多图像,因为我必须手动调整坐标。

在此处输入图像描述

我试过 matplotlib 的 add_axes 方法,但该方法采用原始 plot 的比率浮点数。所以我应该手动调整图像的位置。

我期望的结果如下图所示。 x 标记被替换为图像。 在此处输入图像描述

您不必手动调整坐标。 您可以使用Line2D object 中的get_data获取它们。然后使用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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM