【问题标题】:frequency trail in matplotlibmatplotlib 中的频率轨迹
【发布时间】:2023-03-10 04:41:01
【问题描述】:

我正在研究异常值检测。 Brendan Gregg 有一个非常好的article,我对他的可视化特别感兴趣。他使用的方法之一是frequency trails

我正在尝试使用 this 示例在 matplotlib 中重现此内容。看起来像这样:

情节是基于这个答案:https://stackoverflow.com/a/4152016/948369

现在我的问题是,就像 Brendan 所描述的那样,我有一条连续的线来掩盖异常值(我简化了输入值,以便您仍然可以看到它们):

对于使不存在的值的行“不连续”有什么帮助吗?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    Seaborn 也提供了一个非常简洁的例子:

    然而,他们称之为欢乐/山脊情节:https://seaborn.pydata.org/examples/kde_ridgeplot.html

    #!/usr/bin/python
    import numpy as np
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    sns.set(style="white", rc={"axes.facecolor": (0, 0, 0, 0)})
    
    # Create the data
    rs = np.random.RandomState(1979)
    x = rs.randn(500)
    g = np.tile(list("ABCDEFGHIJ"), 50)
    df = pd.DataFrame(dict(x=x, g=g))
    m = df.g.map(ord)
    df["x"] += m
    
    # Initialize the FacetGrid object
    pal = sns.cubehelix_palette(10, rot=-.25, light=.7)
    g = sns.FacetGrid(df, row="g", hue="g", aspect=15, size=.5, palette=pal)
    
    # Draw the densities in a few steps
    g.map(sns.kdeplot, "x", clip_on=False, shade=True, alpha=1, lw=1.5, bw=.2)
    g.map(sns.kdeplot, "x", clip_on=False, color="w", lw=2, bw=.2)
    g.map(plt.axhline, y=0, lw=2, clip_on=False)
    
    # Define and use a simple function to label the plot in axes coordinates
    def label(x, color, label):
        ax = plt.gca()
        ax.text(0, .2, label, fontweight="bold", color=color, 
                ha="left", va="center", transform=ax.transAxes)
    
    g.map(label, "x")
    
    # Set the subplots to overlap
    g.fig.subplots_adjust(hspace=-.25)
    
    # Remove axes details that don't play will with overlap
    g.set_titles("")
    g.set(yticks=[])
    g.despine(bottom=True, left=True)
    

    【讨论】:

      【解决方案2】:

      我会坚持使用平面 2D 绘图,并将每个级别替换一个设定的垂直量。您必须玩关卡(在下面的代码中,我称之为displace)才能正确查看异常值,但这在复制目标图像方面做得很好。我认为,关键是将“零”值设置为None,这样 pylab 就不会绘制它们。

      import numpy as np
      import pylab as plt
      import itertools
      
      k = 20
      X = np.linspace(0, 20, 500)
      Y = np.zeros((k,X.size))
      
      # Add some fake data
      MU = np.random.random(k)
      for n in xrange(k):
          Y[n] += np.exp(-(X-MU[n]*n)**2 / (1+n/3))
      Y *= 50
      
      # Add some outliers for show
      Y += 2*np.random.random(Y.shape)
      
      displace = Y.max()/4
      
      # Add a cutoff
      Y[Y<1.0] = None
      
      face_colors = itertools.cycle(["#D3D820", "#C9CC54", 
                                     "#D7DA66", "#FDFE42"])
      
      fig = plt.figure()
      ax = fig.add_subplot(111, axisbg='black')
      ax.xaxis.set_visible(False)
      ax.yaxis.set_visible(False)
      
      for n,y in enumerate(Y):
          # Vertically displace each plot
          y0 = np.ones(y.shape) * n * displace
          y1 = y + n*displace
      
          plt.fill_between(X, y0,y1,lw=1, 
                           facecolor=face_colors.next(),
                           zorder=len(Y)-n)  
      plt.show()
      

      【讨论】:

      • 看起来很漂亮,我稍后会试一试,然后给你反馈。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 1970-01-01
      • 1970-01-01
      • 2016-08-05
      相关资源
      最近更新 更多