是否有可能在 Python 中反转雷达图上的轴方向?

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

我正在尝试使用 source.

中的以下代码绘制雷达图

我的目标是,无需重新映射我的数据点即可反转 r 轴,因为我的数据在 1 到 5 的范围内,1 表示非常好,5 表示非常糟糕。 (所以当反转数据点时,我会失去比例的意义)

(已说明)

我的第一个方法是使用固有的 matplotlibs functionality

所以来源是

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

我的方法是

# 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

但问题是,较低的代码永远不会完成。所以我什至不知道如何调试它,因为我没有收到任何错误。

我似乎也不能只反转轴标签(因为采用这种方法,只反转数据和标签是可行的)

如果你想使用反向标签,你必须使用 plt.yticks([10,20,30], ["30", "20", "10"], ...) 因为第一个参数对应于轴的值,并且由于您还没有反转它,所以它们应该保持那个顺序。

我检查了 plt.ylim 反转,它对我来说确实结束了,但抛出了一个相当神秘的错误 posx and posy should be finite values。考虑到 posx 和 posy 不是这个函数的参数,一定有一个底层函数不喜欢这个。另外,在非极坐标图上测试过,我猜问题出在极坐标上。

环顾四周,我在 2018 年 12 月发现了两个 a github issue and an , which resulted in a PR and posterior merge。如果你有最新的 matplotlib 版本,它应该可用并且可以工作。

看看下面的...希望这里有您可以使用的东西。我让它工作的方法是绘制 rmax-r 而不是 r。我还颠倒了刻度的顺序,但保持刻度标签相同。

# 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)

# 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)