如何更改 seaborn jointplot 的横向 kde 图的颜色

How to change the colours of the lateral kde plots of seaborn jointplot

我想创建一个使用标准颜色图 "Greens"sns.jointplot。这个标准颜色循环的输出对于横向 kde 估计密度来说太亮了。我更喜欢使用 "BuGn_r" 颜色图时获得的较深颜色。

有没有简单的方法让它们变暗?

import matplotlib.pyplot as plt
import numpy as np
import pandas as pn
import seaborn as sns

iris = sns.load_dataset("iris")
with sns.axes_style("white"):
    sns.set_palette("BuGn_r")
    g2 = sns.jointplot("sepal_width", "petal_length", data=iris,
                kind="kde", space=0)

with sns.axes_style("white"):
    sns.set_palette("Greens")
    g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
                    kind="kde", space=0)

查看下面的输出:

您可以在使用 kdeplot 创建线条和阴影区域后更改其颜色。边缘轴可以从 g3g3.ax_marg_xg3.ax_marg_y 访问。

您可以更改线条的颜色:

g3.ax_marg_x.lines[0].set_color()

阴影区域的颜色为:

g3.ax_marg_x.collections[0].set_facecolor()

当然,您可以将它们设置为任何有效的 matplotlib 颜色。或者,要获得您在第一个绘图中使用的确切颜色,您还可以在 g2 边距上使用 get_colorget_facecolor

您可以在下面的脚本中看到这一切:

import matplotlib.pyplot as plt
import numpy as np
import pandas as pn
import seaborn as sns

iris = sns.load_dataset("iris")
with sns.axes_style("white"):
    sns.set_palette("BuGn_r")
    g2 = sns.jointplot("sepal_width", "petal_length", data=iris,
                kind="kde", space=0)

    # Find out the face and line colours that BuGn_r uses
    facecolor = g2.ax_marg_x.collections[0].get_facecolor()
    linecolor = g2.ax_marg_x.lines[0].get_color()

    print facecolor 
    # [[ 0.0177624   0.4426759   0.18523645  0.25      ]]

    print linecolor
    # (0.017762399946942051, 0.44267590116052069, 0.18523645330877866)    

with sns.axes_style("white"):
    sns.set_palette("Greens")
    g3 = sns.jointplot("sepal_width", "petal_length", data=iris,
                    kind="kde", space=0)

    # Change the facecolor of the shaded region under the line
    g3.ax_marg_x.collections[0].set_facecolor(facecolor)
    g3.ax_marg_y.collections[0].set_facecolor(facecolor)

    # And change the line colour.
    g3.ax_marg_x.lines[0].set_color(linecolor)
    g3.ax_marg_y.lines[0].set_color(linecolor)

plt.show()

"BuGn_r"

"Greens" 修改颜色

使用jointplotcolor=参数。