Search code examples
pythonmatplotlibchartsdata-visualizationradar-chart

Is it possible, to reverse the axis orientation on a Radar-Chart in Python?


I am trying to plot a Radar-Chart using the following code from the this source.

And my goal is, to reverse the r-Axis without having to remap my data points as my data is on a scale from 1 to 5, with 1 indicating very food and 5 very bad. (so I would loose the meaning of the scale, when reversing the datapoints)

(Which has been described here)

My first approach was to use matplotlibs inherent functionality.

So with the source being

# Draw ylabels
ax.set_rlabel_position(0)
plt.yticks([10,20,30], ["10","20","30"], color="grey", size=7)
plt.ylim(0,40)

My approach would be

# Draw ylabels
ax.set_rlabel_position(0)
plt.yticks([30,20,10], ["30","20","10"], color="grey", size=7)  # Reversed labels
plt.ylim(40,0) # Reversed axis, as described above

But the problem is, that the lower code never finishes. So i don't even know how to debug it, as i don't get any errors.

I also can't seem to reverse only the Axis labels (as with that approach it would be doable to just reverse the data and the labels)


Solution

  • Have a look at the following... hopefully there's something you can use here. The way I got this to work was plotting rmax-r instead of r. I also reversed the order of the ticks, but kept the tick labels the same.

    # Set up the data for plotting.
    N=20
    angles = 2.0*pi*np.linspace(0,1,N)
    rmin = 0
    rmax = 10
    radii = rmax*np.random.random(N)
    
    # Plot the non-reversed plot
    plt.figure()
    ax = plt.subplot(111,polar = True)
    ax.plot(angles,radii)
    ax.fill(angles, radii, 'b', alpha=0.1)
    n_labels = 5
    ticks1 = np.linspace(rmin,rmax,n_labels)
    labels = [str(t) for t in ticks1]
    plt.yticks(ticks1, labels)
    plt.ylim(rmin,rmax)
    

    enter image description here

    # Reverse the plot
    r2 = radii.max()-radii
    plt.figure()
    ax = plt.subplot(111,polar = True)
    ax.plot(angles, r2)
    ticks2 = np.linspace(rmax,rmin,n_labels)
    labels = [str(t) for t in ticks1]
    plt.yticks(ticks2, labels)
    ax.fill_between(angles,r2,rmax,color='b',alpha = 0.1)
    plt.ylim(rmin,rmax)
    

    enter image description here