Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading