示例#1
0
def csplot(series, *args, **kwargs):
    """
    Plots the series to the current :class:`ClimateSeriesPlot` subplot.
    If the current plot is not a :class:`ClimateSeriesPlot`, 
    a new :class:`ClimateFigure` is created.

    """
    # allow callers to override the hold state by passing hold=True|False
    b = pyplot.ishold()
    h = kwargs.pop('hold', None)
    if h is not None:
        pyplot.hold(h)
    # Get the current figure, or create one
    figManager = _pylab_helpers.Gcf.get_active()
    if figManager is not None:
        fig = figManager.canvas.figure
        if not isinstance(fig, ClimateFigure):
            fig = csfigure(series=series)
    else:
        fig = csfigure(series=series)
    # Get the current axe, or create one
    sub = fig._axstack()
    if sub is None:
        sub = fig.add_csplot(111, series=series, **kwargs)
    try:
        ret = sub.csplot(series, *args, **kwargs)
        pyplot.draw_if_interactive()
    except:
        pyplot.hold(b)
        raise
    pyplot.hold(b)
    return ret
示例#2
0
文件: utils.py 项目: acguez/Gitsby
def plot_mean_std_area(x, y, std, ax_handle=None, fignum=None):
    '''
        Plot a given x-y data, with a transparent area for its standard deviation

        If ax_handle is given, plots on this figure.
    '''

    if ax_handle is None:
        f = plt.figure(fignum)
        ax_handle = f.add_subplot(111)
    
    ishold = plt.ishold()

    ax = ax_handle.plot(x, y)
    current_color = ax[-1].get_c()
    
    plt.hold(True)

    if np.any(std > 1e-6):
        ax_handle.fill_between(x, y-std, y+std, facecolor=current_color, alpha=0.4,
                        label='1 sigma range')
    
    ax_handle.get_figure().canvas.draw()

    plt.hold(ishold)

    return ax_handle
def data_mouse():
    """Simple by-hand data generation using the GUI

    Opens a matplotlib plot window, and allows the user to specify points with the mouse.
    Each button is its own class (1,2,3); close the window when done creating data.

  Returns:
      X (arr): Mx2 array of data locations
      Y (arr): Mx1 array of labels (buttons)
    """
    import matplotlib.pyplot as plt
    fig = plt.figure()
    ax = fig.add_subplot(111, xlim=(-1,2), ylim=(-1,2))
    X  = np.zeros( (0,2) )
    Y  = np.zeros( (0,) )
    col = ['bs','gx','ro']

    def on_click(event):
        X.resize( (X.shape[0]+1,X.shape[1]) )
        X[-1,:] = [event.xdata,event.ydata]
        Y.resize( (Y.shape[0]+1,) )
        Y[-1] = event.button
        ax.plot( event.xdata, event.ydata, col[event.button-1])
        fig.canvas.draw()

    fig.canvas.mpl_connect('button_press_event',on_click)
    inter=plt.isinteractive(); hld=plt.ishold();
    plt.ioff(); plt.hold(True); plt.show();
    if inter: plt.ion();
    if not hld: plt.hold(False);
    return X,Y
示例#4
0
def csplot(series, *args, **kwargs):
    """
    Plots the series to the current :class:`ClimateSeriesPlot` subplot.
    If the current plot is not a :class:`ClimateSeriesPlot`, 
    a new :class:`ClimateFigure` is created.

    """
    # allow callers to override the hold state by passing hold=True|False
    b = pyplot.ishold()
    h = kwargs.pop('hold', None)
    if h is not None:
        pyplot.hold(h)
    # Get the current figure, or create one
    figManager = _pylab_helpers.Gcf.get_active()
    if figManager is not None :
        fig = figManager.canvas.figure
        if not isinstance(fig, ClimateFigure):
            fig = csfigure(series=series)
    else:
        fig = csfigure(series=series)
    # Get the current axe, or create one
    sub = fig._axstack()
    if sub is None:
        sub = fig.add_csplot(111, series=series, **kwargs)
    try:
        ret = sub.csplot(series, *args, **kwargs)
        pyplot.draw_if_interactive()
    except:
        pyplot.hold(b)
        raise
    pyplot.hold(b)
    return ret
示例#5
0
def _Draw(Gtraits, G, pos=None, ax=None, hold=None, **kwds):
    if ax is None:
        cf = plt.gcf()
    else:
        cf = ax.get_figure()
    cf.set_facecolor('w')
    if ax is None:
        if cf._axstack() is None:
            ax = cf.add_axes((0, 0, 1, 1))
        else:
            ax = cf.gca()

    b = plt.ishold()
    h = kwds.pop('hold', None)
    if h is not None:
        plt.hold(h)
    try:
        plt.hold(h)
        _DrawGraph(Gtraits, G, pos=pos, ax=ax, **kwds)
        ax.set_axis_off()
        plt.draw_if_interactive()
    except:
        plt.hold(b)
        raise
    plt.hold(b)
    return cf
def projection(embeddings, token_list):
    for k in range(6):
        embeddings=np.concatenate((embeddings, embeddings), axis=0)
    proj = PCA(embeddings)
    PCA_proj=proj.Y
    print PCA_proj.shape
    
    #plotting words within the 2D space of the two principal components:
    list=token_list[0]
    
        
    for n in range(maxlen):
        plt.plot(PCA_proj[n][0]+1,PCA_proj[n][1], 'w.')
        plt.annotate(list[n], xy=(PCA_proj[n][0],PCA_proj[n][1]), xytext=(PCA_proj[n][0],PCA_proj[n][1]))
    plt.show()       
    plt.ishold()
    
  
    return
示例#7
0
def projection(embeddings, token_list):
    for k in range(6):
        embeddings = np.concatenate((embeddings, embeddings), axis=0)
    proj = PCA(embeddings)
    PCA_proj = proj.Y
    print PCA_proj.shape

    #plotting words within the 2D space of the two principal components:
    list = token_list[0]

    for n in range(maxlen):
        plt.plot(PCA_proj[n][0] + 1, PCA_proj[n][1], 'w.')
        plt.annotate(list[n],
                     xy=(PCA_proj[n][0], PCA_proj[n][1]),
                     xytext=(PCA_proj[n][0], PCA_proj[n][1]))
    plt.show()
    plt.ishold()

    return
示例#8
0
文件: pyppms.py 项目: thennen/pyppms
    def recoil_params(self, plot=False):
        '''
        Calculate recoil parameters, SFD, iSFD, eSFD,... add them to instance
        as attributes
        '''
        result = copy.copy(self)
        # Split recoil into return loops as used by hystools.recoil
        Hsplit, Msplit = [], []
        for h, m in zip(self.H, self.M):
            try:
                [h1, h2], [m1, m2] = hys.split(h, m)
                if len(h2) > 0.8*len(h1):
                    # don't append loops that haven't returned at least 80%
                    # Cut out first 10 points, where drift is happening
                    Hsplit.append(h2[10:])
                    Msplit.append(m2[10:])
            except:
                # split failed, probably incomplete loop.
                pass

        rec_params = hys.recoil(Hsplit, Msplit, startdef=1)

        paramstosave = ['SFD', 'iSFD', 'eSFD', 'H_zminor', 'M_zminor']
        # map to these attribute names
        maptoattrib  = ['SFD', 'iSFD', 'eSFD', 'H_zminor', 'M_zminor']

        for p, m in zip(paramstosave, maptoattrib):
            setattr(result, m, rec_params[p])

        if self.verbose:
            paramstoprint = ['SFD', 'iSFD', 'eSFD']
            unitstoprint = ['Oe', 'Oe', 'Oe']
            for param, unit in zip(paramstoprint, unitstoprint):
                print(param.ljust(4) + ': {:.2f} {}'.format(rec_params[param], unit))

        if plot:
            # make summary plot
            #plt.figure()

            ishold = plt.ishold()
            plt.plot(self.H[0], self.M[0])
            plt.hold(True)
            for h, m in zip(Hsplit[1:], Msplit[1:]):
                plt.plot(h, m)
            plt.plot(rec_params['H_zminor'], rec_params['M_zminor'], color='black', linewidth=3)
            plt.scatter(*zip(*rec_params['plotpts']), c='g', s=30, zorder=3)
            plt.grid(True)
            plt.xlabel('Field (Oe)')
            plt.ylabel('M (uemu)')
            plt.title(os.path.split(self.filepath)[1])
            plt.hold(ishold)

        return result
示例#9
0
def _plotonehist2(x,y,parx,pary,smooth=False,colormap=True,ranges={},labels={},bins=50,levels=3,weights=None,
                  color='k',linewidth=1):
    hold = P.ishold()

    hrange = [ranges[parx] if parx in ranges else [N.min(x),N.max(x)],
              ranges[pary] if pary in ranges else [N.min(y),N.max(y)]]

    [h,xs,ys] = N.histogram2d(x,y,bins=bins,normed=True,range=hrange,weights=weights)
    if colormap:
        P.contourf(0.5*(xs[1:]+xs[:-1]),0.5*(ys[1:]+ys[:-1]),h.T,cmap=P.get_cmap('YlOrBr')); P.hold(True)

    H,tmp1,tmp2 = N.histogram2d(x,y,bins=bins,range=hrange,weights=weights)

    if smooth:
        # only need scipy if we're smoothing
        import scipy.ndimage.filters as SNF
        H = SNF.gaussian_filter(H,sigma=1.5 if smooth is True else smooth)

    if weights is None:
        H = H / len(x)
    else:
        H = H / N.sum(H)            # I think this is right...
    Hflat = -N.sort(-H.flatten())   # sort highest to lowest
    cumprob = N.cumsum(Hflat)       # sum cumulative probability

    levels = [N.interp(level,cumprob,Hflat) for level in [0.6826,0.9547,0.9973][:levels]]

    xs = N.linspace(hrange[0][0],hrange[0][1],bins)
    ys = N.linspace(hrange[1][0],hrange[1][1],bins)

    P.contour(xs,ys,H.T,levels,
              colors=color,linestyles=['-','--','-.'][:len(levels)],linewidths=linewidth)
    P.hold(hold)

    if parx in ranges:
        P.xlim(ranges[parx])
    if pary in ranges:
        P.ylim(ranges[pary])

    P.xlabel(labels[parx] if parx in labels else parx)
    P.ylabel(labels[pary] if pary in labels else pary)

    P.locator_params(axis='both',nbins=6)
    P.minorticks_on()

    fx = P.ScalarFormatter(useOffset=True,useMathText=True)
    fx.set_powerlimits((-3,4)); fx.set_scientific(True)

    fy = P.ScalarFormatter(useOffset=True,useMathText=True)
    fy.set_powerlimits((-3,4)); fy.set_scientific(True)

    P.gca().xaxis.set_major_formatter(fx)
    P.gca().yaxis.set_major_formatter(fy)
示例#10
0
def draw_grid(corners,nRows=3,nCols=2):
    '''
    Draw a grid of nRows and nCols starting at the coordinates specified in 'corners'.
    '''
    (xvals,yvals) = define_grid(corners,nRows,nCols)
    holdStatus = plt.ishold()
    plt.hold(True)
    plt.autoscale(False)
    for yval in yvals:
        plt.plot(xvals[[0,-1]],[yval,yval],color=GRIDCOLOR)
    for xval in xvals:
        plt.plot([xval,xval],yvals[[0,-1]],color=GRIDCOLOR)
    plt.hold(holdStatus)
    plt.draw()
示例#11
0
def draw_grid(corners,nRows=3,nCols=2):
    '''
    Draw a grid of nRows and nCols starting at the coordinates specified in 'corners'.
    '''
    (xvals,yvals) = define_grid(corners,nRows,nCols)
    holdStatus = plt.ishold()
    plt.hold(True)
    plt.autoscale(False)
    for yval in yvals:
        plt.plot(xvals[[0,-1]],[yval,yval],color=GRIDCOLOR)
    for xval in xvals:
        plt.plot([xval,xval],yvals[[0,-1]],color=GRIDCOLOR)
    plt.hold(holdStatus)
    plt.draw()
示例#12
0
文件: pyppms.py 项目: thennen/pyppms
 def plot(self, loopnum='all'):
     ''' plot loops'''
     loopind = self._loopind(loopnum)
     #plt.figure()
     ishold = plt.ishold()
     lines = []
     for i,(H,M) in enumerate(zip(self.H, self.M)):
         if i in loopind:
             # label plots by everything before '--' in filename
             label = os.path.split(self.filepath)[1].split('--')[0] + ' ({})'.format(i+1)
             #label = os.path.split(self.filepath)[1] + str(i+1)
             line = plt.plot(H, M, label=label)
             lines.extend(line)
             plt.hold(True)
     plt.grid(True)
     plt.title(os.path.split(self.filepath)[1])
     plt.xlabel('Field (Oe)')
     plt.ylabel('VSM signal ($\mu$ emu)')
     plt.hold(ishold)
     return lines
示例#13
0
def plot_mean_std_area(x, y, std, ax_handle=None, fignum=None, linewidth=1, fmt='-', markersize=1, color=None, xlabel=None, ylabel=None, label='', title=None):
    '''
        Plot a given x-y data, with a transparent area for its standard deviation

        If ax_handle is given, plots on this figure.
    '''

    if ax_handle is None:
        f = plt.figure(fignum)
        ax_handle = f.add_subplot(111)

    ishold = plt.ishold()

    if color is not None:
        ax = ax_handle.plot(x, y, fmt, linewidth=linewidth, markersize=markersize, color=color, label=label)
    else:
        ax = ax_handle.plot(x, y, fmt, linewidth=linewidth, markersize=markersize, label=label)

    current_color = ax[-1].get_c()

    plt.hold(True)

    if std is not None and np.any(std > 1e-6):
        ax_handle.fill_between(x, y-std, y+std, facecolor=current_color, alpha=0.4)

    if xlabel is not None:
        ax_handle.set_xlabel(xlabel)

    if ylabel is not None:
        ax_handle.set_ylabel(ylabel)

    if title is not None:
        ax_handle.set_title(title)

    ax_handle.get_figure().canvas.draw()

    plt.hold(ishold)

    return ax_handle
示例#14
0
文件: pyppms.py 项目: thennen/pyppms
def nplot(filter = '', n=10, loopnum='all', legend=True):
    ''' Plot from n files at once '''
    washeld = plt.ishold()
    if not washeld:
        # clear current axis if not hold
        plt.cla()

    #fig, ax = plt.subplots()
    lines = []
    for fn in reversed(nlatest(filter, n)):
        # plot oldest first
        line = ppms(fn).cplot(loopnum)
        lines.extend(line)
        plt.hold(True)
    plt.hold(False)
    if legend:
        plt.legend(loc='best')

    # leave plot in previous hold state
    if not washeld:
        plt.hold(False)

    return lines
示例#15
0
def main():
    m = 1
    n = 2
    x = np.arange(0, 10, 0.1)
    y = x**2 / 100
    plt.ioff()  #Activar modo interactivo
    plt.figure('Figura1')
    plt.figure('Figura2')
    a = np.random.rand(100)
    b = np.random.rand(100)
    plt.figure('Figura1')
    plt.subplot(m, n, 1)
    y1 = cvector(1, 5, 0.1)
    print y1
    plt.plot(y1)
    plt.subplot(m, n, 2)
    y2 = cvector(5, 1, -0.1)
    plt.plot(y2)
    print y2
    plt.figure('Figura2')
    plt.plot(a, b)
    plt.scatter(x, y)
    plt.figure('Figura3')
    m = 2
    n = 2
    plt.subplot(m, n, 1)
    y = cvector(1, 3, 0.1)
    plt.plot(y)
    plt.subplot(m, n, 2)
    y = cvector(3, 1, -0.1)
    plt.plot(y)
    plt.subplot(2, 1, 2)
    y1 = np.array(cvector(0, 2, 0.1))
    y2 = np.array(cvector(2, 0, -0.1))
    y = np.concatenate((y1, y2), axis=0)
    #y=np.array([0,1,2,2,1,0])
    plt.plot(y)
    fig = plt.figure(frameon=True,
                     edgecolor='b',
                     dpi=100,
                     figsize=(12, 12),
                     facecolor='g')
    fig.canvas.set_window_title('Figura 5')
    #print id(fig)
    #fig.figure('Figura4')
    x = np.array(cvector(0, 10, 0.1))
    y = np.array(cvector(0, 10, 0.1))
    l = len(x)
    print x
    for i in range(0, l):
        if x[i] > 5:
            y[i] = 2
        else:
            y[i] = 0
    plt.plot(x, y)
    # plt.plot(np.random.rand(10))
    # plt.plot(np.random.rand(10))
    #plt.clf()
    plt.show()
    print plt.isinteractive()
    print plt.ishold()
示例#16
0
文件: draw.py 项目: openalea/mtg
def draw(g, pos=None, ax=None, hold=None, ax_size=(0,0,1,1), **kwds):
    """Draw the graph g with Matplotlib.

    Draw the graph as a simple representation with no node
    labels or edge labels and using the full Matplotlib figure area
    and no axis labels by default.  See draw_mtg() for more
    full-featured drawing that allows title, axis labels etc.

    Parameters
    ----------
    g : graph
       A MTG graph

    pos : dictionary, optional
       A dictionary with nodes as keys and positions as values.
       If not specified a spring layout positioning will be computed.
       See mtg.layout for functions that compute node positions.

    ax : Matplotlib Axes object, optional
       Draw the graph in specified Matplotlib axes.

    hold : bool, optional
       Set the Matplotlib hold state.  If True subsequent draw
       commands will be added to the current axes.

    **kwds : optional keywords
       See draw.draw_mtg() for a description of optional keywords.

    Examples
    --------
    >>> g = om.random_mtg()
    >>> draw.draw(g)
    >>> draw.draw(g,pos=om.spring_layout(G)) # use spring layout

    See Also
    --------
    draw_mtg()
    draw_mtg_vertices()
    draw_mtg_edges()
    draw_mtg_labels()
    draw_mtg_edge_labels()

    Notes
    -----
    This function has the same name as pylab.draw and pyplot.draw
    so beware when using

    >>> from openalea.mtg.draw import *

    since you might overwrite the pylab.draw function.

    With pyplot use

    >>> import matplotlib.pyplot as plt
    >>> import openalea.mtg as om
    >>> g=om.random_mtg()
    >>> om.draw(g)  # mtg draw()
    >>> plt.draw()  # pyplot draw()

    Also see the openalea.mtg drawing examples at
    openalea gallery.
    """
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        raise ImportError("Matplotlib required for draw()")
    except RuntimeError:
        print("Matplotlib unable to open display")
        raise

    if ax is None:
        cf = plt.gcf()
    else:
        cf = ax.get_figure()
    cf.set_facecolor('w')
    if ax is None:
        if cf._axstack() is None:
            ax=cf.add_axes(ax_size)
        else:
            ax=cf.gca()

 # allow callers to override the hold state by passing hold=True|False
    b = plt.ishold()
    h = kwds.pop('hold', None)
    if h is not None:
        plt.hold(h)
    try:
        draw_mtg(g, pos=pos,ax=ax,**kwds)
        ax.set_axis_off()
        plt.draw_if_interactive()
    except:
        plt.hold(b)
        raise
    plt.hold(b)
    return
# definimos las proyecciones en los planos X-Y , X-Z , Y-Z
# ########################################################
plt.figure("figura 1")
plt.title("proyeccion en los planos")
plt.subplot(321)
plt.plot(datos_x, datos_y)
plt.xlabel("valores de x")
plt.ylabel("valores de y")
plt.subplot(324)
plt.plot(datos_x, datos_z)
plt.xlabel("valores de x")
plt.ylabel("valores de z")
plt.subplot(325)
plt.plot(datos_y, datos_z)
plt.xlabel("valores de y")
plt.ylabel("valores de z")
# #####################################################
# definimos las proyecciones de los datos en el tiempo
# #####################################################
plt.ishold()
plt.figure("figura 2")
plt.plot(datos_t, datos_x, label="x")
plt.plot(datos_t, datos_y, label="y")
plt.plot(datos_t, datos_z, label="z")
plt.xlabel("valores de t")
plt.ylabel("valores de x-y-z")
plt.title("evolucion temporal")
plt.legend()
plt.show()

示例#18
0
def draw(G, pos=None, ax=None, hold=None, **kwds):
    """Draw the graph G with Matplotlib.

    Draw the graph as a simple representation with no node
    labels or edge labels and using the full Matplotlib figure area
    and no axis labels by default.  See draw_networkx() for more
    full-featured drawing that allows title, axis labels etc.

    Parameters
    ----------
    G : graph
       A networkx graph

    pos : dictionary, optional
       A dictionary with nodes as keys and positions as values.
       If not specified a spring layout positioning will be computed.
       See networkx.layout for functions that compute node positions.

    ax : Matplotlib Axes object, optional
       Draw the graph in specified Matplotlib axes.

    hold : bool, optional
       Set the Matplotlib hold state.  If True subsequent draw
       commands will be added to the current axes.

    **kwds : optional keywords
       See networkx.draw_networkx() for a description of optional keywords.


    Examples
    --------
    >>> G=nx.dodecahedral_graph()
    >>> nx.draw(G)
    >>> nx.draw(G,pos=nx.spring_layout(G)) # use spring layout

    See Also
    --------
    draw_networkx()
    draw_networkx_nodes()
    draw_networkx_edges()
    draw_networkx_labels()
    draw_networkx_edge_labels()

    Notes
    -----
    This function has the same name as pylab.draw and pyplot.draw
    so beware when using

    >>> from networkx import *

    since you might overwrite the pylab.draw function.

    With pyplot use

    >>> import matplotlib.pyplot as plt
    >>> import networkx as nx
    >>> G=nx.dodecahedral_graph()
    >>> nx.draw(G)  # networkx draw()
    >>> plt.draw()  # pyplot draw()

    Also see the NetworkX drawing examples at
    http://networkx.github.io/documentation/latest/gallery.html
    """
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        raise ImportError("Matplotlib required for draw()")
    except RuntimeError:
        print("Matplotlib unable to open display")
        raise

    if ax is None:
        cf = plt.gcf()
    else:
        cf = ax.get_figure()
    cf.set_facecolor('w')
    if ax is None:
        if cf._axstack() is None:
            ax = cf.add_axes((0, 0, 1, 1))
        else:
            ax = cf.gca()

    if 'with_labels' not in kwds:
        kwds['with_labels'] = 'labels' in kwds
    b = plt.ishold()
    # allow callers to override the hold state by passing hold=True|False
    h = kwds.pop('hold', None)
    if h is not None:
        plt.hold(h)
    try:
        draw_networkx(G, pos=pos, ax=ax, **kwds)
        ax.set_axis_off()
        plt.draw_if_interactive()
    except:
        plt.hold(b)
        raise
    plt.hold(b)
    return
示例#19
0
def roc(res_paths):
  paramsets = []
  statsets = []
  for res_path in res_paths:
    f = open(res_path, 'r')
    results = pickle.load(f)
    f.close()
    for paramset in results[0]:
      paramsets.append(paramset)
    for statset in results[1]:
      statsets.append(statset)

  fix_detection_times(paramsets, statsets)
  """
  # Sort just to make sure (TODO: untested) TODO: This will come in handy when
  # getting data from multiple files.
  sorted_indices = [ i[0] for i in sorted(enumerate(paramsets),
                                          key=lambda x:x[1]) ]
  paramsets = sorted(paramsets)
  stats = [ stats[sorted_indices[i]] for i in range(len(stats)) ]
  """
  save_fig = False
  plot = True
  pnt = False
  if plot:
    plt.close('all')
    plt.ion()
    fig = plt.figure(figsize = (10,4.5))
    plt.show()

  if save_fig:
    if not os.path.exists('fig'):
      os.mkdir('fig')  

  # TODO: Get this from params somehow... though the relevant subset still has
  # to be specified.
  all_attrs = ['gamma', 'cmpr_window', 'threshold', 'w_smooth',
               'detection_window_hrs', 'req_consec_detections']
  const_attrs_allowed_values = { 'gamma': [0.1, 1, 10],
                                 'cmpr_window': [10, 80, 115, 150],
                                 #'cmpr_window': [80],
                                 #'threshold': [1],
                                 'threshold': [0.65, 1, 3],
                                 'w_smooth': [10, 80, 115, 150],
                                 'detection_window_hrs': [3, 5, 7, 9],
                                 'req_consec_detections': [1, 3, 5] }

  # Assuming parameter combinations are sorted, go through them sequentially and
  # note the index of the parameter that changes. When that index changes, we
  # make a new plot that show the variation of the variable at the new index.
  for var_attr in all_attrs:
    if save_fig:
      if not os.path.exists(os.path.join('fig', var_attr)):
        os.mkdir(os.path.join('fig', var_attr))
    if plot:
      print 'Press any key'
      raw_input()
      plt.figure(figsize = (10,4.5))
      
      # plt.suptitle('Varying ' + attr_math_name[var_attr], size = 15)
      plt.subplot(121)
      plt.hold(False)
      plt.subplot(122)
      plt.hold(False)
    if pnt:
      print 'Varying', var_attr

    delta_fprs = []
    delta_tprs = []

    # Mapping from paramset to percentage of tpr and fpr deltas above 0.
    delta_rank = {}
    # Mapping from paramset to a measure of how far up or down the ROC curve we are.
    updown_rank = {}
    # Partition deltas by where the ROC curve starts on the FPR/TPR plane.
    deltas_partitioned = {}
    deltas_partitioned['top'] = {}
    deltas_partitioned['center'] = {}
    deltas_partitioned['bottom'] = {}
    # Partition fprs and tprs by where they are in the FPR/TPR plane
    rates_partitioned = {}
    rates_partitioned['top'] = {}
    rates_partitioned['center'] = {}
    rates_partitioned['bottom'] = {}
    # Record the earliness relative to true onset
    earliness = {}
    earliness['top'] = {}
    earliness['top']['fprs'] = []
    earliness['top']['tprs'] = []
    earliness['top']['earlies'] = []
    earliness['top']['lates'] = []
    earliness['top']['params'] = []
    earliness['center'] = {}
    earliness['center']['fprs'] = []
    earliness['center']['tprs'] = []
    earliness['center']['earlies'] = []
    earliness['center']['lates'] = []
    earliness['center']['params'] = []
    earliness['bottom'] = {}
    earliness['bottom']['fprs'] = []
    earliness['bottom']['tprs'] = []
    earliness['bottom']['earlies'] = []
    earliness['bottom']['lates'] = []
    earliness['bottom']['params'] = []

    const_attrs = all_attrs[:]
    const_attrs.remove(var_attr)
    # Sort by variable parameter.
    enum_sorted = sorted(enumerate(paramsets),
      key = lambda x: x[1]._asdict()[var_attr])
    # Sort by constant parameters.
    enum_sorted = sorted(enum_sorted,
      key = lambda x: [ x[1]._asdict()[attr] for attr in const_attrs ])
    paramsets_sorted = [ elt[1] for elt in enum_sorted ]
    indices_sorted = [ elt[0] for elt in enum_sorted ]
    statsets_sorted = [ statsets[indices_sorted[i]] for i in range(len(statsets)) ]

    # Initialize variables for each ROC curve. These will be reset when a new
    # curve is ready to be drawn.
    var_attr_count = 0
    var_attr_values = []
    mean_fprs = []
    mean_tprs = []
    std_fprs = []
    std_tprs = []
    all_fprs = []
    all_tprs = []
    all_earlies = []
    all_lates = []
    
    for psi in xrange(len(paramsets_sorted)):
      # Take the difference between the numerical values.
      curr_params = paramsets_sorted[psi]

      const_attr_str = string.join(
        [ attr + '=' + str(curr_params._asdict()[attr]) 
          for attr in const_attrs ],
        ',')

      if psi > 0:
        prev_params = paramsets_sorted[psi - 1]
        if pnt:
          print psi
          print prev_params
        # print 'curr', curr_params
        delta_params = [ curr_params[i] - prev_params[i]
                         for i in range(2,len(prev_params))
                         if type(curr_params[i]) is type(0) or \
                           type(curr_params[i]) is type(0.0) ]
        if pnt:
          print 'delta', delta_params
        mod_indices = np.where(np.array(delta_params) != 0)
        if len(mod_indices[0]) > 1:
          #=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
          # NEW ROC CURVE! PLOT RELEVANT THINGS FOR OLD ROC CURVE AND RESET
          # VARIABLE IN PREPARATION FOR NEW ROC CURVE.
          #=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~

          # Record values for plotting 2d histograms of ROC curve deltas for
          # var_attr.
          """
          delta_fprs.extend([ (mean_fprs[i] - mean_fprs[i-1]) / \
                                (var_attr_values[i] - var_attr_values[i-1])
                              for i in range(1, len(mean_fprs)) ])
          delta_tprs.extend([ (mean_tprs[i] - mean_tprs[i-1]) / \
                                (var_attr_values[i] - var_attr_values[i-1])
                              for i in range(1, len(mean_tprs)) ])
          """
          # Alternate method for computing delta_fprs and delta_tprs. Compute
          # across all combinations of ROC for different trials.

          # zip on so much data is expensive!
          all_tprs_prod = list(itertools.product(*all_tprs))
          all_fprs_prod = list(itertools.product(*all_fprs))
          # Separate storage of deltas for prev params only (corresponding to
          # the ROC curve we just finished looking at, and are about to
          # analyze..
          delta_fprs_prev_params = []
          delta_tprs_prev_params = []

          for combo_i in xrange(len(all_fprs_prod)):
            fprs_combo = all_fprs_prod[combo_i]
            tprs_combo = all_tprs_prod[combo_i]

            # Don't include deltas that are "stuck" in the 0,0 or 1,1 corner.
            """
            fprs_combo_not_stuck = [ fprs_combo[j]
                                     for j in range(len(fprs_combo))
                                     if fprs_combo[j] != 0 and \
                                       fprs_combo[j] != 1 and \
                                       tprs_combo[j] != 0 and \
                                       tprs_combo[j] != 1 ]
            tprs_combo_not_stuck = [ tprs_combo[j]
                                     for j in range(len(tprs_combo))
                                     if fprs_combo[j] != 0 and \
                                       fprs_combo[j] != 1 and \
                                       tprs_combo[j] != 0 and \
                                       tprs_combo[j] != 1 ]
            """
            fprs_combo_not_stuck = [ fprs_combo[j]
                                     for j in range(1, len(fprs_combo))
                                     if fprs_combo[j] != fprs_combo[j-1] and \
                                       tprs_combo[j] != tprs_combo[j-1] ]
            if fprs_combo:
              fprs_combo_not_stuck.insert(0, fprs_combo[0])
            tprs_combo_not_stuck = [ tprs_combo[j]
                                     for j in range(1, len(tprs_combo))
                                     if fprs_combo[j] != fprs_combo[j-1] and \
                                       tprs_combo[j] != tprs_combo[j-1] ]
            if tprs_combo:
              tprs_combo_not_stuck.insert(0, tprs_combo[0])

            new_delta_fprs = [ (fprs_combo[i] - fprs_combo[i-1]) / \
                                 (var_attr_values[i] - var_attr_values[i-1])
                               for i in range(1, len(fprs_combo_not_stuck)) ]
            new_delta_tprs = [ (tprs_combo[i] - tprs_combo[i-1]) / \
                                 (var_attr_values[i] - var_attr_values[i-1])
                               for i in range(1, len(tprs_combo_not_stuck)) ]
            delta_fprs.extend(new_delta_fprs)
            delta_tprs.extend(new_delta_tprs)
            delta_fprs_prev_params.extend(new_delta_fprs)
            delta_tprs_prev_params.extend(new_delta_tprs)
                
            # Categorize ROC curves by whether they start in the top center or
            # bottom and record the corersponding delta fprs and tprs.
            category = None
            if fprs_combo and tprs_combo:
              category = point_to_category(fprs_combo[0], tprs_combo[0],
                fpr_partition_threshold = fpr_partition_threshold,
                tpr_partition_threshold = tpr_partition_threshold)
              deltas_partitioned[category][prev_params] = \
                  (new_delta_fprs, new_delta_tprs)

            for pidx in xrange(len(fprs_combo)):
              category = point_to_category(fprs_combo[pidx],
                tprs_combo[pidx], fpr_partition_threshold = fpr_partition_threshold,
                tpr_partition_threshold = tpr_partition_threshold)
              # This should really be done at the individual point level, not at
              # the ROC level...
              actual_params_dict = prev_params._asdict()
              actual_params_dict[var_attr] = var_attr_values[pidx]
              actual_params = Params(**actual_params_dict)
              rates_partitioned[category][actual_params] = \
                  (fprs_combo[pidx], tprs_combo[pidx])

          # Record a measure of change in tpr and fpr for the purpose of
          # ranking which parameters cause the most positive and negative
          # deltas.
          """
          f_num_geq_zero = len([ ndfpr for ndfpr in delta_fprs_prev_params
                                 if ndfpr >= 0 ])
          t_num_geq_zero = len([ ndtpr for ndtpr in delta_tprs_prev_params
                                 if ndtpr >= 0 ])
          """
          if len(delta_fprs_prev_params) > 0 and \
                len(delta_tprs_prev_params) > 0:
            """
            f_frac_geq_zero = \
              float(f_num_geq_zero) / len(delta_fprs_prev_params)
            t_frac_geq_zero = \
              float(t_num_geq_zero) / len(delta_tprs_prev_params)
            """
            # Store copies of all_fprs and all_tprs as well so we can plot ROC
            # curves at the end in order of rank.
            delta_rank[prev_params] = (np.mean(delta_fprs_prev_params),
                                       np.mean(delta_tprs_prev_params),
                                       all_fprs[:], all_tprs[:],
                                       delta_fprs_prev_params[:],
                                       delta_tprs_prev_params[:])
            
          # Partition ROC points into top, bottom, center. For each one, record
          # the FPR,TPR coordinates and the earlies and lates.
          if all_fprs and all_tprs and all_earlies and all_lates:
            for point_trials_idx in xrange(len(all_fprs)):
              fprs_point_trials = all_fprs[point_trials_idx]
              tprs_point_trials = all_tprs[point_trials_idx]
              earlies_point_trials = all_earlies[point_trials_idx]
              lates_point_trials = all_lates[point_trials_idx]
              for trial_idx in xrange(len(fprs_point_trials)):
                fpr_trial = fprs_point_trials[trial_idx]
                tpr_trial = tprs_point_trials[trial_idx]
                earlies_trial = earlies_point_trials[trial_idx]
                lates_trial = lates_point_trials[trial_idx]

                category = point_to_category(fpr_trial, tpr_trial,
                  fpr_partition_threshold = fpr_partition_threshold,
                  tpr_partition_threshold = tpr_partition_threshold)

                earliness[category]['fprs'].append(fpr_trial)
                earliness[category]['tprs'].append(tpr_trial)
                earliness[category]['earlies'].append(earlies_trial)
                earliness[category]['lates'].append(lates_trial)
                earliness[category]['params'].append(prev_params)

          # Record measures of 'position' for this ROC curve to use for ranking
          # ROC curves by how far 'up' or 'down' they are. %TODO: awkward
          # phrasing.
          # Store copies of all_fprs and all_tprs as well so we can plot ROC
          # curves at the end in order of rank.
          if all_fprs and all_tprs:
            max_mean_fpr = max([ np.mean(all_fprs_i)
                                 for all_fprs_i in all_fprs ])
            max_mean_tpr = max([ np.mean(all_tprs_i)
                                 for all_tprs_i in all_tprs ])
            min_mean_fpr = min([ np.mean(all_fprs_i)
                                 for all_fprs_i in all_fprs ])
            min_mean_tpr = min([ np.mean(all_tprs_i)
                                 for all_tprs_i in all_tprs ])
            updown_rank[prev_params] = (min_mean_fpr, min_mean_tpr,
                                        max_mean_fpr, max_mean_tpr,
                                        all_fprs[:], all_tprs[:])

          # Slap (0,0) and (1,1) onto mean_fprs, mean_tprs to complete the
          # ROC curve. Also put corresponding 0 stdevs in std_fprs, std_tprs.

          mean_fprs.extend([0,1])
          mean_tprs.extend([0,1])
          std_fprs.extend([0,0])
          std_tprs.extend([0,0])
          
          point_sizes = [ s * 8 for s in np.array(range(0, len(mean_fprs))) + 0.1 ]
          point_sizes.extend([0.1,0.1])

          # Sort from left to right.
          mean_fprs_ltor_enum = sorted(enumerate(mean_fprs),
                                       key = lambda x:x[1])
          mean_fprs_ltor = [ mean_fprs[i] 
                             for (i,v) in mean_fprs_ltor_enum ]
          mean_tprs_ltor = [ mean_tprs[i]
                             for (i,v) in mean_fprs_ltor_enum ]
          std_fprs_ltor = [ std_fprs[i] 
                            for (i,v) in mean_fprs_ltor_enum ]
          std_tprs_ltor = [ std_tprs[i]
                            for (i,v) in mean_fprs_ltor_enum ]

          point_sizes_ltor = [ point_sizes[i]
                               for (i,v) in mean_fprs_ltor_enum ]

          if plot:
            if save_fig:
              if len(mean_fprs) > 3:
                # There were Points other than the manually added (0,0) and (1,1).
                plt.savefig(os.path.join('fig', var_attr,
                                         const_attr_str) + '.png')
            else:
              # +-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
              # | PLOT SCATTER  
              # +-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
              plot_scatter = False
              if plot_scatter and len(mean_fprs) > 3:
                # Don't take the first and last if we've put a dummy 0 and 1 at
                # each end of the means lists.
                plt.subplot(121)
                """
                plt.errorbar(mean_fprs_ltor, mean_tprs_ltor,
                             xerr = std_fprs_ltor, yerr = std_tprs_ltor,
                             color = 'k', linestyle = '-', linewidth = 0.5,
                             marker = 'o', elinewidth = 0.25)
                """
                plt.plot(mean_fprs_ltor[1:-1], mean_tprs_ltor[1:-1], color = 'k', lw = 0.5)
                plt.hold(True)
                # For increasing point sizes.
                # Turn on scatter for markers at points
                """
                plt.scatter(mean_fprs_ltor, mean_tprs_ltor,
                            s = point_sizes_ltor, c = 'b')
                """
                """
                plt.scatter(mean_fprs_ltor, mean_tprs_ltor,
                            s = 15, c = 'k')
                """
                plt.title('All ROC Curves')
                plt.xlabel('$FPR$')
                plt.ylabel('$TPR$')
                plt.grid(True)
                """
                plt.title(const_attr_str + '\n' + var_attr + '=' + \
                            str(var_attr_values),
                          fontsize = 11)
                """
                plt.xlim([-0.1,1.1])
                plt.ylim([-0.1,1.1])
                # Enable for sequential viewing
                #plt.hold(False)
                #raw_input()
                #plt.draw()

              # +-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
              # | PLOT LINES  
              # +-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
              plot_curves = True
              if plot_curves:
                num_unique = len(set([ (mean_fprs_ltor[i], mean_tprs_ltor[i]) 
                                       for i in range(len(mean_fprs_ltor)) ]))
                if num_unique > 3:
                  # Plot bezier curves.
                  plot_poly = True
                  if plot_poly:
                    verts = [ (mean_fprs_ltor[i], mean_tprs_ltor[i])
                              for i in range(len(mean_fprs_ltor)) ]
                    connection = Path.LINETO
                    codes = [ connection ] * (len(verts) - 1)
                    verts.insert(0, (1,1))
                    verts.insert(1, (1,0))
                    codes.insert(0, Path.MOVETO)
                    codes.insert(1, Path.LINETO)
                    codes.insert(2, Path.LINETO)
                    path = Path(verts, codes)

                    plt.subplot(1,2,2)
                    ax = plt.gca()
                    patch = patches.PathPatch(path, facecolor='k', lw=5, alpha = 1)
                    # Manually clear axes. This isn't a plotting command, so hold
                    # = False has no effect.
                    if not plt.ishold():
                      plt.cla()
                    ax.add_patch(patch)

                  plot_lines = False
                  if plot_lines:
                    # Plot lines.
                    plt.plot(mean_fprs_ltor, mean_tprs_ltor, color = 'k',
                             linewidth = 1)

                  plt.xlim([-0.1,1.1])
                  plt.ylim([-0.1,1.1])
                  plt.hold(True)
                  plt.title('ROC Curve Envelope', size = 16)
                  plt.xlabel('$FPR$', size = 16)
                  plt.ylabel('$TPR$', size = 16)
                  plt.grid(True)
                  # Enable for sequential viewing
                  #raw_input()
                  #plt.draw()
            plt.gcf().subplots_adjust(top = 0.85)

          # Reset variables for next ROC curve.
          mean_fprs = []
          mean_tprs = []
          std_fprs = []
          std_tprs = []
          all_fprs = []
          all_tprs = []
          all_earlies = []
          all_lates = []
          var_attr_count = 0
          var_attr_values = []
    
      var_attr_values.append(curr_params._asdict()[var_attr])

      # Save the relevant stats about the current point on the ROC curve.
      if plot:
        fprs = [ stats['fpr']
                 for stats in statsets_sorted[psi]
                 if stats ]
        tprs = [ stats['tpr']
                 for stats in statsets_sorted[psi]
                 if stats ]
        
        earlies_point_trials = []
        lates_point_trials = []
        for stats in statsets_sorted[psi]:
          if not stats:
            continue
          earlies_trial = stats['earlies']
          lates_trial = stats['lates']
          earlies_point_trials.append(earlies_trial)
          lates_point_trials.append(lates_trial)

        if pnt:
          print fprs, tprs
        if fprs and tprs:
          mfprs = np.mean(fprs)
          mtprs = np.mean(tprs)
          sfprs = np.std(fprs)
          stprs = np.std(tprs)

          # Record these so we plot them before moving on to the next ROC curve.
          curr_params_dict = curr_params._asdict()
          if all([ curr_params_dict[const_attr]
                   in const_attrs_allowed_values[const_attr]
                   for const_attr in const_attrs ]):
            mean_fprs.append(mfprs)
            mean_tprs.append(mtprs)
            std_fprs.append(sfprs)
            std_tprs.append(stprs)

            # Record full list of fprs and tprs
            all_fprs.append(fprs)
            all_tprs.append(tprs)

            # Record earlies and lates
            all_earlies.append(earlies_point_trials)
            all_lates.append(lates_point_trials)

      var_attr_count += 1

    print var_attr  

    plot_twitter_killer = False
    if plot_twitter_killer:
      nbins = 4
      ecatd = earliness['center']
      for i in xrange(len(ecatd['fprs'])):
        ecat = [ -e / 3600000.0 for e in ecatd['earlies'][i] ]
        lcat = [ l / 3600000.0 for l in ecatd['lates'][i] ]
        params = ecatd['params'][i]

        param_str = string.join(
          [ '%s$=%s$' % (attr_math_name[attr], str(params._asdict()[attr])) 
            for attr in all_attrs ],
          ', ')
          
        fpr = ecatd['fprs'][i]
        tpr = ecatd['tprs'][i]
        print i, ':', param_str, len(ecat), len(lcat), len(ecat) / float(len(lcat) + len(ecat)), fpr, tpr

        #cond = len(ecat) > 1.45 * len(lcat) and fpr < 0.10 and tpr > 0.92 and \
        #    abs(np.mean(ecat)) > abs(np.mean(lcat))

        cond = fpr < 0.05 and tpr > 0.94
        param_avg = None
        param_count = 0
        
        if cond:
          if param_avg is None:
            param_avg = params
          else:
            param_avg += params
          param_count += 1

          plt.figure(figsize = (9, 2.75))
          n, bins, hpatches = plt.hist(ecat, bins = nbins,
                                       histtype = 'stepfilled', color = 'k',
                                       align = 'mid', label = 'early')
          print bins
          plt.setp(hpatches, 'facecolor', 'w')
          nmax = max(n)
          plt.hold(True)
          n, bins, hpatches = plt.hist(lcat, bins = nbins,
                                       histtype = 'stepfilled', color = 'k',
                                       align = 'mid', label = 'late')
          print bins
          plt.setp(hpatches, 'facecolor', 'k')
          nmax = max(max(n), nmax)
          plt.ylim([0, 1.2 * nmax])

          xmax = max(lcat)
          plate = len(lcat) / float(len(lcat) + len(ecat))
          """
          plt.text(0.4 * xmax, nmax, '$P(early) = %.2f$' % (1 - plate),
                    size = 10)
          plt.text(0.4 * xmax, 0.8 * nmax, 
                    '$P(late) = %.2f$' % plate, size = 10)
          plt.text(0.4 * xmax, 0.6 * nmax,
                    r'$\langle early \rangle = %.2f \; hrs.$' % (-np.mean(ecat)),
                    size = 10)
          plt.text(0.4 * xmax, 0.4 * nmax,
                    r'$\langle late \rangle = %.2f \; hrs.$' % (np.mean(lcat)),
                    size = 10)
          """
          plt.title('$FPR=%.2f$, $TPR=%.2f$, $P(early)=%.2f$, $\langle early \\rangle=%.2f hrs$\n%s' % 
                    (ecatd['fprs'][i], ecatd['tprs'][i], 1 - plate,
                     -np.mean(ecat), param_str), size = 16)
          plt.xlabel('hours late', size = 16)
          plt.ylabel('count', size = 16)
          plt.legend(loc = 1)
          plt.gcf().subplots_adjust(left = 0.07, bottom = 0.20, right = 0.95,
                                    top = 0.80)
          #raw_input()
          

    plot_earliness = False
    if plot_earliness:
      #plt.figure(figsize = (7,14))
      #plt.suptitle('Early detection vs. position on ROC curve', size = 15)
      """
      plt.subplot(211)
      for (ci, category) in enumerate(earliness):
        ecatd = earliness[category]
        plt.scatter(ecatd['fprs'], ecatd['tprs'], s = 5, c = (0.75,0.75,0.75),
                    edgecolors = 'none')
        plt.hold(True)
      """
      cat_means = {}
      for (ci, category) in enumerate(earliness):
        ecatd = earliness[category]
        mfprs = np.mean(ecatd['fprs'])
        sfprs = np.std(ecatd['fprs'])
        mtprs = np.mean(ecatd['tprs'])
        stprs = np.std(ecatd['tprs'])
        cat_means[category] = (mfprs, mtprs)
        """
        plt.errorbar(mfprs, mtprs, xerr = sfprs, yerr = stprs,
                     linestyle = 'None', color = 'k', linewidth = 1.5)
        plt.hold(True)
        plt.scatter(mfprs, mtprs, s = 100, c = 'k')
        plt.text(mfprs + 0.05, mtprs - 0.05, category, size = 15)
        plt.axvline(0, linestyle = ':', color = 'k', linewidth = 1)
        plt.axvline(1, linestyle = ':', color = 'k', linewidth = 1)
        plt.axhline(0, linestyle = ':', color = 'k', linewidth = 1)
        plt.axhline(1, linestyle = ':', color = 'k', linewidth = 1)
        plt.axvline(fpr_partition_threshold, linestyle = ':', color = 'k')
        plt.axhline(tpr_partition_threshold, linestyle = ':', color = 'k')

        plt.xlim([-0.05, 1.05])
        plt.ylim([-0.05, 1.05])
        
        plt.xlabel('$FPR$')
        plt.ylabel('$TPR$')
        """
      nbins = 10
      
      for (ci, category) in enumerate(earliness):
        ecatd = earliness[category]
        ecat = []
        lcat = []
        for e in ecatd['earlies']:
          ecat.extend([ -ei / 3600000.0 for ei in e ])
        for l in ecatd['lates']:
          lcat.extend([ li / 3600000.0 for li in l])

        #plt.subplot(6, 1, ci + 4)
        plt.subplot(1, 3, ci + 1)
        n, bins, hpatches = plt.hist(ecat, bins = nbins,
                                     histtype = 'stepfilled', color = 'k',
                                     align = 'mid', label = 'early')
        plt.setp(hpatches, 'facecolor', 'w')
        nmax = max(n)
        plt.hold(True)
        n, bins, hpatches = plt.hist(lcat, bins = nbins,
                                     histtype = 'stepfilled', color = 'k',
                                     align = 'mid', label = 'late')
        plt.setp(hpatches, 'facecolor', 'k')
        nmax = max(max(n), nmax)
        plt.ylim([0, 1.2 * nmax])

        plate = len(lcat) / float(len(lcat) + len(ecat))
        """
        plt.text(-9.75, nmax, '$P(early) = %.2f$' % (1 - plate),
                  size = 10)
        plt.text(-9.75, 0.8 * nmax, 
                  '$P(late) = %.2f$' % plate, size = 10)
        plt.text(-9.75, 0.6 * nmax,
                  r'$\langle early \rangle = %.2f \; hrs.$' % (-np.mean(ecat)),
                  size = 10)
        plt.text(-9.75, 0.4 * nmax,
                  r'$\langle late \rangle = %.2f \; hrs.$' % (np.mean(lcat)),
                  size = 10)
        """
        plt.title(r'$FPR=%.2f$, $TPR=%.2f$, $P(early)=%.2f$, $\langle early \rangle=%.2f hrs$' % 
                  (cat_means[category][0], cat_means[category][1], 1 - plate,
                  -np.mean(ecat)), size = 16)
        if ci == 2:
          plt.xlabel('hours late', size = 16)
        plt.ylabel('count', size = 16)
        
        if ci == 0:
          plt.legend(loc = 1)

      plt.gcf().subplots_adjust(top = 0.96, bottom = 0.05, hspace = 0.30)
      #plt.savefig('fig/final/early_vs_roc.eps')
      break

    plot_rates_partitioned = False
    if plot_rates_partitioned:
      for category in rates_partitioned:
        print category
        cat_params = rates_partitioned[category].keys()
        attr_to_values = {}
        for attr in all_attrs:
          attr_to_values[attr] = []
          for params in cat_params:
            attr_to_values[attr].append(params._asdict()[attr])

        cat_mean_attr_values = dict([ [attr, np.mean(attr_to_values[attr])]
                                      for attr in attr_to_values
                                      if attr_to_values[attr] ])
        pp.pprint(cat_mean_attr_values)
      break

    # Partition delta ranked results into top, bottom, center.
    plot_deltas_partitioned = False
    if plot_deltas_partitioned:
      plt.figure(figsize = (6,9))
      plt.suptitle(var_attr)
      for (ci, category) in enumerate(deltas_partitioned):
        print category
        deltas_cat = deltas_partitioned[category]
        delta_fprs_cat = []
        for params in deltas_cat:
          delta_fprs_cat.extend(deltas_cat[params][0]) 
        delta_tprs_cat = []
        for params in deltas_cat:
          delta_tprs_cat.extend(deltas_cat[params][1]) 

        nbins = 30
        if delta_fprs_cat:
          delta_fprs_cat = np.array(delta_fprs_cat)
          print 'mean delta fprs = %.4f' % np.mean(delta_fprs_cat)

          """
          pos_ind = np.where(delta_fprs_cat > 0)[0]
          neg_ind = np.where(delta_fprs_cat < 0)[0]

          print 'overall:\t%.4f +/- %.4f' % (np.mean(delta_fprs_cat),
                                             np.std(delta_fprs_cat))
          if len(pos_ind) > 0:
            print 'positive change:\t%.4f +/- %.4f\t(%.2f%%)' % (np.mean(delta_fprs_cat[pos_ind]),
              np.std(delta_fprs_cat[pos_ind]),
              100 * float(len(delta_fprs_cat[pos_ind])) / len(delta_fprs_cat))
          if len(neg_ind) > 0:
            print 'negative change:\t%.4f +/- %.4f\t(%.2f%%)' % (np.mean(delta_fprs_cat[neg_ind]),
              np.std(delta_fprs_cat[neg_ind]),
              100 * float(len(delta_fprs_cat[neg_ind])) / len(delta_fprs_cat))
          print 'no change:\t(%.2f%%)' % \
              (100 * float(len(delta_fprs_cat) - len(pos_ind) - len(neg_ind)) / len(delta_fprs_cat))
          """

          """
          plt.subplot(3, 2, 2 * ci + 1)
          plt.hist(delta_fprs_cat, bins = nbins)
          plt.title(category + '$\Delta_p^{FPR}$')
          """
        else:
          print 'None'

        if delta_tprs_cat:
          delta_tprs_cat = np.array(delta_tprs_cat)
          print 'mean delta tprs = %.4f' % np.mean(delta_tprs_cat)

          """
          pos_ind = np.where(delta_tprs_cat > 0)[0]
          neg_ind = np.where(delta_tprs_cat < 0)[0]

          print 'overall:\t%.4f +/- %.4f' % (np.mean(delta_tprs_cat),
                                             np.std(delta_tprs_cat))
          
          if len(pos_ind) > 0:
            print 'positive change:\t%.4f +/- %.4f\t(%.2f%%)' % (np.mean(delta_tprs_cat[pos_ind]),
              np.std(delta_tprs_cat[pos_ind]),
              100 * float(len(delta_tprs_cat[pos_ind])) / len(delta_tprs_cat))
          if len(neg_ind) > 0:
            print 'negative change:\t%.4f +/- %.4f\t(%.2f%%)' % (np.mean(delta_tprs_cat[neg_ind]),
              np.std(delta_tprs_cat[neg_ind]),
              100 * float(len(delta_tprs_cat[neg_ind])) / len(delta_tprs_cat))
          print 'no change:\t(%.2f%%)' % \
              (100 * float(len(delta_tprs_cat) - len(pos_ind) - len(neg_ind)) / len(delta_tprs_cat))
          """

          """
          plt.subplot(3, 2, 2 * ci + 2)
          plt.hist(delta_tprs_cat, bins = nbins)
          plt.title(category + '$\Delta_p^{TPR}$')
          """
        else:
          print 'None'

      plt.gcf().subplots_adjust(bottom = 0.08, hspace = 0.40)

    # Plot a heatmap of positions in plane for each var_attr
    plot_roc_plane_scatter = False
    if plot_roc_plane_scatter:
      expname = 'threshold_3'
      if not os.path.exists(os.path.join('fig/final/position', expname)):
        os.mkdir(os.path.join('fig/final/position', expname))
      if not os.path.exists(os.path.join('fig/final/position', expname, var_attr)):
        os.mkdir(os.path.join('fig/final/position', expname, var_attr))
      for (cai, const_attr) in enumerate(const_attrs):
        const_attr_values = const_attrs_allowed_values[const_attr]
        plt.figure(figsize = (13, 3))
        for (cavi, const_attr_value) in enumerate(const_attr_values):
          print const_attr, '=', const_attr_value
          udr_fprs = []
          udr_tprs = []
          fprs_for_const_attr_value = []
          fprs_for_const_attr_value = []
          entries_for_const_attr_value = \
              [ updown_rank[udr] for udr in updown_rank 
                if udr._asdict()[const_attr] == const_attr_value ]
          if not entries_for_const_attr_value:
            # It is possible that for the given const_attr_value, none of deltas
            # made the criterion for inclusion. For example, they might have
            # been skipped because they corresponded to points on the ROC curve
            # "stuck" near (0,0) or (1,1), for which deltas are meaningless.
            print 'No matches for ', const_attr, '=', const_attr_value
            continue
          for udr in entries_for_const_attr_value:
            udr_fprs.extend(np.array(udr[4]).flatten())
            udr_tprs.extend(np.array(udr[5]).flatten())
          """
          heatmap, xedges, yedges = np.histogram2d(udr_tprs,
            udr_fprs, bins=40, range = [[0,1], [0,1]])
          heatmap = np.log(heatmap + 0.1)
          extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
          plt.imshow(np.flipud(heatmap), extent = extent)
          """
          plt.subplot(1, 4, cavi + 1)
          plt.scatter(udr_fprs, udr_tprs)
          plt.hold(True)
          mean_udr_fprs = np.mean(udr_fprs)
          mean_udr_tprs = np.mean(udr_tprs)
          print '%.2f,%.2f' % (mean_udr_fprs, mean_udr_tprs)
          plt.axvline(mean_udr_fprs, lw = 1, ls = '--', color = 'k')
          plt.axhline(mean_udr_tprs, lw = 1, ls = '--', color = 'k')
          plt.text(0.4, 0.15, '$FPR_{mean} = %.2f$' % mean_udr_fprs)
          plt.text(0.4, 0.05, '$TPR_{mean} = %.2f$' % mean_udr_tprs)
          plt.xlim([-.1,1.1])
          plt.ylim([-.1,1.1])
          if cavi == 0:
            plt.ylabel('$TPR$')
          plt.xlabel('$FPR$')
          plt.title(const_attr + '=' + str(const_attr_value))
          plt.grid(True)
          plt.hold(False)
          plt.draw()
        plt.gcf().subplots_adjust(left = 0.08, bottom = 0.15, right = 0.94,
                                  wspace = 0.30, hspace = 0.20)
        plt.savefig(os.path.join('fig/final/position/', expname,
                                 var_attr, const_attr + '.eps'))
        #raw_input()

    # For current var_attr, compute rank of all other parameters by how far "up"
    # or "down" the ROC curve lies in the continuum of FPR/TPR tradeoffs.
    updown_rank = [ [udr, updown_rank[udr]] for udr in updown_rank ]
    updown_rank_f_min = sorted(updown_rank, key = lambda x: x[1][0], reverse = True)
    updown_rank_t_min = sorted(updown_rank, key = lambda x: x[1][1], reverse = True)
    updown_rank_f_max = sorted(updown_rank, key = lambda x: x[1][2], reverse = True)
    updown_rank_t_max = sorted(updown_rank, key = lambda x: x[1][3], reverse = True)
    
    plot_roc_updown_ranked = False
    if plot_roc_updown_ranked:
      for updown_rank_sorted in [ updown_rank_f_min, updown_rank_f_max ]:
        plt.figure(figsize = (10,10))
        print '\n\n'
        for udri, udr in enumerate(updown_rank_sorted):
          if udri >= 25:
            break

          udr_params = udr[0]
          udr_f_min_score = udr[1][0]
          udr_t_min_score = udr[1][1]
          udr_f_max_score = udr[1][2]
          udr_t_max_score = udr[1][3]
          print 'upr_f_min_score', udr_f_min_score
          print 'upr_t_min_score', udr_t_min_score
          print 'upr_f_max_score', udr_f_max_score
          print 'upr_t_max_score', udr_t_max_score
          udr_all_fprs = udr[1][4]
          udr_all_tprs = udr[1][5]
          udr_mean_fprs = [ np.mean(daf) for daf in udr_all_fprs ]
          udr_mean_tprs = [ np.mean(dat) for dat in udr_all_tprs ]
          udr_std_fprs = [ np.std(daf) for daf in udr_all_fprs ]
          udr_std_tprs = [ np.std(dat) for dat in udr_all_tprs ]
          
          # Plot thumbnails.
          plt.subplot(5, 5, udri + 1)
          plt.errorbar(udr_mean_fprs, udr_mean_tprs, udr_std_fprs, udr_std_tprs,
                       linestyle = 'None', color = 'b')
          plt.hold(True)
          plt.scatter(udr_mean_fprs, udr_mean_tprs,
                      s = [ 2 + 15 * i for i in range(len(udr_mean_tprs)) ],
                      c = 'b')
          # __str__() doesn't work for some reason... TODO
          plt.title(str([udr_params._asdict()[k]
                         for k in const_attrs]), fontsize = 11)
          plt.setp(plt.gca(), xticklabels=[])
          plt.setp(plt.gca(), yticklabels=[])
          print udr_params.__str__
          plt.xlim([-0.1, 1.1])
          plt.ylim([-0.1, 1.1])
          plt.grid(True)
          plt.hold(False)
          plt.draw()

        raw_input()

    # For current var_attr, compute rank of all other parameters by how much
    # positive and negative delta fprs and delta tprs they cause.
    delta_rank = [ [drk, delta_rank[drk]] for drk in delta_rank ]
    delta_rank_f = sorted(delta_rank, key = lambda x: x[1][0],
                          reverse = True)
    delta_rank_t = sorted(delta_rank, key = lambda x: x[1][1],
                          reverse = True)
    # print '\n\ndelta_rank_f'
    # pp.pprint(delta_rank_f)
    # print '\n\ndelta_rank_t'
    # pp.pprint(delta_rank_t)

    plot_roc_delta_ranked = False
    if plot_roc_delta_ranked:
      for delta_rank_sorted in [ delta_rank_f ]:
        print '\n\n'
        # plt.figure(figsize = (10,10))
        plt.figure(figsize = (10,5))
        plt.suptitle('Top $\Delta_p^{FPR}$ for ' + var_attr + \
                       '\nconstant params = ' + str(const_attrs))
        for dri, dr in enumerate(delta_rank_sorted):
          if dri >= 8:
            break
          dr_params = dr[0]
          dr_fscore = dr[1][0]
          dr_tscore = dr[1][1]
          print 'delta fpr score', dr_fscore, 'delta tpr score', dr_tscore
          dr_all_fprs = dr[1][2]
          dr_all_tprs = dr[1][3]
          dr_mean_fprs = [ np.mean(daf) for daf in dr_all_fprs ]
          dr_mean_tprs = [ np.mean(dat) for dat in dr_all_tprs ]
          dr_std_fprs = [ np.std(daf) for daf in dr_all_fprs ]
          dr_std_tprs = [ np.std(dat) for dat in dr_all_tprs ]

          plt.subplot(2, 4, dri + 1)
          #plt.subplot(4, 4, dri + 1)
          plt.errorbar(dr_mean_fprs, dr_mean_tprs, xerr=dr_std_fprs, yerr=dr_std_tprs,
                       linestyle = 'None', color = 'b')
          plt.hold(True)
          plt.scatter(dr_mean_fprs, dr_mean_tprs,
                      s = [ 2 + 15 * i for i in range(len(dr_mean_tprs)) ],
                      c = 'b')
          plt.title(str([dr_params._asdict()[k]
                         for k in const_attrs]), fontsize = 11)
          # __str__() doesn't work for some reason... TODO
          print dr_params.__str__
          plt.xlim([-0.1, 1.1])
          plt.ylim([-0.1, 1.1])
          plt.xlabel('$FPR$')
          if dri == 0:
            plt.ylabel('$TPR$')
          plt.grid(True)
          plt.draw()
          #plt.setp(plt.gca(), xticklabels=[])
          #plt.setp(plt.gca(), yticklabels=[])
          plt.hold(False)
        plt.gcf().subplots_adjust(top = 0.80, wspace = 0.45, hspace = 0.45)
        plt.savefig('fig/final/top_fpr/' + var_attr + '.eps')
        plt.draw()

    plot_secondary_effect = False
    if plot_secondary_effect:
      for const_attr in const_attrs:
        const_attr_values_f = [ dr[0]._asdict()[const_attr]
                              for dr in delta_rank_f ]
        rank_scores_f = [dr[1][0] for dr in delta_rank_f]
        const_attr_values_t = [ dr[0]._asdict()[const_attr]
                                for dr in delta_rank_t ]
        rank_scores_t = [dr[1][1] for dr in delta_rank_t]

        plt.subplot(121)
        plt.scatter(const_attr_values_f, rank_scores_f)
        plt.hold(True)
        plt.axhline(0, linestyle = '--', color = 'k')
        plt.title('fpr ' + const_attr)
        plt.hold(False)

        plt.subplot(122)
        plt.scatter(const_attr_values_t, rank_scores_t)
        plt.hold(True)
        plt.axhline(0, linestyle = '--', color = 'k')
        plt.title('tpr ' + const_attr)
        plt.hold(False)
        raw_input()

    plot_secondary_effect_hist = False
    if plot_secondary_effect_hist:
      for (cai, const_attr) in enumerate(const_attrs):
        plt.figure(figsize = (14, 3))
        const_attr_values = const_attrs_allowed_values[const_attr]
        # Store all heatmaps and extents, get a common extent, and plot all
        # heatmaps together at the end.
        dr_all_delta_fprs = []
        dr_all_delta_tprs = []
        ranges = []
        for (cavi, const_attr_value) in enumerate(const_attr_values):
          print const_attr, '=', const_attr_value
          dr_delta_fprs = []
          dr_delta_tprs = []
          entries_for_const_attr_value = \
              [ drf[1] for drf in delta_rank_f 
                if drf[0]._asdict()[const_attr] == const_attr_value ]
          if not entries_for_const_attr_value:
            # It is possible that for the given const_attr_value, none of deltas
            # made the criterion for inclusion. For example, they might have
            # been skipped because they corresponded to points on the ROC curve
            # "stuck" near (0,0) or (1,1), for which deltas are meaningless.
            continue
          for dr in entries_for_const_attr_value:
            dr_delta_fprs.extend(dr[4])
            dr_delta_tprs.extend(dr[5])

          #plt.subplot(223)
          dr_all_delta_fprs.append(dr_delta_fprs)
          dr_all_delta_tprs.append(dr_delta_tprs)
          ranges.append([[min(dr_delta_fprs), max(dr_delta_fprs)],
                         [min(dr_delta_tprs), max(dr_delta_tprs)]])

          """
          plt.subplot(334)
          n, bins, hpatches = plt.hist(dr_delta_tprs, bins = 30, normed = False,
                                       histtype = 'stepfilled', color = 'k',
                                       align = 'mid', orientation = 'horizontal')
          plt.setp(hpatches, 'facecolor', 'm')
          plt.axhline(0, linestyle = '--', color = 'k')
          plt.title('$\Delta_p^{TPR}$')
          plt.setp(plt.gca(), xticklabels=[])
          plt.setp(plt.gca(), yticklabels=[])

          plt.subplot(221)
          n, bins, hpatches = plt.hist(dr_delta_fprs, bins = 30, normed = False,
                                       histtype = 'stepfilled', color = 'k',
                                       align = 'mid')
          plt.setp(hpatches, 'facecolor', 'm')
          plt.axvline(0, linestyle = '--', color = 'k')
          plt.title('$\Delta_p^{FPR}$')
          plt.setp(plt.gca(), xticklabels=[])
          plt.setp(plt.gca(), yticklabels=[])
          """
        """
        miny = min([ extent[0] for extent in extents ])
        maxy = max([ extent[1] for extent in extents ])
        minx = min([ extent[2] for extent in extents ])
        maxx = max([ extent[3] for extent in extents ])
        
        extent = [miny, maxy, minx, maxx]
        """
        xmin = min([ rangei[0][0] for rangei in ranges ])
        xmax = max([ rangei[0][1] for rangei in ranges ])
        ymin = min([ rangei[1][0] for rangei in ranges ])
        ymax = max([ rangei[1][1] for rangei in ranges ])
        common_range = [[ymin, ymax], [xmin, xmax]]
        for ri in xrange(len(ranges)):
          dr_delta_fprs = dr_all_delta_fprs[ri]
          dr_delta_tprs = dr_all_delta_tprs[ri]
          plt.subplot(1, 4, ri + 1)
          heatmap, xedges, yedges = np.histogram2d(dr_delta_tprs,
            dr_delta_fprs, bins=25, range = common_range)
          extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
          plt.contour(heatmap, 20, extent = extent)
          plt.hold(True)
          plt.axvline(0, ls = '--', lw = 1.5, color = 'k')
          plt.axhline(0, ls = '--', lw = 1.5, color = 'k')
          plt.axvline(np.mean(dr_delta_fprs), ls = ':',
                      color = 'k')
          plt.axhline(np.mean(dr_delta_tprs), ls = ':',
                      color = 'k')
          plt.title(const_attr + '=' + str(const_attr_values[ri]))
          if ri == 0:
            plt.ylabel('$\Delta_p^{TPR}$', fontsize = 15)
          plt.xlabel('$\Delta_p^{FPR}$', fontsize = 15)
        plt.gcf().subplots_adjust(bottom = 0.20, wspace = 0.35)
        plt.draw()
        #plt.savefig('fig/final/delta_hist_sec/' + var_attr + '/' + \
        #              const_attr + '.eps')
        raw_input()

    # Plot deltas in fpr and tpr as 2d histogram.
    plot_delta_dist = False
    if plot_delta_dist and delta_fprs and delta_tprs:
      plt.figure(figsize = (6,6))
      plt.suptitle(var_attr)

      plt.subplot(223)
      heatmap, xedges, yedges = np.histogram2d(delta_tprs, delta_fprs, bins=30)
      extent = [yedges[0], yedges[-1], xedges[0], xedges[-1]]
      plt.contour(heatmap, 35, extent=extent)
      plt.hold(True)
      plt.axvline(0, linestyle = '--', color = 'k')
      plt.axhline(0, linestyle = '--', color = 'k')

      plt.subplot(224)
      n, bins, hpatches = plt.hist(delta_tprs, bins = 30, normed = False,
                                   histtype = 'stepfilled', color = 'k',
                                   align = 'mid', orientation = 'horizontal')
      plt.setp(hpatches, 'facecolor', 'm')
      plt.axhline(0, linestyle = '--', color = 'k')
      plt.title('$\Delta_p^{TPR}$')
      #plt.title('\Delta_p^{TPR}')

      plt.subplot(221)
      n, bins, hpatches = plt.hist(delta_fprs, bins = 30, normed = False,
                                   histtype = 'stepfilled', color = 'k',
                                   align = 'mid')
      plt.setp(hpatches, 'facecolor', 'm')
      plt.axvline(0, linestyle = '--', color = 'k')
      plt.title('$\Delta_p^{FPR}$')
      #plt.title('\Delta_p^{FPR}')

  """
示例#20
0
def plotMarginal(result,
                 dim=0,
                 lineColor=[0, 105 / 255, 170 / 255],
                 lineWidth=2,
                 xLabel='',
                 yLabel='Marginal Density',
                 labelSize=15,
                 tufteAxis=False,
                 prior=True,
                 priorColor=[.7, .7, .7],
                 CIpatch=True,
                 plotPE=True,
                 axisHandle=None,
                 showImediate=True):
    """
    Plots the marginal for a single dimension.
    result       should be a result struct from the main psignifit routine
    dim          is the parameter to plot:
                   1=threshold, 2=width, 3=lambda, 4=gamma, 5=sigma
    """
    from utils import strToDim
    if isinstance(dim, str): dim = strToDim(dim)

    if len(result['marginals'][dim]) <= 1:
        print(
            'Error: The parameter you wanted to plot was fixed in the analysis!'
        )
        return
    if axisHandle == None: axisHandle = plt.gca()
    try:
        plt.axes(axisHandle)
        plt.rc('text', usetex=True)
    except TypeError:
        raise ValueError('Invalid axes handle provided to plot in.')
    if not xLabel:
        if dim == 0: xLabel = 'Threshold'
        elif dim == 1: xLabel = 'Width'
        elif dim == 2: xLabel = r'$\lambda$'
        elif dim == 3: xLabel = r'$\gamma$'
        elif dim == 4: xLabel = r'$\eta$'

    x = result['marginalsX'][dim]
    marginal = result['marginals'][dim]
    CI = np.hstack(result['conf_Intervals'][dim].T)
    Fit = result['Fit'][dim]

    holdState = plt.ishold()
    if not holdState: plt.cla()
    plt.hold(True)

    # patch for confidence region
    if CIpatch:
        xCI = np.array([CI[0], CI[1], CI[1], CI[0]])
        xCI = np.insert(xCI, 1, x[np.logical_and(x >= CI[0], x <= CI[1])])
        yCI = np.array([
            np.interp(CI[0], x, marginal),
            np.interp(CI[1], x, marginal), 0, 0
        ])
        yCI = np.insert(yCI, 1,
                        marginal[np.logical_and(x >= CI[0], x <= CI[1])])
        from matplotlib.patches import Polygon as patch
        color = .5 * np.array(lineColor) + .5 * np.array([1, 1, 1])
        axisHandle.add_patch(patch(np.array([xCI, yCI]).T, fc=color, ec=color))

    # plot prior
    if prior:
        xprior = np.linspace(min(x), max(x), 1000)
        plt.plot(xprior,
                 result['options']['priors'][dim](xprior),
                 '--',
                 c=priorColor,
                 clip_on=False)

    # posterior
    plt.plot(x, marginal, lw=lineWidth, c=lineColor, clip_on=False)
    # point estimate
    if plotPE:
        plt.plot([Fit, Fit], [0, np.interp(Fit, x, marginal)],
                 'k',
                 clip_on=False)

    plt.xlabel(xLabel, fontsize=labelSize, visible=True)
    plt.ylabel(yLabel, fontsize=labelSize, visible=True)
    plt.tick_params(direction='out', right='off', top='off')
    for side in ['top', 'right']:
        axisHandle.spines[side].set_visible(False)
    plt.ticklabel_format(style='sci', scilimits=(-2, 4))

    plt.hold(holdState)
    if (showImediate):
        plt.xlim([min(x), max(x)])
        plt.ylim([0, 1.1 * max(marginal)])
        plt.show()
    return axisHandle
示例#21
0
文件: draw.py 项目: pomme-abricot/mtg
def draw(g, pos=None, ax=None, hold=None, ax_size=(0, 0, 1, 1), **kwds):
    """Draw the graph g with Matplotlib.

    Draw the graph as a simple representation with no node
    labels or edge labels and using the full Matplotlib figure area
    and no axis labels by default.  See draw_mtg() for more
    full-featured drawing that allows title, axis labels etc.

    Parameters
    ----------
    g : graph
       A MTG graph

    pos : dictionary, optional
       A dictionary with nodes as keys and positions as values.
       If not specified a spring layout positioning will be computed.
       See mtg.layout for functions that compute node positions.

    ax : Matplotlib Axes object, optional
       Draw the graph in specified Matplotlib axes.

    hold : bool, optional
       Set the Matplotlib hold state.  If True subsequent draw
       commands will be added to the current axes.

    **kwds : optional keywords
       See draw.draw_mtg() for a description of optional keywords.

    Examples
    --------
    >>> g = om.random_mtg()
    >>> draw.draw(g)
    >>> draw.draw(g,pos=om.spring_layout(G)) # use spring layout

    See Also
    --------
    draw_mtg()
    draw_mtg_vertices()
    draw_mtg_edges()
    draw_mtg_labels()
    draw_mtg_edge_labels()

    Notes
    -----
    This function has the same name as pylab.draw and pyplot.draw
    so beware when using

    >>> from openalea.mtg.draw import *

    since you might overwrite the pylab.draw function.

    With pyplot use

    >>> import matplotlib.pyplot as plt
    >>> import openalea.mtg as om
    >>> g=om.random_mtg()
    >>> om.draw(g)  # mtg draw()
    >>> plt.draw()  # pyplot draw()

    Also see the openalea.mtg drawing examples at
    openalea gallery.
    """
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        raise ImportError("Matplotlib required for draw()")
    except RuntimeError:
        print("Matplotlib unable to open display")
        raise

    if ax is None:
        cf = plt.gcf()
    else:
        cf = ax.get_figure()
    cf.set_facecolor('w')
    if ax is None:
        if cf._axstack() is None:
            ax = cf.add_axes(ax_size)
        else:
            ax = cf.gca()

# allow callers to override the hold state by passing hold=True|False
    b = plt.ishold()
    h = kwds.pop('hold', None)
    if h is not None:
        plt.hold(h)
    try:
        draw_mtg(g, pos=pos, ax=ax, **kwds)
        ax.set_axis_off()
        plt.draw_if_interactive()
    except:
        plt.hold(b)
        raise
    plt.hold(b)
    return
示例#22
0
def plotMarginal(result,                  
                 dim        = 0,
                 lineColor  = [0, 105/255, 170/255],
                 lineWidth  = 2,
                 xLabel     = '',
                 yLabel     = 'Marginal Density',
                 labelSize  = 15,
                 tufteAxis  = False,
                 prior      = True,
                 priorColor = [.7, .7, .7],
                 CIpatch    = True,
                 plotPE     = True,
                 axisHandle = None):
    """
    Plots the marginal for a single dimension.
    result       should be a result struct from the main psignifit routine
    dim          is the parameter to plot:
                   1=threshold, 2=width, 3=lambda, 4=gamma, 5=sigma
    """
    from utils import strToDim
    if isinstance(dim,str): dim = strToDim(dim)

    if len(result['marginals'][dim]) <= 1:
        print('Error: The parameter you wanted to plot was fixed in the analysis!')
        return
    if axisHandle == None: axisHandle = plt.gca()
    try:
        plt.axes(axisHandle)
        plt.rc('text', usetex=True)
    except TypeError:
        raise ValueError('Invalid axes handle provided to plot in.')
    if not xLabel:
        if   dim == 0: xLabel = 'Threshold'
        elif dim == 1: xLabel = 'Width'
        elif dim == 2: xLabel = r'$\lambda$'
        elif dim == 3: xLabel = r'$\gamma$'
        elif dim == 4: xLabel = r'$\eta$'
    
    x        = result['marginalsX'][dim]
    marginal = result['marginals'][dim]
    CI       = np.hstack(result['conf_Intervals'][dim].T)
    Fit      = result['Fit'][dim]
    
    holdState = plt.ishold()
    if not holdState: plt.cla()
    plt.hold(True)
    
    # patch for confidence region
    if CIpatch:
        xCI = np.array([CI[0], CI[1], CI[1], CI[0]])
        xCI = np.insert(xCI, 1, x[np.logical_and(x>=CI[0], x<=CI[1])])
        yCI = np.array([np.interp(CI[0], x, marginal), np.interp(CI[1], x, marginal), 0, 0])
        yCI = np.insert(yCI, 1, marginal[np.logical_and(x>=CI[0], x<=CI[1])])
        from matplotlib.patches import Polygon as patch
        color = .5*np.array(lineColor) + .5* np.array([1,1,1])
        axisHandle.add_patch(patch(np.array([xCI,yCI]).T, fc=color, ec=color))
    
    # plot prior
    if prior:
        xprior = np.linspace(min(x), max(x), 1000)
        plt.plot(xprior, result['options']['priors'][dim](xprior), '--', c=priorColor, clip_on=False)
    
    # posterior
    plt.plot(x, marginal, lw=lineWidth, c=lineColor, clip_on=False)
    # point estimate
    if plotPE:
        plt.plot([Fit,Fit], [0, np.interp(Fit, x, marginal)], 'k', clip_on=False)
    
    plt.xlim([min(x), max(x)])
    plt.ylim([0, 1.1*max(marginal)])
    
    plt.xlabel(xLabel, fontsize=labelSize, visible=True)
    # if tufteAxis
    plt.ylabel(yLabel, fontsize=labelSize, visible=True)
    # if tufteAxis
    # else:
    plt.tick_params(direction='out', right='off', top='off')
    for side in ['top','right']: axisHandle.spines[side].set_visible(False)
    plt.ticklabel_format(style='sci', scilimits=(-2,4))
    
    plt.hold(holdState)
    plt.show()
    return axisHandle
示例#23
0
def plotPsych(result,
              dataColor      = [0, 105./255, 170./255],
              plotData       = True,
              lineColor      = [0, 0, 0],
              lineWidth      = 2,
              xLabel         = 'Stimulus Level',
              yLabel         = 'Proportion Correct',
              labelSize      = 15,
              fontSize       = 10,
              fontName       = 'Helvetica',
              tufteAxis      = False,
              plotAsymptote  = True,
              plotThresh     = True,
              aspectRatio    = False,
              extrapolLength = .2,
              CIthresh       = False,
              dataSize       = 0,
              axisHandle     = None):
    """
    This function produces a plot of the fitted psychometric function with 
    the data.
    """
    
    fit = result['Fit']
    data = result['data']
    options = result['options']
    
    if axisHandle == None: axisHandle = plt.gca()
    try:
        plt.axes(axisHandle)
    except TypeError:
        raise ValueError('Invalid axes handle provided to plot in.')
    
    if np.isnan(fit[3]): fit[3] = fit[2]
    if data.size == 0: return
    if dataSize == 0: dataSize = 10000. / np.sum(data[:,2])
    
    if 'nAFC' in options['expType']:
        ymin = 1. / options['expN']
        ymin = min([ymin, min(data[:,1] / data[:,2])])
    else:
        ymin = 0
    
    
    # PLOT DATA
    holdState = plt.ishold()
    if not holdState: plt.cla()
    plt.hold(True)
    xData = data[:,0]
    if plotData:
        yData = data[:,1] / data[:,2]
        markerSize = np.sqrt(dataSize/2 * data[:,2])
        for i in range(len(xData)):
            plt.plot(xData[i], yData[i], '.', ms=markerSize[i], c=dataColor, clip_on=False)
    
    # PLOT FITTED FUNCTION
    if options['logspace']:
        xMin = np.log(min(xData))
        xMax = np.log(max(xData))
        xLength = xMax - xMin
        x       = np.exp(np.linspace(xMin, xMax, num=1000))
        xLow    = np.exp(np.linspace(xMin - extrapolLength*xLength, xMin, num=100))
        xHigh   = np.exp(np.linspace(xMax, xMax + extrapolLength*xLength, num=100))
        axisHandle.set_xscale('log')
    else:
        xMin = min(xData)
        xMax = max(xData)
        xLength = xMax - xMin
        x       = np.linspace(xMin, xMax, num=1000)
        xLow    = np.linspace(xMin - extrapolLength*xLength, xMin, num=100)
        xHigh   = np.linspace(xMax, xMax + extrapolLength*xLength, num=100)
    
    fitValuesLow  = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](xLow,  fit[0], fit[1]) + fit[3]
    fitValuesHigh = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](xHigh, fit[0], fit[1]) + fit[3]
    fitValues     = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](x,     fit[0], fit[1]) + fit[3]
    
    plt.plot(x,     fitValues,           c=lineColor, lw=lineWidth, clip_on=False)
    plt.plot(xLow,  fitValuesLow,  '--', c=lineColor, lw=lineWidth, clip_on=False)
    plt.plot(xHigh, fitValuesHigh, '--', c=lineColor, lw=lineWidth, clip_on=False)
    
    # PLOT PARAMETER ILLUSTRATIONS
    # THRESHOLD
    if plotThresh:
        if options['logspace']:
            x = [np.exp(fit[0]), np.exp(fit[0])]
        else:
            x = [fit[0], fit[0]]
        y = [ymin, fit[3] + (1 - fit[2] - fit[3]) * options['threshPC']]
        plt.plot(x, y, '-', c=lineColor)
    # ASYMPTOTES
    if plotAsymptote:
        plt.plot([min(xLow), max(xHigh)], [1-fit[2], 1-fit[2]], ':', c=lineColor, clip_on=False)
        plt.plot([min(xLow), max(xHigh)], [fit[3], fit[3]],     ':', c=lineColor, clip_on=False)
    # CI-THRESHOLD
    if CIthresh:
        CIs = result['confIntervals']
        y = np.array([fit[3] + .5*(1 - fit[2] - fit[3]) for i in range(2)])
        plt.plot(CIs[0,:,0],               y,               c=lineColor)
        plt.plot([CIs[0,0,0], CIs[0,0,0]], y + [-.01, .01], c=lineColor)
        plt.plot([CIs[0,1,0], CIs[0,1,0]], y + [-.01, .01], c=lineColor)
    
    #AXIS SETTINGS
    plt.axis('tight')
    plt.tick_params(labelsize=fontSize)
    plt.xlabel(xLabel, fontname=fontName, fontsize=labelSize)
    plt.ylabel(yLabel, fontname=fontName, fontsize=labelSize)
    if aspectRatio: axisHandle.set_aspect(2/(1 + np.sqrt(5)))

    plt.ylim([ymin, 1])
    # tried to mimic box('off') in matlab, as box('off') in python works differently
    plt.tick_params(direction='out',right='off',top='off')
    for side in ['top','right']: axisHandle.spines[side].set_visible(False)
    plt.ticklabel_format(style='sci',scilimits=(-2,4))
    
    plt.hold(holdState)
    plt.show() 
    return axisHandle
示例#24
0
def plotPZ(H, color='b', markersize=5, showlist=False):
    """
    Plots the poles and zeros of a transfer function.

    Parameters
    ----------
    H : tuple
        transfer function in pzk or nd form
    showlist : bool
        if showlist is true, a list of the poles and zeros is superimposed
        onto the plot.

    Other Parameters
    ----------------
    color : string or list of strings, optional
        color or colors to plot the poles and the zeros (defaults to 'b'
        meaning black)
    markersize : real, optional
        size of the markers used to represent the poles and the zeros
        (defaults to 5)

    Notes
    -----
    See `matplotlib` for info about color codes.

    """

    pole_fmt = {'marker': 'x', 'markersize': markersize}
    zero_fmt = {'marker': 'o', 'markersize': markersize}

    if isinstance(color, list):
        pole_fmt['color'] = color[0]
        zero_fmt['color'] = color[1]
    else:
        pole_fmt['color'] = color
        zero_fmt['color'] = color

    if len(H) == 2:
        H = tf2zpk(*H)
    z = cplxpair(H[0])
    p = cplxpair(H[1])

    hold_status = plt.ishold()
    plt.grid(True)

    # Plot x and o for poles and zeros, respectively
    plt.plot(p.real, p.imag, linestyle='None', **pole_fmt)
    plt.hold(True)
    if len(z) > 0:
        plt.plot(z.real, z.imag, linestyle='None', **zero_fmt)

    # Draw unit circle, real axis and imag axis
    circle = np.exp(2j*np.pi*np.linspace(0, 1, 100))
    plt.plot(circle.real, circle.imag, 'k')
    plt.axis('equal')
    limits = plt.axis()
    plt.plot([0, 0], limits[1:3], 'k:')
    plt.plot(limits[0:2], [0, 0], 'k:')

    if showlist:
        # List the poles and zeros
        pp = p[p.imag >= 0]
        y = 0.05*(len(pp)+1)
        str_p = 'Poles:'
        plt.text(-0.9, y, str_p,
                 horizontalalignment='left',
                 verticalalignment='center')
        y = y - 0.1
        for i in range(len(pp)):
            if pp[i].imag == 0:
                str_p = '$%+.4f$' % pp[i].real
            else:
                str_p = r'$%+.4f\pm \mathrm{j}%.4f$' % (pp[i].real, pp[i].imag)
            plt.text(-0.9, y, str_p,
                     horizontalalignment='left',
                     verticalalignment='center')
            y = y - 0.1
        if len(z) > 0:
            zz = z[z.imag >= 0]
            y = 0.05*(len(zz)+1)
            str_z = 'Zeros:'
            plt.text(0, y, str_z,
                     horizontalalignment='left',
                     verticalalignment='center')
            y = y - 0.1
            for i in range(len(zz)):
                if zz[i].imag == 0:
                    str_z = '$%+.4f$' % zz[i].real
                else:
                    str_z = (r'$%+.4f\pm \mathrm{j}%.4f$' %
                             (zz[i].real, zz[i].imag))
                plt.text(0, y, str_z,
                         horizontalalignment='left',
                         verticalalignment='center')
                y = y - 0.1

    plt.ylabel('Imag')
    plt.xlabel('Real')

    if not hold_status:
        plt.hold(False)
示例#25
0
def _plotonehist2(x,
                  y,
                  parx,
                  pary,
                  smooth=False,
                  colormap=True,
                  ranges={},
                  labels={},
                  bins=50,
                  levels=3,
                  weights=None,
                  color='k',
                  linewidth=1):
    hold = P.ishold()

    hrange = [
        ranges[parx] if parx in ranges else [N.min(x), N.max(x)],
        ranges[pary] if pary in ranges else [N.min(y), N.max(y)]
    ]

    [h, xs, ys] = N.histogram2d(x,
                                y,
                                bins=bins,
                                normed=True,
                                range=hrange,
                                weights=weights)
    if colormap:
        P.contourf(0.5 * (xs[1:] + xs[:-1]),
                   0.5 * (ys[1:] + ys[:-1]),
                   h.T,
                   cmap=P.get_cmap('YlOrBr'))
        P.hold(True)

    H, tmp1, tmp2 = N.histogram2d(x,
                                  y,
                                  bins=bins,
                                  range=hrange,
                                  weights=weights)

    if smooth:
        # only need scipy if we're smoothing
        import scipy.ndimage.filters as SNF
        H = SNF.gaussian_filter(H, sigma=1.5 if smooth is True else smooth)

    if weights is None:
        H = H / len(x)
    else:
        H = H / N.sum(H)  # I think this is right...
    Hflat = -N.sort(-H.flatten())  # sort highest to lowest
    cumprob = N.cumsum(Hflat)  # sum cumulative probability

    levels = [
        N.interp(level, cumprob, Hflat)
        for level in [0.6826, 0.9547, 0.9973][:levels]
    ]

    xs = N.linspace(hrange[0][0], hrange[0][1], bins)
    ys = N.linspace(hrange[1][0], hrange[1][1], bins)

    P.contour(xs,
              ys,
              H.T,
              levels,
              colors=color,
              linestyles=['-', '--', '-.'][:len(levels)],
              linewidths=linewidth)
    P.hold(hold)

    if parx in ranges:
        P.xlim(ranges[parx])
    if pary in ranges:
        P.ylim(ranges[pary])

    P.xlabel(labels[parx] if parx in labels else parx)
    P.ylabel(labels[pary] if pary in labels else pary)

    P.locator_params(axis='both', nbins=6)
    P.minorticks_on()

    fx = P.ScalarFormatter(useOffset=True, useMathText=True)
    fx.set_powerlimits((-3, 4))
    fx.set_scientific(True)

    fy = P.ScalarFormatter(useOffset=True, useMathText=True)
    fy.set_powerlimits((-3, 4))
    fy.set_scientific(True)

    P.gca().xaxis.set_major_formatter(fx)
    P.gca().yaxis.set_major_formatter(fy)
示例#26
0
xmax = max(datos[:, 0])

index = np.where(
    datos == xmax
)[0]  #conozco el inicio (o el final) de cada bloque, me devuelve la posicion en datos[]
#de cada elemento fin de bloque
n = len(
    index
)  #la longitud de este array me dara el numero total de bloques (imagenes)

imagenes = np.split(
    ary=datos, indices_or_sections=n,
    axis=0)  #divido datos en cada uno de los bloques (imagenes)
#axis=0 separa por filas (cada pareja de datos es una fila)
#imagenes[i] me dara la imagen en t=ti
#imagenes[0][:,0] me da las'x' de la imagen t=t0
#imagenes[0][:,1] me da las'y' de la imagen t=t0
ymin = min(
    imagenes[0][:,
                1])  #el min y max de la primera imagen sera max y min globales
ymax = max(imagenes[0][:, 1])

plt.figure()
plt.xlim((xmin, xmax))
plt.ylim((ymin, ymax))
status = plt.ishold()
if status == True:
    plt.hold()
for i in range(n):
    plt.plot(x=imagenes[i][:, 0], y=imagenes[i][:, 1])
    plt.show()
示例#27
0
def draw(G, pos=None, ax=None, hold=None, **kwds):
    """Draw the graph G with Matplotlib.

    Draw the graph as a simple representation with no node
    labels or edge labels and using the full Matplotlib figure area
    and no axis labels by default.  See draw_networkx() for more
    full-featured drawing that allows title, axis labels etc.

    Parameters
    ----------
    G : graph
       A networkx graph

    pos : dictionary, optional
       A dictionary with nodes as keys and positions as values.
       If not specified a spring layout positioning will be computed.
       See networkx.layout for functions that compute node positions.

    ax : Matplotlib Axes object, optional
       Draw the graph in specified Matplotlib axes.

    hold : bool, optional
       Set the Matplotlib hold state.  If True subsequent draw
       commands will be added to the current axes.

    **kwds : optional keywords
       See networkx.draw_networkx() for a description of optional keywords.


    Examples
    --------
    >>> G=nx.dodecahedral_graph()
    >>> nx.draw(G)
    >>> nx.draw(G,pos=nx.spring_layout(G)) # use spring layout

    See Also
    --------
    draw_networkx()
    draw_networkx_nodes()
    draw_networkx_edges()
    draw_networkx_labels()
    draw_networkx_edge_labels()

    Notes
    -----
    This function has the same name as pylab.draw and pyplot.draw
    so beware when using

    >>> from networkx import *

    since you might overwrite the pylab.draw function.

    With pyplot use

    >>> import matplotlib.pyplot as plt
    >>> import networkx as nx
    >>> G=nx.dodecahedral_graph()
    >>> nx.draw(G)  # networkx draw()
    >>> plt.draw()  # pyplot draw()

    Also see the NetworkX drawing examples at
    http://networkx.lanl.gov/gallery.html
    """
    try:
        import matplotlib.pyplot as plt
    except ImportError:
        raise ImportError("Matplotlib required for draw()")
    except RuntimeError:
        print("Matplotlib unable to open display")
        raise

    if ax is None:
        cf = plt.gcf()
    else:
        cf = ax.get_figure()
    cf.set_facecolor('w')
    if ax is None:
        if cf._axstack() is None:
            ax = cf.add_axes((0, 0, 1, 1))
        else:
            ax = cf.gca()

# allow callers to override the hold state by passing hold=True|False

    if 'with_labels' not in kwds:
        kwds['with_labels'] = False
    b = plt.ishold()
    h = kwds.pop('hold', None)
    if h is not None:
        plt.hold(h)
    try:
        draw_networkx(G, pos=pos, ax=ax, **kwds)
        ax.set_axis_off()
        plt.draw_if_interactive()
    except:
        plt.hold(b)
        raise
    plt.hold(b)
    return
示例#28
0
文件: pyppms.py 项目: thennen/pyppms
def live(loopnum='all', corr=False, fitrange=None, hold=False, legend=True):
    ''' Enter infinite loop that keeps plotting from the most recent data '''
    # passing hold=True will keep plots even when file changes
    path = latest()
    print('!Ctrl-C will set you free!')

    washeld = plt.ishold()
    if not washeld:
        # clear current axis if not hold
        plt.cla()

    ccycle = plt.rcParams['axes.color_cycle']
    if corr:
        data = ppms(path).corr(fitrange=fitrange)
    else:
        data = ppms(path)
    lines = data.plot(loopnum=loopnum)
    # give inital xlim and ylim
    try:
        plt.autoscale()
        maxH = np.max(np.abs(data.H[0]))
        maxM = np.max(np.abs(data.M[0]))
        xlimit = plt.xlim()
        ylimit = plt.ylim()
        # Change limit to +-max in data if that's bigger than autorange
        if -maxH < xlimit[0] or maxH > xlimit[1] or -maxM < ylimit[0] or maxM > ylimit[1]:
            plt.xlim((-maxH * 1.1, maxH * 1.1))
            plt.ylim((-maxM * 1.1, maxM * 1.1))
    except:
        pass
    timer = 1
    while True:
        data = ppms(path)
        title = '!' + os.path.split(path)[1] + '!'
        # get colors of last lines
        colors = [l.get_color() for l in lines]
        # clear the last lines
        for l in lines:
            l.remove()
        plt.hold(True)
        # replace lines with the updated ones
        if corr:
            lines = data.cplot(loopnum=loopnum)
        else:
            lines = data.plot(loopnum=loopnum)
        # making sure lines don't change color, and additional lines get the
        # right color ...
        for l, c in zip(lines, colors):
            l.set_color(c)
        if len(lines) > len(colors):
            try:
                nextcolori = (ccycle.index(c[-1]) + 1) % len(ccycle)
                nextcolor = ccycle[nextcolori]
                lines[-1].set_color(nextcolor)
            except:
                # give up
                pass

        if legend:
            plt.legend(loc='best')

        # leave plot in previous hold state during pause
        if not washeld:
            plt.hold(false)

        # display cute blinking ! ! around title
        if timer % 2:
            plt.title(title.replace('!',''))
        else:
            plt.title(title)

        if not timer % 20:
            # check again for latest filename
            oldpath = path
            path = latest()
            if not (path == oldpath) and hold:
                # redefine lines so the existing ones don't get clobbered
                plt.hold(True)
                lines = plt.plot()
        timer += 1
        plt.pause(2)
示例#29
0
def plotPsych(result,
              dataColor=[0, 105. / 255, 170. / 255],
              plotData=True,
              lineColor=[0, 0, 0],
              lineWidth=2,
              xLabel='Stimulus Level',
              yLabel='Proportion Correct',
              labelSize=15,
              fontSize=10,
              fontName='Helvetica',
              tufteAxis=False,
              plotAsymptote=True,
              plotThresh=True,
              aspectRatio=False,
              extrapolLength=.2,
              CIthresh=False,
              dataSize=0,
              axisHandle=None,
              showImediate=True):
    """
    This function produces a plot of the fitted psychometric function with 
    the data.
    """

    fit = result['Fit']
    data = result['data']
    options = result['options']

    if axisHandle == None: axisHandle = plt.gca()
    try:
        plt.axes(axisHandle)
    except TypeError:
        raise ValueError('Invalid axes handle provided to plot in.')

    if np.isnan(fit[3]): fit[3] = fit[2]
    if data.size == 0: return
    if dataSize == 0: dataSize = 10000. / np.sum(data[:, 2])

    if 'nAFC' in options['expType']:
        ymin = 1. / options['expN']
        ymin = min([ymin, min(data[:, 1] / data[:, 2])])
    else:
        ymin = 0

    # PLOT DATA
    holdState = plt.ishold()
    if not holdState:
        plt.cla()
        plt.hold(True)
    xData = data[:, 0]
    if plotData:
        yData = data[:, 1] / data[:, 2]
        markerSize = np.sqrt(dataSize / 2 * data[:, 2])
        for i in range(len(xData)):
            plt.plot(xData[i],
                     yData[i],
                     '.',
                     ms=markerSize[i],
                     c=dataColor,
                     clip_on=False)

    # PLOT FITTED FUNCTION
    if options['logspace']:
        xMin = np.log(min(xData))
        xMax = np.log(max(xData))
        xLength = xMax - xMin
        x = np.exp(np.linspace(xMin, xMax, num=1000))
        xLow = np.exp(
            np.linspace(xMin - extrapolLength * xLength, xMin, num=100))
        xHigh = np.exp(
            np.linspace(xMax, xMax + extrapolLength * xLength, num=100))
        axisHandle.set_xscale('log')
    else:
        xMin = min(xData)
        xMax = max(xData)
        xLength = xMax - xMin
        x = np.linspace(xMin, xMax, num=1000)
        xLow = np.linspace(xMin - extrapolLength * xLength, xMin, num=100)
        xHigh = np.linspace(xMax, xMax + extrapolLength * xLength, num=100)

    fitValuesLow = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](
        xLow, fit[0], fit[1]) + fit[3]
    fitValuesHigh = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](
        xHigh, fit[0], fit[1]) + fit[3]
    fitValues = (1 - fit[2] - fit[3]) * options['sigmoidHandle'](
        x, fit[0], fit[1]) + fit[3]

    plt.plot(x, fitValues, c=lineColor, lw=lineWidth, clip_on=False)
    plt.plot(xLow,
             fitValuesLow,
             '--',
             c=lineColor,
             lw=lineWidth,
             clip_on=False)
    plt.plot(xHigh,
             fitValuesHigh,
             '--',
             c=lineColor,
             lw=lineWidth,
             clip_on=False)

    # PLOT PARAMETER ILLUSTRATIONS
    # THRESHOLD
    if plotThresh:
        if options['logspace']:
            x = [np.exp(fit[0]), np.exp(fit[0])]
        else:
            x = [fit[0], fit[0]]
        y = [ymin, fit[3] + (1 - fit[2] - fit[3]) * options['threshPC']]
        plt.plot(x, y, '-', c=lineColor)
    # ASYMPTOTES
    if plotAsymptote:
        plt.plot([min(xLow), max(xHigh)], [1 - fit[2], 1 - fit[2]],
                 ':',
                 c=lineColor,
                 clip_on=False)
        plt.plot([min(xLow), max(xHigh)], [fit[3], fit[3]],
                 ':',
                 c=lineColor,
                 clip_on=False)
    # CI-THRESHOLD
    if CIthresh:
        CIs = result['confIntervals']
        y = np.array([fit[3] + .5 * (1 - fit[2] - fit[3]) for i in range(2)])
        plt.plot(CIs[0, :, 0], y, c=lineColor)
        plt.plot([CIs[0, 0, 0], CIs[0, 0, 0]], y + [-.01, .01], c=lineColor)
        plt.plot([CIs[0, 1, 0], CIs[0, 1, 0]], y + [-.01, .01], c=lineColor)

    #AXIS SETTINGS
    plt.axis('tight')
    plt.tick_params(labelsize=fontSize)
    plt.xlabel(xLabel, fontname=fontName, fontsize=labelSize)
    plt.ylabel(yLabel, fontname=fontName, fontsize=labelSize)
    if aspectRatio: axisHandle.set_aspect(2 / (1 + np.sqrt(5)))

    plt.ylim([ymin, 1])
    # tried to mimic box('off') in matlab, as box('off') in python works differently
    plt.tick_params(direction='out', right='off', top='off')
    for side in ['top', 'right']:
        axisHandle.spines[side].set_visible(False)
    plt.ticklabel_format(style='sci', scilimits=(-2, 4))

    plt.hold(holdState)
    if (showImediate):
        plt.show()
    return axisHandle
示例#30
0
def plotPZ(H, color='b', markersize=5, showlist=False):
    """
    Plots the poles and zeros of a transfer function.

    Parameters
    ----------
    H : tuple
        transfer function in pzk or nd form
    showlist : bool
        if showlist is true, a list of the poles and zeros is superimposed
        onto the plot.

    Other Parameters
    ----------------
    color : string or list of strings, optional
        color or colors to plot the poles and the zeros (defaults to 'b'
        meaning black)
    markersize : real, optional
        size of the markers used to represent the poles and the zeros
        (defaults to 5)

    Notes
    -----
    See `matplotlib` for info about color codes.

    """

    pole_fmt = {'marker': 'x', 'markersize': markersize}
    zero_fmt = {'marker': 'o', 'markersize': markersize}

    if isinstance(color, list):
        pole_fmt['color'] = color[0]
        zero_fmt['color'] = color[1]
    else:
        pole_fmt['color'] = color
        zero_fmt['color'] = color

    if len(H) == 2:
        H = tf2zpk(*H)
    z = cplxpair(H[0])
    p = cplxpair(H[1])

    hold_status = plt.ishold()
    plt.grid(True)

    # Plot x and o for poles and zeros, respectively
    plt.plot(p.real, p.imag, linestyle='None', **pole_fmt)
    plt.hold(True)
    if len(z) > 0:
        plt.plot(z.real, z.imag, linestyle='None', **zero_fmt)

    # Draw unit circle, real axis and imag axis
    circle = np.exp(2j * np.pi * np.linspace(0, 1, 100))
    plt.plot(circle.real, circle.imag, 'k')
    plt.axis('equal')
    limits = plt.axis()
    plt.plot([0, 0], limits[1:3], 'k:')
    plt.plot(limits[0:2], [0, 0], 'k:')

    if showlist:
        # List the poles and zeros
        pp = p[p.imag >= 0]
        y = 0.05 * (len(pp) + 1)
        str_p = 'Poles:'
        plt.text(-0.9,
                 y,
                 str_p,
                 horizontalalignment='left',
                 verticalalignment='center')
        y = y - 0.1
        for i in range(len(pp)):
            if pp[i].imag == 0:
                str_p = '$%+.4f$' % pp[i].real
            else:
                str_p = r'$%+.4f\pm \mathrm{j}%.4f$' % (pp[i].real, pp[i].imag)
            plt.text(-0.9,
                     y,
                     str_p,
                     horizontalalignment='left',
                     verticalalignment='center')
            y = y - 0.1
        if len(z) > 0:
            zz = z[z.imag >= 0]
            y = 0.05 * (len(zz) + 1)
            str_z = 'Zeros:'
            plt.text(0,
                     y,
                     str_z,
                     horizontalalignment='left',
                     verticalalignment='center')
            y = y - 0.1
            for i in range(len(zz)):
                if zz[i].imag == 0:
                    str_z = '$%+.4f$' % zz[i].real
                else:
                    str_z = (r'$%+.4f\pm \mathrm{j}%.4f$' %
                             (zz[i].real, zz[i].imag))
                plt.text(0,
                         y,
                         str_z,
                         horizontalalignment='left',
                         verticalalignment='center')
                y = y - 0.1

    plt.ylabel('Imag')
    plt.xlabel('Real')

    if not hold_status:
        plt.hold(False)