Python Pandas - 使用Seaborn绘制小提琴图,指定顺序并显示观测值作为线条
Seaborn中的小提琴图用于绘制箱线图和核密度估计的组合。 seaborn.violinplot() 用于此目的。 使用**inner**参数,其值为**stick**,可以将观测值显示为线条。
假设我们的数据集是一个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")
绘制带有学院和年龄的小提琴图。通过传递显式顺序(即基于“学院”的排序)来控制顺序。使用inner参数,值为stick,将观测值显示为线条:
sb.violinplot(x = 'Academy', y = "Age", order=["Western Australia", "Tasmania","South Australia"],data = dataFrame, inner="stick")
示例
代码如下:
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 Academy and Age # Control order by passing an explicit order i.e. ordering on the basis of "Academy" # observations show as a stick using the inner parameter with value stick sb.violinplot(x = 'Academy', y = "Age", order=["Western Australia", "Tasmania","South Australia"],data = dataFrame, inner="stick") # display plt.show()
输出
这将产生以下输出:
广告