python - 有人可以解释一下使用 matplotlib 创建雷达图的代码吗
问题描述
我无法从另一个问题中理解这段代码,我相信这是由于我没有使用 matplotlib 的经验,有人可以概述一下关键功能的作用等。我已经问过我的计算机科学老师,他无法提供帮助。比如,我真的不明白拼接的使用对于程序和数据的一般形成有什么作用。
N = len(positionAttributes)
x_as = [n / float(N) * 2 * pi for n in range(N)]
# Because our chart will be circular we need to append a copy of the first
# value of each list at the end of each list with data
values += values[:1]
x_as += x_as[:1]
# Set color of axes
plt.rc('axes', linewidth=0.5, edgecolor="#888888")
# Create polar plot
ax = plt.subplot(111, polar=True)
# Set clockwise rotation. That is:
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)
# Set position of y-labels
ax.set_rlabel_position(0)
# Set color and linestyle of grid
ax.xaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.1)
ax.yaxis.grid(True, color="#888888", linestyle='solid', linewidth=0.1)
# Set number of radial axes and remove labels
plt.xticks(x_as[:-1], [])
# Set yticks
plt.yticks([20, 40, 60, 80, 100], ["20", "40", "60", "80", "100"])
# Plot data
ax.plot(x_as, values, linewidth=2, linestyle='solid', zorder=3)
# Fill area
ax.fill(x_as, values, 'royalblue', alpha=0.09)
# Set axes limits
plt.ylim(0, 100)
# Draw ytick labels to make sure they fit properly
for i in range(N):
angle_rad = i / float(N) * 2 * pi
if angle_rad == 0:
ha, distance_ax = "center", 10
elif 0 < angle_rad < pi:
ha, distance_ax = "left", 1
elif angle_rad == pi:
ha, distance_ax = "center", 1
else:
ha, distance_ax = "right", 1
ax.text(angle_rad, 100 + distance_ax, positionAttributes[i], size=7, horizontalalignment=ha, verticalalignment="center")
解决方案
推荐阅读
- sql - 添加到 Group By 时出现无效的列名错误
- typescript - Vue & TypeScript 如何在渲染中获取数据属性?
- python - 这是我应该使用十进制库的情况吗?
- sorting - 如何在 Swi Prolog 上进行插入排序
- javascript - 我将如何根据计数增加进度条线?
- arrays - 检查数组中值的特定组合
- php - IF 语句失败
- python - Pandas groupby 计算百分比变化列
- c# - 使用 C# 和 Microsoft.TeamFoundationServer.Client 将代码签入到 TFS 2017
- vba - fillseries vba excel