简体   繁体   English

在 Seaborn 条形图中对列进行分组

[英]Grouping columns in Seaborn barplots

I have a data frame consisting of 32 columns and I wish to generate a bar plot using Seaborn with 8 different groups, each consisting of 4 columns, and with each group assigned a unique color.我有一个由 32 列组成的数据框,我希望使用 Seaborn 生成一个条形图,其中包含 8 个不同的组,每个组由 4​​ 列组成,并且每个组都分配了一种独特的颜色。 Each group of 4 columns represents a repeated sampling for a particular (8 different) experimental condition (they are technical replicates) and I would like to show consistency (in a purely visual manner) of sampling for each of the 8 experimental conditions.每组 4 列代表针对特定(8 种不同)实验条件(它们是技术重复)的重复采样,我想展示 8 种实验条件中每一种的采样一致性(以纯视觉方式)。

Column structure is as follows: Index |列结构如下:索引| Condition1_replicate1 | Condition1_replicate1 | Condition1_replicate2 ... Condition8_replicate3 | Condition1_replicate2 ... Condition8_replicate3 | Condition8_replicate4 Condition8_replicate4

Any help will be much appreciated!任何帮助都感激不尽!

You could create a color palette that repeats 8 colors 4 times:您可以创建一个将 8 种颜色重复 4 次的调色板:

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

df = pd.DataFrame(np.random.rand(100, 32), columns=[f'Cond{i}_r{j}' for i in range(1, 9) for j in range(1, 5)])

palette = np.repeat(sns.color_palette('Set1', 8), 4, axis=0)

fig, ax = plt.subplots(figsize=(12, 3))
sns.barplot(data=df, palette=palette, ax=ax)
ax.set_xticks([])

plt.show()

具有重复颜色的 sns.barplot

PS: You can create a variation for the palette, eg PS:您可以为调色板创建一个变体,例如

colors = sns.color_palette('Set2', 8)
palette = [color_j for color_i in colors for color_j in sns.dark_palette(color_i, 7)[-4:]]

fig, ax = plt.subplots(figsize=(12, 3))
sns.barplot(data=df, palette=palette, ax=ax)
ax.set_xticks(np.arange(1.5, 4 * 8, 4))
ax.set_xticklabels([f'Condition {i}' for i in range(1, 9)])
plt.show()

具有不同调色板的 sns.barplot

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

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