Python seaborn FaceGrid

2024-10-01 19:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我根据销售人员为每个客户计算了绿色/橙色/红色: 比如:

    sellor  customer    red green   orange
0   73  c1  5   96  15
1   77  c1  88  18  79
2   97  c1  58  59  71

我可以用它来构建:

^{pr2}$

现在我想用图形表示数据: 现在我想:

for customer in df.customer.unique():
    df[df.customer==customer].plot.bar(title=customer)

这给了我4张图片,如下所示:

enter image description here

我想我也可以用seaborn FaceGrid做同样的事情,但没有找到真正的方法。 我试过了:

sns.barplot(data=df, x="sellor", y="red", hue="customer")

但它只给了我一个地块(没有按客户细分)+它没有给我卖家的3个小节:( enter image description here

如何使用facetgrid获得与循环相同的结果?在


Tags: 数据图形df客户人员greenredcustomer
1条回答
网友
1楼 · 发布于 2024-10-01 19:18:48

不使用seaborn,您可以将条形图绘制为同一图形的不同子图

import matplotlib.pyplot as plt

u = df.customer.unique()
fig, axes = plt.subplots(ncols=len(u), figsize=(10,3))

for customer, ax in zip(u, axes):
    df[df.customer==customer].plot.bar(x="sellor", title=customer, ax =ax )

plt.tight_layout()    
plt.show()

enter image description here

你可以通过seaborn的FaceGrid实现类似的功能

^{pr2}$

enter image description here

最后,要更改此处使用的颜色以匹配数据帧名称,您可以设置颜色周期,例如

plt.rcParams["axes.prop_cycle"] = plt.cycler("color", ["red", "green", "orange"])

相关问题 更多 >

    热门问题