Search code examples
matplotlibradar-chartaxvline

how to add a xvline in every polar axis in matplot


I am trying to add a line to mark quantiles for every variable in a polar chart. When I try axvline it only draws in the first axis

axes1 = plt.gca(projection='polar')
axes1.axvline(x=0, ymin=0.2,ymax=0.6, color='k',lw=3,alpha=0.5)

only first axis is drawn

I want to add a different mark for every axis but I don't know how to iterate over the 5 axes of the example.


Solution

  • You can divide the circle (2 π) into 5 to obtain an x-coordinate for each of the 5 directions.

    import matplotlib.pyplot as plt
    import numpy as np
    
    axes1 = plt.gca(projection='polar')
    
    xs = np.linspace(0, 2 * np.pi, 5, endpoint=False) + np.pi / 2
    for x in xs:
        axes1.axvline(x=x, ymin=0.2, ymax=0.6, color='r', lw=3, alpha=0.5)
    
    for ls in [':', '-']:
        y = np.random.uniform(0.3, 1, 5)
        plt.plot(np.concatenate([xs, xs[:1]]), np.concatenate([y, y[:1]]), color='g', ls=ls)
        plt.fill_between(np.concatenate([xs, xs[:1]]), np.concatenate([y, y[:1]]), color='g', alpha=0.2)
    
    plt.xticks(xs % (2 * np.pi))
    plt.show()
    

    example plot