frequency trail in matplotlib

matplotlib, python

Solution

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

Problem

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?

Original source

Related problems