如何为熊猫数据框构建并排箱线图

时间:2019-08-17 21:57:17

标签: python pandas matplotlib seaborn

男人和女人在标有“性别”的栏中。我想根据他们的幸福水平来画它们。 因此,一图两列一行。

我尝试提取每种性别:

=IF(ROWS(B$13:B13)<=$B$10,INDEX($A$2:$A$7,SMALL(IF(MMULT(IF(LEFT($B$2:$G$7,LEN($A$13))=$A$13,1,0),TRANSPOSE(COLUMN($B$2:$G$7))^0)>0,ROW($A$2:$A$7)-ROW($A$2)+1),ROWS(B$13:B13))),"")

![https://ibb.co/yqmWKkf]

2 个答案:

答案 0 :(得分:1)

  • Boxplots in python
  • 箱线图需要数字分量,因为它们是静态数据的可视化,专门用于传播。
  • 使用seaborn使您的绘图看起来更好

代码:

import pandas as pd
import matplotlib.pyplot as plt  # doesn't have color by hue
import seaborn as sns
import numpy as np  # for generating random data
import random  # for random gender selection

df = pd.DataFrame({'age': [x for x in np.random.randint(20, 70, 100)],
                   'feeling': [random.choice(['happy', 'sad']) for _ in range(100)], 
                   'gender': [random.choice(['male', 'female']) for _ in range(100)]})

enter image description here

sns.boxplot(y='age', x='feeling', data=df, hue='gender')
plt.show()

enter image description here

仅将groupby与分类数据一起使用:

df = pd.DataFrame({'feeling': [random.choice(['happy', 'sad|']) for _ in range(100)],
                   'gender': [random.choice(['male', 'female']) for _ in range(100)]})

df.groupby(['feeling','gender'])['gender'].count().plot(kind='bar')

enter image description here

备用数据-feeling作为数值:

df = pd.DataFrame({'feeling': [x for x in np.random.randint(0, 101, 100)],
                   'gender': [random.choice(['male', 'female']) for _ in range(100)]})

plt.figure(figsize=(8, 7))
sns.boxplot(y='feeling', x='gender', data=df)
plt.show()

enter image description here

答案 1 :(得分:0)

import pandas as pd
import matplotlib.pyplot as plt

我已经为您的数据框创建了一个虚构的样本。

data = [['men', 55], ['men', 77], ['women', 85],
        ['men', 70], ['women', 68], ['women', 64],
        ['men', 86], ['men', 64], ['women', 54],
        ['men', 43], ['women', 86],  ['women', 91]]

df = pd.DataFrame(data, columns = ['sex', 'happy'])

您可以:

df.boxplot(by=['sex'], sym ='', figsize = [6, 6])

它产生: 我想这就是您想要的。

Hapiness