Python 作为字符串的数字的seaborn色调

Python 作为字符串的数字的seaborn色调,python,matplotlib,seaborn,Python,Matplotlib,Seaborn,我是新的seaborn,我有这个数据集,我想创建一个这样的图,但是在seaborn中 这是我的数据: max_depth = [ 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 10, 10, 10, 10, 10, 12, 12, 12, 12, 12] min_samples_split = [2, 5, 15, 20, 25, 2, 5, 15, 20, 25, 2, 5, 15, 20, 25, 2

我是新的seaborn,我有这个数据集,我想创建一个这样的图,但是在seaborn中

这是我的数据:

max_depth = [ 3,  3,  3,  3,  3,  5,  5,  5,  5,  5,  7,  7,  7,  7,  7, 10, 10,
       10, 10, 10, 12, 12, 12, 12, 12]
min_samples_split = [2, 5, 15, 20, 25, 2, 5, 15, 20, 25, 2, 5,
   15, 20, 25, 2, 5, 15, 20, 25, 2, 5, 15, 20, 25]
test_score = [0.85089537, 0.85089537, 0.85089537, 0.85348114, 0.85354819, 0.87357118, 0.87328475, 0.87147859, 0.87425471, 0.87402261,
       0.86355856, 0.86120602, 0.87259394, 0.87582926, 0.87943536, 0.80913078, 0.82786446, 0.86109688, 0.86773115, 0.87619951,
       0.79090683, 0.8038633 , 0.84915534, 0.86083209, 0.87192132]

results_DT = pd.DataFrame({'max_depth': max_depth, 'min_samples_split': min_samples_split, 'test_score': test_score})
这是我在seaborn的尝试:

但正如您所见,这些类别是不正确的:

当我试图将其转换为字符串时,它会出现一个错误

#convert   
results_DT2 = results_DT
    results_DT2['min_samples_split'] = results_DT2['min_samples_split'].astype(str)

sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT2) # need to work out how to fix this
plt.legend(loc='lower left')
plt.xlabel("Max depth")
plt.ylabel("Mean CV score")
AttributeError:'str'对象没有属性'view'

如何修复此问题?

尝试以下方法:

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT, palette=sns.color_palette("Set1", results_DT.min_samples_split.nunique())) # need to work out how to fix this
plt.legend(loc='lower left')
plt.xlabel("Max depth")
plt.ylabel("Mean CV score")

中所述,尝试以下方法:

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT, palette=sns.color_palette("Set1", results_DT.min_samples_split.nunique())) # need to work out how to fix this
plt.legend(loc='lower left')
plt.xlabel("Max depth")
plt.ylabel("Mean CV score")

中所述,您可以将min_samples_split列转换为Category:

results_DT.min_samples_split = pd.Categorical(results_DT.min_samples_split)
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT)

可以将“最小样本数”拆分列转换为“分类”:

results_DT.min_samples_split = pd.Categorical(results_DT.min_samples_split)
sns.lineplot(x = 'max_depth', y = 'test_score', hue = 'min_samples_split', marker = 'o', data = results_DT)

伟大的谢谢这是一个更好的方式转换为分类太好了。谢谢这也是一种更好的转换为分类的方法。谢谢你的回答。谢谢你的回答。