Python Pandas - 使用Seaborn绘制箱线图并通过显式顺序控制箱子顺序
Seaborn中的箱线图用于绘制箱线图,以显示关于类别的分布。为此使用seaborn.boxplot()。要控制顺序,请使用**order**参数。
假设以下数据集以CSV文件的形式提供:Cricketers.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\Cricketers.csv")
绘制包含学院和年龄的箱线图。通过传递显式顺序(即基于“学院”的排序)来控制箱子顺序。使用order参数进行排序:
sb.boxplot( x = 'Academy',y = 'Age', data = dataFrame, order=["Tasmania", "South Australia", "Victoria"] )
示例
以下是代码:
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\Cricketers.csv") # plotting box plot with Academy and Age # Control box order by passing an explicit order i.e. ordering on the basis of "Academy" # ordering using the order parameter sb.boxplot( x = 'Academy',y = 'Age', data = dataFrame, order=["Tasmania", "South Australia", "Victoria"] ) # display plt.show()
输出
这将产生以下输出:
广告