python - 使用 Seaborn 绘制 numpy 数组

标签 python numpy matplotlib machine-learning seaborn

我正在使用 python 2.7。我知道这将是非常基础的,但是我真的很困惑,我想更好地了解 seaborn。

我有两个 numpy 数组 Xy,我想使用 Seaborn 来绘制它们。

这是我的 X numpy 数组:

[[ 1.82716998 -1.75449225]
 [ 0.09258069  0.16245259]
 [ 1.09240926  0.08617436]]

这里是 y numpy 数组:

[ 1. -1.  1. ]

如何根据 y 数组中的类标签成功绘制数据点?

谢谢,

最佳答案

您可以使用 seaborn 函数绘制图形。执行 dir(sns) 以查看所有图。这是您在 sns.scatterplot 中的输出。您可以查看 api 文档 here或带图的示例代码 here

import seaborn as sns 
import pandas as pd

df = pd.DataFrame([[ 1.82716998, -1.75449225],
 [ 0.09258069,  0.16245259],
 [ 1.09240926,  0.08617436]], columns=["x", "y"])

df["val"] = pd.Series([1, -1, 1]).apply(lambda x: "red" if x==1 else "blue")


sns.scatterplot(df["x"], df["y"], c=df["val"]).plot()

给予

enter image description here 这正是您想要的输入输出吗?

您可以使用 pyplot 来完成,只需导入 seaborn 即可更改 pyplot 颜色和绘图方案

import seaborn as sns 

import matplotlib.pyplot as plt
fig, ax = plt.subplots()

df = pd.DataFrame([[ 1.82716998, -1.75449225],
 [ 0.09258069,  0.16245259],
 [ 1.09240926,  0.08617436]], columns=["x", "y"])
df["val"] = pd.Series([1, -1, 1]).apply(lambda x: "red" if x==1 else "blue")
ax.scatter(x=df["x"], y=df["y"], c=df["val"])
plt.plot()

这是一个stackoverflow post对 sns.lmplot 做同样的事情

关于python - 使用 Seaborn 绘制 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52570086/

相关文章:

python - 有没有办法从 ElementTree 元素中获取逐字字符串?

python - 使用pandas写入和读取3D数据

python - matplotlib.pyplot theta 网格厚度

python - 以 numpy 行索引为标记的 Matplotlib 散点图

python - 如何使用 python scrapy 提取 (href, alt) 对

php - 如何在 Unix/Linux 中为文件夹中的所有文件添加 .xml 扩展名

python - Python 3.5 支持的 dask 版本是什么?

python - 如何有效地 vstack 一系列大型 numpy 数组 block ?

python - Numpy 将一维数组打印为列

python - 将大数据数组压缩为 PNG 文件