Sascha Stenger
Sascha Stenger

Reputation: 75

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)

Upvotes: 1

Views: 1526

Answers (2)

Rob
Rob

Reputation: 316

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

Upvotes: 1

C. Miranda
C. Miranda

Reputation: 75

If you want to go with the reverse labels thing, you have to use plt.yticks([10,20,30], ["30", "20", "10"], ...) since the first parameter corresponds to axes' values, and as you have not yet inverted it, they should remain in that order.

I checked on the plt.ylim inversion and it does end for me, but throws a rather cryptic error posx and posy should be finite values. Taking into account posx and posy are not parameters for this functions, there must be an underlying function that does not like this. In addition, having tested this for a non-polar plot, I guess the problem comes from the polar coordinates.

Looking around, I found both a github issue and an SO question, which resulted in a PR and posterior merge in Dec 2018. It should be available and work if you have the latest matplotlib version.

Upvotes: 0

Related Questions