Python Pandas - 使用Seaborn绘制条形图并通过传递显式顺序控制群体顺序
Seaborn中的条形图用于显示点估计和置信区间作为矩形条。使用seaborn.barplot()。通过传递显式顺序来控制顺序,即使用**order**参数基于特定列进行排序。
假设我们的数据集是一个CSV文件,如下所示:-Cricketers2.csv
首先,导入所需的库:
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt
将CSV文件中的数据加载到Pandas DataFrame中:
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv")
绘制带有“比赛”和“学院”列的水平条形图。通过传递显式顺序来控制顺序,即使用order参数基于“学院”列进行排序:
sb.barplot(x = dataFrame["Academy"], y = dataFrame["Matches"],order = ["Victoria", "Western Australia", "South Australia", "Tasmania"])
示例
代码如下:
import seaborn as sb import pandas as pd import matplotlib.pyplot as plt # Load data from a CSV file into a Pandas DataFrame dataFrame = pd.read_csv("C:\Users\amit_\Desktop\Cricketers2.csv") # plotting horizontal bar plots with Matches and Academy # Control order by passing an explicit order i.e. ordering on the basis of "Academy" using order parameter sb.barplot(x = dataFrame["Academy"], y = dataFrame["Matches"],order = ["Victoria", "Western Australia", "South Australia", "Tasmania"]) # display plt.show()
输出
这将产生以下输出:
广告