简体   繁体   中英

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()

The above code produces the image below. Given r and theta, create an x mark at each polar coordinate.

What I want is to put an image instead of the x mark at that x mark location. I can insert images using the add_axes method, but I cannot insert many images because I have to manually adjust the coordinates.

enter image description here

i've tried add_axes method of matplotlib, but the method takes ratio float of original plot. So i should manually adjust the location of the image.

result that i expect is described in below image. the x mark is replaced by an image. enter image description here

You don't have to adjust the coordinates manually . You can get them with get_data from Line2D object. Then use add_artist to display your images:

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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