mal*_*ave 3 python matplotlib seaborn
我正在使用seaborn按照此答案中的描述创建具有边际分布的kdeplot 。我对代码进行了一些修改,以实现以下目的:
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()
Run Code Online (Sandbox Code Playgroud)
无法以黑白打印。我如何才能以特殊方式(点划线/点划线/ ...)打印kdeplot和distplot线,以使其在黑白打印时能够区分?
任何sns.kdeplot无法识别的关键字都会传递到plt.contour()或plt.contourf()。在你的情况下是contourf,所以你可以传递关键字linestyles(注意复数)。sns.distplot有一个名为 的关键字kde_kws,它接受传递给 的关键字字典plt.plot。在这种情况下,您可以使用lsor linestyle(注意单数)。下面是一个完整的例子:
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()
Run Code Online (Sandbox Code Playgroud)
结果如下:
要使用不同的线型显示kde图的线条,请使用linestyle参数,该参数将传递给matplotlib的plot函数。
sns.kdeplot(setosa.sepal_width, color="r", ax=g.ax_marg_x, linestyle="--")
Run Code Online (Sandbox Code Playgroud)
将此参数提供给通过distplot您生成的kde图,可以使用该kde_kws参数。
sns.distplot(..., kde_kws={"linestyle":"--"})
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有任何理由在这里使用distplot。
对于2D情况,linestyle参数无效。2D kdeplot是contour图。因此,您需要使用contour的linestyles(而不是s)参数。
sns.kdeplot(, linestyles="--")
Run Code Online (Sandbox Code Playgroud)
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()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3621 次 |
| 最近记录: |