Create a gist now

Instantly share code, notes, and snippets.

@phobson /
Last active Mar 14, 2017

Milla's version of horizons in python
import numpy as np
import matplotlib.pyplot as plt
def makeLayer(y, height):
if y > 0:
if y - height >= 0:
pos = height
y -= pos
pos = y
elif y < 0:
if y + height <= 0:
neg = height
y += neg
neg = -y
return pos, neg
def horizonPlot(x, y, nfolds=3, negcolor='CornflowerBlue', poscolor='DarkGreen'):
fig, ax = plt.subplots(figsize=(17,3), dpi=300)
alpha = 1.0/nfolds
vlayer = np.vectorize(makeLayer)
height = np.ceil(np.abs(y).max()/nfolds)
while (y != 0).any():
layer = vlayer(y,height)
y -= layer[0];
y += layer[1]
ax.fill_between(x, 0, layer[0], color=poscolor, alpha=alpha)
ax.fill_between(x, height-layer[1], height, color=negcolor, alpha=alpha)
ax.set_xlim([x.min(), x.max()])
ax.set_ylim([0, height])
return fig
if 1:
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])
fig = horizonPlot(xx, yy, nfolds=3)
miroli commented Mar 14, 2017 edited

Thanks a lot for sharing this. I'm trying to create a mirrored version (as opposed to the offset version, see this block for clarification: and I guess you just have to tweak the if statements in makeLayer but I can't seem to figure it out. Any thoughts @phobson?

phobson commented Mar 14, 2017 edited


What I would do is plot the neg/pos data on two separate axes

fig, axneg = plt.subplots()
axpos = axneg.twinx()
axpos.set_ylim([0, global_abs_max])
axneg.set_ylim([-globabl_abs_max, 0])

miroli commented Mar 14, 2017

Thank you @phobson !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment