【问题标题】:Dotted Seaborn distplot虚线 Seaborn 分布图
【发布时间】:2018-03-29 07:41:36
【问题描述】:

我有一个看起来像这样的海生分布图

我想把一些线条画成虚线。我怎样才能做到这一点? 我尝试以 2 种不同的方式使用线条样式并得到错误

#### approach 1
for x, m in x_list:
    sns.distplot(x, hist=False, label=m, linestyle='--')
#### approach 2
for x, m in x_list:
    sns.distplot(x, hist=False, label=m, kde_kws={'linestyle':'--'})

TypeError: distplot() got an unexpected keyword argument 'linestyle'

【问题讨论】:

    标签: python matplotlib jupyter-notebook seaborn


    【解决方案1】:

    使用kde_kws={'linestyle':'--'} 的第二种方法适用于seaborn 8.1。也许你想更新。

    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np
    
    x_list = [np.random.rayleigh(1+i/2., size=35) for i in range(4)]
    
    for x in x_list:
        sns.distplot(x, hist=False, kde_kws={'linestyle':'--'})
    
    plt.show()
    

    【讨论】:

    • 是的,我有 7.1。由于限制,我无法升级,但感谢指针
    • Seaborn 没有任何限制。它是纯蟒蛇。您可以使用 pip 安装或从 GitHub 下载。
    【解决方案2】:

    您可以使用 ax.lines 从 seaborn distplot 返回的轴中获取 Line2D 对象的列表。然后,使用set_linesytle 遍历这些对象以设置所需的线型。

    例如:

    import seaborn as sns
    
    x = np.random.randn(100)
    ax = sns.distplot(x, hist=False)
    
    [line.set_linestyle("--") for line in ax.lines] 
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-21
      • 2021-03-08
      • 2021-08-09
      • 1970-01-01
      • 2021-11-03
      • 2020-08-03
      • 2019-04-29
      相关资源
      最近更新 更多