Python 在seaborn条纹图中绘制带色调的宽矩阵
我正在尝试使用stripplot绘制数据集。这是标题,共有25列:Python 在seaborn条纹图中绘制带色调的宽矩阵,python,seaborn,Python,Seaborn,我正在尝试使用stripplot绘制数据集。这是标题,共有25列: Labels Acidobacteria Actinobacteria Armatimonadetes Bacteroidetes 0 0 0 495 NaN 27859 1 1 0 1256 NaN
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数据框中,并可以使用以下方法绘制它:
def plot():
ax = sns.stripplot(data = df)
ax.set(xlabel='Bacteria',ylabel='Abundance')
plt.setp(ax.get_xticklabels(),rotation=45)
plt.show()
生产
我想设置颜色以反映“标签”列。当我尝试时:
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
所以我想出来了。我必须通过堆叠和重新索引来重新排列数据:
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)
哪些产出:
index Bacteria Abundance
0 0 Acidobacteria 0.000000
1 0 Actinobacteria 0.005003
2 0 Armatimonadetes 0.000000
3 0 Bacteroidetes 0.281586
接下来,我必须创建一个新列,为每个数据点指定标签:
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()
生产
谢谢你的帮助 我想扩展一下你的答案,实际上我会压缩它,因为这可以在一行中完成:
# 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 Bacteria Abundance
0 0 Acidobacteria 0.0
1 0 Actinobacteria 495.0
2 0 Bacteroidetes 27859.0
3 1 Acidobacteria 0.0
4 1 Actinobacteria 1256.0
5 1 Bacteroidetes 46582.0
6 0 Acidobacteria 0.0
7 0 Actinobacteria 1081.0
8 0 Bacteroidetes 23798.0
9 1 Acidobacteria 0.0
10 1 Actinobacteria 2523.0
11 1 Bacteroidetes 35088.0
12 0 Acidobacteria 0.0
13 0 Actinobacteria 1383.0
14 0 Bacteroidetes 19338.0
回答得好!但要得到一个混合了色调颜色的宽格式矩阵需要做很多工作……我希望Seaborn能对宽格式图形有更多的支持。