Dean
Dean

Reputation: 129

How do I position Seaborn colorbar in this plot?

I made kde plot with iris data but the legends are appearing within the plot.

enter image description here

How do I move the color bars far right or on the left?

Here is my code:

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()

Upvotes: 1

Views: 2588

Answers (2)

r-beginners
r-beginners

Reputation: 35115

@Michael Tough Laksana replied that the position is You can change it, but the graph area becomes narrower. I dare to answer to get other answers.

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

code:

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()

"top" and "right"

enter image description here

"left" and "left"

enter image description here

Upvotes: 3

Michael Teguh Laksana
Michael Teguh Laksana

Reputation: 336

Try to put the following into the sns.kdeplot parameter:

cbar_kws = dict(use_gridspec=False,location="left")

Upvotes: 1

Related Questions