Beispiel #1
0
    def grid_data_and_contour(self):
        # griddata and contour.
        x = self._x
        y = self._y
        z = self._z
        n_labels = self._n_labels
        xi = self._xi
        yi = self._yi
        zi = self._zi
        x_min = self._x_min
        y_min = self._y_min
        x_max = self._x_max
        y_max = self._y_max

        plt.subplot(211)
        plt.contour(xi, yi, zi, n_labels, linewidths=0.5, colors='k')
        plt.contourf(xi,
                     yi,
                     zi,
                     n_labels,
                     cmap=plt.cm.rainbow,
                     norm=plt.normalize(vmax=abs(zi).max(),
                                        vmin=-abs(zi).max()))
        plt.colorbar()  # draw colorbar
        plt.plot(x, y, 'ko', ms=3)
        plt.xlim(x_min, x_max)
        plt.ylim(y_min, y_max)
        print('griddata and contour plotted')
Beispiel #2
0
def showOverlapTable(modes_x, modes_y, **kwargs):
    """Show overlap table using :func:`~matplotlib.pyplot.pcolor`.  *modes_x*
    and *modes_y* are sets of normal modes, and correspond to x and y axes of
    the plot.  Note that mode indices are incremented by 1.  List of modes
    is assumed to contain a set of contiguous modes from the same model.

    Default arguments for :func:`~matplotlib.pyplot.pcolor`:

      * ``cmap=plt.cm.jet``
      * ``norm=plt.normalize(0, 1)``"""

    import matplotlib.pyplot as plt

    overlap = abs(calcOverlap(modes_y, modes_x))
    if overlap.ndim == 0:
        overlap = np.array([[overlap]])
    elif overlap.ndim == 1:
        overlap = overlap.reshape((modes_y.numModes(), modes_x.numModes()))

    cmap = kwargs.pop('cmap', plt.cm.jet)
    norm = kwargs.pop('norm', plt.normalize(0, 1))
    show = (plt.pcolor(overlap, cmap=cmap, norm=norm,
                       **kwargs), plt.colorbar())
    x_range = np.arange(1, modes_x.numModes() + 1)
    plt.xticks(x_range - 0.5, x_range)
    plt.xlabel(str(modes_x))
    y_range = np.arange(1, modes_y.numModes() + 1)
    plt.yticks(y_range - 0.5, y_range)
    plt.ylabel(str(modes_y))
    plt.axis([0, modes_x.numModes(), 0, modes_y.numModes()])
    if SETTINGS['auto_show']:
        showFigure()
    return show
Beispiel #3
0
def plot_veg_types(yc, xc, Cv, baresoil):
    Projection_Parameters = projection

    labels = ['Evergreen Needleleaf', 'Evergreen Broadleaf',
              'Deciduous Needleleaf', 'Deciduous Broadleaf', 'Mixed Cover',
              'Woodland', 'Wooded Grasslands', 'Closed Shrublands',
              'Open Shrublands', 'Grasslands', 'Crop land', 'Bare Soil/Ice']

    fig = plt.figure(figsize=(10, 12))

    gs1 = gridspec.GridSpec(4, 3)

    for loc in xrange(11):
        ax = fig.add_subplot(gs1[loc])
        c = plot_map(ax, yc, xc, Cv[loc], Projection_Parameters, vmin=0,
                     cmap='Jet')
        ax.set_title(labels[loc])
    ax = fig.add_subplot(gs1[11])
    c = plot_map(ax, yc, xc, baresoil, Projection_Parameters, vmin=0,
                 cmap='Jet')
    ax.set_title(labels[11])

    sm = plt.cm.ScalarMappable(cmap='Jet', norm=plt.normalize(vmin=0, vmax=1))
    colorbar_ax = fig.add_axes([0.92, 0.1, 0.03, 0.8])
    sm._A = []
    plt.colorbar(sm, cax=colorbar_ax)
    fig.suptitle('Fraction of Vegetation Type', fontsize=20, fontweight='bold')
    fig.text(0.5, 0.93, 'Regional Arctic Climate Model',
             ha='center', fontsize=18)

    plt.show()
Beispiel #4
0
    def plot_tricontour(self):
        # tricontour.
        x = self._x
        y = self._y
        z = self._z
        n_labels = self._n_labels
        zi = self._zi
        x_min = self._x_min
        y_min = self._y_min
        x_max = self._x_max
        y_max = self._y_max
        title = self._title
        x_label = self._x_label
        y_label = self._y_label

        plt.subplot(111)  # change this if you want to plot both
        triang = tri.Triangulation(x, y)
        plt.tricontour(x, y, z, n_labels, linewidths=0.5, colors='k')
        plt.tricontourf(x,
                        y,
                        z,
                        n_labels,
                        cmap=plt.cm.rainbow,
                        norm=plt.normalize(vmax=abs(zi).max(),
                                           vmin=-abs(zi).max()))
        plt.colorbar()
        plt.plot(x, y, 'ko', ms=3)
        plt.xlim(x_min, x_max)
        plt.ylim(y_min, y_max)
        plt.xlabel(x_label)
        plt.ylabel(y_label)
        plt.title(title)
        print('tricontour plotted')
Beispiel #5
0
def normalize_colors(vmin, vmax, clip=False):
    """Helper to handle matplotlib API"""
    import matplotlib.pyplot as plt
    try:
        return plt.Normalize(vmin, vmax, clip=clip)
    except AttributeError:
        return plt.normalize(vmin, vmax, clip=clip)
Beispiel #6
0
def normalize_colors(vmin, vmax, clip=False):
    """Helper to handle matplotlib API"""
    import matplotlib.pyplot as plt
    if 'Normalize' in vars(plt):
        return plt.Normalize(vmin, vmax, clip=clip)
    else:
        return plt.normalize(vmin, vmax, clip=clip)
Beispiel #7
0
def normalize_colors(vmin, vmax, clip=False):
    """Helper to handle matplotlib API"""
    import matplotlib.pyplot as plt
    try:
        return plt.Normalize(vmin, vmax, clip=clip)
    except AttributeError:
        return plt.normalize(vmin, vmax, clip=clip)
Beispiel #8
0
def normalize_colors(vmin, vmax, clip=False):
    """Helper to handle matplotlib API"""
    import matplotlib.pyplot as plt
    if 'Normalize' in vars(plt):
        return plt.Normalize(vmin, vmax, clip=clip)
    else:
        return plt.normalize(vmin, vmax, clip=clip)
def buildHeatmap(dataDictionary,title,bartitle,barcolor,barmin,barmax):
    # Lambert Conformal map of USA lower 48 states
    geoMap = Basemap(llcrnrlon=-119, llcrnrlat=22, urcrnrlon=-64,
    urcrnrlat=49, projection='lcc', lat_1=33, lat_2=45,
    lon_0=-95, resolution='i', area_thresh=10000)
    # Uses a Shape File from the US Census Bureau
    # http://www2.census.gov/geo/tiger/GENZ2010/
    shapeInfo = geoMap.readshapefile('data/shape_files/gz_2010_us_040_00_500k','states',drawbounds=False)
    
    # Process based off an example from our professor's lecture slides,
    # James Bagrow - UVM Spring 2014
    # http://bagrow.com/dsv/
    # I took the base idea and modified it heavily for the needs 
    # of this project.
    # Color each state based on Population
    colors={}
    statenames=[]
    # Use the Green color map.
    # Others may be found here : http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps
    colorMap = barcolor
    heatmapMinAmt = barmin; heatmapMaxAmt = barmax # Range for Heatmap (Valuewise)
    scalarMapping = plt.cm.ScalarMappable(cmap=colorMap,norm=plt.normalize(vmin=heatmapMinAmt, vmax=heatmapMaxAmt))
    
    for shapedict in geoMap.states_info:
        statename = shapedict['NAME']
        # Grab population info for each state
        try:
            population = float(dataDictionary[statename])
        except KeyError:
            population = 0.0
            
        # Define the color for this state.
        # Population / Max Color Amt.
        colors[statename] = colorMap((population-heatmapMinAmt)/(heatmapMaxAmt-heatmapMinAmt))
        statenames.append(statename)
    
    # Actually color each state in this structure.
    for nshape, seg in enumerate(geoMap.states):
        xx, yy = zip(*seg)
        color = rgb2hex(colors[statenames[nshape]]) 
        plt.fill(xx,yy,color,edgecolor=color)
    
    # Bound our map to the continental US.
    # otherwise, we'll have some weird display issues
    # with Alaska and Hawaii.
    geoMap.drawparallels(np.arange(25,65,20),labels=[0,0,0,0],zorder=-1,color="w")
    geoMap.drawmeridians(np.arange(-120,-40,20),labels=[0,0,0,0],zorder=-1,color="w")
    
    # Build the Color Bar at the bottom of the graph
    mm = plt.cm.ScalarMappable(cmap=colorMap)
    mm.set_array([heatmapMinAmt,heatmapMaxAmt])
    plt.colorbar(mm, label=bartitle,ticks=[0,5000,10000,15000,20000,25000],
    orientation="horizontal",fraction=0.05)
                
    # Adjust size of image
    plt.gcf().set_size_inches(12.0,8.0)
    plt.gca().axis("off")
    # Add Title and show.
    plt.title(title)
    plt.show()
Beispiel #10
0
def colorbar_helper(customcmap, vmin, vmax, interval, label, 
                    cb_alpha=0.05, cb_aspect=16, cb_shrink=0.4,
                    tick_labelsize=14, tick_alpha=0.7, label_fontsize=20,
                    labelpad=30, label_alpha=0.7):
    # Create a fake colorbar
    ctb = LinearSegmentedColormap.from_list('custombar', customcmap, N=2048)
    # Trick from http://stackoverflow.com/questions/8342549/
    # matplotlib-add-colorbar-to-a-sequence-of-line-plots
    sm = plt.cm.ScalarMappable(cmap=ctb, norm=plt.normalize(vmin=vmin, vmax=vmax))
    # Fake up the array of the scalar mappable
    sm._A = []
    # Set colorbar, aspect ratio
    cbar = plt.colorbar(sm, alpha=cb_alpha, aspect=cb_aspect, shrink=cb_shrink)
    cbar.solids.set_edgecolor("face")
    # Remove colorbar container frame
    cbar.outline.set_visible(False)
    # Fontsize for colorbar ticklabels
    cbar.ax.tick_params(labelsize=tick_labelsize)
    # Customize colorbar tick labels
    mytks = np.arange(vmin, vmax, interval)
    cbar.set_ticks(mytks)
    cbar.ax.set_yticklabels([str(a) for a in mytks], alpha = tick_alpha)
    # Colorbar label, customize fontsize and distance to colorbar
    cbar.set_label(label, alpha = label_alpha, rotation=270, 
                   fontsize=label_fontsize, labelpad=labelpad)
    # Remove color bar tick lines, while keeping the tick labels
    cbarytks = plt.getp(cbar.ax.axes, 'yticklines')
    plt.setp(cbarytks, visible=False)
Beispiel #11
0
def showOverlapTable(modes_x, modes_y, **kwargs):
    """Show overlap table using :func:`~matplotlib.pyplot.pcolor`.  *modes_x*
    and *modes_y* are sets of normal modes, and correspond to x and y axes of
    the plot.  Note that mode indices are incremented by 1.  List of modes
    is assumed to contain a set of contiguous modes from the same model.

    Default arguments for :func:`~matplotlib.pyplot.pcolor`:

      * ``cmap=plt.cm.jet``
      * ``norm=plt.normalize(0, 1)``"""

    import matplotlib.pyplot as plt

    overlap = abs(calcOverlap(modes_y, modes_x))
    if overlap.ndim == 0:
        overlap = np.array([[overlap]])
    elif overlap.ndim == 1:
        overlap = overlap.reshape((modes_y.numModes(), modes_x.numModes()))

    cmap = kwargs.pop('cmap', plt.cm.jet)
    norm = kwargs.pop('norm', plt.normalize(0, 1))
    show = (plt.pcolor(overlap, cmap=cmap, norm=norm, **kwargs),
            plt.colorbar())
    x_range = np.arange(1, modes_x.numModes() + 1)
    plt.xticks(x_range-0.5, x_range)
    plt.xlabel(str(modes_x))
    y_range = np.arange(1, modes_y.numModes() + 1)
    plt.yticks(y_range-0.5, y_range)
    plt.ylabel(str(modes_y))
    plt.axis([0, modes_x.numModes(), 0, modes_y.numModes()])
    if SETTINGS['auto_show']:
        showFigure()
    return show
Beispiel #12
0
def discrete_calldata_colormesh(X,
                                labels=None,
                                colors='wbgrcmyk',
                                states=None,
                                ax=None,
                                **kwargs):
    """
    Make a meshgrid from discrete calldata (e.g., genotypes).

    Parameters
    ----------

    X: array
        2-dimensional array of integers of shape (#variants, #samples)
    labels: sequence of strings
        Axis labels (e.g., sample IDs)
    colors: sequence
        Colors to use for different values of the array
    states: sequence
        Manually specify discrete calldata states (if not given will be determined from the data)
    ax: axes
        Axes on which to draw

    Remaining keyword arguments are passed to ax.pcolormesh.

    """

    # set up axes
    if ax is None:
        fig = plt.figure(figsize=(7, 5))
        ax = fig.add_subplot(111)

    # determine discrete states
    if states is None:
        states = np.unique(X)
    colors = colors[:max(states) - min(states) +
                    1]  # only need as many colors as states

    # plotting defaults
    pltargs = {
        'cmap': ListedColormap(colors),
        'norm': plt.normalize(min(states),
                              max(states) + 1),
    }
    pltargs.update(kwargs)

    ax.pcolormesh(X.T, **pltargs)
    ax.set_xlim(0, X.shape[0])
    ax.set_ylim(0, X.shape[1])

    ax.set_yticks(np.arange(X.shape[1]) + .5)
    if labels is not None:
        #        labels = ['%s [%s] ' % (s, i) for (i, s) in enumerate(labels)]
        ax.set_yticklabels(labels, rotation=0)

    return ax
Beispiel #13
0
def plotPCA(V_g,
            w_g,
            ctypes,
            k=5,
            fn=None,
            fig=None,
            figsize=None,
            markersize=None):
    if markersize is None:
        markersize = 4
    if fig is None:
        if figsize is None:
            fig = plt.figure(figsize=(k * 4, k * 4))
        else:
            fig = plt.figure(figsize=(figsize, figsize))
    ax = fig.add_subplot(k - 1, k - 1, 1)

    ### choose colormap and adapt normalization
    cmap = plt.get_cmap('jet')
    norm = plt.normalize(0, np.unique(ctypes).shape[0])
    ### plot first k main axes of variation
    for k1 in range(0, k):
        cnt = 1
        for k2 in range(k1 + 1, k):
            ax = fig.add_subplot(k - 1, k - 1, (k1 * (k - 1)) + cnt)
            cnt += 1
            for idx, ct in enumerate(np.unique(ctypes)):
                c_idx = np.where(ctypes == ct)[0]
                if c_idx.shape[0] > 0:
                    ax.plot(V_g[k1, c_idx],
                            V_g[k2, c_idx],
                            markers.MarkerStyle.filled_markers[idx % 13],
                            color=cmap(norm(idx)),
                            label=ct,
                            ms=markersize,
                            alpha=0.75)
            ax.set_title('PC %i vs %i' % (k1 + 1, k2 + 1))
            ax.set_xticks(ax.get_xticks()[::2])
            ax.set_yticks(ax.get_yticks()[::2])
            ax.tick_params(axis='both', which='major', labelsize=10)
            ax.tick_params(axis='both', which='minor', labelsize=10)
        if k1 == (k - 1):
            ax.legend(numpoints=1,
                      ncol=2,
                      loc='center left',
                      bbox_to_anchor=(1.2, 0.5))
    plt.tight_layout()
    if fn is not None:
        plt.savefig(fn + '.pdf', dpi=1200, format='pdf')
Beispiel #14
0
def discrete_calldata_colormesh(X, labels=None, colors='wbgrcmyk', states=None, ax=None, **kwargs):
    """
    Make a meshgrid from discrete calldata (e.g., genotypes).

    Parameters
    ----------

    X: array
        2-dimensional array of integers of shape (#variants, #samples)
    labels: sequence of strings
        Axis labels (e.g., sample IDs)
    colors: sequence
        Colors to use for different values of the array
    states: sequence
        Manually specify discrete calldata states (if not given will be determined from the data)
    ax: axes
        Axes on which to draw

    Remaining keyword arguments are passed to ax.pcolormesh.

    """

    # set up axes
    if ax is None:
        fig = plt.figure(figsize=(7, 5))
        ax = fig.add_subplot(111)

    # determine discrete states
    if states is None:
        states = np.unique(X)
    colors = colors[:max(states)-min(states)+1]  # only need as many colors as states

    # plotting defaults
    pltargs = {
        'cmap': ListedColormap(colors),
        'norm': plt.normalize(min(states), max(states)+1),
    }
    pltargs.update(kwargs)

    ax.pcolormesh(X.T, **pltargs)
    ax.set_xlim(0, X.shape[0])
    ax.set_ylim(0, X.shape[1])

    ax.set_yticks(np.arange(X.shape[1]) + .5)
    if labels is not None:
#        labels = ['%s [%s] ' % (s, i) for (i, s) in enumerate(labels)]
        ax.set_yticklabels(labels, rotation=0)

    return ax
Beispiel #15
0
    def coRes(self):
        layers = self.iface.legendInterface().layers()
        for ly in layers:
            if ly.name() == 'r1d_in_data':
                vl2 = ly
        print vl2.name()
        x = []
        y = []
        values = []
        ab = self.dockwidget.txtAB2CoRes.text()
        print ab

        for f in vl2.getFeatures():
            #print f['ab2']
            if f['ab2'] == float(ab):
                geom = f.geometry()
                x.append(geom.asPoint().x())
                y.append(geom.asPoint().y())
                values.append(f['ra'])

        #Creating the output grid (100x100, in the example)
        #x = [10,60,40,70,10,50,20,70,30,60]
        #y = [10,20,30,30,40,50,60,70,80,90]
        #values = [1,2,2,3,4,6,7,7,8,10]
        print x
        print y
        #print xx

        txi = np.linspace(min(x), max(x), 10000)
        tyi = np.linspace(min(y), max(y), 10000)
        XI, YI = np.meshgrid(ti, ti)

        #Creating the interpolation function and populating the output matrix value
        rbf = Rbf(x, y, values, function='inverse')
        ZI = rbf(XI, YI)

        # Plotting the result
        n = plt.normalize(0.0, 1000.0)
        plt.subplot(1, 1, 1)
        plt.pcolor(XI, YI, ZI)
        plt.scatter(x, y, 1000, values)
        plt.title('RBF interpolation')
        plt.xlim(min(x), max(x))
        plt.ylim(min(y), max(y))
        plt.colorbar()

        plt.show()
Beispiel #16
0
    def coRes(self):
        layers = self.iface.legendInterface().layers()
        for ly in layers:
            if ly.name() == 'r1d_in_data':
                vl2 = ly
        print vl2.name()
        x=[]
        y=[]
        values =[]
        ab = self.dockwidget.txtAB2CoRes.text()
        print ab

        for f in vl2.getFeatures():
            #print f['ab2']
            if f['ab2'] == float(ab):
                geom = f.geometry()
                x.append(geom.asPoint().x())
                y.append(geom.asPoint().y())
                values.append(f['ra'])

        #Creating the output grid (100x100, in the example)
        #x = [10,60,40,70,10,50,20,70,30,60]
        #y = [10,20,30,30,40,50,60,70,80,90]
        #values = [1,2,2,3,4,6,7,7,8,10]
        print x
        print y
        #print xx

        txi = np.linspace(min(x), max(x), 10000)
        tyi = np.linspace(min(y), max(y), 10000)
        XI, YI = np.meshgrid(ti, ti)

        #Creating the interpolation function and populating the output matrix value
        rbf = Rbf(x, y, values, function='inverse')
        ZI = rbf(XI, YI)

        # Plotting the result
        n = plt.normalize(0.0, 1000.0)
        plt.subplot(1, 1, 1)
        plt.pcolor(XI, YI, ZI)
        plt.scatter(x, y, 1000, values)
        plt.title('RBF interpolation')
        plt.xlim(min(x), max(x))
        plt.ylim(min(y), max(y))
        plt.colorbar()

        plt.show()
Beispiel #17
0
def plot_veg_types(yc, xc, Cv, baresoil):
    Projection_Parameters = projection

    labels = [
        'Evergreen Needleleaf', 'Evergreen Broadleaf', 'Deciduous Needleleaf',
        'Deciduous Broadleaf', 'Mixed Cover', 'Woodland', 'Wooded Grasslands',
        'Closed Shrublands', 'Open Shrublands', 'Grasslands', 'Crop land',
        'Bare Soil/Ice'
    ]

    fig = plt.figure(figsize=(10, 12))

    gs1 = gridspec.GridSpec(4, 3)

    for loc in xrange(11):
        ax = fig.add_subplot(gs1[loc])
        c = plot_map(ax,
                     yc,
                     xc,
                     Cv[loc],
                     Projection_Parameters,
                     vmin=0,
                     cmap='Jet')
        ax.set_title(labels[loc])
    ax = fig.add_subplot(gs1[11])
    c = plot_map(ax,
                 yc,
                 xc,
                 baresoil,
                 Projection_Parameters,
                 vmin=0,
                 cmap='Jet')
    ax.set_title(labels[11])

    sm = plt.cm.ScalarMappable(cmap='Jet', norm=plt.normalize(vmin=0, vmax=1))
    colorbar_ax = fig.add_axes([0.92, 0.1, 0.03, 0.8])
    sm._A = []
    plt.colorbar(sm, cax=colorbar_ax)
    fig.suptitle('Fraction of Vegetation Type', fontsize=20, fontweight='bold')
    fig.text(0.5,
             0.93,
             'Regional Arctic Climate Model',
             ha='center',
             fontsize=18)

    plt.show()
Beispiel #18
0
def showOverlapTable(rows, cols, **kwargs):
    """Show overlap table using :func:`~matplotlib.pyplot.pcolor`.  *rows* and 
    *cols* are sets of normal modes, and correspond to rows and columns of the 
    displayed overlap matrix.  Note that mode indices are incremented by 1.  
    List of modes should contain a set of contiguous modes from the same model. 
    
    Default arguments for :func:`~matplotlib.pyplot.pcolor`:
        
      * ``cmap=plt.cm.jet``
      * ``norm=plt.normalize(0, 1)``
    
    .. plot::
       :context:
       :include-source:
        
       plt.figure(figsize=(5,4))
       showOverlapTable( p38_pca[:6], p38_anm[:6] )
       plt.title('p38 PCA vs ANM')

    .. plot::
       :context:
       :nofigs:
        
       plt.close('all')"""
    
    import matplotlib.pyplot as plt
    
    overlap = abs(calcOverlap(rows, cols))
    if isinstance(rows, NMA):
        rows = rows[:]
    if isinstance(cols, NMA):
        cols = cols[:]
    cmap = kwargs.pop('cmap', plt.cm.jet)
    norm = kwargs.pop('norm', plt.normalize(0, 1))
    show = (plt.pcolor(overlap, cmap=cmap, norm=norm, **kwargs),
            plt.colorbar())
    x_range = np.arange(1, len(cols)+1)
    plt.xticks(x_range-0.5, x_range)
    plt.xlabel(str(cols))
    y_range = np.arange(1, len(rows)+1)
    plt.yticks(y_range-0.5, y_range)
    plt.ylabel(str(rows))
    plt.axis([0, len(cols), 0, len(rows)])
    return show
Beispiel #19
0
def make_2D_RDF_of_gridded_data(res='1x1',
                                X_locs=None,
                                Y_locs=None,
                                Z_data=None):
    """
    Make a 2D interpolation using RadialBasisFunctions
    """
    import numpy as np
    from scipy.interpolate import Rbf
    import matplotlib.pyplot as plt
    # - Process dataframe here for now
    X_locs = df['Longitude'].values
    Y_locs = df['Latitude'].values
    Z_data = df['Iodide'].values
    # Degrade resolution
    if res == '1x1':
        X_COORDS, Y_COORDS, NIU = AC.get_latlonalt4res(res=res)
    # Remove double ups in data for now...
    print([len(i) for i in (X_locs, Y_locs)])
    # Degrade to 1x1 resolution...
    X_locs = [int(i) for i in X_locs]
    Y_locs = [int(i) for i in Y_locs]
    # Make a dictionary to remove double ups...
    Z_dict = dict(list(zip(list(zip(X_locs, Y_locs)), Z_data)))
    # Unpack
    locs = sorted(Z_dict.keys())
    Z_data = [Z_dict[i] for i in locs]
    X_locs, Y_locs = list(zip(*locs))
    print([len(i) for i in (X_locs, Y_locs)])
    # Setup meshgrid...
    XI, YI = np.meshgrid(X_COORDS, Y_COORDS)
    # Interpolate onto this...
    # Creating the interpolation function and populating the output matrix value
    rbf = Rbf(X_locs, Y_locs, Z_data, function='inverse')
    ZI = rbf(XI, YI)
    # Plotting the result
    n = plt.normalize(0.0, 100.0)
    plt.subplot(1, 1, 1)
    plt.pcolor(XI, YI, ZI)
    plt.scatter(X_locs, Y_locs, 100, Z_data)
    plt.title('RBF interpolation')
    plt.xlim(-180, 180)
    plt.ylim(-90, 90)
    plt.colorbar()
Beispiel #20
0
def quick_ortho_plot(x,y,z, bbox=[], loc=None, title='', norm=None):
    import matplotlib.pyplot as pp
    import xipy.volume_utils as vu
    from xipy.vis.single_slice_plot import SliceFigure
    # find or make the x, y, z plane extents
    if bbox:
        extents = vu.limits_to_extents(bbox)
    else:
        extents = [ reduce(lambda x,y: x+y, zip([0,0],p.shape[:2][::-1]))
                    for p in (x, y, z) ]
        
    if norm is None and len(x.shape) < 3:
        mx = max([ x.max(), y.max(), z.max() ])
        mn = min([ x.min(), y.min(), z.min() ])
        norm = pp.normalize(mn, mx)
    fig = pp.figure()

    loc = list(loc)
    zloc = loc[0],loc[1] if loc else None
    ax_z = fig.add_subplot(131)
    sf_z = SliceFigure(fig, extents[2])
    img_z = sf_z.spawn_image(z, extent=extents[2], loc=zloc,
                             interpolation='nearest', norm=norm)
    ax_z.set_title('plot z')

    yloc = loc[0],loc[2] if loc else None
    ax_y = fig.add_subplot(132)
    sf_y = SliceFigure(fig, extents[1])
    img_y = sf_y.spawn_image(y, extent=extents[1], loc=yloc,
                             interpolation='nearest', norm=norm)
    ax_y.set_title('plot y')

    xloc = loc[1],loc[2] if loc else None
    ax_x = fig.add_subplot(133)
    sf_x = SliceFigure(fig, extents[0])
    img_x = sf_x.spawn_image(x, extent=extents[0], loc=xloc,
                             interpolation='nearest', norm=norm)
    ax_x.set_title('plot x')
    if title:
        fig.text(.5, .05, title, ha='center')
    pp.colorbar(img_x.img)
    pp.show()
    return fig
Beispiel #21
0
def find_image_threshold(arr, percentile=90., debug=False):
    nbins = 200
    bsizes, bpts = np.histogram(arr.flatten(), bins=nbins)
    # heuristically, this should show up near the middle of the
    # second peak of the intensity histogram
    start_pt = np.abs(bpts - arr.max() / 2.).argmin()
    db = np.diff(bsizes[:start_pt])
    ##     zcross = np.argwhere((db[:-1] < 0) & (db[1:] >= 0)).flatten()[0]
    bval = bsizes[1:start_pt - 1][(db[:-1] < 0) & (db[1:] >= 0)].min()
    zcross = np.argwhere(bval == bsizes).flatten()[0]
    thresh = (bpts[zcross] + bpts[zcross + 1]) / 2.
    # interpolate the percentile value from the bin edges
    bin_lo = int(percentile * nbins / 100.0)
    bin_hi = int(round(percentile * nbins / 100.0 + 0.5))
    p_hi = percentile - bin_lo  # proportion of hi bin
    p_lo = bin_hi - percentile  # proportion of lo bin
    ##     print bin_hi, bin_lo, p_hi, p_lo
    pval = bpts[bin_lo] * p_lo + bpts[bin_hi] * p_hi
    if debug:
        import matplotlib as mpl
        import matplotlib.pyplot as pp
        f = pp.figure()
        ax = f.add_subplot(111)
        ax.hist(arr.flatten(), bins=nbins)
        l = mpl.lines.Line2D([thresh, thresh], [0, .25 * bsizes.max()],
                             linewidth=2,
                             color='r')
        ax.add_line(l)
        ax.xaxis.get_major_formatter().set_scientific(True)
        f = pp.figure()
        norm = pp.normalize(0, pval)
        ax = f.add_subplot(211)
        plot_arr = arr
        while len(plot_arr.shape) > 2:
            plot_arr = plot_arr[plot_arr.shape[0] / 2]
        ax.imshow(plot_arr, norm=norm)
        ax = f.add_subplot(212)
        simple_mask = (plot_arr < thresh)
        ax.imshow(np.ma.masked_array(plot_arr, mask=simple_mask), norm=norm)
        pp.show()

    return thresh, pval
Beispiel #22
0
def draw():
    global m, learn, z
    pyp.background(200,50)

    learn.collect_data()
    m = learn.get_mean()


    pyp.loadPixels()
    m = np.atleast_2d(m)
    norm = normalize(vmin=min(min(m)), vmax=max(max(m)))
    cmap = get_cmap('jet')
    m_normed = norm(m)
    rgba_data=cmap(m_normed)*255
    r = rgba_data[0,:,0].astype('uint32')
    g = rgba_data[0,:,1].astype('uint32')
    b = rgba_data[0,:,2].astype('uint32')
    a = rgba_data[0,:,3].astype('uint32')
    pyp.screen.pixels = a << 24 | r << 16 | g << 8 | b
    pyp.updatePixels()
Beispiel #23
0
def find_image_threshold(arr, percentile=90., debug=False):
    nbins = 200
    bsizes, bpts = np.histogram(arr.flatten(), bins=nbins)
    # heuristically, this should show up near the middle of the
    # second peak of the intensity histogram
    start_pt = np.abs(bpts - arr.max()/2.).argmin()
    db = np.diff(bsizes[:start_pt])
##     zcross = np.argwhere((db[:-1] < 0) & (db[1:] >= 0)).flatten()[0]
    bval = bsizes[1:start_pt-1][ (db[:-1] < 0) & (db[1:] >= 0) ].min()
    zcross = np.argwhere(bval==bsizes).flatten()[0]
    thresh = (bpts[zcross] + bpts[zcross+1])/2.
    # interpolate the percentile value from the bin edges
    bin_lo = int(percentile * nbins / 100.0)
    bin_hi = int(round(percentile * nbins / 100.0 + 0.5))
    p_hi = percentile - bin_lo # proportion of hi bin
    p_lo = bin_hi - percentile # proportion of lo bin
##     print bin_hi, bin_lo, p_hi, p_lo
    pval = bpts[bin_lo] * p_lo + bpts[bin_hi] * p_hi
    if debug:
        import matplotlib as mpl
        import matplotlib.pyplot as pp
        f = pp.figure()
        ax = f.add_subplot(111)
        ax.hist(arr.flatten(), bins=nbins)
        l = mpl.lines.Line2D([thresh, thresh], [0, .25*bsizes.max()],
                             linewidth=2, color='r')
        ax.add_line(l)
        ax.xaxis.get_major_formatter().set_scientific(True)
        f = pp.figure()
        norm = pp.normalize(0, pval)
        ax = f.add_subplot(211)
        plot_arr = arr
        while len(plot_arr.shape) > 2:
            plot_arr = plot_arr[plot_arr.shape[0]/2]
        ax.imshow(plot_arr, norm=norm)
        ax = f.add_subplot(212)
        simple_mask = (plot_arr < thresh)
        ax.imshow(np.ma.masked_array(plot_arr, mask=simple_mask), norm=norm)
        pp.show()
    
    return thresh, pval
def vectorize(hillshade_file, m_value_file):

    import matplotlib.pyplot as pp
    import numpy as np
    import matplotlib.colors as colors
    import matplotlib.cm as cmx
    from matplotlib import rcParams

    #get data
    hillshade, hillshade_header = read_flt(hillshade_file)
    m_values, m_values_header = read_flt(m_value_file)

    #handle plotting hillshades which are larger than the m_value raster
    #cannot cope with m_value raster larger than the hillshade
    corrected_x = 0
    corrected_y = 0
    if (hillshade_header[0] != m_values_header[0]) or (hillshade_header[1] !=
                                                       m_values_header[1]):
        corrected_x = (m_values_header[2] -
                       hillshade_header[2]) / hillshade_header[4]
        corrected_y = ((
            (m_values_header[3] / m_values_header[4]) + m_values_header[1]) -
                       ((hillshade_header[3] / hillshade_header[4]) +
                        hillshade_header[1])) * -1

    #ignore nodata values
    hillshade = np.ma.masked_where(hillshade == -9999, hillshade)
    m_values = np.ma.masked_where(m_values == -9999, m_values)

    #fonts
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['Liberation Sans']
    rcParams['font.size'] = 12

    fig, ax = pp.subplots()

    ax.imshow(hillshade, vmin=0, vmax=255, cmap=cmx.gray)

    xlocs, xlabels = pp.xticks()
    ylocs, ylabels = pp.yticks()

    new_x_labels = np.linspace(
        hillshade_header[2],
        hillshade_header[2] + (hillshade_header[1] * hillshade_header[4]),
        len(xlocs))
    new_y_labels = np.linspace(
        hillshade_header[3],
        hillshade_header[3] + (hillshade_header[0] * hillshade_header[4]),
        len(ylocs))

    new_x_labels = [str(x).split('.')[0] for x in new_x_labels
                    ]  #get rid of decimal places in axis ticks
    new_y_labels = [str(y).split('.')[0]
                    for y in new_y_labels][::-1]  #invert y axis
    pp.xticks(xlocs[1:-1], new_x_labels[1:-1],
              rotation=30)  #[1:-1] skips ticks where we have no data
    pp.yticks(ylocs[1:-1], new_y_labels[1:-1])

    pp.xlabel('Easting (m)')
    pp.ylabel('Northing (m)')

    # SET UP COLOURMAPS
    jet = pp.get_cmap('jet')

    m_MIN = np.min(m_values)
    m_MAX = np.max(m_values)
    cNorm_m_values = colors.Normalize(vmin=m_MIN, vmax=m_MAX)
    scalarMap_m_values = cmx.ScalarMappable(norm=cNorm_m_values, cmap=jet)

    for i in xrange(len(m_values)):
        for j in xrange(len(m_values[0])):
            if m_values[i][j] > 0:
                colorVal = scalarMap_m_values.to_rgba(m_values[i][j])
                pp.scatter(j + corrected_x,
                           i + corrected_y,
                           marker=".",
                           color=colorVal,
                           edgecolors='none')

    # Configure final plot
    sm = pp.cm.ScalarMappable(cmap=jet,
                              norm=pp.normalize(vmin=m_MIN, vmax=m_MAX))
    sm._A = []
    cbar = pp.colorbar(sm)
    cbar.set_label('M Values')

    pp.show()
Beispiel #25
0
leg = ax.legend([l1, l2, l3], labels, ncol=3, frameon=False, fontsize=16, 
                bbox_to_anchor=[1.1, 0.11], handlelength=2, 
                handletextpad=1, columnspacing=2, title='Average Page Size')
 
# Customize legend title
# Set position to increase space between legend and labels
plt.setp(leg.get_title(), fontsize=20, alpha=a)
leg.get_title().set_position((0, 10))
# Customize transparency for legend labels
[plt.setp(label, alpha=a) for label in leg.get_texts()]

# Create a fake colorbar
ctb = LinearSegmentedColormap.from_list('custombar', customcmap, N=2048)
# Trick from http://stackoverflow.com/questions/8342549/
# matplotlib-add-colorbar-to-a-sequence-of-line-plots
sm = plt.cm.ScalarMappable(cmap=ctb, norm=plt.normalize(vmin=1, vmax=3))
# Fake up the array of the scalar mappable
sm._A = []
 
# Set colorbar, aspect ratio
cbar = plt.colorbar(sm, alpha=0.05, aspect=16, shrink=0.4)
cbar.solids.set_edgecolor("face")
# Remove colorbar container frame
cbar.outline.set_visible(False)
# Fontsize for colorbar ticklabels
cbar.ax.tick_params(labelsize=16)
# Customize colorbar tick labels
mytks = range(1,4,1)
cbar.set_ticks(mytks)
cbar.ax.set_yticklabels([str(i) for i in mytks], alpha=a)
 
Beispiel #26
0
#######################
#daily and total rate plot
fig1 = plt.figure(figsize=(11.7, 6.0))
gs = gridspec.GridSpec(3, 2)
ax1 = fig1.add_subplot(gs[:, 0])

###########
#locations
m = Basemap(llcrnrlon=lon_min, llcrnrlat=lat_min, urcrnrlon=lon_max, urcrnrlat=lat_max,
            resolution='h',projection='tmerc',lon_0=lon_max +(lon_max-lon_min)/2.,lat_0=lat_max +(lat_max-lat_min)/2.)

m.drawcoastlines()
x,y = m(Cat_mc[:,2],Cat_mc[:,1])

m.scatter(x,y,s=10, c=range(len(Cat_mc[:,0])), norm=plt.normalize(vmin=0, vmax=N), marker='o', edgecolor='none')
m.drawmapboundary(fill_color='lightgrey')
m.drawparallels(np.arange(27.6,28.0,0.1),labels=[1,0,0,1])
m.drawmeridians(np.arange(-18.4,-17.8,0.1),labels=[1,0,0,1])

mdates.DateFormatter('dd/mm/yyyy')

plt.text(0.67, 0.95, mdates.num2date(t_start+i).strftime('%d/%m/%Y'), fontsize = 9, transform=ax1.transAxes)

###########
ax2 = fig1.add_subplot(gs[0,1],axisbg='lightgrey')
ax2.bar(mdates.num2date(day_bins[:-1]), DER, color='darkslategrey', edgecolor='darkslategrey')
ax2.set_ylabel('Daily earthquakes (M>1.5)', fontsize=8)
ax2.set_xlim(mdates.num2date(t_start), mdates.num2date(t_end))
ax2.set_ylim(0, 250)
print "partial segmentation : %d / %d pixels labeled" % ((s_img1 >= 0).sum(), s_img1.size)
f = plot_masked_centroid_image(rgb_img, s_img1)
f.savefig(iname + "_persistence_%d_parts_model.pdf" % n_parts)
f = plot_masked_segmap(s_img1)
f.savefig(iname + "_persistence_%d_parts_model_labels.pdf" % n_parts)

bmap = ut.draw_boundaries(s_img1, m_seek.saddles)
f = pp.figure()
pp.imshow(bmap, cmap=pp.cm.gray)
f.savefig(iname + "_boundaries.pdf")

if video:
    lab_vid = np.array([colors.rgb2lab(vf) for vf in vid])
    svid = cls.classify_sequence(classifier, lab_vid)
    segcmap = colors.npt_colormap(svid.max())
    n = pp.normalize()
    svid_masked = np.where(svid < 0, 0, svid)
    svid_mapped = segcmap(n(svid_masked).ravel()).reshape(svid.shape + (4,))
    svid_mapped = (svid_mapped[..., :3] * 255).astype("B")
    both_vids = np.concatenate((vid, svid_mapped), axis=2)

    anim = ut.animate_frames(both_vids, movie_name=iname + "_joint", fps=25)
    anim.repeat = False

    ## anim = ut.animate_frames(
    ##     np.ma.masked_where(svid < 0, svid),
    ##     movie_name=iname+'_naive_classifier',
    ##     fps=25,
    ##     cmap=colors.npt_colormap(svid.max())
    ##     )
    ## anim.repeat = False
def make_plots():
    

    label_size = 20
    #title_size = 30
    axis_size = 28

    # Set up fonts for plots
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['arial']
    rcParams['font.size'] = label_size
    
    

    #########################
    #                       #
    #   READ IN THE DATA    #
    #                       #
    #########################

    # open file
    #DataDirectory = 'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\India\\'
    #DataDirectory =  'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\PA\\'
    #DataDirectory =  'm:\\topographic_tools\\LSDRaster_chi_package\\Test_data\\'  
    #DataDirectory =  'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\Test_data\\'
    #DataDirectory =  'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\Child_runs_mk4\\'   
    DataDirectory =  'm:\\topographic_tools\\LSDRaster_chi_package\\Child_runs_mk4\\'   
    #FileName = 'pa_basin_fullProfileMC_mainstem_1189.tree'
    #FileName = 'pa_basin_fullProfileMC_mainstem_3124.tree'    
    #FileName = 'rio_torto_fullProfileMC_mainstem_633.tree'
    #FileName = 'rio_torto_fullProfileMC_mainstem_114.tree'
    #FileName = 'rio_torto_fullProfileMC_colinear_114.tree'  
    #FileName = 'rio_torto_fullProfileMC_colinear_110.tree' 
    #FileName = 'rio_torto_fullProfileMC_mainstem_110.tree' 
    #FileName = 'rio_torto_fullProfileMC_forced_0.4_110.tree' 
    #FileName = 'UniUplift09b_mk4_t6_fullProfileMC_colinear.tree' 
    FileName = 'UniUplift09b_mk4_t6_fullProfileMC_colinear_3_1_12_100.tree'
  
  
    #OutputFigureName = 'chi_plot'
    #OutputFigureFormat = 'eps'
    f = open(DataDirectory + FileName,'r')  # open file
    lines = f.readlines()   # read in the data
    n_lines = len(lines)   # get the number of lines (=number of data)
    n_data = n_lines -1
    # data variables
    channel_id = np.zeros(n_data)     # ID number for channel segment
    receiver_channel = np.zeros(n_data)
    #node_on_receiver_channel = np.zeros(n_data, dtype=np.int)
    node = np.zeros(n_data)           # node number
    row = np.zeros(n_data)            # row
    col = np.zeros(n_data)            # column
    flow_dist = np.zeros(n_data)      # flow distance
    chi = np.zeros(n_data)            # chi
    elevation = np.zeros(n_data)           # elevation
    drainage_area = np.zeros(n_data)  # drainage area
    n_data_points_uic = np.zeros(n_data)
    m_mean = np.zeros(n_data)
    #m_standard_deviation = np.zeros(n_data)
    m_standard_error = np.zeros(n_data)
    b_mean = np.zeros(n_data)
    #b_standard_deviation = np.zeros(n_data)
    b_standard_error = np.zeros(n_data)
    DW_mean = np.zeros(n_data)
    #DW_standard_deviation = np.zeros(n_data)
    DW_standard_error = np.zeros(n_data)
    fitted_elevation_mean = np.zeros(n_data)
    #fitted_elevation_standard_deviation = np.zeros(n_data)
    fitted_elevation_standard_error = np.zeros(n_data)
    
    print "Reading " + FileName
    
    # get the A_0 and m/n values
    line = lines[0].strip().split(" ")
    A_0 = float(line[0]) 
    m_over_n = float(line[1]) 
    
    for i in range (0,n_data):
        line = lines[i+1].strip().split(" ")
        #print line
        channel_id[i] = int(float(line[0]))
        receiver_channel[i] = int(float(line[1])) 
        #node_on_receiver_channel = int(float(line[2]))
        node[i] = int(float(line[3]))
        row[i] = int(line[4])
        col[i] = int(line[5])
        flow_dist[i] = float(line[6])
        chi[i] = float(line[7])
        elevation[i] = float(line[8])
        drainage_area[i] = float(line[9])
        n_data_points_uic[i] = float(line[10])
        m_mean[i] = float(line[11])
        #m_standard_deviation[i] = float(line[12])
        m_standard_error[i] = float(line[13])
        b_mean[i] = float(line[14])
        #b_standard_deviation[i] = float(line[15])
        b_standard_error[i] = float(line[16])
        DW_mean[i] = float(line[17])
        #DW_standard_deviation[i] = float(line[18])
        DW_standard_error[i] = float(line[19])
        fitted_elevation_mean[i] = float(line[20])
        #fitted_elevation_standard_deviation = float(line[21])
        fitted_elevation_standard_error[i] = float(line[22])
    f.close()
    
    # Determine number of segments, and their respective lengths
    n_channel_segments = channel_id[n_data-1]+1
    segment_lengths = np.zeros(n_channel_segments, dtype=np.int)
    for i in range (0,n_data):
        segment_lengths[channel_id[i]] = segment_lengths[channel_id[i]] + 1  
        
    #########################
    #                       #
    #   MAKE CHI-PLOTS      #
    #                       #
    #########################    
    print "Producing figures..."
    
    # SET UP COLOURMAPS
    jet = plt.get_cmap('jet')
    hot = plt.get_cmap('RdYlBu_r')
    
    
    # m-values
    m_MIN = np.min(m_mean)
    m_MAX = np.max(m_mean)
    cNorm_m_values  = colors.Normalize(vmin=m_MIN, vmax=m_MAX)  # the max number of channel segs is the 'top' colour
    scalarMap_m_values = cmx.ScalarMappable(norm=cNorm_m_values, cmap=hot)

    # channel ID
    Channel_ID_MIN = np.min(channel_id)
    Channel_ID_MAX = np.max(channel_id)
    cNorm_channel_ID  = colors.Normalize(vmin=Channel_ID_MIN, vmax=Channel_ID_MAX)  # the max number of channel segs is the 'top' colour
    scalarMap_channel_ID = cmx.ScalarMappable(norm=cNorm_channel_ID, cmap=jet)

    minfd = min(flow_dist)
    print "minimum flow distance: " +str(minfd)
    
    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # PLOT OF CHI m-VALUE (+ERROR) AGAINST CHI WITH EACH CHANNEL LABELLED WITH DISTINCT COLOUR
    plt.figure(2, facecolor='white',figsize=(10,7.5))    
    ax = plt.subplot(1,1,1)
    for i in range (0,len(channel_id)):
        
        colorVal = scalarMap_channel_ID.to_rgba(channel_id[i])  
        if channel_id[i]==0:
            plt.plot(chi[i], m_mean[i], "o", markersize=10, color='black', markeredgecolor = 'black',alpha=0.7)  
        else:
            plt.plot(chi[i], m_mean[i], "o", markersize=8, color=colorVal, markeredgecolor = colorVal,alpha = 0.7)
                 
           # plt.plot(chi[i], m_mean[i] + m_standard_error[i], ".", linewidth=0.25, color='k')
           # plt.plot(chi[i], m_mean[i] - m_standard_error[i], ".", linewidth=0.25, color='k')
 
    rect1 =plt.Rectangle((21.4577,0), 18.2625, 20, color='red', alpha= 0.1)
    rect2 =plt.Rectangle((69.0736,0), 29.3623, 20, color='green', alpha= 0.1)
    ax.add_artist(rect1)
    ax.add_artist(rect2)

           
    # Configure final plot
    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5) 
    ax.tick_params(axis='both', width=2.5)    
    plt.xlabel('$\chi$ (m)', fontsize = axis_size)
    plt.ylabel('Gradient in $\chi$ space', fontsize = axis_size)
    plt.title('$A_0$: '+str(A_0)+' m$^2$, and $m/n$: '+str(m_over_n), fontsize = label_size)

    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # LONGITUDINAL PROFILES WITH COLOUR SCALE GIVING CHI-m VALUE
    plt.figure(3, facecolor='white',figsize=(10,7.5))  
    ax = plt.subplot(1,1,1)    
    for i in range (0,len(channel_id)):
            colorVal = scalarMap_m_values.to_rgba(m_mean[i])
            plt.plot((flow_dist[i]-minfd)/1000, elevation[i], "o", markersize=6, color=colorVal, markeredgecolor = colorVal,alpha = 0.5)       
                  
    # Configure final plot
    sm = plt.cm.ScalarMappable(cmap=hot,norm=plt.normalize(vmin=np.min(m_mean), vmax=np.max(m_mean)))
    sm._A = []
    
    cbar = plt.colorbar(sm,orientation='horizontal',use_gridspec=True)
    cbar.set_label('Gradient in $\chi$ space', fontsize = axis_size)
    plt.xlabel('Distance upstream (km)', fontsize = axis_size)
    plt.ylabel('Elevation (m)', fontsize = axis_size)
    plt.title('$A_0$: '+str(A_0)+' m$^2$, and $m/n$: '+str(m_over_n), fontsize = label_size)
    
    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5) 
    ax.tick_params(axis='both', width=2.5)   
    
    cbar.ax.spines['top'].set_linewidth(2.5)
    cbar.ax.spines['left'].set_linewidth(2.5)
    cbar.ax.spines['right'].set_linewidth(2.5)
    cbar.ax.spines['bottom'].set_linewidth(2.5) 
    cbar.ax.tick_params(axis='both', width=2.5)       
    
    plt.tight_layout()

    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # BASIC CHI-PLOT WITH EACH CHANNEL LABELLED WITH A DIFFERENT COLOUR
    plt.figure(1, facecolor='white',figsize=(10,7.5))
    ax = plt.subplot(1,1,1)

    
    data_pointer = 0    # points to data element in chi and elev vectors
    for i in range (0,len(segment_lengths)):
        chi_seg = np.zeros(segment_lengths[i])
        elev_seg = np.zeros(segment_lengths[i])
        
        for j in range (0,segment_lengths[i]):
            chi_seg[j] = chi[data_pointer]
            elev_seg[j] = elevation[data_pointer]
            data_pointer = data_pointer + 1

        if i == 0:
            # plot trunk stream in black, with thicker line 
            l1, = ax.plot(chi_seg, elev_seg, "k-", linewidth=4, alpha = 0.3)
        else:
            # plot other stream segments plot
            colorVal = scalarMap_channel_ID.to_rgba(i) # this gets the distinct colour for this segment
            ax.plot(chi_seg, elev_seg, "-", linewidth=4, color=colorVal, alpha = 0.3) 


   # now loop again plotting the fitted segments    
    data_pointer = 0    # points to data element in chi and elev vectors
    for i in range (0,len(segment_lengths)):
        chi_seg = np.zeros(segment_lengths[i])
        elev_seg = np.zeros(segment_lengths[i])
        
        for j in range (0,segment_lengths[i]):
            chi_seg[j] = chi[data_pointer]
            elev_seg[j] = fitted_elevation_mean[data_pointer]
            data_pointer = data_pointer + 1

        if i == 0:
            # plot trunk stream in black, with thicker line 
            l2, = plt.plot(chi_seg, elev_seg, "k", linewidth=3, dashes=(10,2))
        else:
            # plot other stream segments plot
            colorVal = scalarMap_channel_ID.to_rgba(i) # this gets the distinct colour for this segment
            ax.plot(chi_seg, elev_seg, linewidth=3, color=colorVal, dashes=(10,2)) 
    
    # Configure final plot
    plt.xlabel('$\chi$ (m)', fontsize= axis_size)
    plt.ylabel('Elevation (m)', fontsize =axis_size)
    plt.title('$A_0$: '+str(A_0)+' m$^2$, and $m/n$: '+str(m_over_n), fontsize = label_size)  
    plt.legend((l1, l2), ('Data', 'Best fit segments'), 'lower right',prop={'size':label_size}) 
    
    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5) 
    ax.tick_params(axis='both', width=2.5)  


    #plt.savefig(OutputFigureName + '.' + OutputFigureFormat, format=OutputFigureFormat)


    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#    # CHI-PLOT USING FITTED PROFILES (+ERROR) RATHER THAN RAW DATA, LABELLED WITH DISTINCT COLOUR
#    plt.figure(4, facecolor='white')    
#    data_pointer = 0    # points to data element in chi and elev vectors
#    for i in range (0,len(segment_lengths)):
#        chi_seg = np.zeros(segment_lengths[i])
#        fitted_elev_seg = np.zeros(segment_lengths[i])
#        fitted_elev_ulim = np.zeros(segment_lengths[i])        
#        fitted_elev_llim = np.zeros(segment_lengths[i])
#        
#        for j in range (0,segment_lengths[i]):
#            chi_seg[j] = chi[data_pointer]
#            fitted_elev_seg[j] = fitted_elevation_mean[data_pointer]            
#            fitted_elev_ulim[j] = fitted_elevation_mean[data_pointer] + fitted_elevation_standard_error[data_pointer]            
#            fitted_elev_llim[j] = fitted_elevation_mean[data_pointer] - fitted_elevation_standard_error[data_pointer]
#            data_pointer = data_pointer + 1
#            # plot other stream segments plot
#        colorVal = scalarMap_channel_ID.to_rgba(i)
#        plt.plot(chi_seg, fitted_elev_ulim, "-k", linewidth=0.2)
#        plt.plot(chi_seg, fitted_elev_llim, "-k", linewidth=0.2)
#        plt.plot(chi_seg, fitted_elev_seg, "-", linewidth=0.5, color=colorVal) 
#            
#    # Configure final plot
#    plt.xlabel('$\chi$ / m')
#    plt.ylabel('fitted elevation / m')
#
#    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#    # CHI-PLOT DISPLAYING BOTH RAW DATA AND FITTED PROFILES
#    plt.figure(5, facecolor='white')    
# 
#    data_pointer = 0    # points to data element in chi and elev vectors
#    for i in range (0,len(segment_lengths)):
#        chi_seg = np.zeros(segment_lengths[i])
#        elev_seg = np.zeros(segment_lengths[i])
#        fitted_elev_seg = np.zeros(segment_lengths[i])
#        
#        for j in range (0,segment_lengths[i]):
#            chi_seg[j] = chi[data_pointer]
#            elev_seg[j] = elevation[data_pointer]
#            fitted_elev_seg[j] = fitted_elevation_mean[data_pointer]
#            data_pointer = data_pointer + 1
#
#        # plot other stream segments plot
#        colorVal = scalarMap_channel_ID.to_rgba(i) # this gets the distinct colour for this segment
#        plt.plot(chi_seg, elev_seg, "-k", linewidth=1)
#        plt.plot(chi_seg, fitted_elev_seg, "-", linewidth=1, color=colorVal)
#        
#     # Configure final plot
#    plt.xlabel('$\chi$ / m')
#    plt.ylabel('elevation / m')
#
#    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#    # CHI-PLOT USING ONLY MAIN TRIBUTARY WITH FITTED PROFILES (+ERROR)
#    plt.figure(6, facecolor='white')    
#    for i in range (0,len(channel_id)):   
#        plt.plot(chi[channel_id==0], fitted_elevation_mean[channel_id==0] + fitted_elevation_standard_error[channel_id==0], "-k", linewidth=0.2)
#        plt.plot(chi[channel_id==0], fitted_elevation_mean[channel_id==0] - fitted_elevation_standard_error[channel_id==0], "-k", linewidth=0.2)
#        plt.plot(chi[channel_id==0], elevation[channel_id==0], "-b", linewidth=0.5)            
#        plt.plot(chi[channel_id==0], fitted_elevation_mean[channel_id==0], "-r", linewidth=0.5)
#
#
#
#     # Configure final plot
#    plt.xlabel('$\chi$ / m')
#    plt.ylabel('elevation / m')
#
#    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#    # PLOT OF CHI b-VALUE (+ERROR) AGAINST CHI WITH EACH CHANNEL LABELLED WITH DISTINCT COLOUR
#    plt.figure(7, facecolor='white')    
#    for i in range (0,len(channel_id)):
#            colorVal = scalarMap_channel_ID.to_rgba(channel_id[i])
#            plt.plot(chi[i], b_mean[i], ".", linewidth=0.2, color=colorVal)            
#            #plt.plot(chi[i], b_mean[i] + b_standard_error[i], ".", linewidth=0.25, color='k')
#            #plt.plot(chi[i], b_mean[i] - b_standard_error[i], ".", linewidth=0.25, color='k')
#            
#    # Configure final plot
#    plt.xlabel('$\chi$ / m')
#    plt.ylabel('b-value / m')
#    
#    
#    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#    # PLOT OF DW STATISTIC (+ERROR) AGAINST CHI WITH EACH CHANNEL LABELLED WITH DISTINCT COLOUR
#    plt.figure(8, facecolor='white')    
#    for i in range (0,len(channel_id)):
#            colorVal = scalarMap_channel_ID.to_rgba(channel_id[i])
#            plt.plot(chi[i], DW_mean[i], ".", linewidth=0.2, color=colorVal)            
#            #plt.plot(chi[i], DW_mean[i] + DW_standard_error[i], ".", linewidth=0.25, color='k')
#            #plt.plot(chi[i], DW_mean[i] - DW_standard_error[i], ".", linewidth=0.25, color='k')
#            
#    # Configure final plot
#    plt.xlabel('$\chi$ / m')
#    plt.ylabel('DW statistic')
    
    plt.show()
# Sepcify the size of the fig, Default(8x8)
fig = plt.figure(figsize=(30, 30))

# Specify layout of the graph
pos = nx.spring_layout(g)

n_colors = n_features
# Create colormap
cm = plt.get_cmap('gist_rainbow')

ax = fig.add_subplot(111)
ax.set_color_cycle([cm(1. * i / n_colors) for i in range(n_colors)])

## Draw nodes
# Colormap
sm = plt.cm.ScalarMappable(cmap=cm, norm=plt.normalize(vmin=0, vmax=1))
sm._A = []

# markers that specify the shapes of nodes
marker = ['o','s','*','8','D','+','h','v','^','<','>','x','p',\
        'H','d','|','_','1','2','3','4']
for i in range(n_features):
    # Find the Ranking of this node by comparing with features_ordered
    tmp = features_ordered[features_name_con[i]]
    nx.draw_networkx_nodes(g,
                           pos,
                           nodelist=[features_name_con[i]],
                           node_size=300,
                           alpha=1.0,
                           node_color=cm(1. * tmp / n_colors),
                           node_shape=marker[labels[i]],
Beispiel #30
0
def main():

   try:
      import matplotlib
   except ImportError:
      sys.stderr.write("This script needs the 'matplotlib' library, which ")
      sys.stderr.write("was not found. Please install it." )
   matplotlib.use('PDF')
   from matplotlib import pyplot


   # **** Parse command line ****

   optParser = optparse.OptionParser( usage = "%prog [options] read_file",
      description=
	 "This script take a file with high-throughput sequencing reads " +
	 "(supported formats: SAM, Solexa _export.txt, FASTQ, Solexa " +
	 "_sequence.txt) and performs a simply quality assessment by " +
	 "producing plots showing the distribution of called bases and " +
	 "base-call quality scores by position within the reads. The " +
	 "plots are output as a PDF file.",
      epilog = 
	 "Written by Simon Anders ([email protected]), European Molecular Biology " +
	 " Laboratory (EMBL). (c) 2010. Released under the terms of the GNU General " +
	 " Public License v3. Part of the 'HTSeq' framework, version %s." % HTSeq.__version__ )
   optParser.add_option( "-t", "--type", type="choice", dest="type",
      choices = ("sam", "bam", "solexa-export", "fastq", "solexa-fastq"),
      default = "sam", help="type of read_file (one of: sam [default], bam, " +
	 "solexa-export, fastq, solexa-fastq)" )
   optParser.add_option( "-o", "--outfile", type="string", dest="outfile",
      help="output filename (default is <read_file>.pdf)" )
   optParser.add_option( "-r", "--readlength", type="int", dest="readlen",
      help="the maximum read length (when not specified, the script guesses from the file" )
   optParser.add_option( "-g", "--gamma", type="float", dest="gamma", 
      default = 0.3,
      help="the gamma factor for the contrast adjustment of the quality score plot" )
   optParser.add_option( "-n", "--nosplit", action="store_true", dest="nosplit",
      help="do not split reads in unaligned and aligned ones" )
   optParser.add_option( "-m", "--maxqual", type="int", dest="maxqual", default=41,
      help="the maximum quality score that appears in the data (default: 41)" )

   if len( sys.argv ) == 1:
      optParser.print_help()
      sys.exit(1)

   (opts, args) = optParser.parse_args()

   if len( args ) != 1:
      sys.stderr.write( sys.argv[0] + ": Error: Please provide one argument (the read_file).\n" )
      sys.stderr.write( "  Call with '-h' to get usage information.\n" )
      sys.exit( 1 )

   readfilename = args[0]

   if opts.type == "sam":
      readfile = HTSeq.SAM_Reader( readfilename )
      isAlnmntFile = True
   elif opts.type == "bam":
      readfile = HTSeq.BAM_Reader( readfilename )
      isAlnmntFile = True
   elif opts.type == "solexa-export":
      readfile = HTSeq.SolexaExportReader( readfilename )
      isAlnmntFile = True
   elif opts.type == "fastq":
      readfile = HTSeq.FastqReader( readfilename )
      isAlnmntFile = False
   elif opts.type == "solexa-fastq":
      readfile = HTSeq.FastqReader( readfilename, "solexa" )
      isAlnmntFile = False
   else:
      sys.error( "Oops." )

   twoColumns = isAlnmntFile and not opts.nosplit

   if opts.outfile is None:
      outfilename = os.path.basename( readfilename ) + ".pdf"
   else:   
      outfilename = opts.outfile


   # **** Get read length ****

   if opts.readlen is not None:
      readlen = opts.readlen
   else:
      readlen = 0
      if isAlnmntFile:
	 reads = ( a.read for a in readfile )
      else:
	 reads = readfile
      for r in islice( reads, 10000 ):
	 if len( r ) > readlen:
            readlen = len( r )

   max_qual = opts.maxqual
   gamma = opts.gamma


   # **** Initialize count arrays ****

   base_arr_U = numpy.zeros( ( readlen, 5 ), numpy.int )
   qual_arr_U = numpy.zeros( ( readlen, max_qual+1 ), numpy.int )
   if twoColumns:
      base_arr_A = numpy.zeros( ( readlen, 5 ), numpy.int )
      qual_arr_A = numpy.zeros( ( readlen, max_qual+1 ), numpy.int )


   # **** Main counting loop ****

   i = 0
   try:
      for a in readfile:
	 if isAlnmntFile:
	    r = a.read
	 else:
	    r = a
	 if twoColumns and (isAlnmntFile and a.aligned):
	    r.add_bases_to_count_array( base_arr_A )
	    r.add_qual_to_count_array( qual_arr_A )
	 else:
	    r.add_bases_to_count_array( base_arr_U )
	    r.add_qual_to_count_array( qual_arr_U )   
	 i += 1
	 if i % 200000 == 0:
            print i, "reads processed"
   except:
      sys.stderr.write( "Error occured in: %s\n" %
         readfile.get_line_number_string() )
      raise
   print i, "reads processed"


   # **** Normalize result ****

   def norm_by_pos( arr ):
      arr = numpy.array( arr, numpy.float )
      arr_n = ( arr.T / arr.sum( 1 ) ).T
      arr_n[ arr == 0 ] = 0
      return arr_n

   def norm_by_start( arr ):
      arr = numpy.array( arr, numpy.float )
      arr_n = ( arr.T / arr.sum( 1 )[ 0 ] ).T
      arr_n[ arr == 0 ] = 0
      return arr_n


   base_arr_U_n = norm_by_pos( base_arr_U )
   qual_arr_U_n = norm_by_start( qual_arr_U )
   nreads_U = base_arr_U[0,:].sum()
   if twoColumns:
      base_arr_A_n = norm_by_pos( base_arr_A )
      qual_arr_A_n = norm_by_start( qual_arr_A )
      nreads_A = base_arr_A[0,:].sum()


   # **** Make plot ****

   def plot_bases( arr ):
      xg = numpy.arange( readlen )   
      pyplot.plot( xg, arr[ : , 0 ], marker='.', color='red')
      pyplot.plot( xg, arr[ : , 1 ], marker='.', color='darkgreen')
      pyplot.plot( xg, arr[ : , 2 ], marker='.',color='lightgreen')
      pyplot.plot( xg, arr[ : , 3 ], marker='.',color='orange')
      pyplot.plot( xg, arr[ : , 4 ], marker='.',color='grey')
      pyplot.axis( (0, readlen-1, 0, 1 ) )
      pyplot.text( readlen*.70, .9, "A", color="red" )
      pyplot.text( readlen*.75, .9, "C", color="darkgreen" )
      pyplot.text( readlen*.80, .9, "G", color="lightgreen" )
      pyplot.text( readlen*.85, .9, "T", color="orange" )
      pyplot.text( readlen*.90, .9, "N", color="grey" )

   pyplot.figure()
   pyplot.subplots_adjust( top=.85 )
   pyplot.suptitle( os.path.basename(readfilename), fontweight='bold' )

   if twoColumns:

      pyplot.subplot( 221 )
      plot_bases( base_arr_U_n )
      pyplot.ylabel( "proportion of base" )
      pyplot.title( "non-aligned reads\n%.0f%% (%.3f million)" % 
	 ( 100. * nreads_U / (nreads_U+nreads_A), nreads_U / 1e6 ) )

      pyplot.subplot( 222 )
      plot_bases( base_arr_A_n )
      pyplot.title( "aligned reads\n%.0f%% (%.3f million)" % 
	 ( 100. * nreads_A / (nreads_U+nreads_A), nreads_A / 1e6 ) )

      pyplot.subplot( 223 )
      pyplot.pcolor( qual_arr_U_n.T ** gamma, cmap=pyplot.cm.Greens,
	  norm=pyplot.normalize( 0, 1 ) )
      pyplot.axis( (0, readlen-1, 0, max_qual+1 ) )
      pyplot.xlabel( "position in read" )
      pyplot.ylabel( "base-call quality score" )

      pyplot.subplot( 224 )
      pyplot.pcolor( qual_arr_A_n.T ** gamma, cmap=pyplot.cm.Greens,
	   norm=pyplot.normalize( 0, 1 ) )
      pyplot.axis( (0, readlen-1, 0, max_qual+1 ) )
      pyplot.xlabel( "position in read" )

   else:

      pyplot.subplot( 211 )
      plot_bases( base_arr_U_n )
      pyplot.ylabel( "proportion of base" )
      pyplot.title( "%.3f million reads" % ( nreads_U / 1e6 ) )

      pyplot.subplot( 212 )
      pyplot.pcolor( qual_arr_U_n.T ** gamma, cmap=pyplot.cm.Greens,
	  norm=pyplot.normalize( 0, 1 ) )
      pyplot.axis( (0, readlen-1, 0, max_qual+1 ) )
      pyplot.xlabel( "position in read" )
      pyplot.ylabel( "base-call quality score" )


   pyplot.savefig( outfilename )
Beispiel #31
0
def main():

    try:
        import matplotlib
    except ImportError:
        sys.stderr.write("This script needs the 'matplotlib' library, which ")
        sys.stderr.write("was not found. Please install it.")
    matplotlib.use('PDF')
    from matplotlib import pyplot

    # **** Parse command line ****

    optParser = optparse.OptionParser(
        usage="%prog [options] read_file",
        description=
        "This script take a file with high-throughput sequencing reads " +
        "(supported formats: SAM, Solexa _export.txt, FASTQ, Solexa " +
        "_sequence.txt) and performs a simply quality assessment by " +
        "producing plots showing the distribution of called bases and " +
        "base-call quality scores by position within the reads. The " +
        "plots are output as a PDF file.",
        epilog=
        "Written by Simon Anders ([email protected]), European Molecular Biology "
        +
        " Laboratory (EMBL). (c) 2010. Released under the terms of the GNU General "
        + " Public License v3. Part of the 'HTSeq' framework, version %s." %
        HTSeq.__version__)
    optParser.add_option(
        "-t",
        "--type",
        type="choice",
        dest="type",
        choices=("sam", "bam", "solexa-export", "fastq", "solexa-fastq"),
        default="sam",
        help="type of read_file (one of: sam [default], bam, " +
        "solexa-export, fastq, solexa-fastq)")
    optParser.add_option("-o",
                         "--outfile",
                         type="string",
                         dest="outfile",
                         help="output filename (default is <read_file>.pdf)")
    optParser.add_option(
        "-r",
        "--readlength",
        type="int",
        dest="readlen",
        help=
        "the maximum read length (when not specified, the script guesses from the file"
    )
    optParser.add_option(
        "-g",
        "--gamma",
        type="float",
        dest="gamma",
        default=0.3,
        help=
        "the gamma factor for the contrast adjustment of the quality score plot"
    )
    optParser.add_option(
        "-n",
        "--nosplit",
        action="store_true",
        dest="nosplit",
        help="do not split reads in unaligned and aligned ones")
    optParser.add_option(
        "-m",
        "--maxqual",
        type="int",
        dest="maxqual",
        default=41,
        help="the maximum quality score that appears in the data (default: 41)"
    )

    if len(sys.argv) == 1:
        optParser.print_help()
        sys.exit(1)

    (opts, args) = optParser.parse_args()

    if len(args) != 1:
        sys.stderr.write(
            sys.argv[0] +
            ": Error: Please provide one argument (the read_file).\n")
        sys.stderr.write("  Call with '-h' to get usage information.\n")
        sys.exit(1)

    readfilename = args[0]

    if opts.type == "sam":
        readfile = HTSeq.SAM_Reader(readfilename)
        isAlnmntFile = True
    elif opts.type == "bam":
        readfile = HTSeq.BAM_Reader(readfilename)
        isAlnmntFile = True
    elif opts.type == "solexa-export":
        readfile = HTSeq.SolexaExportReader(readfilename)
        isAlnmntFile = True
    elif opts.type == "fastq":
        readfile = HTSeq.FastqReader(readfilename)
        isAlnmntFile = False
    elif opts.type == "solexa-fastq":
        readfile = HTSeq.FastqReader(readfilename, "solexa")
        isAlnmntFile = False
    else:
        sys.error("Oops.")

    twoColumns = isAlnmntFile and not opts.nosplit

    if opts.outfile is None:
        outfilename = os.path.basename(readfilename) + ".pdf"
    else:
        outfilename = opts.outfile

    # **** Get read length ****

    if opts.readlen is not None:
        readlen = opts.readlen
    else:
        readlen = 0
        if isAlnmntFile:
            reads = (a.read for a in readfile)
        else:
            reads = readfile
        for r in islice(reads, 10000):
            if len(r) > readlen:
                readlen = len(r)

    max_qual = opts.maxqual
    gamma = opts.gamma

    # **** Initialize count arrays ****

    base_arr_U = numpy.zeros((readlen, 5), numpy.int)
    qual_arr_U = numpy.zeros((readlen, max_qual + 1), numpy.int)
    if twoColumns:
        base_arr_A = numpy.zeros((readlen, 5), numpy.int)
        qual_arr_A = numpy.zeros((readlen, max_qual + 1), numpy.int)

    # **** Main counting loop ****

    i = 0
    try:
        for a in readfile:
            if isAlnmntFile:
                r = a.read
            else:
                r = a
            if twoColumns and (isAlnmntFile and a.aligned):
                r.add_bases_to_count_array(base_arr_A)
                r.add_qual_to_count_array(qual_arr_A)
            else:
                r.add_bases_to_count_array(base_arr_U)
                r.add_qual_to_count_array(qual_arr_U)
            i += 1
            if i % 200000 == 0:
                print i, "reads processed"
    except:
        sys.stderr.write("Error occured in: %s\n" %
                         readfile.get_line_number_string())
        raise
    print i, "reads processed"

    # **** Normalize result ****

    def norm_by_pos(arr):
        arr = numpy.array(arr, numpy.float)
        arr_n = (arr.T / arr.sum(1)).T
        arr_n[arr == 0] = 0
        return arr_n

    def norm_by_start(arr):
        arr = numpy.array(arr, numpy.float)
        arr_n = (arr.T / arr.sum(1)[0]).T
        arr_n[arr == 0] = 0
        return arr_n

    base_arr_U_n = norm_by_pos(base_arr_U)
    qual_arr_U_n = norm_by_start(qual_arr_U)
    nreads_U = base_arr_U[0, :].sum()
    if twoColumns:
        base_arr_A_n = norm_by_pos(base_arr_A)
        qual_arr_A_n = norm_by_start(qual_arr_A)
        nreads_A = base_arr_A[0, :].sum()

    # **** Make plot ****

    def plot_bases(arr):
        xg = numpy.arange(readlen)
        pyplot.plot(xg, arr[:, 0], marker='.', color='red')
        pyplot.plot(xg, arr[:, 1], marker='.', color='darkgreen')
        pyplot.plot(xg, arr[:, 2], marker='.', color='lightgreen')
        pyplot.plot(xg, arr[:, 3], marker='.', color='orange')
        pyplot.plot(xg, arr[:, 4], marker='.', color='grey')
        pyplot.axis((0, readlen - 1, 0, 1))
        pyplot.text(readlen * .70, .9, "A", color="red")
        pyplot.text(readlen * .75, .9, "C", color="darkgreen")
        pyplot.text(readlen * .80, .9, "G", color="lightgreen")
        pyplot.text(readlen * .85, .9, "T", color="orange")
        pyplot.text(readlen * .90, .9, "N", color="grey")

    pyplot.figure()
    pyplot.subplots_adjust(top=.85)
    pyplot.suptitle(os.path.basename(readfilename), fontweight='bold')

    if twoColumns:

        pyplot.subplot(221)
        plot_bases(base_arr_U_n)
        pyplot.ylabel("proportion of base")
        pyplot.title("non-aligned reads\n%.0f%% (%.3f million)" %
                     (100. * nreads_U / (nreads_U + nreads_A), nreads_U / 1e6))

        pyplot.subplot(222)
        plot_bases(base_arr_A_n)
        pyplot.title("aligned reads\n%.0f%% (%.3f million)" %
                     (100. * nreads_A / (nreads_U + nreads_A), nreads_A / 1e6))

        pyplot.subplot(223)
        pyplot.pcolor(qual_arr_U_n.T**gamma,
                      cmap=pyplot.cm.Greens,
                      norm=pyplot.normalize(0, 1))
        pyplot.axis((0, readlen - 1, 0, max_qual + 1))
        pyplot.xlabel("position in read")
        pyplot.ylabel("base-call quality score")

        pyplot.subplot(224)
        pyplot.pcolor(qual_arr_A_n.T**gamma,
                      cmap=pyplot.cm.Greens,
                      norm=pyplot.normalize(0, 1))
        pyplot.axis((0, readlen - 1, 0, max_qual + 1))
        pyplot.xlabel("position in read")

    else:

        pyplot.subplot(211)
        plot_bases(base_arr_U_n)
        pyplot.ylabel("proportion of base")
        pyplot.title("%.3f million reads" % (nreads_U / 1e6))

        pyplot.subplot(212)
        pyplot.pcolor(qual_arr_U_n.T**gamma,
                      cmap=pyplot.cm.Greens,
                      norm=pyplot.normalize(0, 1))
        pyplot.axis((0, readlen - 1, 0, max_qual + 1))
        pyplot.xlabel("position in read")
        pyplot.ylabel("base-call quality score")

    pyplot.savefig(outfilename)
Beispiel #32
0

if __name__ == "__main__":
    power=1
    smoothing=20

    #Creating some data, with each coodinate and the values stored in separated lists
    xv = [10,60,40,70,10,50,20,70,30,60]
    yv = [10,20,30,30,40,50,60,70,80,90]
    values = [1,2,2,3,4,6,7,7,8,10]

    #Creating the output grid (100x100, in the example)
    ti = np.linspace(0, 100, 100)
    XI, YI = np.meshgrid(ti, ti)

    #Creating the interpolation function and populating the output matrix value
    ZI = invDist(xv,yv,values,100,100,power,smoothing)


    # Plotting the result
    n = plt.normalize(0.0, 100.0)
    plt.subplot(1, 1, 1)
    plt.pcolor(XI, YI, ZI)
    plt.scatter(xv, yv, 100, values)
    plt.title('Inv dist interpolation - power: ' + str(power) + ' smoothing: ' + str(smoothing))
    plt.xlim(0, 100)
    plt.ylim(0, 100)
    plt.colorbar()

    plt.show()
# Customize x tick lables
xticks = [10,20,30,40,50,60]
ax.xaxis.set_ticks(xticks)
ax.set_xticklabels(xticks, fontsize=16, alpha=a)

# Customize y tick labels
yticks = [item.get_text() for item in ax.get_yticklabels()]
ax.set_yticklabels(yticks, fontsize=16, alpha=a)
ax.yaxis.set_tick_params(pad=12)

# Create a fake colorbar to express department population
ctb = LinearSegmentedColormap.from_list('custombar', customcmap, N=2048)
# Trick from http://stackoverflow.com/questions/8342549/
# matplotlib-add-colorbar-to-a-sequence-of-line-plots
sm = plt.cm.ScalarMappable(cmap=ctb, norm=plt.normalize(vmin=20, vmax=800))
# Fake up the array of the scalar mappable
sm._A = []
 
# Set colorbar, aspect ratio
cbar = plt.colorbar(sm, alpha=0.05, aspect=16, shrink=0.4)
cbar.solids.set_edgecolor("face")
# Remove colorbar container frame
cbar.outline.set_visible(False)
# Fontsize for colorbar ticklabels
cbar.ax.tick_params(labelsize=16)
# Customize colorbar tick labels
mytks = range(0,800,100)
cbar.set_ticks(mytks)
cbar.ax.set_yticklabels([str(a) for a in mytks], alpha=a)
 
Beispiel #34
0
            llcrnrlat=lat_min,
            urcrnrlon=lon_max,
            urcrnrlat=lat_max,
            resolution='h',
            projection='tmerc',
            lon_0=lon_max + (lon_max - lon_min) / 2.,
            lat_0=lat_max + (lat_max - lat_min) / 2.)

m.drawcoastlines()
x, y = m(Cat_mc[:, 2], Cat_mc[:, 1])

m.scatter(x,
          y,
          s=10,
          c=range(len(Cat_mc[:, 0])),
          norm=plt.normalize(vmin=0, vmax=N),
          marker='o',
          edgecolor='none')
m.drawmapboundary(fill_color='lightgrey')
m.drawparallels(np.arange(27.6, 28.0, 0.1), labels=[1, 0, 0, 1])
m.drawmeridians(np.arange(-18.4, -17.8, 0.1), labels=[1, 0, 0, 1])

mdates.DateFormatter('dd/mm/yyyy')

plt.text(0.67,
         0.95,
         mdates.num2date(t_start + i).strftime('%d/%m/%Y'),
         fontsize=9,
         transform=ax1.transAxes)

###########
Beispiel #35
0
    z.append(p.z)

print "Array populated."

#Creating the output grid (100x100, in the example)

print "Creating output grid."
ti = np.linspace(h.max[0] - h.min[0], h.max[1] - h.min[1])
XI, YI = sp.sparse.lil_matrix(x, y)
print "Output grid created"

#Creating the interpolation function and populating the output matrix value

print "Running RBF."
rbf = Rbf(x, y, z, function='inverse')
ZI = rbf(XI, YI)
print "RBF complete"

print "Plotting output."
# Plotting the result
n = plt.normalize(h.min[2], h.max[2])
plt.subplot(1, 1, 1)
plt.pcolor(XI, YI, ZI)
plt.scatter(x, y, z)
plt.title('RBF interpolation')
plt.xlim(h.min[0], h.max[0])
plt.ylim(h.min[1], h.max[1])
#plt.colorbar()
plt.show()

print "Done."
npts = 200
ngridx = 100
ngridy = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)

# griddata and contour.
start = time.clock()
plt.subplot(211)
xi = np.linspace(-2.1,2.1,ngridx)
yi = np.linspace(-2.1,2.1,ngridy)
zi = griddata(x,y,z,xi,yi,interp='linear')
plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
plt.contourf(xi,yi,zi,15,cmap=plt.cm.rainbow, 
             norm=plt.normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))
plt.colorbar() # draw colorbar
plt.plot(x, y, 'ko', ms=3)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata and contour (%d points, %d grid points)' % (npts, ngridx*ngridy))
print ('griddata and contour seconds: %f' % (time.clock() - start))

# tricontour.
start = time.clock()
plt.subplot(212)
triang = tri.Triangulation(x, y)
plt.tricontour(x, y, z, 15, linewidths=0.5, colors='k')
plt.tricontourf(x, y, z, 15, cmap=plt.cm.rainbow, 
                norm=plt.normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))
plt.colorbar()
Beispiel #37
0
def GetColorBar(ax, colormap='autumn_r', min=0, max=1):
    cm = plt.cm.ScalarMappable(cmap=plt.get_cmap(colormap), norm=plt.normalize(vmin=min, vmax=max))
    # fake up the array of the scalar mappable...
    cm._A = []
    return cm
Beispiel #38
0
    f = liblas.file.File(sys.argv[1],mode='r')

    lgrid = LidarGrid (f)

    print lgrid.minz
    print lgrid.maxz
    
    print "Adding data to grid"
    for p in f:
        if p.return_number == 1:
            lgrid.add (p.x, p.y, p.z)
    #   print "Add: "+str(p.x)+", "+str(p.y)+", "+str(p.z)

    print "Data added to grid."

    

    plt.subplot(111)
    n = plt.normalize(75, 155)
#img = plt.imshow(lgrid.grid, extent=(0, lgrid.xsize, lgrid.ysize, 0), cmap=plt.cm.Greys_r)
    img = plt.imshow(lgrid.grid, norm=n, interpolation='nearest', cmap=plt.cm.jet)
#    img.set_clim(lgrid.minz, 120)
    plt.colorbar()
    plt.show()

    print "Done."

#for x in range (0, lidar_grid.xsize):
#   for y in range (0, lidar_grid.ysize):
#       print "Get: "+str(lidar_grid.get (x, y))
Beispiel #39
0
"""

import numpy as np
from scipy.interpolate import Rbf

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from matplotlib import cm

# 2-d tests - setup scattered data
x = np.random.rand(100) * 4.0 - 2.0
y = np.random.rand(100) * 4.0 - 2.0
z = x * np.exp(-x**2 - y**2)
ti = np.linspace(-2.0, 2.0, 100)
XI, YI = np.meshgrid(ti, ti)

# use RBF
rbf = Rbf(x, y, z, epsilon=2)
ZI = rbf(XI, YI)

# plot the result
n = plt.normalize(-2., 2.)
plt.subplot(1, 1, 1)
plt.pcolor(XI, YI, ZI, cmap=cm.jet)
plt.scatter(x, y, 100, z, cmap=cm.jet)
plt.title('RBF interpolation - multiquadrics')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()
plt.savefig('rbf2d.png')
def main(separator='\t'):

    # **** Parse command line ****

    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument( "-g", "--gamma", type=float, dest="gamma", default = 0.3,\
      help="the gamma factor for the contrast adjustment of the quality score plot, default 0.3" )
    opts = parser.parse_args()

    gamma = opts.gamma
    twoColumns = True

    # input comes from STDIN (standard input)
    data = read_mapper_output(sys.stdin, separator=separator)
    # groupby groups multiple word-count pairs by word,
    # and creates an iterator that returns consecutive keys and their group:
    # current_word - string containing a word (the key)
    # group - iterator yielding all ["&lt;current_word&gt;","&lt;count&gt;"] items
    former_element = None
    array_types = {}
    for current_element, group in groupby(data, itemgetter(0)):
        try:
            total_count = sum(
                np.fromstring(line, sep=' ', dtype=int)
                for current_element, line in group)
            #	print "%s%s%s" % (current_element, separator, ' '.join(str(x) for x in total_count))
            #	print current_element, total_count
            if not former_element:
                arr = total_count
                former_element = current_element
            else:
                if current_element.split('_')[:-1] == former_element.split(
                        '_')[:-1]:
                    arr = np.vstack([arr, total_count])
                else:
                    #print 'change array', former_element, current_element
                    array_types['_'.join(former_element.split('_')[:-1])] = arr
                    former_element = current_element
                    arr = total_count
        except ValueError:
            # count was not a number, so silently discard this item
            pass
    array_types['_'.join(former_element.split('_')[:-1])] = arr

    base_arr_U_n = norm_by_pos(array_types['base_arr_U'])
    qual_arr_U_n = norm_by_start(array_types['qual_arr_U'])
    nreads_U = array_types['nreads_U']  # base_arr_U[0,:].sum()
    if twoColumns:
        base_arr_A_n = norm_by_pos(array_types['base_arr_A'])
        qual_arr_A_n = norm_by_start(array_types['qual_arr_A'])
        nreads_A = array_types['nreads_A']  #base_arr_A[0,:].sum()

    # get the read length from the data
    readlen = len(base_arr_A_n)
    #get the max qual from the data
    max_qual = len(qual_arr_U_n[0, :])
    #	print readlen, max_qual, len(base_arr_U_n)
    #	sys.exit()
    #	print "number of aligned reads", base_arr_A[50,:].sum()
    #	print "number of unaligned reads", base_arr_U[60,:].sum()
    #
    #
    ## **** Make plot ****
    #

    pyplot.figure()
    pyplot.subplots_adjust(top=.85)
    pyplot.suptitle(os.path.basename("readfilename"), fontweight='bold')

    if twoColumns:

        pyplot.subplot(221)
        plot_bases(base_arr_U_n)
        pyplot.ylabel("proportion of base")
        pyplot.title("non-aligned reads\n%.0f%% (%.3f million)" %
                     (100. * nreads_U / (nreads_U + nreads_A), nreads_U / 1e6))

        pyplot.subplot(222)
        plot_bases(base_arr_A_n)
        pyplot.title("aligned reads\n%.0f%% (%.3f million)" %
                     (100. * nreads_A / (nreads_U + nreads_A), nreads_A / 1e6))

        pyplot.subplot(223)
        pyplot.pcolor(qual_arr_U_n.T**gamma,
                      cmap=pyplot.cm.Greens,
                      norm=pyplot.normalize(0, 1))
        pyplot.axis((0, readlen - 1, 0, max_qual + 1))
        pyplot.xlabel("position in read")
        pyplot.ylabel("base-call quality score")

        pyplot.subplot(224)
        pyplot.pcolor(qual_arr_A_n.T**gamma,
                      cmap=pyplot.cm.Greens,
                      norm=pyplot.normalize(0, 1))
        pyplot.axis((0, readlen - 1, 0, max_qual + 1))
        pyplot.xlabel("position in read")

    else:

        pyplot.subplot(211)
        plot_bases(base_arr_U_n)
        pyplot.ylabel("proportion of base")
        pyplot.title("%.3f million reads" % (nreads_U / 1e6))

        pyplot.subplot(212)
        pyplot.pcolor(qual_arr_U_n.T**gamma,
                      cmap=pyplot.cm.Greens,
                      norm=pyplot.normalize(0, 1))
        pyplot.axis((0, readlen - 1, 0, max_qual + 1))
        pyplot.xlabel("position in read")
        pyplot.ylabel("base-call quality score")

    pyplot.savefig(sys.stdout)
Beispiel #41
0
# adjust figure aspect ratio and size
w, h = plt.figaspect(1.)
fig = plt.figure(figsize=(3. * w, 3. * h))

# graph image
# we use the rasterized graph since the graph PDF could reach sizes > 50MB
img = mpimg.imread('graph-iteration-' + "{0:06d}".format(iteration) + '.png')
ax = plt.subplot2grid((3, 3), (0, 0), rowspan=2, colspan=2)
ax.imshow(img, interpolation='none')
ax.axis('off')
ax.set_title('Search tree for ' + "{:,}".format(iteration) + ' iterations')

# graph image color bar
sm = plt.cm.ScalarMappable(cmap=cm.jet,
                           norm=plt.normalize(vmin=0, vmax=iteration))
sm._A = []
cb = plt.colorbar(sm,
                  orientation='horizontal',
                  fraction=0.025,
                  aspect=40,
                  pad=0.0)
cb.set_label('Iteration')

# incumbent trajectory: X, Y
ax = plt.subplot2grid((3, 3), (0, 2))
ax.set_title('Incumbent solution\n')
ax.set_xlabel('X (AU)')
ax.set_ylabel('Y (AU)')
ax.plot(xAU, yAU, 'k-')
ax.plot(xAU[0], yAU[0], 'bo', label='Earth departure')
Beispiel #42
0
def stability_analysis(file_dir, figsize=(6,4),cmap = plt.cm.jet):
    N_vals = np.array(np.linspace(50,400,11),dtype=int)
    rho_vals = np.array(np.linspace(1,10,30))

    #=============== rho_max analysis =================
    fig1, ax1 = plt.subplots(figsize=figsize)
    fig2, ax2 = plt.subplots(figsize=figsize)
    axes = ax1,ax2
    step = 5.0/200.
    n = len(rho_vals)
    ev_i = 0
    error_values = []
    color_values = cmap(np.linspace(0,1,n)) 
    sm = plt.cm.ScalarMappable(cmap=cmap,
            norm=plt.normalize(vmin=rho_vals[0], vmax=rho_vals[-1]))
    sm.set_array([])
    for i, rho_max in enumerate(rho_vals):
        color = color_values[i]
        N = int(rho_max/step)
        _, axes, rel_error = plot_noninteracting(rho_max, N, file_dir, axes=axes, 
                color = color, rho_analysis=True, eigenvalue_i=ev_i)
        error_values.append(rel_error)

    
    cb = fig1.colorbar(sm, ax=ax1)
    cb.set_label('$\\rho$ max')

    

    #[ax.grid('on') for ax in [ax1,ax2]]

    ax2.semilogy(rho_vals,error_values)
    ax2.legend(["$\\epsilon_%d$" % i for i in range(len(error_values))])
    # ax1.legend()
    ax1.set_xlim([0,4])
    #ax2.axhline(11.)
    ax1.set_xlabel('$\\rho$ ')
    ax1.set_ylabel('$u_{%d}^2$'%ev_i)
    ax2.set_ylabel('$E_%d$' %ev_i)
    ax2.set_xlabel('$\\rho_{\\text{max}}$')

    #================ N analysis ==============
    fig3, ax3 = plt.subplots(1,figsize=figsize)
    fig4, ax4 = plt.subplots(1,figsize=figsize)
    axes = [ax3,ax4]
    rho_max = 5.0
    N = 200
    n = len(N_vals)
    error_values = []

    sm = plt.cm.ScalarMappable(cmap=cmap,
            norm=plt.normalize(vmin=N_vals[0], vmax=N_vals[-1]))
    sm.set_array([])

    for i, N in enumerate(N_vals):
        color = cmap(float(i)/(n-1)) 
        _, axes, rel_error = plot_noninteracting(rho_max, N, file_dir, axes=axes, 
                color = color, rho_analysis=False, eigenvalue_i=ev_i)
        error_values.append(rel_error)

    error_values = np.array(error_values)
    cb = fig3.colorbar(sm, ax=ax3)
    cb.set_label('$\\rho$ max')

    #[ax.grid('on') for ax in [ax3,ax4]]
    logN = np.log(N_vals) 
    logError = np.log(error_values)
    print(logError.shape, logN.shape, (logError.T/logN).shape)
    gradient = np.average(logError.T/logN, axis = 1)
    print gradient
    ax4.axis('equal')
    print(N_vals.shape, error_values.shape)
    for i in range(len(gradient)):
        ax4.loglog(N_vals,error_values[:,i], label='$\\epsilon_%d$'%(i+1))
    ax4.legend()

    # ax3.legend()
    ax3.set_xlim([0,4])
    #ax4.axhline(11.)
    ax3.set_xlabel('$\\rho$ ')
    ax3.set_ylabel('$u_{%d}^2$'%ev_i)
    ax4.set_ylabel('$E_%d$' %ev_i)
    ax4.set_xlabel('$N$')


    try:
        fig1.savefig('results/rhoMaxAnalysis1.pdf')
        fig2.savefig('results/rhoMaxAnalysis2.pdf')
        fig3.savefig('results/dimAnalysis1.pdf')
        fig4.savefig('results/dimAnalysis2.pdf')
        plt.show()
    except ValueError:
        # \\text obviously doesnt work some times
        ax2.set_xlabel('rho_max')
        fig1.savefig('results/rhoMaxAnalysis1.pdf')
        fig2.savefig('results/rhoMaxAnalysis2.pdf')
        fig3.savefig('results/dimAnalysis1.pdf')
        fig4.savefig('results/dimAnalysis2.pdf')
        plt.show()
def main(args=None):
  """The main function; parses options and plots"""
  ## ---------- build and read options ----------
  from optparse import OptionParser
  optParser = OptionParser()
  optParser.add_option("-n", "--net", dest="net", metavar="FILE",
                         help="Defines the network to read")
  optParser.add_option("-i", "--dump-inputs", dest="dumps", metavar="FILE",
                         help="Defines the dump-output files to use as input")
  optParser.add_option("-m", "--measures", dest="measures", 
                         default="speed,entered", help="Define which measure to plot")
  optParser.add_option("-w", "--default-width", dest="defaultWidth", 
                         type="float", default=.1, help="Defines the default edge width")
  optParser.add_option("-c", "--default-color", dest="defaultColor", 
                         default='k', help="Defines the default edge color")
  optParser.add_option("--min-width", dest="minWidth", 
                         type="float", default=.5, help="Defines the minimum edge width")
  optParser.add_option("--max-width", dest="maxWidth", 
                         type="float", default=3, help="Defines the maximum edge width")
  optParser.add_option("--log-colors", dest="logColors", action="store_true",
                         default=False, help="If set, colors are log-scaled")
  optParser.add_option("--log-widths", dest="logWidths", action="store_true",
                         default=False, help="If set, widths are log-scaled")
  optParser.add_option("--min-color-value", dest="colorMin", 
                         type="float", default=None, help="If set, defines the minimum edge color value")
  optParser.add_option("--max-color-value", dest="colorMax", 
                         type="float", default=None, help="If set, defines the maximum edge color value")
  optParser.add_option("--min-width-value", dest="widthMin", 
                         type="float", default=None, help="If set, defines the minimum edge width value")
  optParser.add_option("--max-width-value", dest="widthMax", 
                         type="float", default=None, help="If set, defines the maximum edge width value")
  optParser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                         default=False, help="If set, the script says what it's doing")
  # standard plot options
  helpers.addInteractionOptions(optParser)
  helpers.addPlotOptions(optParser)
  # parse
  options, remaining_args = optParser.parse_args(args=args)

  if options.net==None: 
    print "Error: a network to load must be given."
    return 1
  if options.verbose: print "Reading network from '%s'" % options.net
  net = sumolib.net.readNet(options.net)
  
  if options.measures==None: 
    print "Error: a dump file must be given."
    return 1
  
  times = []
  hc = None
  if options.dumps.split(",")[0]!="":  
    if options.verbose: print "Reading colors from '%s'" % options.dumps.split(",")[0]
    hc = WeightsReader(options.measures.split(",")[0])
    sumolib.output.parse_sax(options.dumps.split(",")[0], hc)
    times = hc._edge2value
  
  hw = None
  if options.dumps.split(",")[1]!="":  
    if options.verbose: print "Reading widths from '%s'" % options.dumps.split(",")[1]
    hw = WeightsReader(options.measures.split(",")[1])
    sumolib.output.parse_sax(options.dumps.split(",")[1], hw)
    times = hw._edge2value

  for t in times: 
    colors = {}
    maxColorValue = None
    minColorValue = None
    for e in net._id2edge:
      if hc and t in hc._edge2value and e in hc._edge2value[t]: 
        if options.colorMax!=None and hc._edge2value[t][e]>options.colorMax: hc._edge2value[t][e] = options.colorMax
        if options.colorMin!=None and hc._edge2value[t][e]<options.colorMin: hc._edge2value[t][e] = options.colorMin
        if maxColorValue==None or maxColorValue<hc._edge2value[t][e]: maxColorValue = hc._edge2value[t][e] 
        if minColorValue==None or minColorValue>hc._edge2value[t][e]: minColorValue = hc._edge2value[t][e]
        colors[e] = hc._edge2value[t][e] 
    if options.colorMax!=None: maxColorValue = options.colorMax 
    if options.colorMin!=None: minColorValue = options.colorMin 
    if options.logColors: 
      helpers.logNormalise(colors, maxColorValue)
    else:
      helpers.linNormalise(colors, minColorValue, maxColorValue)
    for e in colors:
      colors[e] = helpers.getColor(options, colors[e], 1.)
    if options.verbose: print "Color values are between %s and %s" % (minColorValue, maxColorValue)
  
    widths = {}
    maxWidthValue = None
    minWidthValue = None
    for e in net._id2edge:
      if hw and t in hw._edge2value and e in hw._edge2value[t]:
        v = abs(hw._edge2value[t][e]) 
        if options.widthMax!=None and v>options.widthMax: v = options.widthMax
        if options.widthMin!=None and v<options.widthMin: v = options.widthMin
        if not maxWidthValue or maxWidthValue<v: maxWidthValue = v 
        if not minWidthValue or minWidthValue>v: minWidthValue = v
        widths[e] = v
    if options.widthMax!=None: maxWidthValue = options.widthMax 
    if options.widthMin!=None: minWidthValue = options.widthMin 
    if options.logWidths: 
      helpers.logNormalise(widths, options.colorMax)
    else:
      helpers.linNormalise(widths, minWidthValue, maxWidthValue)
    for e in widths:
      widths[e] = options.minWidth + widths[e] * (options.maxWidth-options.minWidth)
    if options.verbose: print "Width values are between %s and %s" % (minWidthValue, maxWidthValue)
  
    fig, ax = helpers.openFigure(options)
    ax.set_aspect("equal", None, 'C')
    helpers.plotNet(net, colors, widths, options)
    
    
    # drawing the legend, at least for the colors
    sm = matplotlib.cm.ScalarMappable(cmap=get_cmap(options.colormap), norm=plt.normalize(vmin=minColorValue, vmax=maxColorValue))
    # "fake up the array of the scalar mappable. Urgh..." (pelson, http://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots)
    sm._A = []
    plt.colorbar(sm)
    
    options.nolegend = True
    helpers.closeFigure(fig, ax, options)
    return 0 # !!! hack 
  return 0
Beispiel #44
0
if __name__ == "__main__":
    power=1
    smoothing=20

    #Creating some data, with each coodinate and the values stored in separated lists
    xv = [10,60,40,70,10,50,20,70,30,60]
    yv = [10,20,30,30,40,50,60,70,80,90]
    values = [1,2,2,3,4,6,7,7,8,10]
    
    #Creating the output grid (100x100, in the example)
    ti = np.linspace(0, 100, 100)
    XI, YI = np.meshgrid(ti, ti)

    #Creating the interpolation function and populating the output matrix value
    ZI = invDist(xv,yv,values,100,100,power,smoothing)


    # Plotting the result
    n = plt.normalize(0.0, 100.0)
    plt.subplot(1, 1, 1)
    plt.pcolor(XI, YI, ZI)
    plt.scatter(xv, yv, 100, values)
    plt.title('Inv dist interpolation - power: ' + str(power) + ' smoothing: ' + str(smoothing))
    plt.xlim(0, 100)
    plt.ylim(0, 100)
    plt.colorbar()

    plt.show()
    
matplotlib.use("Agg")
import matplotlib.pyplot as plt
from matplotlib import cm

# 2-d tests - setup scattered data
x = np.random.rand(100) * 4.0 - 2.0
y = np.random.rand(100) * 4.0 - 2.0
z = x * np.exp(-x ** 2 - y ** 2)
ti = np.linspace(-2.0, 2.0, 100)
XI, YI = np.meshgrid(ti, ti)

# use RBF
rbf = Rbf(x, y, z, epsilon=2)
ZI = rbf(XI, YI)

# plot the result
n = plt.normalize(-2.0, 2.0)
plt.subplot(1, 1, 1)
plt.pcolor(XI, YI, ZI, cmap=cm.jet)
plt.scatter(x, y, 100, z, cmap=cm.jet)
plt.title("RBF interpolation - multiquadrics")
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()
plt.savefig("rbf2d.png")

# <markdowncell>

# ![](files/RadialBasisFunctions_attachments/rbf2dnew.png
#
    def __init__(self,
                 size=None,
                 position=None,
                 lambda_=None,
                 theta=None,
                 sigma=None,
                 phase=None,
                 trim=None):
        """Create a Gabor Patch.

        Parameters
        ----------
        size : (int, int), optional
            size (x, y) of the mask
        position  : (int, int), optional
            position of the mask stimulus
        lambda_ : int, optional
            Spatial frequency (pixel per cycle)
        theta : int or float, optional
            Grating orientation in degrees
        sigma : int or float, optional
            gaussian standard deviation (in pixels)
        phase : float
            0 to 1 inclusive

        Notes
        -----
        The background colour of the stimulus depends of the parameters of
        the Gabor patch and can be determined (e.g. for plotting) with the
        property `GaborPatch.background_colour`.

        """

        # Parts of the code has be ported from http://www.icn.ucl.ac.uk/courses/MATLAB-Tutorials/Elliot_Freeman/html/gabor_tutorial.html

        import types
        if type(np) is not types.ModuleType:
            message = """GaborPatch can not be initialized.
The Python package 'Numpy' is not installed."""
            raise ImportError(message)

        if type(pyplot) is not types.ModuleType:
            message = """GaborPatch can not be initialized.
The Python package 'Matplotlib' is not installed."""
            raise ImportError(message)

        if size is None:
            size = defaults.gaborpatch_size
        if position is None:
            position = defaults.gaborpatch_position
        if lambda_ is None:
            lambda_ = defaults.gaborpatch_lambda_
        if theta is None:
            theta = defaults.gaborpatch_theta
        if sigma is None:
            sigma = defaults.gaborpatch_sigma
        if phase is None:
            phase = defaults.gaborpatch_phase

        fid, filename = tempfile.mkstemp(
            dir=expyriment.stimuli.defaults.tempdir, suffix=".png")
        os.close(fid)
        Picture.__init__(self, filename, position)

        # make linear ramp
        X0 = (np.linspace(1, size, size) / size) - .5
        # Set wavelength and phase
        freq = size / float(lambda_)
        phaseRad = phase * 2 * np.pi
        # Make 2D grating
        Xm, Ym = np.meshgrid(X0, X0)
        # Change orientation by adding Xm and Ym together in different proportions
        thetaRad = (theta / 360.) * 2 * np.pi
        Xt = Xm * np.cos(thetaRad)
        Yt = Ym * np.sin(thetaRad)
        grating = np.sin(((Xt + Yt) * freq * 2 * np.pi) + phaseRad)
        # 2D Gaussian distribution
        gauss = np.exp(-((Xm**2) + (Ym**2)) / (2 * (sigma / float(size))**2))
        # Trim
        gauss[gauss < trim] = 0

        self._pixel_array = grating * gauss

        #save stimulus
        color_map = pyplot.get_cmap('gray')
        color_map.set_over(color="y")

        pyplot.imsave(fname=filename,
                      arr=self._pixel_array,
                      cmap=color_map,
                      format="png")

        # determine background color
        norm = pyplot.normalize(vmin=np.min(self._pixel_array),
                                vmax=np.max(self._pixel_array))
        bgc = color_map(norm(0))
        self._background_colour = map(lambda x: int(x * 255), bgc[:3])
def map_dict(export_fn,states_happiness):
    # Lambert Conformal map of lower 48 states.
    m = Basemap(llcrnrlon=-119,llcrnrlat=22,
                    urcrnrlon=-64, urcrnrlat=49,
                                projection='lcc', lat_1=33,lat_2=45,lon_0=-95)
    
    # laod state boundaries.
    # data from U.S Census Bureau
    # http://www.census.gov/geo/www/cob/st1990.html
    shp_info = m.readshapefile('gz_2010_us_040_00_500k/gz_2010_us_040_00_500k','states',drawbounds=False)
    # This loads three files:
    #   gz_2010_us_040_00_500k.dbf
    #   gz_2010_us_040_00_500k.shp
    #   gz_2010_us_040_00_500k.shx
    
    max_score = -sys.maxint - 1
    min_score = sys.maxint+1
    happiest_state = ''
    saddest_state = ''
    for state in states_happiness:
          score = states_happiness[state]
          if (score > max_score):
                happiest_state = state
                max_score = score
          if (score < min_score):
                saddest_state = state
                min_score = score
    
    for state in states_happiness:
        states_happiness[state] -= min_score
    
    min_score = 0
    max_score = max_score-min_score
    
    # choose a color for each state based on happiness score.
    colors={}
    statenames=[]
    cmap = plt.cm.Blues_r # use 'hot' colormap
    vmin = min_score; vmax = max_score # set range.
    sm = plt.cm.ScalarMappable(cmap=cmap, 
                                   norm=plt.normalize(vmin=vmin, vmax=vmax))
    
    for shapedict in m.states_info:
        statename = shapedict['NAME']
        #print statesNames[statename], ' ', statename
        try:
            #score = states_happiness[statesNames[statename]]
            score = states_happiness[statename]
        except KeyError:
            score = 0.0
            
        # calling colormap with value between 0 and 1 returns
        # rgba value. Invert color range (hot colors are
        # high
        # population), take sqrt root to spread out
        # colors more.
        try:
            colors[statename] = cmap((score-vmin)/(vmax-vmin))[:3]
        except KeyError:
            colors[statename] = cmap(0)[:3]
        statenames.append(statename)
        
    # cycle through state names,
    # color each one.
    for nshape,seg in enumerate(m.states):
        xx,yy = zip(*seg)
        if statenames[nshape] != 'District of Columbia' and \
        statenames[nshape] != "Puerto Rico":
            color = rgb2hex(colors[statenames[nshape]]) 
            plt.fill(xx,yy,color,edgecolor='black')
    
    # draw
    # meridians
    # and
    # parallels.
    m.drawparallels(np.arange(25,65,20),   labels=[0,0,0,0],
                        zorder=-1,color="w")
    m.drawmeridians(np.arange(-120,-40,20),labels=[0,0,0,0],
                        zorder=-1,color="w")
     
    # set  up colorbar:
    mm = plt.cm.ScalarMappable(cmap=cmap)
    mm.set_array([0,1])
    #plt.colorbar(mm, label="Happiness",
    #                              orientation="horizontal", fraction=0.05)
    plt.colorbar(mm,orientation="horizontal", fraction=0.05)
     
    #plt.title('Filling State Polygons by Population Density')
    plt.gca().axis("off")
    plt.savefig(export_fn)
    plt.close('all')
def vectorize(hillshade_file, m_value_file):

    import matplotlib.pyplot as pp
    import numpy as np
    import matplotlib.colors as colors
    import matplotlib.cm as cmx
    from matplotlib import rcParams

    # get data
    hillshade, hillshade_header = read_flt(hillshade_file)
    m_values, m_values_header = read_flt(m_value_file)

    # handle plotting hillshades which are larger than the m_value raster
    # cannot cope with m_value raster larger than the hillshade
    corrected_x = 0
    corrected_y = 0
    if (hillshade_header[0] != m_values_header[0]) or (hillshade_header[1] != m_values_header[1]):
        corrected_x = (m_values_header[2] - hillshade_header[2]) / hillshade_header[4]
        corrected_y = (
            ((m_values_header[3] / m_values_header[4]) + m_values_header[1])
            - ((hillshade_header[3] / hillshade_header[4]) + hillshade_header[1])
        ) * -1

    # ignore nodata values
    hillshade = np.ma.masked_where(hillshade == -9999, hillshade)
    m_values = np.ma.masked_where(m_values == -9999, m_values)

    # fonts
    rcParams["font.family"] = "sans-serif"
    rcParams["font.sans-serif"] = ["arial"]
    rcParams["font.size"] = 12

    fig, ax = pp.subplots()

    ax.imshow(hillshade, vmin=0, vmax=255, cmap=cmx.gray)

    xlocs, xlabels = pp.xticks()
    ylocs, ylabels = pp.yticks()

    new_x_labels = np.linspace(
        hillshade_header[2], hillshade_header[2] + (hillshade_header[1] * hillshade_header[4]), len(xlocs)
    )
    new_y_labels = np.linspace(
        hillshade_header[3], hillshade_header[3] + (hillshade_header[0] * hillshade_header[4]), len(ylocs)
    )

    new_x_labels = [str(x).split(".")[0] for x in new_x_labels]  # get rid of decimal places in axis ticks
    new_y_labels = [str(y).split(".")[0] for y in new_y_labels][::-1]  # invert y axis
    pp.xticks(xlocs[1:-1], new_x_labels[1:-1], rotation=30)  # [1:-1] skips ticks where we have no data
    pp.yticks(ylocs[1:-1], new_y_labels[1:-1])

    pp.xlabel("Easting (m)")
    pp.ylabel("Northing (m)")

    # SET UP COLOURMAPS
    jet = pp.get_cmap("jet")

    m_MIN = np.min(m_values)
    m_MAX = np.max(m_values)
    cNorm_m_values = colors.Normalize(vmin=m_MIN, vmax=m_MAX)
    scalarMap_m_values = cmx.ScalarMappable(norm=cNorm_m_values, cmap=jet)

    for i in xrange(len(m_values)):
        for j in xrange(len(m_values[0])):
            if m_values[i][j] > 0:
                colorVal = scalarMap_m_values.to_rgba(m_values[i][j])
                pp.scatter(j + corrected_x, i + corrected_y, marker=".", color=colorVal, edgecolors="none")

    # Configure final plot
    sm = pp.cm.ScalarMappable(cmap=jet, norm=pp.normalize(vmin=m_MIN, vmax=m_MAX))
    sm._A = []
    cbar = pp.colorbar(sm)
    cbar.set_label("M Values")

    pp.show()
Beispiel #49
0
nat_interp = griddata(x, y, z, XI, YI)
nat_filtered = gaussian_filter(XI, YI, nat_interp, 0.1, 0.1)
nat_rms = rms(nat_interp - truth)

# Cressman
Rc = 0.5
cress_interp = grid_data(z, XI, YI, x, y, cressman_weights, Rc)
cress_rms = rms(cress_interp - truth)

# Barnes
kstar = 0.1
barnes_interp = grid_data(z, XI, YI, x, y, barnes_weights, (0.5, kstar))
barnes_rms = rms(barnes_interp - truth)

# plot the results
val_norm = plt.normalize(-0.45, 0.45)
diff_norm = plt.normalize(-1.0, 1.0)
cmap = plt.get_cmap('jet')

fig = plt.figure()
fig.suptitle('Interpolation Results', fontsize=14)
fig.canvas.manager.set_window_title('Actual Fields')

ax = plt.subplot(2, 3, 1)
plt.pcolor(XI, YI, truth, cmap=cmap, norm=val_norm)
plt.scatter(x, y, 80, z, cmap=cmap, norm=val_norm)
plt.title('Truth')

ax2 = plt.subplot(2, 3, 2, sharex=ax, sharey=ax)
plt.pcolor(XI, YI, rbfm_interp, cmap=cmap, norm=val_norm)
plt.title('RBF - Multiquadrics $(\epsilon=%.1f)$' % epsm)
Beispiel #50
0
def main(args=None):
    """The main function; parses options and plots"""
    # ---------- build and read options ----------
    from optparse import OptionParser
    optParser = OptionParser()
    optParser.add_option("-n",
                         "--net",
                         dest="net",
                         metavar="FILE",
                         help="Defines the network to read")
    optParser.add_option("--edge-width",
                         dest="defaultWidth",
                         type="float",
                         default=1,
                         help="Defines the edge width")
    optParser.add_option("--edge-color",
                         dest="defaultColor",
                         default='k',
                         help="Defines the edge color")
    optParser.add_option("--minV",
                         dest="minV",
                         type="float",
                         default=None,
                         help="Define the minimum value boundary")
    optParser.add_option("--maxV",
                         dest="maxV",
                         type="float",
                         default=None,
                         help="Define the maximum value boundary")
    optParser.add_option("-v",
                         "--verbose",
                         dest="verbose",
                         action="store_true",
                         default=False,
                         help="If set, the script says what it's doing")
    # standard plot options
    helpers.addInteractionOptions(optParser)
    helpers.addPlotOptions(optParser)
    # parse
    options, remaining_args = optParser.parse_args(args=args)

    if options.net is None:
        print("Error: a network to load must be given.")
        return 1
    if options.verbose:
        print("Reading network from '%s'" % options.net)
    net = sumolib.net.readNet(options.net)

    speeds = {}
    minV = None
    maxV = None
    for e in net._id2edge:
        v = net._id2edge[e]._speed
        if minV is None or minV > v:
            minV = v
        if maxV is None or maxV < v:
            maxV = v
        speeds[e] = v
    if options.minV is not None:
        minV = options.minV
    if options.maxV is not None:
        maxV = options.maxV
    # if options.logColors:


#    helpers.logNormalise(colors, maxColorValue)
#  else:
#    helpers.linNormalise(colors, minColorValue, maxColorValue)

    helpers.linNormalise(speeds, minV, maxV)
    for e in speeds:
        speeds[e] = helpers.getColor(options, speeds[e], 1.)
    fig, ax = helpers.openFigure(options)
    ax.set_aspect("equal", None, 'C')
    helpers.plotNet(net, speeds, {}, options)

    # drawing the legend, at least for the colors
    print("%s -> %s" % (minV, maxV))
    sm = matplotlib.cm.ScalarMappable(cmap=get_cmap(options.colormap),
                                      norm=plt.normalize(vmin=minV, vmax=maxV))
    # "fake up the array of the scalar mappable. Urgh..." (pelson, http://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots)
    sm._A = []
    plt.colorbar(sm)
    options.nolegend = True
    helpers.closeFigure(fig, ax, options)
'Nevada': 7.03,
'Idaho': 6.04,
'New Mexico': 5.79,
'South Dakota': 3.84,
'North Dakota': 3.59,
'Montana': 2.39,
'Wyoming': 1.96,
'Alaska': 0.42}

# choose a color for each state based on population density.
colors={}
statenames=[]
cmap = plt.cm.Blues_r # use 'hot' colormap
vmin = 0; vmax = 450 # set range.
sm = plt.cm.ScalarMappable(cmap=cmap, 
                           norm=plt.normalize(vmin=vmin, vmax=vmax))

for shapedict in m.states_info:
    statename = shapedict['NAME']

    try:
        pop = popdensity[statename]
    except KeyError:
        pop = 0.0
        
    # calling colormap with value between 0 and 1 returns
    # rgba value. Invert color range (hot colors are high
    # population), take sqrt root to spread out colors more.
    colors[statename] = cmap((pop-vmin)/(vmax-vmin))[:3]
    statenames.append(statename)
        
def make_plots():

    label_size = 20
    #title_size = 30
    axis_size = 28

    # Set up fonts for plots
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['arial']
    rcParams['font.size'] = label_size

    #########################
    #                       #
    #   READ IN THE DATA    #
    #                       #
    #########################

    # open file
    #DataDirectory = 'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\India\\'
    #DataDirectory =  'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\PA\\'
    #DataDirectory =  'm:\\topographic_tools\\LSDRaster_chi_package\\Test_data\\'
    #DataDirectory =  'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\Test_data\\'
    DataDirectory = 'c:\\code\\topographic_analysis\\LSDRaster_chi_package\\Child_runs_mk4\\'
    #FileName = 'pa_basin_fullProfileMC_mainstem_1189.tree'
    #FileName = 'pa_basin_fullProfileMC_mainstem_3124.tree'
    #FileName = 'rio_torto_fullProfileMC_mainstem_633.tree'
    #FileName = 'rio_torto_fullProfileMC_mainstem_114.tree'
    #FileName = 'rio_torto_fullProfileMC_colinear_114.tree'
    #FileName = 'rio_torto_fullProfileMC_colinear_110.tree'
    #FileName = 'rio_torto_fullProfileMC_mainstem_110.tree'
    #FileName = 'rio_torto_fullProfileMC_forced_0.4_110.tree'
    #FileName = 'UniUplift09b_mk4_t6_fullProfileMC_colinear.tree'
    FileName = 'UniUplift09b_mk4_t6_fullProfileMC_colinear_1_2_10_90.tree'

    #OutputFigureName = 'chi_plot'
    #OutputFigureFormat = 'eps'
    f = open(DataDirectory + FileName, 'r')  # open file
    lines = f.readlines()  # read in the data
    n_lines = len(lines)  # get the number of lines (=number of data)
    n_data = n_lines - 1
    # data variables
    channel_id = np.zeros(n_data)  # ID number for channel segment
    receiver_channel = np.zeros(n_data)
    #node_on_receiver_channel = np.zeros(n_data, dtype=np.int)
    node = np.zeros(n_data)  # node number
    row = np.zeros(n_data)  # row
    col = np.zeros(n_data)  # column
    flow_dist = np.zeros(n_data)  # flow distance
    chi = np.zeros(n_data)  # chi
    elevation = np.zeros(n_data)  # elevation
    drainage_area = np.zeros(n_data)  # drainage area
    n_data_points_uic = np.zeros(n_data)
    m_mean = np.zeros(n_data)
    #m_standard_deviation = np.zeros(n_data)
    m_standard_error = np.zeros(n_data)
    b_mean = np.zeros(n_data)
    #b_standard_deviation = np.zeros(n_data)
    b_standard_error = np.zeros(n_data)
    DW_mean = np.zeros(n_data)
    #DW_standard_deviation = np.zeros(n_data)
    DW_standard_error = np.zeros(n_data)
    fitted_elevation_mean = np.zeros(n_data)
    #fitted_elevation_standard_deviation = np.zeros(n_data)
    fitted_elevation_standard_error = np.zeros(n_data)

    print "Reading " + FileName

    # get the A_0 and m/n values
    line = lines[0].strip().split(" ")
    A_0 = float(line[0])
    m_over_n = float(line[1])

    for i in range(0, n_data):
        line = lines[i + 1].strip().split(" ")
        #print line
        channel_id[i] = int(float(line[0]))
        receiver_channel[i] = int(float(line[1]))
        #node_on_receiver_channel = int(float(line[2]))
        node[i] = int(float(line[3]))
        row[i] = int(line[4])
        col[i] = int(line[5])
        flow_dist[i] = float(line[6])
        chi[i] = float(line[7])
        elevation[i] = float(line[8])
        drainage_area[i] = float(line[9])
        n_data_points_uic[i] = float(line[10])
        m_mean[i] = float(line[11])
        #m_standard_deviation[i] = float(line[12])
        m_standard_error[i] = float(line[13])
        b_mean[i] = float(line[14])
        #b_standard_deviation[i] = float(line[15])
        b_standard_error[i] = float(line[16])
        DW_mean[i] = float(line[17])
        #DW_standard_deviation[i] = float(line[18])
        DW_standard_error[i] = float(line[19])
        fitted_elevation_mean[i] = float(line[20])
        #fitted_elevation_standard_deviation = float(line[21])
        fitted_elevation_standard_error[i] = float(line[22])
    f.close()

    # Determine number of segments, and their respective lengths
    n_channel_segments = channel_id[n_data - 1] + 1
    segment_lengths = np.zeros(n_channel_segments, dtype=np.int)
    for i in range(0, n_data):
        segment_lengths[channel_id[i]] = segment_lengths[channel_id[i]] + 1

    #########################
    #                       #
    #   MAKE CHI-PLOTS      #
    #                       #
    #########################
    print "Producing figures..."

    # SET UP COLOURMAPS
    jet = plt.get_cmap('jet')
    hot = plt.get_cmap('RdYlBu_r')

    # m-values
    m_MIN = np.min(m_mean)
    m_MAX = np.max(m_mean)
    cNorm_m_values = colors.Normalize(
        vmin=m_MIN,
        vmax=m_MAX)  # the max number of channel segs is the 'top' colour
    scalarMap_m_values = cmx.ScalarMappable(norm=cNorm_m_values, cmap=hot)

    # channel ID
    Channel_ID_MIN = np.min(channel_id)
    Channel_ID_MAX = np.max(channel_id)
    cNorm_channel_ID = colors.Normalize(
        vmin=Channel_ID_MIN, vmax=Channel_ID_MAX
    )  # the max number of channel segs is the 'top' colour
    scalarMap_channel_ID = cmx.ScalarMappable(norm=cNorm_channel_ID, cmap=jet)

    minfd = min(flow_dist)
    print "minimum flow distance: " + str(minfd)

    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # PLOT OF CHI m-VALUE (+ERROR) AGAINST CHI WITH EACH CHANNEL LABELLED WITH DISTINCT COLOUR
    plt.figure(2, facecolor='white', figsize=(10, 7.5))
    ax = plt.subplot(1, 1, 1)
    for i in range(0, len(channel_id)):

        colorVal = scalarMap_channel_ID.to_rgba(channel_id[i])
        if channel_id[i] == 0:
            plt.plot(chi[i],
                     m_mean[i],
                     "o",
                     markersize=10,
                     color='black',
                     markeredgecolor='black',
                     alpha=0.7)
        else:
            plt.plot(chi[i],
                     m_mean[i],
                     "o",
                     markersize=8,
                     color=colorVal,
                     markeredgecolor=colorVal,
                     alpha=0.7)

        # plt.plot(chi[i], m_mean[i] + m_standard_error[i], ".", linewidth=0.25, color='k')
        # plt.plot(chi[i], m_mean[i] - m_standard_error[i], ".", linewidth=0.25, color='k')

    # Configure final plot
    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5)
    ax.tick_params(axis='both', width=2.5)
    plt.xlabel('$\chi$ (m)', fontsize=axis_size)
    plt.ylabel('Gradient in $\chi$ space', fontsize=axis_size)
    plt.title('$A_0$: ' + str(A_0) + ' m$^2$, and $m/n$: ' + str(m_over_n),
              fontsize=label_size)

    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # LONGITUDINAL PROFILES WITH COLOUR SCALE GIVING CHI-m VALUE
    plt.figure(3, facecolor='white', figsize=(10, 7.5))
    ax = plt.subplot(1, 1, 1)
    for i in range(0, len(channel_id)):
        colorVal = scalarMap_m_values.to_rgba(m_mean[i])
        plt.plot((flow_dist[i] - minfd) / 1000,
                 elevation[i],
                 "o",
                 markersize=6,
                 color=colorVal,
                 markeredgecolor=colorVal,
                 alpha=0.5)

    # Configure final plot
    sm = plt.cm.ScalarMappable(cmap=hot,
                               norm=plt.normalize(vmin=np.min(m_mean),
                                                  vmax=np.max(m_mean)))
    sm._A = []

    cbar = plt.colorbar(sm, orientation='horizontal', use_gridspec=True)
    cbar.set_label('Gradient in $\chi$ space', fontsize=axis_size)
    plt.xlabel('Distance upstream (km)', fontsize=axis_size)
    plt.ylabel('Elevation (m)', fontsize=axis_size)
    plt.title('$A_0$: ' + str(A_0) + ' m$^2$, and $m/n$: ' + str(m_over_n),
              fontsize=label_size)

    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5)
    ax.tick_params(axis='both', width=2.5)

    cbar.ax.spines['top'].set_linewidth(2.5)
    cbar.ax.spines['left'].set_linewidth(2.5)
    cbar.ax.spines['right'].set_linewidth(2.5)
    cbar.ax.spines['bottom'].set_linewidth(2.5)
    cbar.ax.tick_params(axis='both', width=2.5)

    plt.tight_layout()

    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    # BASIC CHI-PLOT WITH EACH CHANNEL LABELLED WITH A DIFFERENT COLOUR
    plt.figure(1, facecolor='white', figsize=(10, 7.5))
    ax = plt.subplot(1, 1, 1)

    data_pointer = 0  # points to data element in chi and elev vectors
    for i in range(0, len(segment_lengths)):
        chi_seg = np.zeros(segment_lengths[i])
        elev_seg = np.zeros(segment_lengths[i])

        for j in range(0, segment_lengths[i]):
            chi_seg[j] = chi[data_pointer]
            elev_seg[j] = elevation[data_pointer]
            data_pointer = data_pointer + 1

        if i == 0:
            # plot trunk stream in black, with thicker line
            l1, = ax.plot(chi_seg, elev_seg, "k-", linewidth=4, alpha=0.3)
        else:
            # plot other stream segments plot
            colorVal = scalarMap_channel_ID.to_rgba(
                i)  # this gets the distinct colour for this segment
            ax.plot(chi_seg,
                    elev_seg,
                    "-",
                    linewidth=4,
                    color=colorVal,
                    alpha=0.3)

# now loop again plotting the fitted segments
    data_pointer = 0  # points to data element in chi and elev vectors
    for i in range(0, len(segment_lengths)):
        chi_seg = np.zeros(segment_lengths[i])
        elev_seg = np.zeros(segment_lengths[i])

        for j in range(0, segment_lengths[i]):
            chi_seg[j] = chi[data_pointer]
            elev_seg[j] = fitted_elevation_mean[data_pointer]
            data_pointer = data_pointer + 1

        if i == 0:
            # plot trunk stream in black, with thicker line
            l2, = plt.plot(chi_seg, elev_seg, "k", linewidth=3, dashes=(10, 2))
        else:
            # plot other stream segments plot
            colorVal = scalarMap_channel_ID.to_rgba(
                i)  # this gets the distinct colour for this segment
            ax.plot(chi_seg,
                    elev_seg,
                    linewidth=3,
                    color=colorVal,
                    dashes=(10, 2))

    # Configure final plot
    plt.xlabel('$\chi$ (m)', fontsize=axis_size)
    plt.ylabel('Elevation (m)', fontsize=axis_size)
    plt.title('$A_0$: ' + str(A_0) + ' m$^2$, and $m/n$: ' + str(m_over_n),
              fontsize=label_size)
    plt.legend((l1, l2), ('Data', 'Best fit segments'),
               'lower right',
               prop={'size': label_size})

    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5)
    ax.tick_params(axis='both', width=2.5)

    #plt.savefig(OutputFigureName + '.' + OutputFigureFormat, format=OutputFigureFormat)

    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    #    # CHI-PLOT USING FITTED PROFILES (+ERROR) RATHER THAN RAW DATA, LABELLED WITH DISTINCT COLOUR
    #    plt.figure(4, facecolor='white')
    #    data_pointer = 0    # points to data element in chi and elev vectors
    #    for i in range (0,len(segment_lengths)):
    #        chi_seg = np.zeros(segment_lengths[i])
    #        fitted_elev_seg = np.zeros(segment_lengths[i])
    #        fitted_elev_ulim = np.zeros(segment_lengths[i])
    #        fitted_elev_llim = np.zeros(segment_lengths[i])
    #
    #        for j in range (0,segment_lengths[i]):
    #            chi_seg[j] = chi[data_pointer]
    #            fitted_elev_seg[j] = fitted_elevation_mean[data_pointer]
    #            fitted_elev_ulim[j] = fitted_elevation_mean[data_pointer] + fitted_elevation_standard_error[data_pointer]
    #            fitted_elev_llim[j] = fitted_elevation_mean[data_pointer] - fitted_elevation_standard_error[data_pointer]
    #            data_pointer = data_pointer + 1
    #            # plot other stream segments plot
    #        colorVal = scalarMap_channel_ID.to_rgba(i)
    #        plt.plot(chi_seg, fitted_elev_ulim, "-k", linewidth=0.2)
    #        plt.plot(chi_seg, fitted_elev_llim, "-k", linewidth=0.2)
    #        plt.plot(chi_seg, fitted_elev_seg, "-", linewidth=0.5, color=colorVal)
    #
    #    # Configure final plot
    #    plt.xlabel('$\chi$ / m')
    #    plt.ylabel('fitted elevation / m')
    #
    #    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    #    # CHI-PLOT DISPLAYING BOTH RAW DATA AND FITTED PROFILES
    #    plt.figure(5, facecolor='white')
    #
    #    data_pointer = 0    # points to data element in chi and elev vectors
    #    for i in range (0,len(segment_lengths)):
    #        chi_seg = np.zeros(segment_lengths[i])
    #        elev_seg = np.zeros(segment_lengths[i])
    #        fitted_elev_seg = np.zeros(segment_lengths[i])
    #
    #        for j in range (0,segment_lengths[i]):
    #            chi_seg[j] = chi[data_pointer]
    #            elev_seg[j] = elevation[data_pointer]
    #            fitted_elev_seg[j] = fitted_elevation_mean[data_pointer]
    #            data_pointer = data_pointer + 1
    #
    #        # plot other stream segments plot
    #        colorVal = scalarMap_channel_ID.to_rgba(i) # this gets the distinct colour for this segment
    #        plt.plot(chi_seg, elev_seg, "-k", linewidth=1)
    #        plt.plot(chi_seg, fitted_elev_seg, "-", linewidth=1, color=colorVal)
    #
    #     # Configure final plot
    #    plt.xlabel('$\chi$ / m')
    #    plt.ylabel('elevation / m')
    #
    #    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    #    # CHI-PLOT USING ONLY MAIN TRIBUTARY WITH FITTED PROFILES (+ERROR)
    #    plt.figure(6, facecolor='white')
    #    for i in range (0,len(channel_id)):
    #        plt.plot(chi[channel_id==0], fitted_elevation_mean[channel_id==0] + fitted_elevation_standard_error[channel_id==0], "-k", linewidth=0.2)
    #        plt.plot(chi[channel_id==0], fitted_elevation_mean[channel_id==0] - fitted_elevation_standard_error[channel_id==0], "-k", linewidth=0.2)
    #        plt.plot(chi[channel_id==0], elevation[channel_id==0], "-b", linewidth=0.5)
    #        plt.plot(chi[channel_id==0], fitted_elevation_mean[channel_id==0], "-r", linewidth=0.5)
    #
    #
    #
    #     # Configure final plot
    #    plt.xlabel('$\chi$ / m')
    #    plt.ylabel('elevation / m')
    #
    #    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    #    # PLOT OF CHI b-VALUE (+ERROR) AGAINST CHI WITH EACH CHANNEL LABELLED WITH DISTINCT COLOUR
    #    plt.figure(7, facecolor='white')
    #    for i in range (0,len(channel_id)):
    #            colorVal = scalarMap_channel_ID.to_rgba(channel_id[i])
    #            plt.plot(chi[i], b_mean[i], ".", linewidth=0.2, color=colorVal)
    #            #plt.plot(chi[i], b_mean[i] + b_standard_error[i], ".", linewidth=0.25, color='k')
    #            #plt.plot(chi[i], b_mean[i] - b_standard_error[i], ".", linewidth=0.25, color='k')
    #
    #    # Configure final plot
    #    plt.xlabel('$\chi$ / m')
    #    plt.ylabel('b-value / m')
    #
    #
    #    #=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
    #    # PLOT OF DW STATISTIC (+ERROR) AGAINST CHI WITH EACH CHANNEL LABELLED WITH DISTINCT COLOUR
    #    plt.figure(8, facecolor='white')
    #    for i in range (0,len(channel_id)):
    #            colorVal = scalarMap_channel_ID.to_rgba(channel_id[i])
    #            plt.plot(chi[i], DW_mean[i], ".", linewidth=0.2, color=colorVal)
    #            #plt.plot(chi[i], DW_mean[i] + DW_standard_error[i], ".", linewidth=0.25, color='k')
    #            #plt.plot(chi[i], DW_mean[i] - DW_standard_error[i], ".", linewidth=0.25, color='k')
    #
    #    # Configure final plot
    #    plt.xlabel('$\chi$ / m')
    #    plt.ylabel('DW statistic')

    plt.show()
def main(args=None):
  """The main function; parses options and plots"""
  ## ---------- build and read options ----------
  from optparse import OptionParser
  optParser = OptionParser()
  optParser.add_option("-n", "--net", dest="net", metavar="FILE",
                         help="Defines the network to read")
  optParser.add_option("--edge-width", dest="defaultWidth", 
                         type="float", default=1, help="Defines the edge width")
  optParser.add_option("--edge-color", dest="defaultColor", 
                         default='k', help="Defines the edge color")
  optParser.add_option("--minV", dest="minV", 
                         type="float", default=None, help="Define the minimum value boundary")
  optParser.add_option("--maxV", dest="maxV", 
                         type="float", default=None, help="Define the maximum value boundary")
  optParser.add_option("-v", "--verbose", dest="verbose", action="store_true",
                         default=False, help="If set, the script says what it's doing")
  # standard plot options
  helpers.addInteractionOptions(optParser)
  helpers.addPlotOptions(optParser)
  # parse
  options, remaining_args = optParser.parse_args(args=args)

  if options.net==None: 
    print "Error: a network to load must be given."
    return 1
  if options.verbose: print "Reading network from '%s'" % options.net
  net = sumolib.net.readNet(options.net)

  speeds = {}
  minV = None
  maxV = None
  for e in net._id2edge:
    v = net._id2edge[e]._speed
    if minV==None or minV>v:
        minV = v
    if maxV==None or maxV<v:
        maxV = v
    speeds[e] = v
  if options.minV!=None: minV = options.minV 
  if options.maxV!=None: maxV = options.maxV 
  #if options.logColors: 
#    helpers.logNormalise(colors, maxColorValue)
#  else:
#    helpers.linNormalise(colors, minColorValue, maxColorValue)
  
  helpers.linNormalise(speeds, minV, maxV)
  for e in speeds:
    speeds[e] = helpers.getColor(options, speeds[e], 1.)
  fig, ax = helpers.openFigure(options)
  ax.set_aspect("equal", None, 'C')
  helpers.plotNet(net, speeds, {}, options)

  # drawing the legend, at least for the colors
  print "%s -> %s" % (minV, maxV)
  sm = matplotlib.cm.ScalarMappable(cmap=get_cmap(options.colormap), norm=plt.normalize(vmin=minV, vmax=maxV))
  # "fake up the array of the scalar mappable. Urgh..." (pelson, http://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots)
  sm._A = []
  plt.colorbar(sm)
  options.nolegend = True
  helpers.closeFigure(fig, ax, options)
npts = 200
ngridx = 100
ngridy = 200
x = uniform(-2,2,npts)
y = uniform(-2,2,npts)
z = x*np.exp(-x**2-y**2)

# griddata and contour.
start = time.clock()
plt.subplot(211)
xi = np.linspace(-2.1,2.1,ngridx)
yi = np.linspace(-2.1,2.1,ngridy)
zi = griddata(x,y,z,xi,yi,interp='linear')
plt.contour(xi,yi,zi,15,linewidths=0.5,colors='k')
plt.contourf(xi,yi,zi,15,cmap=plt.cm.rainbow,
             norm=plt.normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))
plt.colorbar() # draw colorbar
plt.plot(x, y, 'ko', ms=3)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.title('griddata and contour (%d points, %d grid points)' % (npts, ngridx*ngridy))
print ('griddata and contour seconds: %f' % (time.clock() - start))

# tricontour.
start = time.clock()
plt.subplot(212)
triang = tri.Triangulation(x, y)
plt.tricontour(x, y, z, 15, linewidths=0.5, colors='k')
plt.tricontourf(x, y, z, 15, cmap=plt.cm.rainbow,
                norm=plt.normalize(vmax=abs(zi).max(), vmin=-abs(zi).max()))
plt.colorbar()
Beispiel #55
0
import numpy as np
from scipy.interpolate import Rbf
import matplotlib.pyplot as plt
from matplotlib import cm

# 2-d tests - setup scattered data
x = np.random.rand(100)*4.0-2.0
y = np.random.rand(100)*4.0-2.0
z = x*np.exp(-x**2-y**2)
ti = np.linspace(-2.0, 2.0, 100)
XI, YI = np.meshgrid(ti, ti)

# use RBF
rbf = Rbf(x, y, z, epsilon=2)
ZI = rbf(XI, YI)

# plot the result
n = plt.normalize(-2., 2.)
plt.subplot(1, 1, 1)
plt.pcolor(XI, YI, ZI, cmap=cm.jet)
plt.scatter(x, y, 100, z, cmap=cm.jet)
plt.title('RBF interpolation - multiquadrics')
plt.xlim(-2, 2)
plt.ylim(-2, 2)
plt.colorbar()
plt.show()
    # color_index.append(len(dep_fac_dict[d]))


node_labels['CEWIT'] = 'CEWIT'
node_sizes['CEWIT'] = 1500
node_colors['CEWIT'] = '#E1D8B7'

# print node_sizes.keys()
# print node_sizes.values()
# print node_labels.keys()
# print node_labels.values()
edges = G.edges()
weights = [G[u][v]['weight'] for u,v in edges]

fig = plt.figure('Distribution of CEWIT Faculty Members-caption above',figsize=(16,8)) 

cm = mpl.colors.ListedColormap(C)

pos = nx.spring_layout(G, k=0.9, scale = 2)

nx.draw(G, pos, linewidths = 0.5,labels = node_labels, font_size = 12, edges = edges, width = weights, nodelist = node_sizes.keys(), node_size = node_sizes.values(), font_family = 'Century Gothic', node_color=node_colors.values(), edge_color='#44697D',with_labels=True)

sm = plt.cm.ScalarMappable(cmap=cm, norm=plt.normalize(vmin=5, vmax=35))
sm._A = []
plt.colorbar(sm, shrink = 0.7, pad = 0.15, orientation = 'vertical', anchor = (-2,0.5),fraction = 0.1, aspect = 25, drawedges = False)

figtext = 'Distribution of CEWIT Faculty Members'
fig.text(.38,.93,figtext, weight = 'heavy', size = 20, ha = 'center', family = 'Century Gothic')


plt.show() # display
def m_values_over_hillshade(hillshade_file, tree_file):
    """
    Plots m values of channels taken from the *.tree file over a hillshade
    
    """
 
    label_size = 20
    #title_size = 30
    axis_size = 28
   
    import matplotlib.pyplot as pp
    import numpy as np
    import matplotlib.colors as colors
    import matplotlib.cm as cmx
    from matplotlib import rcParams
    import matplotlib.lines as mpllines
    
    #get data
    hillshade, hillshade_header = read_flt(hillshade_file)
    
    #ignore nodata values    
    hillshade = np.ma.masked_where(hillshade == -9999, hillshade)    
    
    #fonts
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['arial']
    rcParams['font.size'] = label_size  

    #get coordinates of streams from tree file   
    M_chi_value = []
    channel_id = []
    row = []
    col = []
        
    with open(tree_file, 'r') as f:
        lines = f.readlines()
        
    for q,line in enumerate(lines):
        if q > 0: #skip first line
            channel_id.append(float(line.split()[0]))
            M_chi_value.append(float(line.split()[11]))
            row.append(float(line.split()[4]))
            col.append(float(line.split()[5]))

    #get bounding box & pad to 10% of the dimension
    x_max = max(col)
    x_min = min(col)
    y_max = max(row)
    y_min = min(row) 
    
    pad_x = (x_max - x_min) * 0.1
    pad_y = (x_max - y_min) * 0.1
    
    if (pad_y > pad_x):
        pad_x = pad_y
    else:
        pad_y = pad_x
    
    x_max += pad_x
    x_min -= pad_x
    y_max += pad_y
    y_min -= pad_y 
    
    fig = pp.figure(1, facecolor='white',figsize=(10,7.5))
    ax = fig.add_subplot(1,1,1)
    ax.imshow(hillshade, vmin=0, vmax=255, cmap=cmx.gray)
    
    # now get the tick marks    
    n_target_tics = 5
    xlocs,ylocs,new_x_labels,new_y_labels = format_ticks_for_UTM_imshow(hillshade_header,x_max,x_min,y_max,y_min,n_target_tics)  
    pp.xticks(xlocs, new_x_labels, rotation=60)  #[1:-1] skips ticks where we have no data
    pp.yticks(ylocs, new_y_labels) 
    
    for line in ax.get_xticklines():
        line.set_marker(mpllines.TICKDOWN)
        #line.set_markeredgewidth(3)

    for line in ax.get_yticklines():
        line.set_marker(mpllines.TICKLEFT)
        #line.set_markeredgewidth(3)  
 
    pp.xlim(x_min,x_max)    
    pp.ylim(y_max,y_min)  
   
    pp.xlabel('Easting (m)',fontsize = axis_size)
    pp.ylabel('Northing (m)',fontsize = axis_size)    
              
    # channel ID
    M_chi_value_MIN = np.min(M_chi_value)
    M_chi_value_MAX = np.max(M_chi_value)
    cNorm_M_chi_value  = colors.Normalize(vmin=M_chi_value_MIN, vmax=M_chi_value_MAX)  # the max number of channel segs is the 'top' colour
    hot = pp.get_cmap('RdYlBu_r')
    scalarMap_M_chi_value = cmx.ScalarMappable(norm=cNorm_M_chi_value, cmap=hot) 
    
    
    for a,i in enumerate(M_chi_value):
        #print "a: " +str(a)+" i: " +str(i)
        if channel_id[a] != 0:     
            # plot other stream segments
            colorVal = scalarMap_M_chi_value.to_rgba(i) # this gets the distinct colour for this segment
            pp.scatter(col[a], row[a], 30,marker=".", color=colorVal,edgecolors=colorVal) 

    for a,i in enumerate(M_chi_value):
        if channel_id[a] == 0:
            # plot trunk stream in black
            colorVal = scalarMap_M_chi_value.to_rgba(i)
            pp.scatter(col[a], row[a], 40,marker=".", color=colorVal,edgecolors=colorVal)

    sm = pp.cm.ScalarMappable(cmap=hot, norm=pp.normalize(vmin=min(M_chi_value), vmax=max(M_chi_value)))
    sm._A = []
    


    
    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5) 
    ax.tick_params(axis='both', width=2.5)      
    
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    divider = make_axes_locatable(pp.gca())
    cax = divider.append_axes("right", "5%", pad="3%")
    pp.colorbar(sm, cax=cax).set_label('$M_{\chi}$',fontsize=axis_size) 
    cax.tick_params(labelsize=label_size) 
    
    #pp.xlim(x_min,x_max)    
    #pp.ylim(y_max,y_min) 

    pp.tight_layout()
        
    pp.show()
Beispiel #58
0
def main(args=None):
    """The main function; parses options and plots"""
    ## ---------- build and read options ----------
    from optparse import OptionParser
    optParser = OptionParser()
    optParser.add_option("-n",
                         "--net",
                         dest="net",
                         metavar="FILE",
                         help="Defines the network to read")
    optParser.add_option("-i",
                         "--dump-inputs",
                         dest="dumps",
                         metavar="FILE",
                         help="Defines the dump-output files to use as input")
    optParser.add_option("-m",
                         "--measures",
                         dest="measures",
                         default="speed,entered",
                         help="Define which measure to plot")
    optParser.add_option("-w",
                         "--default-width",
                         dest="defaultWidth",
                         type="float",
                         default=.1,
                         help="Defines the default edge width")
    optParser.add_option("-c",
                         "--default-color",
                         dest="defaultColor",
                         default='k',
                         help="Defines the default edge color")
    optParser.add_option("--min-width",
                         dest="minWidth",
                         type="float",
                         default=.5,
                         help="Defines the minimum edge width")
    optParser.add_option("--max-width",
                         dest="maxWidth",
                         type="float",
                         default=3,
                         help="Defines the maximum edge width")
    optParser.add_option("--log-colors",
                         dest="logColors",
                         action="store_true",
                         default=False,
                         help="If set, colors are log-scaled")
    optParser.add_option("--log-widths",
                         dest="logWidths",
                         action="store_true",
                         default=False,
                         help="If set, widths are log-scaled")
    optParser.add_option("--min-color-value",
                         dest="colorMin",
                         type="float",
                         default=None,
                         help="If set, defines the minimum edge color value")
    optParser.add_option("--max-color-value",
                         dest="colorMax",
                         type="float",
                         default=None,
                         help="If set, defines the maximum edge color value")
    optParser.add_option("--min-width-value",
                         dest="widthMin",
                         type="float",
                         default=None,
                         help="If set, defines the minimum edge width value")
    optParser.add_option("--max-width-value",
                         dest="widthMax",
                         type="float",
                         default=None,
                         help="If set, defines the maximum edge width value")
    optParser.add_option("-v",
                         "--verbose",
                         dest="verbose",
                         action="store_true",
                         default=False,
                         help="If set, the script says what it's doing")
    # standard plot options
    helpers.addInteractionOptions(optParser)
    helpers.addPlotOptions(optParser)
    # parse
    options, remaining_args = optParser.parse_args(args=args)

    if options.net == None:
        print "Error: a network to load must be given."
        return 1
    if options.verbose: print "Reading network from '%s'" % options.net
    net = sumolib.net.readNet(options.net)

    if options.measures == None:
        print "Error: a dump file must be given."
        return 1

    times = []
    hc = None
    if options.dumps.split(",")[0] != "":
        if options.verbose:
            print "Reading colors from '%s'" % options.dumps.split(",")[0]
        hc = WeightsReader(options.measures.split(",")[0])
        sumolib.output.parse_sax(options.dumps.split(",")[0], hc)
        times = hc._edge2value

    hw = None
    if options.dumps.split(",")[1] != "":
        if options.verbose:
            print "Reading widths from '%s'" % options.dumps.split(",")[1]
        hw = WeightsReader(options.measures.split(",")[1])
        sumolib.output.parse_sax(options.dumps.split(",")[1], hw)
        times = hw._edge2value

    for t in times:
        colors = {}
        maxColorValue = None
        minColorValue = None
        for e in net._id2edge:
            if hc and t in hc._edge2value and e in hc._edge2value[t]:
                if options.colorMax != None and hc._edge2value[t][
                        e] > options.colorMax:
                    hc._edge2value[t][e] = options.colorMax
                if options.colorMin != None and hc._edge2value[t][
                        e] < options.colorMin:
                    hc._edge2value[t][e] = options.colorMin
                if maxColorValue == None or maxColorValue < hc._edge2value[t][
                        e]:
                    maxColorValue = hc._edge2value[t][e]
                if minColorValue == None or minColorValue > hc._edge2value[t][
                        e]:
                    minColorValue = hc._edge2value[t][e]
                colors[e] = hc._edge2value[t][e]
        if options.colorMax != None: maxColorValue = options.colorMax
        if options.colorMin != None: minColorValue = options.colorMin
        if options.logColors:
            helpers.logNormalise(colors, maxColorValue)
        else:
            helpers.linNormalise(colors, minColorValue, maxColorValue)
        for e in colors:
            colors[e] = helpers.getColor(options, colors[e], 1.)
        if options.verbose:
            print "Color values are between %s and %s" % (minColorValue,
                                                          maxColorValue)

        widths = {}
        maxWidthValue = None
        minWidthValue = None
        for e in net._id2edge:
            if hw and t in hw._edge2value and e in hw._edge2value[t]:
                v = abs(hw._edge2value[t][e])
                if options.widthMax != None and v > options.widthMax:
                    v = options.widthMax
                if options.widthMin != None and v < options.widthMin:
                    v = options.widthMin
                if not maxWidthValue or maxWidthValue < v: maxWidthValue = v
                if not minWidthValue or minWidthValue > v: minWidthValue = v
                widths[e] = v
        if options.widthMax != None: maxWidthValue = options.widthMax
        if options.widthMin != None: minWidthValue = options.widthMin
        if options.logWidths:
            helpers.logNormalise(widths, options.colorMax)
        else:
            helpers.linNormalise(widths, minWidthValue, maxWidthValue)
        for e in widths:
            widths[e] = options.minWidth + widths[e] * (options.maxWidth -
                                                        options.minWidth)
        if options.verbose:
            print "Width values are between %s and %s" % (minWidthValue,
                                                          maxWidthValue)

        fig, ax = helpers.openFigure(options)
        ax.set_aspect("equal", None, 'C')
        helpers.plotNet(net, colors, widths, options)

        # drawing the legend, at least for the colors
        sm = matplotlib.cm.ScalarMappable(cmap=get_cmap(options.colormap),
                                          norm=plt.normalize(
                                              vmin=minColorValue,
                                              vmax=maxColorValue))
        # "fake up the array of the scalar mappable. Urgh..." (pelson, http://stackoverflow.com/questions/8342549/matplotlib-add-colorbar-to-a-sequence-of-line-plots)
        sm._A = []
        plt.colorbar(sm)

        options.nolegend = True
        helpers.closeFigure(fig, ax, options)
        return 0  # !!! hack
    return 0
def m_values_over_hillshade(hillshade_file, tree_file):
    """
    Plots m values of channels taken from the *.tree file over a hillshade
    
    """
 
    label_size = 20
    #title_size = 30
    axis_size = 28
   
    import matplotlib.pyplot as pp
    import numpy as np
    import matplotlib.colors as colors
    import matplotlib.cm as cmx
    from matplotlib import rcParams
    import matplotlib.lines as mpllines
    
    #get data
    hillshade, hillshade_header = read_flt(hillshade_file)
    
    #ignore nodata values    
    hillshade = np.ma.masked_where(hillshade == -9999, hillshade)    
    
    #fonts
    rcParams['font.family'] = 'sans-serif'
    rcParams['font.sans-serif'] = ['arial']
    rcParams['font.size'] = label_size  

    #get coordinates of streams from tree file   
    M_chi_value = []
    channel_id = []
    row = []
    col = []
        
    with open(tree_file, 'r') as f:
        lines = f.readlines()
        
    for q,line in enumerate(lines):
        if q > 0: #skip first line
            channel_id.append(float(line.split()[0]))
            M_chi_value.append(float(line.split()[11]))
            row.append(float(line.split()[4]))
            col.append(float(line.split()[5]))

    #get bounding box & pad to 10% of the dimension
    x_max = max(col)
    x_min = min(col)
    y_max = max(row)
    y_min = min(row) 
    
    pad_x = (x_max - x_min) * 0.1
    pad_y = (x_max - y_min) * 0.1
    
    if (pad_y > pad_x):
        pad_x = pad_y
    else:
        pad_y = pad_x
    
    x_max += pad_x
    x_min -= pad_x
    y_max += pad_y
    y_min -= pad_y 
    
    fig = pp.figure(1, facecolor='white',figsize=(10,7.5))
    ax = fig.add_subplot(1,1,1)
    ax.imshow(hillshade, vmin=0, vmax=255, cmap=cmx.gray)
    
    # now get the tick marks    
    n_target_tics = 5
    xlocs,ylocs,new_x_labels,new_y_labels = format_ticks_for_UTM_imshow(hillshade_header,x_max,x_min,y_max,y_min,n_target_tics)  
    pp.xticks(xlocs, new_x_labels, rotation=60)  #[1:-1] skips ticks where we have no data
    pp.yticks(ylocs, new_y_labels) 
    
    for line in ax.get_xticklines():
        line.set_marker(mpllines.TICKDOWN)
        #line.set_markeredgewidth(3)

    for line in ax.get_yticklines():
        line.set_marker(mpllines.TICKLEFT)
        #line.set_markeredgewidth(3)  
 
    pp.xlim(x_min,x_max)    
    pp.ylim(y_max,y_min)  
   
    pp.xlabel('Easting (m)',fontsize = axis_size)
    pp.ylabel('Northing (m)',fontsize = axis_size)    
              
    # channel ID
    M_chi_value_MIN = np.min(M_chi_value)
    M_chi_value_MAX = np.max(M_chi_value)
    cNorm_M_chi_value  = colors.Normalize(vmin=M_chi_value_MIN, vmax=M_chi_value_MAX)  # the max number of channel segs is the 'top' colour
    hot = pp.get_cmap('RdYlBu_r')
    scalarMap_M_chi_value = cmx.ScalarMappable(norm=cNorm_M_chi_value, cmap=hot) 
    
    
    for a,i in enumerate(M_chi_value):
        #print "a: " +str(a)+" i: " +str(i)
        if channel_id[a] != 0:     
            # plot other stream segments
            colorVal = scalarMap_M_chi_value.to_rgba(i) # this gets the distinct colour for this segment
            pp.scatter(col[a], row[a], 30,marker=".", color=colorVal,edgecolors=colorVal) 

    for a,i in enumerate(M_chi_value):
        if channel_id[a] == 0:
            # plot trunk stream in black
            colorVal = scalarMap_M_chi_value.to_rgba(i)
            pp.scatter(col[a], row[a], 40,marker=".", color=colorVal,edgecolors=colorVal)

    sm = pp.cm.ScalarMappable(cmap=hot, norm=pp.normalize(vmin=min(M_chi_value), vmax=max(M_chi_value)))
    sm._A = []
    


    
    ax.spines['top'].set_linewidth(2.5)
    ax.spines['left'].set_linewidth(2.5)
    ax.spines['right'].set_linewidth(2.5)
    ax.spines['bottom'].set_linewidth(2.5) 
    ax.tick_params(axis='both', width=2.5)      
    
    from mpl_toolkits.axes_grid1 import make_axes_locatable
    divider = make_axes_locatable(pp.gca())
    cax = divider.append_axes("right", "5%", pad="3%")
    pp.colorbar(sm, cax=cax).set_label('$M_{\chi}$',fontsize=axis_size) 
    cax.tick_params(labelsize=label_size) 
    
    #pp.xlim(x_min,x_max)    
    #pp.ylim(y_max,y_min) 

    pp.tight_layout()
        
    pp.show()