Filling between two polar curves with matplotlib fill_between

I have a feeling that I will pretend to be forehead on this, but I'm trying to fill in the general interior of the two polar functions r = 4 sin(2θ)and r = 2. I seem to get the opposite of what I want. Any ideas?

import numpy as np
import matplotlib.pyplot as plt

theta = np.arange(0, 2, 1./180)*np.pi
r = abs(4*np.sin(2*theta))
r2 = 2 + 0*theta

plt.polar(theta, r, lw=3)
plt.polar(theta, r2, lw=3)
plt.fill_between(theta, r, r2, alpha=0.2)
plt.show()

Polar plot

+6
source share
1 answer

Perhaps calculate mininum from r and r2, and then fill in between 0 and this minimum:

import numpy as np
import matplotlib.pyplot as plt

theta = np.arange(0, 2, 1./180)*np.pi
r = abs(4*np.sin(2*theta))
r2 = 2 + 0*theta
r3 = np.minimum(r, r2)
plt.polar(theta, r, lw=3)
plt.polar(theta, r2, lw=3)
plt.fill_between(theta, 0, r3, alpha=0.2)
plt.show()

enter image description here

+5
source

All Articles