簡體   English   中英

如何在此 plot 中的 position Seaborn 顏色條?

[英]How do I position Seaborn colorbar in this plot?

我用虹膜數據制作了 kde plot,但圖例出現在 plot 中。

在此處輸入圖像描述

如何將彩條移到最右邊或最左邊?

這是我的代碼:

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

@Michael Tough Laksana 回答說 position 是你可以改變它,但圖形區域變得更窄。 我敢於回答以獲得其他答案。

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

“上”和“右”

在此處輸入圖像描述

“左”和“左”

在此處輸入圖像描述

嘗試將以下內容放入sns.kdeplot參數中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM