正如斯科特所评论的,你可以根据dayofyear
, and seaborn
帮助您通过使用hue
参数来分隔数据'Year'
.
测试于python 3.12.0
, pandas 2.1.1
, matplotlib 3.8.0
, seaborn 0.13.0
提取或创建附加数据
# Date is in the index, so reset
df = df.reset_index()
# verify column is a datetime dtype
df['Date'] = pd.to_datetime(df['Date'])
# extract the Year, which will be used for the color
df['Year'] = df.Date.dt.year
# extract the day of the year for the x-axis
df['DoY'] = df.Date.dt.day_of_year
情节与Axes-level
method
fig, ax = plt.subplots(figsize=(8, 5))
sns.lineplot(data=df, x='DoY', y='Close', hue='Year', legend='full', ax=ax)
sns.move_legend(ax, bbox_to_anchor=(1, 0.5), loc='center left', frameon=False, ncols=3)
情节与Figure-level
method
g = sns.relplot(kind='line', data=df, x='DoY', y='Open', hue='Year', legend='full', height=7, aspect=1)