简体   繁体   中英

Horizontal barplot with offset in seaborn

My dataset is like this, where the data points in each row or column are pandas objects. 在此处输入图像描述

Here is the dataset: https://github.com/aebk2015/multipleboxplot.git

I want to have bar plots for each of the columns "Location" (P1 -P14) for each categories (92A11, 92B11, 82B11); something like this: 在此处输入图像描述

I have tried something like this and i can have a bar plots for each individual Pi (i=1...14) but not only is it a laborious, it does not look what I want:

fig, ax = plt.subplots(2, 3, figsize=(8,2))

sns.stripplot(data=df.loc[7]['92A11'].split(','), dodge=True, linewidth=1, ax=ax[0,0], color='black', jitter=False, orient='h')
sns.violinplot(data=df.loc[7]['92A11'].split(','),  ax=ax[0,0], color='orange', orient='h')

sns.stripplot(data=df.loc[7]['92B11'].split(','), dodge=True, linewidth=1, ax=ax[0,1], color='black', jitter=False, orient='h')
sns.violinplot(data=df.loc[7]['92B11'].split(','),  ax=ax[0,1], color='orange', orient='h')

sns.stripplot(data=df.loc[7]['82B11'].split(','), dodge=True, linewidth=1, ax=ax[0,2], color='black', jitter=False, orient='h')
sns.violinplot(data=df.loc[7]['82B11'].split(','),  ax=ax[0,2], color='orange', orient='h')

sns.stripplot(data=df.loc[6]['92A11'].split(','), dodge=True, linewidth=1, ax=ax[1,0], color='black', jitter=False, orient='h')
sns.violinplot(data=df.loc[6]['92A11'].split(','),  ax=ax[1,0], color='orange', orient='h')

sns.stripplot(data=df.loc[6]['92B11'].split(','), dodge=True, linewidth=1, ax=ax[1,1], color='black', jitter=False, orient='h')
sns.violinplot(data=df.loc[6]['92B11'].split(','),  ax=ax[1,1], color='orange', orient='h')

sns.stripplot(data=df.loc[6]['82B11'].split(','), dodge=True, linewidth=1, ax=ax[1,2], color='black', jitter=False, orient='h')
sns.violinplot(data=df.loc[6]['82B11'].split(','),  ax=ax[1,2], color='orange', orient='h')

ax[0,0].set_xlim(0,200)
ax[0,1].set_xlim(0,200)
ax[0,2].set_xlim(0,200)
ax[1,0].set_xlim(0,200)
ax[1,1].set_xlim(0,200)
ax[1,2].set_xlim(0,200)

ax[1,0].set_xlabel('92A11')
ax[1,1].set_xlabel('92A11')
ax[1,2].set_xlabel('92A11')

ax[0,0].set_ylabel('P8')
ax[1,0].set_ylabel('P7')
fig.tight_layout()

在此处输入图像描述

Seaborn works easiest with its data in long form. pd.melt can convert the 3 individual columns into a variable-name and value column. pd.explode can convert lists in cells into multiple rows.

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd

df = pd.read_csv('raw_data.csv')
df = df.rename(columns={df.columns[0]: 'Location'})

df_melted = df.melt(id_vars='Location', var_name='Dataset', value_name='Datapoint')
df_melted['Datapoint'] = df_melted['Datapoint'].map(
    lambda s: [np.nan] if s == '0' else [float(v) for v in s.split(',')])
df_exploded = df_melted.explode(column='Datapoint')

g = sns.catplot(data=df_exploded, col='Dataset', kind='box', x='Datapoint', y='Location')
g.set(xlabel='', ylabel='') # remove x and y label

来自宽数据帧的 sns.catplot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM