📌  相关文章
📜  Seaborn – 对 Barplot 中的 Bars 进行排序

📅  最后修改于: 2022-05-13 01:54:54.868000             🧑  作者: Mango

Seaborn – 对 Barplot 中的 Bars 进行排序

先决条件: Seaborn、Barplot

在本文中,我们将看到如何在Python中使用 Seaborn 对 barplot 中的条进行排序。

Seaborn是一个了不起的可视化库,用于在Python中绘制统计图形。它提供了漂亮的默认样式和调色板,使统计图更具吸引力。它建立在matplotlib库的顶部,并且还与 pandas 的数据结构紧密集成

方法:

  • 导入模块。
  • 创建一个数据框。
  • 创建一个条形图
  • 使用DataFrame.sort_values()Dataframe列进行排序。
  • 将排序数据框显示到 barplot

因此,让我们根据上述方法使用 seaborn 实现对 barplot 中的 bar 进行排序。

第 1 步:需要导入 包。

Python3
# Import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns


Python3
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
  
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
  
# Display Dataframe
df


Python3
# make barplot
sns.barplot(x='State', y="Growth", data=df)


Python3
# sort dataframe
df.sort_values('Growth')


Python3
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth').State)


Python3
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth',ascending = False).State)


Python3
# import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  
  
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
  
  
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
  
  
# sort dataframe
df.sort_values('Growth')
  
  
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth').State)


Python3
# import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  
  
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
  
  
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
  
  
# sort dataframe
df.sort_values('Growth')
  
  
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth',ascending = False).State)


第 2 步:创建一个Dataframe来创建一个barplot

蟒蛇3

# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
  
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
  
# Display Dataframe
df

输出:

第 3 步:使用此Dataframe创建一个条形图

蟒蛇3

# make barplot
sns.barplot(x='State', y="Growth", data=df)

输出:

第 4 步:让我们使用 DataFrame.sort_values() 对Dateframe列(Growth 列)进行排序。

蟒蛇3

# sort dataframe
df.sort_values('Growth')

输出:

我们可以在barplot中使用相同的技术。

蟒蛇3

# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth').State)

输出:

第 5 步:让我们按照降序对条形图进行排序。

蟒蛇3

# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth',ascending = False).State)

输出:

注意:升序的默认值始终为 True,如果我们将此参数更改为 False,则表示其按降序排列。

下面是完整的例子:

示例 1

蟒蛇3

# import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  
  
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
  
  
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
  
  
# sort dataframe
df.sort_values('Growth')
  
  
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth').State)

输出:

示例 2

蟒蛇3

# import module
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
  
  
# Initialize data
State = ["Rajasthan", "Bihar", "Madhya Pradesh",
         "Gujarat", "Maharashtra"]
growth = [342239, 94163, 308245, 196024, 307713]
  
  
# Create a pandas dataframe
df = pd.DataFrame({"State": State,
                   "Growth": growth})
  
  
# sort dataframe
df.sort_values('Growth')
  
  
# make barplot and sort bars
sns.barplot(x='State',
            y="Growth", data=df, 
            order=df.sort_values('Growth',ascending = False).State)

输出: