Created
          March 1, 2013 23:05 
        
      - 
      
- 
        Save phobson/5068667 to your computer and use it in GitHub Desktop. 
    Trying to get a decent horizon implementation with matplotlib
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # 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() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment