用seaborn stripp绘制带色调的宽矩阵

2024-10-01 13:45:09 发布

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

我试图用stripplot绘制数据集。这是标题(共有25列):

    Labels  Acidobacteria  Actinobacteria  Armatimonadetes  Bacteroidetes  
0       0              0             495              NaN          27859   
1       1              0            1256              NaN          46582
2       0              0            1081              NaN          23798   
3       1              0            2523              NaN          35088   
4       0              0            1383              NaN          19338  

我将此数据集存储在pandas数据框中,并可以使用以下方式绘制:

^{pr2}$

产生this plot。在

我想设置色调以反映'Labels'列。当我尝试:

sns.stripplot(x=df.columns.values.tolist(),y=df,data=df,hue='Labels') 

我得到:

ValueError: cannot copy sequence with size 26 to array axis with dimension 830

Tags: 数据标题pandasdflabelswith方式绘制
2条回答

所以我想出来了。我不得不通过叠加和重新索引来重新排列数据:

cols = df.columns.values.tolist()[3:]
stacked = df[cols].stack().reset_index()
stacked.rename(columns={'level_0':'index','level_1':'Bacteria',0:'Abundance'},inplace=True)

哪些输出:

^{pr2}$

接下来,我必须创建一个新列,为每个数据点分配标签:

label_col = np.array([[label for _ in range(len(cols))] for label in df['Labels']])
label_col = label_col.flatten()

stacked['Labels'] = label_col

所以现在:

   index         Bacteria  Abundance  Labels
0      0    Acidobacteria   0.000000       0
1      0   Actinobacteria   0.005003       0
2      0  Armatimonadetes   0.000000       0
3      0    Bacteroidetes   0.281586       0
4      0       Chlamydiae   0.000000       0

然后画出:

def plot():
    ax = sns.stripplot(x='Bacteria',y='Abundance',data=stacked,hue='Labels',jitter=True)
    ax.set(xlabel='Bacteria',ylabel='Abundance')
    plt.setp(ax.get_xticklabels(),rotation=45)
    plt.show()
plot()

产生this graph。在

谢谢你的帮助!在

我想扩展一下你的答案(实际上我会压缩它),因为这可以在“一行代码”中完成:

# To select specific columns:
cols = ["Acidobacteria", "Actinobacteria", "Armatimonadetes", "Bacteroidetes"]
df.set_index("Labels")[cols]\
    .stack()\
    .reset_index()\
    .rename(columns={'level_1':'Bacteria', 0:'Abundance'})

# If you want to stack all columns but "Labels", this is enough:
df.set_index("Labels")\
    .stack()\
    .reset_index()\
    .rename(columns={'level_1':'Bacteria', 0:'Abundance'})

避免重新创建"Labels"列的技巧是在堆栈之前将其设置为index。在

输出:

^{pr2}$

相关问题 更多 >