簡體   English   中英

以不同順序在 Seaborn lineplot 中繪制每月數據

[英]Plotting monthly data in Seaborn lineplot in different order

我正在尋找 plot 線圖中的每月空氣質量數據 Seaborn。 由於空氣質量最差的月份是 11 月至 2 月,我希望將這些月份放在線圖的中間。 我的問題是,這可以做到嗎?

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")

這個就是目前正在生產的plot

在此處輸入圖像描述

我嘗試對月份值進行分類排序,但這返回了一個空白圖表

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

這是我正在嘗試的數據 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

這是我想要實現的 excel 圖

在此處輸入圖像描述

感謝您的幫助@JohanC。 我沒有意識到我必須在應用 pd.Categorical 后對值進行排序。 (我也意識到我錯誤地將月份數字放在引號中)

# 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)

一旦我這樣做了,我就可以使用你對 sns.pointplot 的有用建議來實現我想要的圖表。

# 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'])

在此處輸入圖像描述

暫無
暫無

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

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