Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill Between Two Polar Curves with matplotlib fill_between

I have a feeling I'm going to be smacking my forehead on this one, but I'm trying to fill the common interior of two polar functions r = 4 sin(2θ) and r = 2. It seems I'm getting 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

like image 654
Brian W. Avatar asked Dec 01 '14 13:12

Brian W.


People also ask

How do I fill two curves in Matplotlib?

You can easily fill in the area between values in a Matplotlib plot by using following functions: fill_between(): Fill the area between two horizontal curves.

How do I fill between in Matplotlib?

fill_between() is used to fill area between two horizontal curves. Two points (x, y1) and (x, y2) define the curves. this creates one or more polygons describing the filled areas. The 'where' parameter can be used to selectively fill some areas.

How do you fill two lines in python?

With the use of the fill_between() function in the Matplotlib library in Python, we can easily fill the color between any multiple lines or any two horizontal curves on a 2D plane.


1 Answers

Perhaps compute the mininum of r and r2, and then fill between 0 and that 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

like image 71
unutbu Avatar answered Oct 04 '22 20:10

unutbu