繁体   English   中英

在 seaborn 中具有偏移量的水平条形图

[英]Horizontal barplot with offset in seaborn

我的数据集是这样的,每行或每列的数据点是pandas个对象。 在此处输入图像描述

这是数据集: https://github.com/aebk2015/multipleboxplot.git

我想为每个类别(92A11、92B11、82B11)的每个列“位置”(P1 -P14)绘制条形图; 是这样的: 在此处输入图像描述

我已经尝试过这样的事情,我可以为每个单独的 Pi (i=1...14) 绘制条形图,但这不仅费力,而且看起来也不是我想要的:

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 最容易处理其长格式数据。 pd.melt可以将 3 个单独的列转换为变量名称和值列。 pd.explode可以将单元格中的列表转换为多行。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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