python - 数字作为字符串的 seaborn 色调

标签 python matplotlib seaborn

我是 seaborn 的新手,我有这个数据集,想创建一个这样的图表,但要使用 seaborn

enter image description here

这是我的数据:

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 中的尝试:

%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) # need to work out how to fix this
plt.legend(loc='lower left')
plt.xlabel("Max depth")
plt.ylabel("Mean CV score")

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

enter image description here

当我尝试将其转换为字符串时,出现错误。

#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' object has no attribute 'view'

我该如何解决这个问题?

最佳答案

您可以将 min_samples_split 列转换为分类:

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)

enter image description here

关于python - 数字作为字符串的 seaborn 色调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61747669/

相关文章:

python - 填充 seaborn/matplotlib 中两个正态分布之间的重叠区域

python - 我们可以在 python 3.5 下运行旧版 python 2.7 代码吗?

python - 如何计算由端点和弧上点给出的两条弧的交点?

python - numpy 数据类型转换在 x=x+a 和 x+=a 中的行为不同

python - 如何防止for循环停止?

python - 从 Seaborn 调色板中选择颜色

python-3.x - Seaborn散点图图例未显示

python - 自动使 matplotlib 图像与其他子图标签齐平

python - 如何使 matplotlibs nbagg 后端生成 SVG?

python - 在matplotlib、python中画几张图