Search code examples
pythonmatplotlibplotfillpolar-coordinates

fill between two lines in polar coordinates


I'm trying to fill in the space between two lines that aren't closed at both ends. I can't get plt.fill_between with plt.polar, any ideas? Here's the code I'm using to draw the lines:

import matplotlib.pyplot as plt
import numpy as np 

inner_offset = 0.05
r = np.arange(inner_offset,1. + inner_offset,1./720.)
theta = np.arange(0.,2.,1./360.)*np.pi 

plt.polar(theta/2.,r) #first part of spiral
plt.polar((theta/2.)+(np.pi/3.),r)

plt.show()

Solution

  • Since your radius ("y") is the same, you can use plt.fill_betweenx():

    import matplotlib.pyplot as plt
    import numpy as np
    
    plt.figure()
    inner_offset = 0.05
    r = np.arange(inner_offset,1. + inner_offset,1./720.)
    theta = np.arange(0.,2.,1./360.)*np.pi
    
    c1 = plt.polar(theta/2.,r, color='b')[0]
    x1 = c1.get_xdata()
    y1 = c1.get_ydata()
    c2 = plt.polar((theta/2.)+(np.pi/3.),r, color='y')[0]
    x2 = c2.get_xdata()
    y2 = c2.get_ydata()
    
    plt.fill_betweenx(y1, x1, x2, color='g')
    plt.show()
    

    giving:

    enter image description here