【问题标题】:Simple line plots using seaborn使用 seaborn 的简单线图
【发布时间】:2015-09-13 04:08:02
【问题描述】:

我正在尝试使用 seaborn (python) 绘制 ROC 曲线。 使用 matplotlib 我只需使用函数plot:

plt.plot(one_minus_specificity, sensitivity, 'bs--')

其中one_minus_specificitysensitivity 是两个配对值列表。

seaborn 中是否有简单的绘图功能对应物?我查看了图库,但没有找到任何简单的方法。

【问题讨论】:

    标签: python matplotlib plot seaborn roc


    【解决方案1】:
    import matplotlib.pyplot as plt
    import seaborn as sns
    
    sns.set()
    plt.plot(one_minus_specificity, sensitivity) # x,y
    

    【讨论】:

      【解决方案2】:

      可以使用 seaborn.lineplot() 完成此操作,但它涉及将 numpy 数组转换为 pandas 数据帧的一些额外工作。这是一个完整的例子:

      # imports
      import seaborn as sns
      import numpy as np
      import pandas as pd
      
      # inputs
      In [41]: num = np.array([1, 2, 3, 4, 5])
      In [42]: sqr = np.array([1, 4, 9, 16, 25])
      
      # convert to pandas dataframe
      In [43]: d = {'num': num, 'sqr': sqr}
      In [44]: pdnumsqr = pd.DataFrame(d)
      
      # plot using lineplot
      In [45]: sns.set(style='darkgrid')
      In [46]: sns.lineplot(x='num', y='sqr', data=pdnumsqr)
      Out[46]: <matplotlib.axes._subplots.AxesSubplot at 0x7f583c05d0b8>
      

      我们得到以下情节:

      【讨论】:

        【解决方案3】:

        是的,您可以直接在 Seaborn 中执行相同的操作。这是通过 tsplot() 完成的,它允许单个数组作为输入,或者两个数组,其中另一个是“时间”,即 x 轴。

        import seaborn as sns
        
        data =  [1,5,3,2,6] * 20
        time = range(100)
        
        sns.tsplot(data, time)
        

        【讨论】:

        • tsplot 将被 lineplot 取代
        【解决方案4】:

        由于 seaborn 也使用 matplotlib 进行绘图,因此您可以轻松地将两者结合起来。如果您只想采用 seaborn 的样式,set_style 函数应该可以帮助您入门:

        import matplotlib.pyplot as plt
        import numpy as np
        import seaborn as sns
        
        sns.set_style("darkgrid")
        plt.plot(np.cumsum(np.random.randn(1000,1)))
        plt.show()
        

        结果:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-03-08
          • 2022-01-14
          • 2019-08-01
          • 2021-09-23
          • 2022-01-13
          • 1970-01-01
          相关资源
          最近更新 更多