[英]Compare multiple lines in a graph using Matplotlib
I create a dataframe with this code:我使用以下代码创建了一个数据框:
sg = df[['Month','price','Year']].groupby(['Year','Month']).mean().reset_index().sort_values(['Year','Month'])
sg
And I want to compare the months of two years with line plots.我想用线图比较两年的月份。 I attempted to do this as follows:
我试图这样做:
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)
and I get the this visualization:我得到了这个可视化:
But I want to create an image like this:但我想创建一个这样的图像:
Add a hue
or a style
parameter .添加
hue
或style
参数。
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)
If you do not want the background color which is ci
parameter add ci=None
.如果您不想要
ci
参数的背景颜色,请添加ci=None
。
Also, notice how I added data=sg
and I removed sg[...]
before x
and y
.另外,请注意我如何添加
data=sg
并在x
和y
之前删除sg[...]
。 ax=ax
is not needed as well.也不需要
ax=ax
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.