简体   繁体   English

是否有可能在Python中的Radar-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. 我的目标是,在不必重新映射我的数据点的情况下反转r轴,因为我的数据的比例从1到5,其中1表示非常食物,5表示非常糟糕。 (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 . 我的第一种方法是使用matplotlibs固有的功能

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) 我似乎也无法仅反转Axis标签(因为这种方法只能反转数据和标签)

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. 如果你想使用反向标签的东西,你必须使用plt.yticks([10,20,30], ["30", "20", "10"], ...)因为第一个参数对应对于轴的值,并且由于你尚未将它反转,它们应该保持在那个顺序中。

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 . 我检查了plt.ylim反转,它确实为我结束了,但抛出一个相当神秘的错误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. 考虑到posx和posy不是这个函数的参数,必须有一个不喜欢这个的底层函数。 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. 环顾四周,我发现了一个github问题和一个SO问题 ,导致2018年12月PR和后合并 。如果你有最新的matplotlib版本,它应该可用并且有用。

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 . 我让这个工作的方式是绘制rmax-r而不是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)

在此输入图像描述

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM