如何格式化数据以用于seaborn

时间:2017-08-30 12:36:35

标签: python matplotlib seaborn

我计算了文档距离,并在sklearn中使用MDS用matplotlib绘制它们。我想用seaborn(pairplot)绘制它们,但不知道如何翻译MDS数据,以便seaborn可以读取它。

HAL_SPI_Transmit_IT

2 个答案:

答案 0 :(得分:2)

the documentation for pairplot()中所述,此函数需要一个长格式数据框,其中每列是一个变量,每一行都是一个观察点。 最简单的方法是使用Pandas来构造这个数据帧(虽然我相信一个numpy数组可以工作)。

长格式数据框的行数与观察值一样多,每列都是变量。 seaborn的强大功能是使用分类列来分割数据帧是不同的组。

在您的情况下,数据框可能如下所示:

    X           Y           label
0   0.094060    0.484758    Label_00
1   0.375537    0.150206    Label_00
2   0.215755    0.796629    Label_02
3   0.204077    0.921016    Label_01
4   0.673787    0.884718    Label_01
5   0.854112    0.044506    Label_00
6   0.225218    0.552961    Label_00
7   0.668262    0.482514    Label_00
8   0.935415    0.100438    Label_00
9   0.697016    0.633550    Label_01
(...)

您可以将其传递给pairplot,如此:

sns.pairplot(data=df, hue='label')

enter image description here

答案 1 :(得分:1)

作为对Diziet Asahi的回应的补充,下面是创建DataFrame的简约代码:

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

data = {'col1':[1, 1, 1 ,1 ,1 ,1 ,12, 3, 4,5], 'col2':[1, 1, 1 ,1 ,1 ,1 ,12, 3, 4,5]}
df = pd.DataFrame(data)
sns.violinplot(data=df, palette="Pastel1")
plt.show()

这是此代码的结果: enter image description here

在这里,您可以找到other ways to build a Panda DataFrame