I have a pandas dataframe with the following format:
TEILNR VALUE date
351 10 2019-01-01
833 20 2019-01-01
...
351 40 2020-05-01
which has the dtypes:
TEILNR object
VALUE int64
date object
when I use the following command for plotting:
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()
I get the following output:
I am confused about the legend, this should respect the values of my TEILNR
column or not? Why is there not any 351 or 833?
You need to explicitly convert it to categorical:
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
I plot the two different types side by side, you can see once TEILNR is converted to categorical, the plot is correct, where hue treats it as categorical:
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])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.