Spaces:
Runtime error
Runtime error
Create spider_plot.py
Browse files- spider_plot.py +39 -0
spider_plot.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
def spider_plot(df):
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from math import pi
|
| 5 |
+
|
| 6 |
+
# number of variable
|
| 7 |
+
categories=list(df)
|
| 8 |
+
N = len(categories)
|
| 9 |
+
|
| 10 |
+
# We are going to plot the first line of the data frame.
|
| 11 |
+
# But we need to repeat the first value to close the circular graph:
|
| 12 |
+
values=df.loc[0].values.flatten().tolist()
|
| 13 |
+
values += values[:1]
|
| 14 |
+
values
|
| 15 |
+
|
| 16 |
+
# What will be the angle of each axis in the plot? (we divide the plot / number of variable)
|
| 17 |
+
angles = [n / float(N) * 2 * pi for n in range(N)]
|
| 18 |
+
angles += angles[:1]
|
| 19 |
+
|
| 20 |
+
# Initialise the spider plot
|
| 21 |
+
ax = plt.subplot(111, polar=True)
|
| 22 |
+
|
| 23 |
+
# Draw one axe per variable + add labels
|
| 24 |
+
plt.xticks(angles[:-1], categories, color='grey', size=8)
|
| 25 |
+
|
| 26 |
+
# Draw ylabels
|
| 27 |
+
ax.set_rlabel_position(0)
|
| 28 |
+
plt.yticks([10,20,30], ["10","20","30"], color="grey", size=7)
|
| 29 |
+
plt.ylim(0,1)
|
| 30 |
+
|
| 31 |
+
# Plot data
|
| 32 |
+
ax.plot(angles, values, linewidth=1, linestyle='solid')
|
| 33 |
+
|
| 34 |
+
# Fill area
|
| 35 |
+
ax.fill(angles, values, 'b', alpha=0.1)
|
| 36 |
+
|
| 37 |
+
# Show the graph
|
| 38 |
+
plt.show()
|
| 39 |
+
|