Python seaborn线型图显示了错误的图例

Python seaborn线型图显示了错误的图例,python,pandas,seaborn,Python,Pandas,Seaborn,我有一个熊猫数据框,格式如下: TEILNR VALUE date 351 10 2019-01-01 833 20 2019-01-01 ... 351 40 2020-05-01 其中包含以下数据类型: TEILNR object VALUE int64 date object 使用以下命令进行打印时: sns.set(style="whitegrid") plt.figure(figsize=(12,10)) sns.linep

我有一个熊猫数据框,格式如下:

TEILNR VALUE date
351    10    2019-01-01
833    20    2019-01-01
...
351   40    2020-05-01
其中包含以下数据类型:

TEILNR object
VALUE   int64
date object
使用以下命令进行打印时:

sns.set(style="whitegrid")
plt.figure(figsize=(12,10))
sns.lineplot(x= 'date', y='value', hue='TEILNR', ci=None,
                 data=df, lw=1)
plt.legend(bbox_to_anchor=(1.04,1), loc="upper left")

plt.xticks(rotation=45)
plt.show()
我得到以下输出:


我对图例感到困惑,这是否应该尊重我的
TEILNR
列的值?为什么没有任何351或833?

您需要将其显式转换为分类:

DATE = pd.date_range('2020-01-01', periods=8, freq='M')
df = pd.DataFrame({'TEILNR':np.repeat(['351','833'],8),
                  'value':np.random.normal(0,1,16),
                  'date':pd.concat([pd.Series(DATE),pd.Series(DATE)])})

df.dtypes

TEILNR            object
value            float64
date      datetime64[ns]
dtype: object
fig, ax = plt.subplots(1,2,figsize = (10,4))
sns.set(style="whitegrid")
sns.lineplot(x= 'date', y='value', hue='TEILNR', ci=None,data=df, lw=1,ax=ax[0])

df['TEILNR'] = df['TEILNR'].astype('category')
sns.lineplot(x= 'date', y='value', hue='TEILNR', ci=None,data=df, lw=1,ax=ax[1])
我将两种不同的类型并排绘制,您可以看到,一旦TEILNR转换为“分类”,则绘制是正确的,其中hue将其视为“分类”:

DATE = pd.date_range('2020-01-01', periods=8, freq='M')
df = pd.DataFrame({'TEILNR':np.repeat(['351','833'],8),
                  'value':np.random.normal(0,1,16),
                  'date':pd.concat([pd.Series(DATE),pd.Series(DATE)])})

df.dtypes

TEILNR            object
value            float64
date      datetime64[ns]
dtype: object
fig, ax = plt.subplots(1,2,figsize = (10,4))
sns.set(style="whitegrid")
sns.lineplot(x= 'date', y='value', hue='TEILNR', ci=None,data=df, lw=1,ax=ax[0])

df['TEILNR'] = df['TEILNR'].astype('category')
sns.lineplot(x= 'date', y='value', hue='TEILNR', ci=None,data=df, lw=1,ax=ax[1])