我正在使用 python 2.7。我知道这将是非常基础的,但是我真的很困惑,我想更好地了解 seaborn。
我有两个 numpy 数组 X
和 y
,我想使用 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()
给予
您可以使用 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/