【问题标题】:Set edgecolor on seaborn jointplot在 seaborn 联合图上设置边缘颜色
【发布时间】:2020-07-30 02:23:26
【问题描述】:

我可以通过传入 hist_kws 参数来为 seaborn 直方图设置边缘颜色:

sns.distplot(ad_data["Age"], kde = False, bins = 35, hist_kws = {"ec":"black"})

但是,我无法为 seaborn 联合图中的直方图设置类似的边缘颜色。它不接受 hist_kws 参数或任何其他类似的参数来设置边缘颜色。我在文档中找不到任何解决此问题的内容。任何帮助将不胜感激。

作为参考,我使用的是 seaborn 0.9 和 matplotlib 3.1。

【问题讨论】:

  • 你可以使用edgecolor=代替dupe中显示的color=
  • @DavidG 不幸的是,它没有。 sns.jointplot("Age", "Area Income", data = ad_data, margin_kws={'edgecolor': 'green'}) 我使用上面的代码得到了一个 TypeError: distplot() got an unexpected keyword argument 'edgecolor' .但是,使用 sns.set 设置 sns 样式确实设置了 edgecolors。

标签: python-3.x matplotlib seaborn


【解决方案1】:

您需要在“marginal_kws”中添加一个“hist_kws”:

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

x = np.random.normal(np.repeat([2, 8, 7, 10], 1000), 1)
y = np.random.normal(np.repeat([7, 2, 9, 4], 1000), 1)

g = sns.jointplot(x=x, y=y, color='purple', alpha=0.1,
                  marginal_kws={'color': 'tomato', 'hist_kws': {'edgecolor': 'black'}})
plt.show()

在这种情况下,jointplotmarginal_kws 发送到distplot,而distplot 又将hist_kws 发送到matplotlib 的hist

同样,你也可以为distplot设置kde的参数:

g = sns.jointplot(x=x, y=y, kind='hex', color='indigo', 
                  marginal_kws={'color': 'purple', 'kde': True,
                                'kde_kws': {'color': 'crimson', 'lw': 1},
                                'hist_kws': {'ec': 'black', 'lw': 2}})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-15
    • 2013-09-16
    • 1970-01-01
    相关资源
    最近更新 更多