簡體   English   中英

使用 Matplotlib 比較圖形中的多條線

[英]Compare multiple lines in a graph using Matplotlib

我使用以下代碼創建了一個數據框:


sg = df[['Month','price','Year']].groupby(['Year','Month']).mean().reset_index().sort_values(['Year','Month'])
sg

我想用線圖比較兩年的月份。 我試圖這樣做:

sns.set_style('whitegrid')
fig,ax = plt.subplots(figsize=(15,7))


chart=sns.lineplot(x=sg['Month'], y=sg['price'], ax=ax)
sns.despine(left=True)

我得到了這個可視化:

第一張圖片

但我想創建一個這樣的圖像:

第二張圖片

添加huestyle參數

sns.set_style('whitegrid')
fig,ax = plt.subplots(figsize=(15,7))


chart=sns.lineplot(x='Month', y='price', hue='Year', data=sg)
sns.despine(left=True)

如果您不想要ci參數的背景顏色,請添加ci=None

另外,請注意我如何添加data=sg並在xy之前刪除sg[...] 也不需要ax=ax

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM