Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

legend color spyder plot python

From the following sample dataset:

df_toy = pd.DataFrame({"Group":[1,2],
                   "Var1":[100,20],
                   "Var2":[50,40],
                   "Var3":[10,14],
                   "Var4":[10,140],
                   "Var5":[100,14]})

I am plotting a spyder/polar plot by means of the following code:

variables = [col for col in df_toy.columns if col != "Group"]
labels= variables + [variables[0]]

np.random.seed(1)
angles = np.linspace(0, 2 * np.pi, len(variables), endpoint=False)

# The first value is repeated to close the chart.
angles=np.concatenate((angles, [angles[0]]))

# polar plot each row separately
for row in df_toy.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.legend(df_toy["Group"])
plt.thetagrids(angles * 180 / np.pi, labels)
plt.show()

which produces the following plot, However, there is a wrong correspondence between legend and line color:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

enter image description here

what I am doing wrong?

>Solution :

Label the legend entries while plotting:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
...
...
# polar plot each row separately
for row in df_toy.values.tolist():
    values = row[1:] + [row[1]]
    plt.polar(angles, values, 'o-', linewidth=2, label=row[0])
    plt.fill(angles, values, alpha=0.25)

# Representation of the spider graph
plt.legend()
...

Sample output:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading