Search code examples
pythonmatplotlibpolar-coordinates

How can I reduce or otherwise influence the number of radial ticks in a polar plot?


I want a polar plot, but with a reduced number of radial (r dimension) ticks. I already tried the suggested solutions from other questions, such as

pyplot.locator_params(axis='y', nticks=6)

but it did not seem to change anything.

I tried using pyplot.gca().set_rticks([...]) but that requires to know the ticks in advance, while I am interested in just setting the maximum number of them.

What else could I try in order to reduce the number of ticks (or circles)?


Solution

  • You may indeed use ax.set_rticks() to specify the specific ticklabels you want, e.g.

    ax.set_rticks([0.5, 1, 1.5, 2])
    

    as shown in his example on the matplotlib page.

    In some cases this may be undesired and the usual locator parameters would be preferable. You can get the locator via ax.yaxis.get_major_locator().base and set the parameters via .set_params(). Here you want to change the nbins parameter,

    ax.yaxis.get_major_locator().base.set_params(nbins=3)
    

    Complete 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.yaxis.get_major_locator().base.set_params(nbins=3)
    
    ax.set_title("A line plot on a polar axis", va='bottom')
    plt.show()
    

    enter image description here