Reputation: 77
I am using panda in order to assign a score to some gamers.
I computed, using the same KPIs, some attributes for every gamer and now I have, for each player, a row with the results.
The dataframe looks like this (the only difference is that it has more columns) :
I thought it would be nice to plot a radar chart (https://en.wikipedia.org/wiki/Radar_chart) for each row using matplotlib (if possible).
How would you do it?
Is there a better alternative to matplotlib?
Thanks.
Upvotes: 1
Views: 1477
Reputation: 12992
To get this spider-look, you need at least three columns. So, I've added a Random column to your dataframe:
import pandas as pd
df = pd.DataFrame({"Name": ["G1", "G4"],
"Speed": [0.32, 0.31],
"ATK": [0.89, 0.76],
"Random": [0.4, 0.8]})
print(df)
# Name Speed ATK Random
#0 G1 0.32 0.89 0.4
#1 G4 0.31 0.76 0.8
Now, let's see how to plot this simple dataframe. The follwing code is adapted from this blog post:
# import necessary modules
import numpy as np
import matplotlib.pyplot as plt
from math import pi
# obtain df information
categories = list(df)[1:]
values = df.mean().values.flatten().tolist()
values += values[:1] # repeat the first value to close the circular graph
angles = [n / float(len(categories)) * 2 * pi for n in range(len(categories))]
angles += angles[:1]
# define plot
fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(8, 8),
subplot_kw=dict(polar=True))
plt.xticks(angles[:-1], categories, color='grey', size=12)
plt.yticks(np.arange(0.5, 2, 0.5), ['0.5', '1.0', '1.5'],
color='grey', size=12)
plt.ylim(0, 2)
ax.set_rlabel_position(30)
# draw radar-chart:
for i in range(len(df)):
val_c1 = df.loc[i].drop('Name').values.flatten().tolist()
val_c1 += val_c1[:1]
ax.plot(angles, val_c1, linewidth=1, linestyle='solid',
label=df.loc[i]["Name"])
ax.fill(angles, val_c1, alpha=0.4)
# add legent and show plot
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
plt.show()
Which results a graph like so:
Upvotes: 1