使用sns对每个分类值进行单独绘图

2024-10-04 05:25:19 发布

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

我可以找到两种方法,一种是使用seaborn快速显示多个绘图,一种是使用PairPlot,另一种是使用FaceGrid。然而,我想要的是有一种快速的方法,为列中的每个分类值绘制一个saperate图(无需为每个分类值创建saperate数据帧),但上述两种方法都没有提供我想要的输出。因为我不想为我的图创建单独的数据帧,而想为每个值创建单独的图,所以我想知道实现这一点的快速方法

enter image description here

在这张图片中,我只有一个绘图,所以我想为masterkard、Chinapay、viza和carte rouge单独绘图(不创建单独的数据帧)


Tags: 数据方法绘图绘制分类图片seabornrouge
2条回答

您可以使用以下代码:

fig = plt.figure(figsize=(20,14))
ax1 = fig.add_subplot(131)
ax1 = fig.add_subplot(132)
ax1 = fig.add_subplot(133)

在我的例子中,在水平轴上产生了三个图形

正如您在评论中提到的,这应该是有效的:

g = sns.FacetGrid(transactions, col="Processed Card Scheme")
g.map(sns.barplot,'Status','Transaction amount')

还是像这样

import matplotlib.pyplot as plt
cardschemes = transactions['Processed Card Scheme'].values.unique()
fig, axs = plt.subplots(ncols=len(cardschemes))
for i, scheme in enumerate(cardschemes):
    sns.barplot(x="Status", 
                y="Transaction amount", 
                data=transactions[transactions['Processed Card Scheme'] == scheme],
                ax=axs[i])

顺便说一句,如果你使用minimum reproducible example,回答问题会更容易

相关问题 更多 >