Search code examples
pythonmatplotlibpolar-coordinates

How do you reverse the axis and set the zero position for a polar plot in Matplotlib?


When using Matplotlib's polar plot, the default zero position for the theta axis is or the right with the angle increasing counter-clockwise, as shown in this example.

enter image description here

How do you specify the zero position and reverse the direction of increasing theta? As of writing this, the documentation is relatively limited.


Solution

  • The axis methods set_theta_zero_location and set_theta_direction allow you to specify the zero location and the direction of increasing theta, respectively. Here is a modified version of the Matplotlib example.

    import numpy as np
    import matplotlib.pyplot as plt
    
    r = np.arange(0, 2, 0.01)
    theta = 2 * np.pi * r
    
    ax = plt.subplot(111, projection='polar')
    ax.plot(theta, r)
    ax.set_rmax(2)
    ax.set_rticks([0.5, 1, 1.5, 2])  # less radial ticks
    ax.set_rlabel_position(-22.5)  # get radial labels away from plotted line
    ax.grid(True)
    
    # ---- mod here ---- #
    ax.set_theta_zero_location("N")  # theta=0 at the top
    ax.set_theta_direction(-1)  # theta increasing clockwise
    # ---- mod here ---- #
    
    ax.set_title("A line plot on a polar axis", va='bottom')
    plt.show()
    

    enter image description here

    Note that for a quiverplot, these methods currently produce strange results (see GitHub issue).