这是 seaborn.lineplot 色调参数中的错误吗? [英] Is this an error in the seaborn.lineplot hue parameter?

查看:52
本文介绍了这是 seaborn.lineplot 色调参数中的错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用此代码片段,我期望每个色调有一条线的线图,其具有以下不同的值:[1, 5, 10, 20, 40].

With this code snippet, I'm expecting a line plot with one line per hue, which has these distinct values: [1, 5, 10, 20, 40].

import math
import pandas as pd
import seaborn as sns

sns.set(style="whitegrid")

TANH_SCALING = [1, 5, 10, 20, 40]
X_VALUES = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
COLUMNS = ['x', 'y', 'hue group']

tanh_df = pd.DataFrame(columns=COLUMNS)

for sc in TANH_SCALING:
    data = {
        COLUMNS[0]: X_VALUES,
        COLUMNS[1]: [math.tanh(x/sc) for x in X_VALUES],
        COLUMNS[2]: len(X_VALUES)*[sc]}
    tanh_df = tanh_df.append(
        pd.DataFrame(data=data, columns=COLUMNS),
        ignore_index=True
    )

sns.lineplot(x=COLUMNS[0], y=COLUMNS[1], hue=COLUMNS[2], data=tanh_df);

然而,我得到的是一个值为 [0, 15, 30, 45] 的色调图例,以及一个额外的行,如下所示:

However, what I get is a hue legend with values [0, 15, 30, 45], and an additional line, like so:

这是一个错误还是我遗漏了一些明显的东西?

Is this a bug or am I missing something obvious?

推荐答案

这是 seaborn 的一个已知错误,当​​色调可以转换为整数时.您可以为色调添加前缀,以便转换为整数失败:

This is a known bug of seaborn when the hue can be cast to integers. You could add a prefix to the hue so casting to integers fails:

for sc in TANH_SCALING:
    data = {
        COLUMNS[0]: X_VALUES,
        COLUMNS[1]: [math.tanh(x/sc) for x in X_VALUES],
        COLUMNS[2]: len(X_VALUES)*[f'A{sc}']}             # changes here
    tanh_df = tanh_df.append(
        pd.DataFrame(data=data, columns=COLUMNS),
        ignore_index=True
    )

输出:

或者在您创建数据之后:

Or after you created your data:

# data creation
for sc in TANH_SCALING:
    data = {
        COLUMNS[0]: X_VALUES,
        COLUMNS[1]: [math.tanh(x/sc) for x in X_VALUES],
        COLUMNS[2]: len(X_VALUES)*[f'A{sc}']}
    tanh_df = tanh_df.append(
        pd.DataFrame(data=data, columns=COLUMNS),
        ignore_index=True
    )


# hue manipulation
sns.lineplot(x=COLUMNS[0], y=COLUMNS[1], 
             hue='A_' + tanh_df[COLUMNS[2]].astype(str), # change hue here
             data=tanh_df);

这篇关于这是 seaborn.lineplot 色调参数中的错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆