如何使用seaborn仅绘制y轴

时间:2020-04-11 14:48:29

标签: matplotlib seaborn

我是机器学习的新手,我想比较预测值和实际值,现在我想比较图中的这两个数据,以查看两个值是否相同。

数据:

[-0.26112159,  1.84683522,  2.23912728,  1.58848056,  1.28589823,
        2.01355579, -0.144594  ,  0.8845673 , -0.19764173,  0.00837658,
        1.3515489 ,  0.18876488,  1.07088203,  1.11333346,  0.99854107,
        1.67141781,  1.74938417,  1.17907989,  1.57017018,  2.04269495,
       -0.10662102,  0.96283466, -0.01117658,  0.01610438,  1.31111783,
       -0.08608504, -0.09535655, -0.0227967 ,  1.82867539,  1.4492189 ]

这是我的A和B数据集的数据样本

我想这样画,

enter image description here

我更喜欢使用seaborn

3 个答案:

答案 0 :(得分:2)

Seaborn提供了一些详尽的功能来显示数据。在内部,它在很大程度上取决于matplotlib。由于所要求的地块不属于Seaborn excells类别,因此直接使用matplotlib的散点似乎更合适:

import numpy as np
from matplotlib import pyplot as plt

A = [-0.26112159, 1.84683522, 2.23912728, 1.58848056, 1.28589823,
     2.01355579, -0.144594, 0.8845673, -0.19764173, 0.00837658,
     1.3515489, 0.18876488, 1.07088203, 1.11333346, 0.99854107,
     1.67141781, 1.74938417, 1.17907989, 1.57017018, 2.04269495,
     -0.10662102, 0.96283466, -0.01117658, 0.01610438, 1.31111783,
     -0.08608504, -0.09535655, -0.0227967, 1.82867539, 1.4492189]
B = A
x = np.arange(len(A))
plt.scatter(x - 0.2, A, marker='o', color='tomato', label='Dataset A')
plt.scatter(x + 0.2, B, marker='o', color='deepskyblue', label='Dataset B')
plt.legend()

plt.show()

resulting plot

要使用叉号作为标记,请使用marker='x'marker='+'

通过Seaborn绘制swarmplot(非常类似于stripplot):

import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns

# A, B = ...
sns.swarmplot(x=np.repeat(['Dataset A', 'Dataset B'], len(A)), y=np.concatenate([A, B]))

plt.show()

swarmplot

kde plot可用于比较统计分布。这是一个示例,其中添加了一些噪声以使两个集合略有不同:

import numpy as np
from matplotlib import pyplot as plt
import seaborn as sns

# A = ...
A = np.array(A)
B = A + np.random.normal(0,.1, len(A))
sns.kdeplot(A, label='Dataset A')
sns.kdeplot(B, label='Dataset B')

plt.show()

kde plot

答案 1 :(得分:1)

如果您坚持使用Seaborn,则以下是一种方法。但是,stripplot非常适合这种情况,因为您不必显式传递x值。

import seaborn as sns
sns.set()

sns.scatterplot(range(len(A)), A, marker='x', color='orange', label='Dataset A')
sns.scatterplot(range(len(A)), A+0.1, marker='x', color='blue', label='Dataset B')

enter image description here

答案 2 :(得分:0)

只是想出一种方法,请让我知道是否有更好的方法可以比较两个数组。

fig,_ax=plt.subplots(1,2,figsize=(15,10))
sns.stripplot(y=predictions,ax=_ax[0])
sns.stripplot(y=y_test,ax=_ax[1],color='red')