Search code examples
pythonmatplotlibpolar-coordinates

How to fill between 3 lines in a polar plot?


I plot a circle and two straight lines in polar plot and I would like to fill between them (a quarter of circle). But I don't know how.

enter image description here

import numpy as np
from matplotlib import pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(111, polar = True)

theta1 = np.linspace(0, 2*np.pi, 100)
r1 = theta*0 + 1
ax.plot(theta1, r1, color='b')

theta2 = np.array([np.pi/2]*100)
r2 = np.linspace(0, 1, 100)
ax.plot(theta2, r2, color='b')

theta3 = np.array([0]*100)
r3 = np.linspace(0, 1, 100)
ax.plot(theta3, r3, color='b')
plt.show()

Solution

  • You need to modify your code a bit to include the region you want to plot, the n use the fill_between method. In the specific case of the first quadrant, we have to fill between 0-90 degrees and 0-1 radius. The code:

    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.add_subplot(111, polar=True)
    
    theta1 = np.linspace(0, 2 * np.pi, 100)
    r1 = theta1 * 0 + 1
    ax.plot(theta1, r1, color='b')
    
    theta2 = np.array([np.pi / 2] * 100)
    r2 = np.linspace(0, 1, 100)
    ax.plot(theta2, r2, color='b')
    
    theta3 = np.array([0] * 100)
    r3 = np.linspace(0, 1, 100)
    ax.plot(theta3, r3, color='b')
    
    theta4 = np.linspace(0, np.pi / 2, 100)
    ax.fill_between(theta4, 0, 1)
    
    plt.show()
    

    Will plot: enter image description here