简体   繁体   中英

Plotting monthly data in Seaborn lineplot in different order

I am looking to plot monthly air quality data in a Seaborn lineplot. As the months with the worst air quality are Nov-Feb, I would like to have these months in the middle of the lineplot. My question is, can this be done?

concentration vs month
axes[1] = sns.lineplot(ax=axes[1],
                       data=df_month,
                       #x=df['date'],
                       x=df_month['month'],
                       y=df_month['monthly mean'],
                       color='red',
                       linewidth=1.5,
                       #hue=hue,
                       palette="hls")

This is the plot that is currently being produced

在此处输入图像描述

I tried to order the month values categorically but this returned a blank chart

df_month['month'] = pd.Categorical(df_month['month'],
                                    categories=['6', '7', '8', '9', '10', '11', '12', '1', 
                                    '2', '3', '4', '5'],
                                    ordered=True)

This is the data I am trying to plot

    location    month   monthly mean
1   Location A  3   87.98375
2   Location A  4   45.91740741
3   Location A  5   21.71923077
4   Location A  6   12.84966667
5   Location A  7   10.09612903
6   Location A  8   13.80387097
7   Location A  9   18.598
8   Location A  10  37.799
9   Location A  11  108.124
10  Location A  12  71.87241379
11  Location A  1   138.5916129
12  Location A  2   55.36103448

This is an excel graph of what I am trying to achieve

在此处输入图像描述

Thanks for your help @JohanC. I did not realise that I had to sort the values after applying pd.Categorical. (I also realised I was incorrectly putting the month numbers in quotation marks)

# set categorical order
  df_month['month'] = pd.Categorical(df_month['month'], categories=[6, 7, 8, 9, 
  10, 11, 12, 1, 2, 3, 4, 5], ordered=True)
  df_month.sort_values('month', inplace=True)

Once I did that, I could use your helpful suggestion of sns.pointplot to achieve the chart I was aiming for.

# concentration vs month
axes[1] = sns.pointplot(ax=axes[1],
                       data=df_month,
                       x='month',
                       y='monthly max mean',
                       color='red',
                       scale =0.6,
                       markers='',
                       order=df_month['month'])

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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