首页 > 解决方案 > seaborn swarmplot 中的水平图

问题描述

情况
我有以下熊猫数据集:

|user_id|total|is_fat|
|-------|-----|------|
|1      |100  |1     |
|2      |150  |0     |
|3      |400  |1     |
|4      |500  |1     |
|5      |10   |0     |

其中总的元素是整数,is_fat 的元素是字符串。

我用 表示上述数据集df

然后运行以下代码:

import seaborn as sos
sns.swarmplot(x = 'total', y ='is_fat', data = df)

现在我期望的图表看起来像: 在此处输入图像描述

问题 但是,输出图如下:

在此处输入图像描述

为什么?

搜索
如果我将 '1' 转换为 'fat' 并将 '0' 转换为 'not_fat',那么我会得到预期的图表。

标签: pythonseabornswarmplot

解决方案


我模拟了一些数据并更改is_fat为分类,如图所示:

import seaborn as sns
import pandas as pd
import numpy as np

df = pd.DataFrame({"total":abs(np.random.randn(100)), "is_fat": [1,0]*50})
df.is_fat = df.is_fat.astype("category")
sns.swarmplot(x = 'total', y ='is_fat', data = df)

这产生了下图: 在此处输入图像描述

我希望这有帮助。


推荐阅读