是否有可能在Python中反转Radar-Chart上的轴方向?

时间:2019-04-01 11:23:08

标签: python matplotlib charts data-visualization radar-chart

我正在尝试使用此source中的以下代码绘制雷达图。

我的目标是反转r轴而不必重新映射我的数据点,因为我的数据的范围是从1到5,其中1表示食物非常好,而5表示非常不好。 (所以在反转数据点时,我会失去标度的含义)

(已被描述为here

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

所以来源是

# 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

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

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

2 个答案:

答案 0 :(得分:0)

如果要使用反向标签,则必须使用 plt.yticks([10,20,30], ["30", "20", "10"], ...),因为第一个参数对应于轴的值,并且您尚未将其反转,因此它们应保持该顺序。

我检查了plt.ylim的反转,它对我来说确实结束了,但是抛出了一个相当神秘的错误posx and posy should be finite values。考虑到posx和posy不是此函数的参数,必须有一个不喜欢此函数的基础函数。此外,在针对非极坐标图进行了测试之后,我认为问题出在极坐标上。

环顾四周,我同时发现了a github issueSO question,这在2018年12月产生了PR and posterior merge。如果您具有最新的matplotlib版本,它应该可以使用并工作。 / p>

答案 1 :(得分:0)

请看以下内容...希望您可以在此处使用某些功能。我采用的方法是绘制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)

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