python - 如何在此图中定位 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()
解决方案
@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()
“上”和“右”
“左”和“左”
推荐阅读
- cypress - 在 Cypress 中运行时应用程序抖动或抖动?
- java - 将 GMT+0 时间转换为 Android 上的本地时间,并以字符串形式给出 Hours/Min
- html - 用 \n 替换 enter 按键事件
- scala - 通过 Scala/Spark 列出 Amazon S3 中的对象
- atlassian-sourcetree - SourceTree 不会停止用密码对话框来烦我
- azure - Azure ADB2C cookie 大小,如何减小?
- javascript - 如何使用正则表达式匹配 ctx.url,如 ''/?id=xxx'' 和 koa 中的“/”?
- slack - 通过发布请求使用 Slack 机器人发送频道警报
- javascript - 删除突出显示的蓝色矩形以在织物 js 中进行选择
- c# - 触发器执行的次数