Python Pandas - 使用Seaborn绘制小提琴图并通过传递显式顺序控制顺序
Seaborn中的小提琴图用于绘制箱线图和核密度估计的组合。 seaborn.violinplot() 用于此目的。使用小提琴图的**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")
绘制带有角色和年龄的小提琴图。通过传递显式顺序来控制顺序,即基于“角色”排序。
sb.violinplot(x = 'Role', y = "Age", order=["Batsman", "Bowler"], data = dataFrame)
示例
以下是代码:
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 violin plot with Role and Age # Control order by passing an explicit order i.e. ordering on the basis of "Role" # ordering set using the order parameter sb.violinplot(x = 'Role', y = "Age", order=["Batsman", "Bowler"], data = dataFrame) # display plt.show()
输出
这将生成以下输出:
广告