Python Pandas - 使用Seaborn绘制点图并通过显式顺序控制顺序
Seaborn中的点图使用散点图符号显示点估计和置信区间。 seaborn.pointplot() 用于此目的。对于显式顺序,请使用 pointplot() 方法的 **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")
使用“Academy”和“Age”绘制点图。通过传递显式顺序来控制顺序,即根据“Academy”排序。使用order参数进行排序:
sb.pointplot( 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") sb.set_theme(style="darkgrid") # plotting point plot with Academy and Age # Control order by passing an explicit order i.e. ordering on the basis of "Academy" # ordering using the order parameter sb.pointplot( x = 'Academy',y = 'Age', data = dataFrame, order=["Tasmania", "South Australia", "Victoria"] ) # display plt.show()
输出
这将产生以下输出:
广告