雷达图 matplotlib - yticks 的位置
Radar plot matplotlib - position of yticks
我已经完成了雷达图,看起来几乎是我想要的样子。但是,由于图上有很多值,我想修改 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)
这就是我实现的情节。如您所见,只有两个标签(当然是由于for-loop
)。
只在指定坐标处绘制文本会更容易。
# 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)
我已经完成了雷达图,看起来几乎是我想要的样子。但是,由于图上有很多值,我想修改 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)
这就是我实现的情节。如您所见,只有两个标签(当然是由于for-loop
)。
只在指定坐标处绘制文本会更容易。
# 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)