matplotlib中的频率轨迹 [英] frequency trail in matplotlib

查看:101
本文介绍了matplotlib中的频率轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究异常值检测.布伦丹·格雷格(Brendan Gregg)有一个非常好的文章,我特别是他的可视化效果引起了人们的兴趣.他使用的一种方法是频率跟踪. /p>

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

该图基于以下答案: https://stackoverflow.com/a/4152016/948369

现在,正如布伦丹(Brendan)所述,我的问题是,我有一条连续的线遮盖了异常值(我简化了输入值,以便您仍然可以看到它们):

对将不存在的值设置为非连续"行有帮助吗?

解决方案

我会坚持使用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()

I'm looking into outliers detection. Brendan Gregg has a really nice article and I'm especially intrigued by his visualizations. One of the methods he uses are frequency trails.

I'm trying to reproduce this in matplotlib using this example. Which looks like this:

And the plot is based on this answer: https://stackoverflow.com/a/4152016/948369

Now my issue is, like described by Brendan, that I have a continuous line that masks the outlier (I simplified the input values so you can still see them):

Any help on making the line "non-continuous" for non existent values?

解决方案

I would stick with a flat 2D plot and displace each level by a set vertical amount. You'll have to play the the levels (in the code below I called it displace) to properly see the outliers, but this does a pretty good job at replicating your target image. The key, I think, is to set the "zero" values to None so pylab does not draw them.

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()

这篇关于matplotlib中的频率轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆