Python Pandas - 使用Seaborn绘制具有两个类别变量嵌套分组的垂直条形图
Seaborn中的条形图用于显示点估计和置信区间作为矩形条。为此使用**seaborn.barplot()**。通过使用x、y或**hue**参数传递类别变量,绘制按类别变量分组的垂直条形图。
假设我们的数据集如下所示,它是一个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")
绘制按两个类别变量分组的垂直条形图。hue参数也已设置
sb.barplot(x = dataFrame["Role"], y = dataFrame["Matches"], hue = "Academy", 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\Cricketers2.csv") # plotting vertical bar plots grouped by two categorical variables # hue parameter also set sb.barplot(x = dataFrame["Role"], y = dataFrame["Matches"], hue = "Academy", data= dataFrame) # display plt.show()
输出
这将产生以下输出:
广告