我已经完成了雷达绘图,看起来几乎是我想要的样子。但是,由于图中有许多值,我想修改ylabels
/ yticks
的对齐方式。
我可以在给定角度的一行中用正确的值创建ylabels
/ yticks
,但我希望ylabels
处于相应的值,而不是他们在同一条线上。因此,对于每个角度,应在图上的相应值附近放置两个ylabels
。在图片中,您可以看到正确放置在相应值处的值4.14
和4.71
。
我的想法在Python中根本可行吗?
那是我使用的代码:
# number of variable
categories=list(data)[0:]
N = len(categories)
# Angles for plotting
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
# Initialise
f, ax = plt.subplots(1,1,figsize=(40,20))
ax = plt.subplot(111, polar=True)
# Draw one axe per variable
plt.xticks(angles[:-1], color='grey', size=16)
# the main problem in my code - placing yticks 'around' the plot
# to be near the corresponding values
for idx, an in enumerate(angles):
ax.set_rlabel_position(an) # positioning labels at a given angle
tick_values = [blue_values[idx],red_values[idx]] # to get the two labels values
plt.yticks(tick_values, [x.rstrip('.0') for x in list(map(str, tick_values))], color="black", size=16)
plt.ylim(0,max(red_values))
# Add plots
# Plot data
ax.plot(angles, blue_values, linewidth=1, linestyle='solid')
# Fill area
ax.fill(angles, blue_values, 'b', alpha=0.5)
# Plot data
ax.plot(angles, red_values, linewidth=1, linestyle='solid')
# Fill area
ax.fill(angles, red_values, 'r', alpha=0.2)
答案 0 :(得分:1)
仅在指定坐标处绘制文本会更容易。
# number of variable
categories=list(data)[0:]
N = len(categories)
# Angles for plotting
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
# Initialise
f, ax = plt.subplots(1,1,figsize=(40,20))
ax = plt.subplot(111, polar=True)
# Draw one axe per variable
plt.xticks(angles[:-1], color='grey', size=16)
# the main problem in my code - placing yticks 'around' the plot
# to be near the corresponding values
for idx, an in enumerate(angles):
plt.text(an, red_values[idx], str(red_values[idx]), color="black", size=16)
plt.text(an, blue_values[idx], str(blue_values[idx]), color="black", size=16)
plt.yticks([])
# plt.yticks(blue_values + red_values, []) # if you want to keep the circles.
plt.ylim(0,max(red_values))
# Add plots
# Plot data
ax.plot(angles, blue_values, linewidth=1, linestyle='solid')
# Fill area
ax.fill(angles, blue_values, 'b', alpha=0.5)
# Plot data
ax.plot(angles, red_values, linewidth=1, linestyle='solid')
# Fill area
ax.fill(angles, red_values, 'r', alpha=0.2)