'How can I adjust the Hue of a Seaborn Lineplot without having it connect to the next time the category shows up?

I wish to add a hue to my seaborn lineplot, however the following keeps happening, as shown in the image.

My code is as follows:

plt.figure(figsize=(15,5))
sns.lineplot(x = 'DATETIME', y = 'TOTALDEMAND', data = df_day.loc[df_day['STATE'] == 'NSW'],hue="Season")

Is there a way to have the hue not connect to the next season?

enter image description here



Solution 1:[1]

An idea could be to draw the lineplot with a fixed color, and use a scatterplot for colorizing:

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame({'DATETIME': pd.date_range('20160101', periods=1000, freq='D'),
                   'TOTALDEMAND': np.random.randint(-10, 11, 1000).cumsum() + 30000})
df['SEASON'] = pd.Categorical.from_codes((df.DATETIME.dt.month - 1) // 3, ['winter', 'spring', 'summer', 'autumn'])

plt.figure(figsize=(15, 6))
ax = sns.lineplot(data=df, x='DATETIME', y='TOTALDEMAND', color='black')
sns.scatterplot(data=df, x='DATETIME', y='TOTALDEMAND', s=20, alpha=0.2, hue='SEASON', ax=ax)
sns.despine()
plt.show()

sns.lineplot with hue coloring

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohanC