首页 > 解决方案 > 有人可以解释一下使用 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")

标签: pythonmatplotlib

解决方案


推荐阅读