【问题标题】:How to set the line style for each kdeplot in a jointgrid如何为jointgrid中的每个kdeplot设置线型
【发布时间】:2018-04-05 11:17:59
【问题描述】:

我正在使用 seaborn 创建具有边际分布的 kdeplot,如 this answer 中所述。我稍微修改了代码来给我这个:

import matplotlib.pyplot as plt
import seaborn as sns

iris = sns.load_dataset("iris")
setosa = iris.loc[iris.species == "setosa"]
virginica = iris.loc[iris.species == "virginica"]

g = sns.JointGrid(x="sepal_width", y="petal_length", data=iris)
sns.kdeplot(setosa.sepal_width, setosa.sepal_length, cmap="Reds",
        shade=False, shade_lowest=False, ax=g.ax_joint)
sns.kdeplot(virginica.sepal_width, virginica.sepal_length, cmap="Blues",
        shade=False, shade_lowest=False, ax=g.ax_joint)
sns.distplot(setosa.sepal_width, kde=True, hist=False, color="r", ax=g.ax_marg_x)
sns.distplot(virginica.sepal_width, kde=True, hist=False, color="b", ax=g.ax_marg_x)
sns.distplot(setosa.sepal_length, kde=True, hist=False, color="r", ax=g.ax_marg_y, vertical=True)
sns.distplot(virginica.sepal_length, kde=True, hist=False, color="b", ax=g.ax_marg_y, vertical=True)
plt.show()

这是不可能以黑白打印的。 如何让 seaborn 以特定样式(点/虚线/...)的方式打印 kdeplot 和 distplot 线,以使它们在黑白打印时可区分?

related questions 处理似乎支持这一点的其他类型的情节,但kdeplotdistplot 似乎不支持这一点。

【问题讨论】:

  • 您得到的答案为您提供了minimal reproducible example。然而,你不使用这个,而是使用一些没人可以运行的代码。
  • @ImportanceOfBeingErnest 好点。对其进行了编辑以包含一个更好的示例。
  • 你是如何让两个 kdeplots 出现在同一个图上的?我尝试了非常相似的代码,但一次只显示一个。

标签: python matplotlib seaborn


【解决方案1】:

边际

要以不同的线条样式显示 kde 绘图的线条,请使用 linestyle 参数,该参数将传递给 matplotlib 的绘图函数。

sns.kdeplot(setosa.sepal_width, color="r", ax=g.ax_marg_x, linestyle="--")

要将此参数提供给通过distplot 生成的kde 图,您可以使用kde_kws 参数。

sns.distplot(..., kde_kws={"linestyle":"--"})

但是,这里似乎没有任何理由使用 distplot。

联合 KDE

对于 2D 情况,linestyle 参数无效。 2D kdeplot 是 contour 图。因此,您需要使用contourlinestyles(不是s)参数。

sns.kdeplot(, linestyles="--")

完整代码

import matplotlib.pyplot as plt
import seaborn as sns

iris = sns.load_dataset("iris")
setosa = iris.loc[iris.species == "setosa"]
virginica = iris.loc[iris.species == "virginica"]

g = sns.JointGrid(x="sepal_width", y="petal_length", data=iris)

sns.kdeplot(setosa.sepal_width, setosa.sepal_length, cmap="Reds",
        shade=False, shade_lowest=False, ax=g.ax_joint, linestyles="--")
sns.kdeplot(virginica.sepal_width, virginica.sepal_length, cmap="Blues",
        shade=False, shade_lowest=False, ax=g.ax_joint, linestyles=":")

sns.kdeplot(setosa.sepal_width, color="r", legend=False,  
             ax=g.ax_marg_x, linestyle="--")
sns.kdeplot(virginica.sepal_width, color="b", legend=False, 
             ax=g.ax_marg_x, linestyle=":")
sns.kdeplot(setosa.sepal_length, color="r", legend=False, 
             ax=g.ax_marg_y, vertical=True, linestyle="--")
sns.kdeplot(virginica.sepal_length, color="b", legend=False, 
             ax=g.ax_marg_y, vertical=True, linestyle=":")
plt.show()

【讨论】:

    【解决方案2】:

    sns.kdeplot 无法识别的任何关键字都将传递给plt.contour()plt.contourf()。在您的情况下,它是contourf,因此您可以传递关键字linestyles(注意复数)。 sns.distplot 有一个名为kde_kws 的关键字,它接受传递给plt.plot 的关键字字典。在这种情况下,您可以使用lslinestyle(注意单数)。下面是一个完整的例子:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    iris = sns.load_dataset("iris")
    setosa = iris.loc[iris.species == "setosa"]
    virginica = iris.loc[iris.species == "virginica"]
    
    g = sns.JointGrid(x="sepal_width", y="petal_length", data=iris)
    sns.kdeplot(
        setosa.sepal_width, setosa.sepal_length, cmap="Greys",
        shade=False, shade_lowest=False, ax=g.ax_joint,
        linestyles='--'
    )
    sns.kdeplot(
        virginica.sepal_width, virginica.sepal_length, cmap="Greys",
        shade=False, shade_lowest=False, ax=g.ax_joint,
        linestyles=':'
    )
    sns.distplot(
        setosa.sepal_width, kde=True, hist=False, color="k",
        kde_kws=dict(ls ='--'), ax=g.ax_marg_x
    )
    sns.distplot(
        virginica.sepal_width, kde=True, hist=False, color="k",
        kde_kws=dict(ls=':'), ax=g.ax_marg_x
    )
    sns.distplot(
        setosa.sepal_length, kde=True, hist=False, color="k",
        kde_kws=dict(ls ='--'), ax=g.ax_marg_y, vertical=True
    )
    sns.distplot(
        virginica.sepal_length, kde=True, hist=False, color="k",
        kde_kws=dict(ls=':'), ax=g.ax_marg_y, vertical=True
    )
    plt.show()
    

    结果如下:

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2022-08-02
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      相关资源
      最近更新 更多