简体   繁体   English

使用 Matplotlib 比较图形中的多条线

[英]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 .添加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)

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并在xy之前删除sg[...] ax=ax is not needed as well.也不需要ax=ax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM