The main goal is to plot a polar graph from the following dataframe
sample.
df = pd.DataFrame({"Group":[1,2],
"Var1":[100,20],
"Var2":[50,40],
"Var3":[10,14],
"Var4":[10,140],
"Var5":[100,14]})
What I have so far is the following code to accomplish this issue for only one "group" or line plot.
# Create graph with values of most relevant variables
# Create a random data set
np.random.seed(1)
angles = np.linspace(0, 2 * np.pi, 5, endpoint=False)
values = np.random.random(5)
# The first value is repeated to close the chart.
angles=np.concatenate((angles, [angles[0]]))
values=np.concatenate((values, [values[0]]))
labels=['Var1', 'Var2', 'Var3', 'Var4', 'Var5']
# Representation of the spider graph
plt.polar(angles, values, 'o-', linewidth=2)
plt.fill(angles, values, alpha=0.25)
plt.thetagrids(angles * 180 / np.pi, labels)
plt.show()
Not sure how to transform df
in order to plot group 1 and 2 together in same plot.
CodePudding user response:
You can just loop through data frame rows and polar plot each row:
import matplotlib.pyplot as plt
np.random.seed(1)
angles = np.linspace(0, 2 * np.pi, 5, endpoint=False)
# The first value is repeated to close the chart.
angles=np.concatenate((angles, [angles[0]]))
labels=['Var1', 'Var2', 'Var3', 'Var4', 'Var5', 'Var1']
# polar plot each row separately
for row in df.values.tolist():
values = row[1:] [row[1]]
plt.polar(angles, values, 'o-', linewidth=2)
plt.fill(angles, values, alpha=0.25)
# Representation of the spider graph
plt.thetagrids(angles * 180 / np.pi, labels)
plt.show()