Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีทำ radar (spider) chart หลายเหลี่ยมใน Python Matplotlib

ในการทำ radar (spider) chart หลายเหลี่ยมใน Python สามารถทำตามขั้นตอนดังนี้:

ขั้นตอน

  • ตั้งค่าขนาดรูปและปรับ padding ระหว่างและรอบ subplots

  • สร้าง Pandas dataframe คอลัมน์ sports และ values

  • สร้าง figure ใหม่หรือ activate existing figure

  • เพิ่ม ax ให้ figure เป็นส่วนของ subplot arrangement

  • จากค่า data frame คำนวณ theta value

  • ได้รายการ values จาก data frame

  • สร้าง bar plot ด้วย theta และ values

  • เติมสีพื้นที่ระหว่าง polygon

  • แสดงรูปด้วยเมธอด show()

ตัวอย่าง

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
df = pd.DataFrame({'sports': ['Strength', 'Speed', 'Power', 'Agility', 'Endurance', 'Analytical Aptitude'],
                   'values': [7, 8, 6, 10, 8, 9]})
fig = plt.figure()
ax = fig.add_subplot(111, projection="polar")
theta = np.arange(len(df) + 1) / float(len(df)) * 2 * np.pi
values = df['values'].values
values = np.append(values, values[0])
l1, = ax.plot(theta, values, color="purple", marker="o", label="Name of values")
ax.tick_params(pad=10)
ax.fill(theta, values, 'green', alpha=0.3)
plt.show()

ผลลัพธ์

จะแสดง radar chart 6 มุม สีม่วงขอบ สีเขียวเติม alpha 0.3

วิธีทำ radar (spider) chart หลายเหลี่ยมใน Python Matplotlib