在Matplotlib中实现视野图 [英] Implementing horizon charts in matplotlib

查看:31
本文介绍了在Matplotlib中实现视野图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在matplotlib中实现视野图(请参阅: http://square.github.com/立体主义/)

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

基本思想是您以窄纵横比显示时间序列,随着值的增加(超出 y 轴限制),它们会从底部以较深的颜色重新开始(想想旧的 Atari 游戏,当您会越过屏幕顶部并从底部弹出).

The basic idea is that you display a time series in narrow aspect ratio, and as values increase (beyond the y-axis limit), they start back up from the bottom in a darker color (think of old Atari games when you'd go past the top of the screen and pop out in the bottom).

我的基本方法是使用 ax.twinx() 将 y 数据划分为卡盘并在新轴上绘制每个垂直组并适当设置限制.

My basic approach is to divide the y-data into chucks and plot each vertical group on a new axes using ax.twinx() and setting the limits appropriately.

仅就正面或负面数据而言,这似乎运行良好.

For positive or negative data alone, this seems to be working well.

正面:

负面:

但出于某种原因,这两件事都搞砸了:

But for some reason, doing both screws up:

# setup the environment
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, np.pi*4, 137)
y = (2*np.random.normal(size=137) + x**2)

# reflect everything around the origin
xx = np.hstack([-1*x[::-1], x])
yy = np.hstack([-1*y[::-1], y])

# function to do the plot
def horizonPlot(ax, x, y, nfolds=3, inverty=False, color='CornflowerBlue'):
    axes = [ax]
    if inverty:
        ylims = np.linspace(y.min(), y.max(), nfolds + 1)[::-1]
    else:
        ylims = np.linspace(y.min(), y.max(), nfolds + 1)

    for n in range(1, nfolds):
        newax = axes[-1].twinx()
        axes.append(newax)

    for n, ax in enumerate(axes):        
        ax.fill_between(x, y, y2=ylims[n], facecolor=color, alpha=1.0/nfolds, zorder=n)
        ax.set_ylim([ylims[n], ylims[n+1]])
        ax.set_yticklabels([])
        ax.set_yticks([])

        if inverty:
            ax.invert_yaxis()

    ax.set_xlim([x.min(), x.max()])
    return fig

fig, baseax = plt.subplots(figsize=(6.5,1.5))
posax = baseax.twinx()
negax = posax.twinx()
fig = horizonPlot(posax, xx, np.ma.masked_less(yy, 0), inverty=False, color='DarkGreen')
fig = horizonPlot(negax, xx, np.ma.masked_greater(yy, 0), inverty=True,   color='CornflowerBlue')
for ax in fig.get_axes():
    ax.set_yticklabels([])

fig.tight_layout()
plt.show()

不良图表(请注意,正面缺少多层结构):

The bad chart (notice the lack of multiple layers on the positive side):

任何想法将不胜感激!

推荐答案

我实际上不知道为什么您的计算机无法正常工作,因为在我的计算机上它可以正常工作.但是,由于我对这种绘图真的很感兴趣,因此我尝试自己实现它而没有所有花哨的 twinx 东西.

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.

我只是在彼此之上绘制这些区域,因为这实际上是该绘制的重要内容.因此我不需要调整 alpha,它们只是相加.

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.

这篇关于在Matplotlib中实现视野图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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