Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put text on polar chart using matplotlib?

This is a demo from the document of matplotlib Scatter plot on polar axis

import numpy as np
import matplotlib.pyplot as plt


# Fixing random state for reproducibility
np.random.seed(19680801)

# Compute areas and colors
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

enter image description here

Now I want to replace these dots with some texts, likeenter image description here

What modification should I do to these code?

Further more, I also want to put picture instead of texts, is that possible?

Thanks!!! enter image description here

like image 332
zjnyly Avatar asked Feb 01 '26 09:02

zjnyly


1 Answers

This is the original code:

import numpy as np
import matplotlib.pyplot as plt


# Fixing random state for reproducibility
np.random.seed(19680801)

# Compute areas and colors
N = 150
r = 2 * np.random.rand(N)
theta = 2 * np.pi * np.random.rand(N)
area = 200 * r**2
colors = theta

fig = plt.figure()
ax = fig.add_subplot(projection='polar')
c = ax.scatter(theta, r, c=colors, s=area, cmap='hsv', alpha=0.75)

If you add this two lines:

plt.text(0.67, 0.9, 'I am cartesian coordinate', transform=plt.gcf().transFigure)
plt.text(np.pi, r[len(r)-1], 'I am polar coordinate')

You will get

enter image description here

and if you add this code:

im = Image.open('smurf.png')
newax = fig.add_axes([0.5, 0.5, 0.2, 0.2], zorder=1)
newax.imshow(im)
newax.axis('off')
newax = fig.add_axes([0.3, 0.3, 0.2, 0.2], zorder=1)
newax.imshow(im)
newax.axis('off')

You will get

enter image description here

But it requires conversion calculation to get to polar coordinate

like image 118
Karina Avatar answered Feb 03 '26 23:02

Karina



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!