图例颜色 spyder 图 python

legend color spyder plot python

来自以下示例数据集:

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

我正在通过以下代码绘制 spyder/polar 图:

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()

生成如下图,但是,图例和线条颜色之间的对应关系有误:

我做错了什么?

我没有重现你的问题:

import matplotlib.pyplot as plt

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

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()

输出:

在绘图时标记图例条目:

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()
...

示例输出: