Craver2000
Craver2000

Reputation: 453

Shading a segment between two lines on polar axis (matplotlib)

Currently, I have a plot with lines indicating the orientation of sunrise (blue), midday (red), sunset (orange), and an object (green). I want to create a yellow shaded area between sunrise and sunset. I thought of the plt.fill method but that didnt seem to work when I added plt.fill_between(theta1, theta2, R1, alpha=0.2). I think that is because the two lines do not intersect. My current polar axis plot looks like:

enter image description here

The relevant part of the script is:

    #for object
    theta0 = np.deg2rad([190, 190])
    print ("theta0= %s" %theta0)    
    R0 = [0,0.4]

    #for sunrise
    theta1 = np.deg2rad([105, 105])
    print ("theta1= %s" %theta1)    
    R1 = [0,1]

    #for sunset
    theta2 = np.deg2rad([254, 254])
    print ("theta2= %s" %theta2)    
    R1 = [0,1]

    #for midday 
    theta3 = np.deg2rad([0.2, 0.2])
    print ("theta3= %s" %theta3)    
    R3 = [0,1]

    ax.set_theta_zero_location("S")
    ax.set_theta_direction(-1)

    ax.plot(theta1,R1, theta2, R1, theta0, R0, lw=5)

    #plt.fill_between(theta1, theta2, R1, alpha=0.2) #does not work


    plt.savefig('plot.png')

I also thought of using the azimuth of the midday and then using the width, calculated from the angle difference between sunset and sunrise and then using these to generate the shaded area. How can I go about doing that? I would like just a yellow shaded area between sunrise and sunset, hence I thought generating width from the theta of midday would be a good idea. I tried doing a duplicate of ax to form ax2 which I can then potentially use it to show the width (shaded area) of midday. But with the addition of ax2, it seem to mess up the plot. How else can I resolve this?

Upvotes: 4

Views: 3754

Answers (1)

bheklilr
bheklilr

Reputation: 54078

To use fill_between on a polar plot, you have to call it as

plt.fill_between(angles_to_go_through, minimum_radii, maximum_radii)

So for example, filling between 0 and 90 degrees would be

ax.fill_between(
    np.linspace(0, pi/2, 100),  # Go from 0 to pi/2
    0,                          # Fill from radius 0
    1,                          # To radius 1
)

This would produce a plot like

polar fill

In your case, you would call

ax.fill_between(
    np.linspace(theta1[0], theta2[0], 100),  # Need high res or you'll fill a triangle
    0,
    1,
    alpha=0.2,
    color='y',
)

Upvotes: 10

Related Questions