seaborn Swarmlot水平图

2024-04-19 16:12:34 发布

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

形势 我有以下数据集:

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

其中,total的元素是整数,is\u fat的元素是字符串。你知道吗

我用df表示上面的数据集。你知道吗

那就跑吧

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

现在我期望的图表是 enter image description here

问题 但是,输出图如下所示:

enter image description here

为什么?你知道吗

搜索
如果我将“1”转换为“fat”,将“0”转换为“not\u fat”, 然后得到期望的图。你知道吗


Tags: 数据字符串importid元素dfisas
1条回答
网友
1楼 · 发布于 2024-04-19 16:12:34

我模拟了一些数据并将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)

这产生了下图: enter image description here

我希望这有帮助。你知道吗

相关问题 更多 >