首页 > 解决方案 > 如何在此图中定位 Seaborn 颜色条?

问题描述

我用 iris 数据制作了 kde 图,但图例出现在图中。

在此处输入图像描述

如何将彩条移到最右边或最左边?

这是我的代码:

import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/iris.csv")
setosa = df.loc[df.Name == "Iris-setosa"]
virginica = df.loc[df.Name == "Iris-virginica"]

g = sns.JointGrid(x="SepalWidth", y="PetalLength", data=df)
sns.kdeplot(setosa.SepalWidth, setosa.SepalLength, cmap="Reds", cbar=True, 
        shade=True, shade_lowest=False, ax=g.ax_joint)
sns.kdeplot(virginica.SepalWidth, virginica.SepalLength, cmap="Blues", cbar=True,
        shade=True, shade_lowest=False, ax=g.ax_joint)
sns.distplot(setosa.SepalWidth, kde=False, color="r", ax=g.ax_marg_x)
sns.distplot(virginica.SepalWidth, kde=False, color="b", ax=g.ax_marg_x)
sns.distplot(setosa.SepalLength, kde=False, color="r", ax=g.ax_marg_y, vertical=True)
sns.distplot(virginica.SepalLength, kde=False, color="b", ax=g.ax_marg_y, vertical=True)
plt.show()

标签: pythonseaborncolorbar

解决方案


@Michael Tough Laksana 回答说位置是你可以改变它,但是图形区域变窄了。我敢于回答以获得其他答案。

cbar_kws={"use_gridspec":False, "location":"top"}

代码:

import matplotlib.pyplot as plt
import seaborn as sns

df = pd.read_csv("https://raw.githubusercontent.com/plotly/datasets/master/iris.csv")
setosa = df.loc[df.Name == "Iris-setosa"]
virginica = df.loc[df.Name == "Iris-virginica"]

g = sns.JointGrid(x="SepalWidth", y="PetalLength", data=df)
sns.kdeplot(setosa.SepalWidth, setosa.SepalLength, cmap="Reds", cbar=True, 
        shade=True, shade_lowest=False, ax=g.ax_joint, cbar_kws={"use_gridspec":False, "location":"top"})
sns.kdeplot(virginica.SepalWidth, virginica.SepalLength, cmap="Blues", cbar=True,
        shade=True, shade_lowest=False, ax=g.ax_joint,cbar_kws={"use_gridspec":False, "location":"right"})
sns.distplot(setosa.SepalWidth, kde=False, color="r", ax=g.ax_marg_x)
sns.distplot(virginica.SepalWidth, kde=False, color="b", ax=g.ax_marg_x)
sns.distplot(setosa.SepalLength, kde=False, color="r", ax=g.ax_marg_y, vertical=True)
sns.distplot(virginica.SepalLength, kde=False, color="b", ax=g.ax_marg_y, vertical=True)
plt.show()

“上”和“右”

在此处输入图像描述

“左”和“左”

在此处输入图像描述


推荐阅读