Implementing horizon charts in matplotlib

后端 未结 1 742
离开以前
离开以前 2021-02-20 13:30

I\'m trying to implement horizon charts in matplotlib (see: http://square.github.com/cubism/)

The basic idea is that you display a time series in narrow aspect ratio, an

1条回答
  •  心在旅途
    2021-02-20 13:56

    I actually do not know, why yours is not working, because on my computer it works fine. But since I am really interested in this plotting, I tried to implement it on my own without all this fancy twinx stuff.

    I just plot these areas on top of eachother, since this is actually the great thing about the plot. Thus I do not need to adjust the alpha, they just add up.

    import numpy as np
    from matplotlib.pyplot import *
    
    def layer(y,height):
        neg=0.0;pos=0.0
        if y>0:
            if y-height>=0:
                pos=height
                y-= pos
            else : 
                pos = y
        elif y<0:
            if y+height<=0:
                neg=height
                y += neg
            else : 
                neg = -y
        return pos,neg
    
    def horizonPlot(x,y,height=50.0,colors=['CornflowerBlue','DarkGreen']):
        alpha = .10
        vlayer = np.vectorize(layer)
        while (y != 0).any():
            l = vlayer(y,height)
            y -= l[0];y += l[1]
            fill_between(x,0,l[0],color=colors[0], alpha=alpha)
            fill_between(x,height-l[1],height,color=colors[1], alpha=alpha)
    
    def main():
        x = np.linspace(0, np.pi*4, 137)
        y = (2*np.random.normal(size=137) + x**2)
        xx = np.hstack([-1*x[::-1], x])
        yy = np.hstack([-1*y[::-1], y])
        horizonPlot(xx,yy)
        show()
    

    This looks like the following on my machine. Hope it works on yours, but I just use basic plotting methods.

    enter image description here

    0 讨论(0)
提交回复
热议问题