예제 #1
0
def plot_features_train_val(paradigm, models, train, val):
    """
    plot RF scores as function of the given key name
    :param paradigm: the current paradigm name that is being tested (for the title)
    :param models: all the models. list of tuples where the second index of the tuple is the model's name
    :param train: list of train scores (same order as the models)
    :param val: list of validation scores (same order as the models)
    """
    title = "%s_train_validation_results" % paradigm
    models_names = [model[1] for model in models]  # gets the models name
    # gets the data frame for the current plot
    train = special_df(models_names, train, "train", FEATURES_NUMS, "features")
    if len(train) <= 1:
        # no data to plot (no 2 different values for x axis)
        return
    test = special_df(models_names, val, "val", FEATURES_NUMS, "features")

    fig = plt.figure()
    ax = fig.add_subplot(111)
    # set the plot's colors so it will have different color for each model
    ax.set_prop_cycle(color=COLORS[:test.shape[1]])

    test.plot(ax=ax)
    train.plot(ax=ax, style="--")
    set_plt_params(ax, title.replace("_", " "), "Number of features",
                   "Accuracy")

    # saves figure legend as a different file
    fig_legend = pylab.figure(figsize=(4, 10))
    pylab.figlegend(*ax.get_legend_handles_labels(), loc='center')
    fig_legend.savefig(path.join(FIGS_DIR, title + '_legend.png'))
    ax.get_legend().remove()

    fig.savefig(path.join(FIGS_DIR, title + '.png'))
    plt.close()  # close the figure
예제 #2
0
    def piechart(self, domain_type):
        pie_file = self.output[0]
        pylab.figure(figsize=(5, 5))

        probs, labels = ([], [])
        for type in DOMAIN_TYPES:
            mean_coverage = mean(
                domain_type[type]) if len(domain_type[type]) > 0 else 0.0
            debug(self.family_name, type, mean_coverage)
            probs.append(mean_coverage)
            labels.append(type)  #+":%0.2f" % mean_coverage)

        #ax = pylab.axes([0.6, 0.6, 0.4, 0.4])
        explode = [0.05 for i in xrange(len(DOMAIN_TYPES))]
        patches, texts = pylab.pie(probs,
                                   explode=None,
                                   labels=None,
                                   shadow=False,
                                   colors=DOMAIN_COLORS)
        pylab.figlegend(patches,
                        labels,
                        "lower left",
                        fancybox=True,
                        markerscale=0.2)
        pylab.title(self.family_name)
        pylab.savefig(pie_file)
예제 #3
0
def plot(x, y, yci, methods, title, xlabel, ylabel, filename):
    fig = pylab.figure()

    ax = pylab.gca()
    for method in methods:
        print method, y(method), yci(method)
        lines = ax.errorbar(x(method),
                            y(method),
                            yci(method),
                            fmt=markers[method],
                            label=legends[method],
                            mfc='none',
                            markersize=15,
                            capsize=5)

    # easiest way to plot brute force
    """
  method = 'brute'
  lines = ax.errorbar(range(1,4), [0,] * 3, [0,] * 3, fmt=markers[method], label=legends[method], mfc='none', markersize=15, capsize=5)
  """

    ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

    pylab.title(title)
    pylab.xlabel(xlabel)
    pylab.ylabel(ylabel)
    #pylab.ylim([0, 0.2])
    pylab.gcf().subplots_adjust(bottom=0.2, left=0.2)
    fig.savefig(filename + ".pdf", dpi=300, format="pdf")

    figLegend = pylab.figure(figsize=(6, 4))
    pylab.figlegend(*ax.get_legend_handles_labels(), loc='upper left')
    figLegend.savefig("legend.pdf", dpi=300, format="pdf")

    pylab.close()
예제 #4
0
def plot(x, y, yci, methods, title, xlabel, ylabel, filename):
  fig = pylab.figure()

  ax = pylab.gca()
  for method in methods:
    print method, y(method), yci(method)
    lines = ax.errorbar(x(method), y(method), yci(method), fmt=markers[method], label=legends[method], mfc='none', markersize=15, capsize=5)
  
  # easiest way to plot brute force
  """
  method = 'brute'
  lines = ax.errorbar(range(1,4), [0,] * 3, [0,] * 3, fmt=markers[method], label=legends[method], mfc='none', markersize=15, capsize=5)
  """

  ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

  pylab.title(title)
  pylab.xlabel(xlabel)
  pylab.ylabel(ylabel)
  #pylab.ylim([0, 0.2])
  pylab.gcf().subplots_adjust(bottom=0.2, left=0.2)
  fig.savefig(filename + ".pdf", dpi=300, format="pdf")
  
  figLegend = pylab.figure(figsize = (6, 4))
  pylab.figlegend(*ax.get_legend_handles_labels(), loc = 'upper left')
  figLegend.savefig("legend.pdf", dpi=300, format="pdf")
  
  pylab.close()
예제 #5
0
def findBasicLineData(data, coordPairs, bPlot = True) :
    xdata =pylab.arange(0,len(data),1)
    
    basicData = []
    for lineRange in coordPairs :
        #print 'findBasicLineData> lineRange', lineRange
        xmean  = (data[lineRange[0]:lineRange[1]]*xdata[lineRange[0]:lineRange[1]]).sum()/(data[lineRange[0]:lineRange[1]]).sum()
        ymax   = data[lineRange[0]:lineRange[1]].min()
        #amplitude set as the minimum value between the defined coords
        xwidth = lineRange[1]-lineRange[0]
        
        basicData.append([xmean,ymax,xwidth])

   
    if bPlot:
        pylab.figure(20)
        pylab.clf()
        pylab.plot(data)
        i=1
        pylab.xlabel('Pixels')
        pylab.ylabel('no. Photoelectrons')
        pylab.title('Guess Parameters')
        for d in basicData :
            a=pylab.axvline(d[0],color='r',ls='--')
            pylab.text(d[0],data.max(),i,fontsize=10)
            b=pylab.axhline(d[1],color='k',ls='--')
            i+=1
        pylab.figlegend((a,b),('$\mu$','Amplitude'),'best',prop={'size':10})
    return pylab.array(basicData)
  def plot(self,yl='',yr='',x='s',idx=slice(None),clist='k r b g c m',lattice=True,newfig=True):
    self._clist=clist.split()
    if newfig:
      f=_p.figure()
    else:
      f=_p.gcf()
    sp=_p.subplot(111)
    _p.subplots_adjust(right=0.72)
    _p.setp( sp.yaxis, visible=False)
    xd=getattr(self,x)[idx]
    out=qdplot(figure=f,subplot=sp,plots=[],lines=[],legends=[],xaxis=xd)
    if lattice:
      self._lattice(out,idx,['kn0l','angle'],"#a0ffa0",'Bend h')
      self._lattice(out,idx,['ks0l'],"#ffa0a0",'Bend v')
      self._lattice(out,idx,['kn1l','k1l'],"#a0a0ff",'Quad')
      self._lattice(out,idx,['hkick'],"#e0a0e0",'Kick h')
      self._lattice(out,idx,['vkick'],"#a0e0e0",'Kick v')
    for i in yl.split():
      self._column(out,idx,i,'left')
    for i in yr.split():
      self._column(out,idx,i,'right')

    _p.xlabel(_mylbl(axlabel,x))

    _p.xlim(min(xd),max(xd))
    _p.figlegend(out.lines,out.legends,'upper right')
    _p.grid()
    _p.draw()
    del self._clist
    if hasattr(self,'_colAxleft'): delattr(self,'_colAxleft')
    if hasattr(self,'_colAxright'): delattr(self,'_colAxright')
    self.currplot=out
    return out
예제 #7
0
  def update(self):
    if callable(self.pre):
      self.pre()
    _p.ioff()
    self.lines=[]
    self.legends=[]
#    self.figure.lines=[]
#    self.figure.patches=[]
#    self.figure.texts=[]
#    self.figure.images = []
    self.figure.legends = []

    if self.lattice:
      self.lattice.patches=[]
      self._lattice(['k0l','kn0l','angle'],"#a0ffa0",'Bend h')
      self._lattice(['ks0l'],"#ffa0a0",'Bend v')
      self._lattice(['kn1l','k1l'],"#a0a0ff",'Quad')
      self._lattice(['hkick'],"#e0a0e0",'Kick h')
      self._lattice(['vkick'],"#a0e0e0",'Kick v')
    if self.left:
      self.left.lines=[]
      for i in self.yl:
        self._column(i,self.left,self.color[i])
    if self.right:
      self.right.lines=[]
      for i in self.yr:
        self._column(i,self.right,self.color[i])
    _p.xlabel(_mylbl(axlabel,self.x))
    self.figure.gca().set_xlim(min(self.xaxis[self.idx]),max(self.xaxis[self.idx]))
    _p.figlegend(self.lines,self.legends,'upper right')
    _p.grid(True)
#    self.figure.canvas.mpl_connect('button_release_event',self.button_press)
#    self.figure.canvas.mpl_connect('pick_event',self.pick)
    _p.ion()
    _p.draw()
예제 #8
0
def plot_all_shapelets(result):
    f, rows = plt.subplots(5, 2, sharex=True)
    axs = list(rows[:, 0]) + list(rows[:, 1])
    lines = dict()
    classifiers = dict()
    for i, (label, (classifier, _)) in enumerate(result.items()):
        classifiers[label] = classifier
    order = [("wipe", "wipe_start"), ("force_inc", "force_inc"),
             ("slide", "slide_left_start"), ("slide_r", "slide_right_start"),
             ("push", "movable_box"), ("wipe_end", "wipe_end"),
             ("force_dec", "force_dec"), ("slide_end", "slide_left_end"),
             ("slide_r_end", "slide_right_end"), ("screw", "fixed_screw")]
    j = 0
    for i, (label, title) in enumerate(order):
        if classifiers.has_key(label):
            classifier = classifiers[label]
            axs[j].set_title(title)
            lines.update(
                plot_shapelet(axs[j], classifier.shapelet, classifier.dim_s))
            axs[j].set_xlim(0, 50)
            plt.setp(axs[j].get_yticklabels(), visible=False)
            j += 1
    classifiers = sorted(lines.items(), key=lambda x: x[0])
    plt.figlegend([x[1] for x in classifiers], [x[0] for x in classifiers],
                  loc='lower center',
                  ncol=5,
                  labelspacing=0.)
    plt.subplots_adjust(left=.05,
                        bottom=.15,
                        right=.95,
                        top=.93,
                        wspace=.12,
                        hspace=.51)
    plt.show()
예제 #9
0
def plot_train_test(general_results, general_train, mode):
    for feature_set in FEATURES_FILES:
        name = feature_set[0][:-4]
        title = "%strain_test_results_%s" % (mode, name)
        test = prepare_train_test_df(general_results, name, "test")
        train = prepare_train_test_df(general_train, name, "train")

        fig = plt.figure()
        ax = fig.add_subplot(111)
        # setting the amount of colors in the plot to be the amount of models
        # cm = plt.rcParams['axes.prop_cycle'].by_key()['color']
        # cm = list(mcolors.XKCD_COLORS.values())
        # ax.set_color_cycle(cm[::len(cm) // (test.shape[1] - 1)])
        ax.set_color_cycle(COLORS[:test.shape[1]])

        test.plot(ax=ax)
        train.plot(ax=ax, style="--")
        set_plt_params(ax,
                       title.replace("_", " "),
                       "Number of features",
                       "Accuracy score",
                       width=WIDE_WIN)

        if DEBUG:
            plt.show()
        else:
            # saves figure legend as a different file
            fig_legend = pylab.figure(figsize=(4, 10))
            pylab.figlegend(*ax.get_legend_handles_labels(), loc='center')
            fig_legend.savefig(
                path.join(OUTPUT_PATH, FIGS_DIR, title + '_legend.png'))
            ax.get_legend().remove()

            fig.savefig(path.join(OUTPUT_PATH, FIGS_DIR, title + '.png'))
            plt.close()  # close the figure
예제 #10
0
def plot_results(t_hr, I_mA, avg_t_hr, lifetime_hr, lifetime_error = None,
                 show_plot = False):
    """"""
    
    # Set show_plot to True to stop the code here for examining the plot
    if show_plot:
        fig = plt.figure()
            
        ax1 = fig.add_subplot(111)
        h1 = ax1.plot(t_hr, I_mA, '.-', label='Data', linewidth=0.5)
        ax1.set_xlabel('Time [hr]')
        ax1.set_ylabel('Beam Current [mA]')
            
        ax2 = ax1.twinx()
        
        if lifetime_error is not None :
            h2 = ax2.errorbar(avg_t_hr, lifetime_hr,
                              yerr = lifetime_error,
                              fmt = '-', ecolor = 'r',
                              color = 'r', linestyle = '-', marker = '.', 
                              label = 'Lifetime')
        else :
            h2 = ax2.plot(avg_t_hr, lifetime_hr,
                          color = 'r', linestyle = '-', marker = '.',
                          label = 'Lifetime')
            
        ax2.set_ylabel('Lifetime [hr]')
            
        plt.figlegend( (h1[0], h2[0]), 
                       (h1[0].get_label(), h2[0].get_label()), 
                       loc='upper right')

        plt.show()  
예제 #11
0
def plotMatches(imgsources, refsources, matches, wcs, W, H, prefix,
                saveplot=True, format='png'):
    plt.clf()

    # Image sources
    ix = np.array([s.getXAstrom() for s in imgsources])
    iy = np.array([s.getYAstrom() for s in imgsources])
    iflux = np.array([s.getPsfFlux() for s in imgsources])
    I = np.argsort(-iflux)
    # First 200: red dots
    II = I[:200]
    p1 = plt.plot(ix[II], iy[II], 'r.', zorder=10)
    # Rest: tiny dots
    II = I[200:]
    p2 = plt.plot(ix[II], iy[II], 'r.', markersize=1, zorder=9)

    # Ref sources:
    # Only getRa() (not getRaAstrom(), getRaObject()) is non-zero.

    rx,ry = [],[]
    for r in refsources:
        xy = wcs.skyToPixel(r.getRaDec())
        rx.append(xy[0])
        ry.append(xy[1])
    rx = np.array(rx)
    ry = np.array(ry)
    p3 = plt.plot(rx, ry, 'bo', mec='b', mfc='none', markersize=6, zorder=20)

    x,y = [],[]
    dx,dy = [],[]
    for m in matches:
        x0,x1 = m.first.getXAstrom(), m.second.getXAstrom()
        y0,y1 = m.first.getYAstrom(), m.second.getYAstrom()
        #plt.plot([x0, x1], [y0, y1], 'g.-')
        x.append(x0)
        y.append(y0)
        dx.append(x1-x0)
        dy.append(y1-y0)
    #plt.plot(x, y, 's', mec='g', mfc='none', markersize=5)
    p4 = plt.plot(x, y, 'o', mec='g', mfc='g', alpha=0.5, markersize=8, zorder=5)
    p5 = plt.quiver(x, y, dx, dy, angles='xy', scale=30., zorder=30)
    plt.axis('scaled')
    plt.axis([0, W, 0, H])
    #print p1, p2, p3, p4, p5

    plt.figlegend((p1, p2, p3, p4), #, p5),
              ('Image sources (brightest 200)',
               'Image sources (rest)',
               'Reference sources',
               'Matches',),
              'center right',
              numpoints=1,
              prop=FontProperties(size='small'))

    fn = prefix + '-matches.' + format
    return _output(fn, format, saveplot)
예제 #12
0
    def plot_circles(optimal_choices, labels, num_col, title, dir_path, file_name, color_set,
                     b_nach=(0.5, -0.05), legend_loc=9, print_title=True, print_legend=True):

        legend_labels = []
        legend_markers = []

        for i in range(0, len(labels)):
            legend_labels.append(labels[i])
            legend_markers.append(patches.Circle((0.5, 0.5), 0.25, facecolor=color_set[i],
                                                 edgecolor=color_set[i], linewidth=1))

        fig = plt.figure()
        ax = fig.add_subplot(111, aspect='equal')

        if print_title is True:
            # LaTeX rendering case. You may use the next line if you have LaTeX installed.
            # plt.title(r'Classification Model Recommendation vs.\ \textsc{' + title.title() + '} Data Stream Over Time'
            #          + '\n' + r'(\textit{from top-left corner to bottom-right corner})', fontsize=8, loc='center')
            plt.title('Classification Model Recommendation vs. ' + title.title() + ' Data Stream Over Time'
                      + '\n' + 'from top-left corner to bottom-right corner', fontsize=8, loc='center')

        ax.set_xlabel(r'$\rightarrow$')
        ax.set_ylabel(r'$\downarrow$')

        ax.set_xticklabels([])
        ax.set_yticklabels([])

        x = 0.0125
        y = 0.9875
        l = 0.0125
        for i in range(0, len(optimal_choices)):
            ax.add_patch(patches.Circle((x, y), 0.011, color=color_set[optimal_choices[i][0]]))
            if x > 1 - 2 * l:
                x = 0.0125
                y -= 2 * l
                y = round(y, 4)
            else:
                x += 2 * l
                x = round(x, 4)

        file_path = (dir_path + file_name + "_" + title).lower()

        if print_legend is True:
            ax.legend(legend_markers, legend_labels, bbox_to_anchor=b_nach, loc=legend_loc,
                      fontsize=8, ncol=num_col, frameon=True, framealpha=1,
                      handler_map={patches.Circle: HandlerCircle()})
            fig.savefig(file_path + '_circle.png', dpi=150, bbox_inches='tight')
            fig.savefig(file_path + '_circle.pdf', dpi=150, bbox_inches='tight')
        else:
            fig_leg = pylab.figure(figsize=(13.5, 3.5), dpi=150)
            pylab.figlegend(legend_markers, legend_labels, loc='center', fontsize=14, ncol=num_col, framealpha=1,
                            handler_map={patches.Circle: HandlerCircle()})
            fig_leg.savefig(file_path + "_circle_legend.pdf", dpi=150)
            fig.savefig(file_path + "_circle.pdf", dpi=150, bbox_inches='tight')
            fig.savefig(file_path + "_circle.png", dpi=150, bbox_inches='tight')
예제 #13
0
def plot(cluster, filename="plot.png", func=lambda a: a.id, 
         plot_title='', cmap='Paired', filter=lambda a: True, 
         draw_legend=False, radius='2.25', sym=None):
    ac = rc.load(cluster)
    clusters = rc.load_clusters(cluster)

    p = get_population()
    pops = [0 for i in range(30000)]
    cluster_pops = [] 
    cluster_pop_max = []
    for clust in range(len(clusters)):
        cluster_pops.append(pops[:])
        cluster_pop_max.append([0,0,-1,0])

    for clust,agents in enumerate(clusters):
        for agent in agents:
            a = Agent(agent)
            for i in range(a.birth, a.death):
                cluster_pops[clust][i] += 1
                if cluster_pop_max[clust][2] == -1:
                    cluster_pop_max[clust][2] = i
                if i > cluster_pop_max[clust][3]:
                    cluster_pop_max[clust][3] = i
                if cluster_pops[clust][i] > cluster_pop_max[clust][0]:
                    cluster_pop_max[clust][0] = cluster_pops[clust][i]
                    cluster_pop_max[clust][1] = i
    
    lines=[]
    for i,clust in enumerate(cluster_pops):
        lines.append(pylab.plot(range(30000),clust, 
                         label=("%d: k=%d" % (i, len(clusters[i]))),
                         color=pylab.cm.Paired(float(i)/len(clusters))))

    if draw_legend:
        pylab.figlegend(lines, ["%d: k=%d" % (i, len(clust))
                                    for i,clust in enumerate(clusters)], 
                        'center right', 
                        ncol=((len(clusters)/35)+1), prop=dict(size=6))
    else:
        print "should not draw!!!"

    title = r"Cluster Population ($\epsilon$ = %s, %d clusters)" % (radius, len(clusters))
    pylab.title(title, weight='black')
    pylab.xlabel("Time", weight='bold')
    pylab.ylabel("Population Size", weight='bold')
    if sym is not None:
        pylab.figtext(0,.954, '(%s)' % sym, size=6, weight='black')

    pylab.savefig(filename, dpi=300)

    print 'cluster, totalPop, start, peak, stop, maxPop'
    for clust,agents in enumerate(clusters):
        print clust, len(agents), cluster_pop_max[clust][2], cluster_pop_max[clust][1], cluster_pop_max[clust][3]+1, cluster_pop_max[clust][0]
예제 #14
0
def figure2 ( ):
    w,h = 20,7
    fig = pl.figure ( figsize=(fullwidth,h*fullwidth/w) )

    a,b = place_axes ( fig, 1.1,1, [7,7,5], [6,6,0], [True,False],
            [.5,.5,1], (w,h), twiny=True )
    kl = convenience.kernelplot ( data, results, plotinfo, a, b, None )
    textfile.write ( "Figure 2\n" )
    M = results['model_w_hist']
    hr = data.gethistorykernel ( M.w[data.hf0:data.hf0+data.hlen], M.w[1] )
    hz = data.gethistorykernel ( M.w[data.hf0+data.hlen:],      M.w[1] )
    bootstrap = results['bootstrap']
    kernellen = (bootstrap.shape[1]-1)/2
    ci_stim,ci_resp,ci_corr,ci_inco = statistics.history_kernel_ci (
            bootstrap[:,kernellen:-2], bootstrap[:,:kernellen],
            hz, hr )

    textfile.write ( "  stim[-1] = %g in (%g,%g)\n" % (hz[0], ci_stim[0,0], ci_stim[1,0]) )
    textfile.write ( "  resp[-1] = %g in (%g,%g)\n" % (hr[0], ci_resp[0,0], ci_resp[1,0]) )
    textfile.write ( "  correct[-1] = %g in (%g,%g)\n" % (hz[0]+hr[0],   ci_corr[0,0], ci_corr[1,0]) )
    textfile.write ( "  incorrect[-1] = %g in (%g,%g)\n" % (-hz[0]+hr[0], ci_inco[0,0], ci_inco[1,0]) )

    a.text ( .05, laby, r"{\bf a}", transform=a.transAxes )
    b.text ( .05, laby, r"{\bf b}", transform=b.transAxes )
    pl.setp ( (a,b), title="", xlabel="Lag" )
    pl.setp ( b, yticks=(), ylabel="" )
    pl.setp ( a, ylabel="weight" )
    a.xaxis.set_major_formatter ( myformatter )
    b.xaxis.set_major_formatter ( myformatter )
    a.yaxis.set_major_formatter ( myformatter )

    handles = []
    annotations = []
    for l in kl:
        handles.append ( l )
        annotations.append ( l.get_label() )
    assert len(handles)==4
    pl.figlegend ( handles, annotations,
            'lower left', bbox_to_anchor=(15./w,2./h),
            numpoints=1,
            title='Weights of\nprevious...')

    # mx = abs ( pl.array([ci_inco, ci_stim, ci_resp, ci_corr]) ).max( )
    # mx = 0.5*10**pl.ceil(pl.log10(2*mx))
    # pl.setp ( (a,b), ylim = (-mx,mx) )
    a.yaxis.set_major_locator ( tckr ( density=0.5, figure=fig, which=1 ) )
    b.yaxis.set_visible ( False )

    if observer in ['sim_KP','sim_KP_nh']:
        pl.setp ( (a,b), ylim = (-1,1) )

    pl.savefig ( "figures/%s2.pdf" % (figname,) )
    pl.savefig ( "figures/%s2.eps" % (figname,) )
예제 #15
0
def plot(cluster_file,
         filename="plot.png",
         func=lambda a: a.id,
         plot_title='',
         cmap='Paired',
         filter=lambda a: True,
         draw_legend=False,
         radius='2.25',
         sym=None,
         run_dir='../run/'):
    """
    Creates a line plot showing population per cluster
    """

    # retrieve cluster data from cluster file
    clusters = rc.load_clusters(cluster_file)

    # grab cluster population
    p = get_population(run_dir=run_dir)

    lines = []
    for cluster, agents in enumerate(clusters):
        pop_by_time = list(repeat(0, 30000))
        for agent in agents:
            a = Agent(agent)
            for i in range(a.birth, a.death):
                pop_by_time[i] += 1

        lines.append(
            pylab.plot(range(30000),
                       pop_by_time,
                       label=("%d: k=%d" % (i, len(clusters[cluster]))),
                       color=pylab.cm.Paired(float(cluster) / len(clusters))))

    if draw_legend:
        pylab.figlegend(lines, [
            "%d: k=%d" % (i, len(cluster))
            for i, cluster in enumerate(clusters)
        ],
                        'center right',
                        ncol=((len(clusters) / 35) + 1),
                        prop=dict(size=6))

    title = r"Cluster Population ($\epsilon$ = %s, %d clusters)" % (
        radius, len(clusters))
    pylab.title(title, weight='black')
    pylab.xlabel("Time", weight='bold')
    pylab.ylabel("Population Size", weight='bold')
    if sym is not None:
        pylab.figtext(0, .954, '(%s)' % sym, size=6, weight='black')

    pylab.savefig(filename, dpi=300)
예제 #16
0
def plot_graph(processes, series_filter, save_file_path=None):
    """
    Create a graph.  Either display the graph or save the
    graph. This function will only create ONE graph.

    processes -- a list of Process instance.
    save_file_path -- If not None, save the graph to the file path
                      specified by this parameter (a string).
    """
    g.figure(figsize=(12, 7))

    handles = []
    labels = []
    sizer = Sizer(len(series_filter.series))

    for p in processes:
        time_series = make_time_series(p.time_series)
        colour = COLOURS.next()
        print p.id, colour

        for count, series in enumerate(series_filter.series):
            ax = g.axes(sizer.coordinates(count))
            try:
                h = g.plot(time_series,
                           p.get(series.name),
                           series.marker,
                           color=colour,
                           label=p.id)
            except:
                print "Error in generating graph for %s of %s!" \
                        % (p.id, series.name)
                raise

            g.ylabel("%s %s" % (series.name, series.unit))
            g.grid(True)
            g.setp(ax.get_xticklabels(), visible=False)

        handles.append(h)
        labels.append(p.id)

    g.setp(ax.get_xticklabels(), visible=True)
    g.xlabel("Elapsed Time (min)")
    legend = g.figlegend(handles,
                         labels,
                         'lower right',
                         pad=0.1,
                         labelsep=0.0025,
                         handlelen=0.025,
                         handletextsep=0.01,
                         axespad=0.08)

    for t in legend.get_texts():
        t.set_fontsize(8)

    if save_file_path != None:
        g.savefig(save_file_path, orientation='landscape')
        g.close("all")
    else:
        g.show()
예제 #17
0
 def piechart(self, domain_type):
     pie_file = self.output[0]
     pylab.figure(figsize=(5,5))
     
     probs,labels = ([],[])
     for type in DOMAIN_TYPES:
         mean_coverage = mean(domain_type[type]) if len(domain_type[type])>0 else 0.0
         debug(self.family_name,type,mean_coverage)
         probs.append(mean_coverage)
         labels.append(type)#+":%0.2f" % mean_coverage)
     
     #ax = pylab.axes([0.6, 0.6, 0.4, 0.4])
     explode = [0.05 for i in xrange(len(DOMAIN_TYPES))]
     patches, texts = pylab.pie(probs,explode=None,labels=None,shadow=False,colors=DOMAIN_COLORS)
     pylab.figlegend(patches, labels, "lower left", fancybox=True, markerscale=0.2)
     pylab.title(self.family_name)
     pylab.savefig(pie_file)
예제 #18
0
파일: summarise.py 프로젝트: JohnReid/infpy
def plot_categories(categories, category_names=None, distribution_names=None, numrows=None, numcols=None):
    "Plot the categories as sub-plots."

    import pylab as P
    assert 2 == len(categories.shape)

    # determine number of rows and columns
    num_dists = categories.shape[0]
    num_categories = categories.shape[1]
    if not numrows and not numcols:
        numrows = int(numpy.sqrt(num_categories))
    if not numrows:
        numrows = num_categories / numcols
        if num_categories % numcols:
            numrows += 1
    elif not numcols:
        numcols = num_categories / numrows
        if num_categories % numrows:
            numcols += 1

    # plot each category
    max, min = categories.max(), categories.min()
    ind = numpy.arange(num_dists)    # the x locations for the groups
    # the width of the bars: can also be len(x) sequence
    width = 1.
    for c in range(num_categories):
        # overlaps, subplot(111) is killed
        splt = P.subplot(numrows, numcols, c + 1)
        handles = [
            P.bar((i,), (x,), width, color=color)
            for color, i, x
            in zip(pylab_utils.simple_colours, ind, categories[:, c])
        ]
        splt.set_ylim(min, max)
        if category_names:
            P.title(category_names[c])
        if c % numcols:
            splt.set_yticklabels(())
        if distribution_names:
            P.xticks(ind + width / 2., distribution_names)
        else:
            splt.set_xticklabels(())
        splt.set_xticks(tuple())
    if distribution_names:
        P.figlegend(handles, distribution_names, 'upper right')
예제 #19
0
    def plot(self,
             yl='',
             yr='',
             x='s',
             idx=slice(None),
             clist='k r b g c m',
             lattice=True,
             newfig=True):
        self._clist = clist.split()
        if newfig:
            f = _p.figure()
        else:
            f = _p.gcf()
        sp = _p.subplot(111)
        _p.subplots_adjust(right=0.72)
        _p.setp(sp.yaxis, visible=False)
        xd = getattr(self, x)[idx]
        out = qdplot(figure=f,
                     subplot=sp,
                     plots=[],
                     lines=[],
                     legends=[],
                     xaxis=xd)
        if lattice:
            self._lattice(out, idx, ['kn0l', 'angle'], "#a0ffa0", 'Bend h')
            self._lattice(out, idx, ['ks0l'], "#ffa0a0", 'Bend v')
            self._lattice(out, idx, ['kn1l', 'k1l'], "#a0a0ff", 'Quad')
            self._lattice(out, idx, ['hkick'], "#e0a0e0", 'Kick h')
            self._lattice(out, idx, ['vkick'], "#a0e0e0", 'Kick v')
        for i in yl.split():
            self._column(out, idx, i, 'left')
        for i in yr.split():
            self._column(out, idx, i, 'right')

        _p.xlabel(_mylbl(axlabel, x))

        _p.xlim(min(xd), max(xd))
        _p.figlegend(out.lines, out.legends, 'upper right')
        _p.grid()
        _p.draw()
        del self._clist
        if hasattr(self, '_colAxleft'): delattr(self, '_colAxleft')
        if hasattr(self, '_colAxright'): delattr(self, '_colAxright')
        self.currplot = out
        return out
예제 #20
0
def draw_stacked_bar_w_err_bar(c, means_d, stderr_d, category_labels, outpfile, f_type):
    N = len(category_labels)
    # Get colors from ColorBrewer (all colorbrewer scales: http://bl.ocks.org/mbostock/5577023)
    custom_color_list = list()
    custom_color_list.extend(brewer2mpl.get_map('Paired', 'qualitative', 10).mpl_colors)
    custom_color_list.extend(brewer2mpl.get_map('Set2', 'qualitative', 6).mpl_colors)
    custom_color_list.extend(brewer2mpl.get_map('Set1', 'qualitative', 7).mpl_colors)
    custom_color_list.extend(brewer2mpl.get_map('Set3', 'qualitative', 10).mpl_colors)
    
    # Set the random seed for consistency
    numpy.random.seed(12)
    ind = numpy.arange(N)    # the x locations for the groups
    width = 0.35       # the width of the bars
    
    bottom_value = [0 for m in category_labels]
    legend_bar = list()
    legend_text = list()
    counter = 0
    #taxons_sorted_list = sorted(means_d) # this sorts the taxons (keys of the means_d dictionary) and saves it as a list

    # print in bar graph (bottom to top), the taxons in decreasing order of average abundance across groups.
    #http://www.quora.com/How-do-I-sum-the-values-of-each-key-in-a-Python-dictionary-and-then-display-the-result-as-key-summed-value
    d_total_list_values = dict((k, sum([float(i) for i in v])/float(len(v))) for k,v in means_d.items()) # convert values in list to float, avg them, assign total as value to key
    taxons_sorted_list = sorted(d_total_list_values, key=d_total_list_values.get, reverse=True)# this sorts the taxons (keys) as per values and saves it as a list
    #print d_total_list_values, taxons_sorted_list
    for t in taxons_sorted_list:
        if counter == len(custom_color_list):  # cycle through the list of available colors if we have more taxons
            counter = 0
        mean = [float(i) for i in means_d[t]]
        stderr = [float(i) for i in stderr_d[t]]
        p = plt.bar(ind, mean, width, color=custom_color_list[counter], yerr=stderr, bottom=bottom_value)
        bottom_value = [x+y for x,y in zip(bottom_value, mean)]
        legend_bar.append(p[0])
        legend_text.append(t)
        counter += 1
    plt.ylabel('Cumulative relative abundance')
    plt.xlabel(c)
    plt.title('Taxonomic summary')
    plt.xticks(ind+width/2., tuple(category_labels) )
    #plt.legend( tuple(legend_bar), tuple(legend_text) )
    figname = outpfile + '_' + c + '.' + f_type
    plt.savefig(figname, close=True)

    # Klaus se at stackexchange
    #http://stackoverflow.com/questions/4534480/get-legend-as-a-seperate-picture-in-matplotlib
    legend_fig = pylab.figure()
    # print in legend (top to bottom), the taxons in decreasing order of average abundance across groups.
    #legend = pylab.figlegend( tuple(legend_bar), tuple(legend_text), loc = 'center' )
    #print in legend (bottom to top), the taxons in decreasing order of average abundance across groups.
    rev_legend_bar = list(reversed(legend_bar))
    rev_legend_text = list(reversed(legend_text))
    legend = pylab.figlegend( tuple(rev_legend_bar), tuple(rev_legend_text), loc = 'center' )
    legend_fig.canvas.draw()
    legend_figname = 'legend_' + figname 
    legend_fig.savefig(legend_figname, bbox_inches=legend.get_window_extent().transformed(legend_fig.dpi_scale_trans.inverted()))
    pylab.close()
예제 #21
0
def plot_results(t_hr,
                 I_mA,
                 avg_t_hr,
                 lifetime_hr,
                 lifetime_error=None,
                 show_plot=False):
    """"""

    # Set show_plot to True to stop the code here for examining the plot
    if show_plot:
        fig = plt.figure()

        ax1 = fig.add_subplot(111)
        h1 = ax1.plot(t_hr, I_mA, '.-', label='Data', linewidth=0.5)
        ax1.set_xlabel('Time [hr]')
        ax1.set_ylabel('Beam Current [mA]')

        ax2 = ax1.twinx()

        if lifetime_error is not None:
            h2 = ax2.errorbar(avg_t_hr,
                              lifetime_hr,
                              yerr=lifetime_error,
                              fmt='-',
                              ecolor='r',
                              color='r',
                              linestyle='-',
                              marker='.',
                              label='Lifetime')
        else:
            h2 = ax2.plot(avg_t_hr,
                          lifetime_hr,
                          color='r',
                          linestyle='-',
                          marker='.',
                          label='Lifetime')

        ax2.set_ylabel('Lifetime [hr]')

        plt.figlegend((h1[0], h2[0]), (h1[0].get_label(), h2[0].get_label()),
                      loc='upper right')

        plt.show()
    def plot_multiple(pairs_names, num_instances, performances_array, y_title,
                      project_name, dir_path, file_name, y_lim, b_anch, legend_loc, col_num, zip_size,
                      color_set, z_orders, print_legend=True):

        x = []
        y = []
        for i in range(0, len(pairs_names)):
            y.append([])
        for i in range(0, num_instances):
            if i % zip_size == 0 or i == num_instances - 1:
                x.append((i / num_instances) * 100)
                for j in range(0, len(pairs_names)):
                    y[j].append(performances_array[j][i])

        fig = plt.figure()

        ax = fig.add_subplot(111)

        # LaTeX rendering case. You may use the next line if you have LaTeX installed.
        # ax.set_title(r'\textsc{' + project_name.title() + '}', fontsize=14)
        ax.set_title(project_name.title(), fontsize=14)

        ax.set_xlim(0, 100)
        if y_lim is not None:
            ax.set_ylim(y_lim[0], y_lim[1])
        ax.set_ylabel(y_title, fontsize=14)
        ax.set_xlabel("Percentage of Instances", fontsize=14)
        ax.grid()

        for i in range(0, len(pairs_names)):
            ax.plot(x, y[i], label=pairs_names[i], color=color_set[i], linewidth=1.2, zorder=z_orders[i])

        # LaTeX rendering case. You may use the next line if you have LaTeX installed.
        # ax.xaxis.set_major_formatter(FuncFormatter(lambda ix, _: '%1.0f' % ix + '\%'))
        ax.xaxis.set_major_formatter(FuncFormatter(lambda ix, _: '%1.0f' % ix + '%'))

        file_path = (dir_path + file_name + "_" + y_title).lower()

        if print_legend is True:
            leg = ax.legend(bbox_to_anchor=b_anch, loc=legend_loc, ncol=col_num, fontsize=8, framealpha=1)
            for leg_obj in leg.legendHandles:
                leg_obj.set_linewidth(1)
            fig.savefig(file_path + ".pdf", dpi=150, bbox_inches='tight')
            fig.savefig(file_path + ".png", dpi=150, bbox_inches='tight')
        else:
            fig_leg = pylab.figure(figsize=(13.5, 3.5), dpi=150)
            leg = pylab.figlegend(*ax.get_legend_handles_labels(), loc='center', ncol=col_num, fontsize=14
                                  , framealpha=1)
            for leg_obj in leg.legendHandles:
                leg_obj.set_linewidth(3.0)
            fig_leg.savefig(file_path + "_legend.pdf", dpi=150)
            fig_leg.savefig(file_path + "_legend.png", dpi=150)
            fig.savefig(file_path + ".pdf", dpi=150, bbox_inches='tight')
            fig.savefig(file_path + ".png", dpi=150, bbox_inches='tight')
예제 #23
0
def plot(cluster_file, filename="plot.png", func=lambda a: a.id, 
         plot_title='', cmap='Paired', filter=lambda a: True, 
         draw_legend=False, radius='2.25', sym=None, run_dir='../run/'):
    """
    Creates a line plot showing population per cluster
    """

    # retrieve cluster data from cluster file
    clusters = rc.load_clusters(cluster_file)

    # grab cluster population
    p = get_population(run_dir=run_dir)

    lines=[]
    for cluster, agents in enumerate(clusters):
        pop_by_time = list(repeat(0, 30000))
        for agent in agents:
            a = Agent(agent)
            for i in range(a.birth, a.death):
                pop_by_time[i] += 1
    
        lines.append(
            pylab.plot(range(30000), pop_by_time, 
                       label=("%d: k=%d" % (i, len(clusters[cluster]))),
                       color=pylab.cm.Paired(float(cluster)/len(clusters))))

    if draw_legend:
        pylab.figlegend(lines, ["%d: k=%d" % (i, len(cluster))
                                    for i,cluster in enumerate(clusters)], 
                        'center right', 
                        ncol=((len(clusters)/35)+1), prop=dict(size=6))

    title = r"Cluster Population ($\epsilon$ = %s, %d clusters)" % (radius, len(clusters))
    pylab.title(title, weight='black')
    pylab.xlabel("Time", weight='bold')
    pylab.ylabel("Population Size", weight='bold')
    if sym is not None:
        pylab.figtext(0,.954, '(%s)' % sym, size=6, weight='black')

    pylab.savefig(filename, dpi=300)
def plot_all_shapelets(result):
    f, rows = plt.subplots(2, 2, sharex=True)
    axs = list(rows[:, 0]) + list(rows[:, 1])
    lines = dict()
    classifiers = dict()
    for i, (label, (classifier, _)) in enumerate(result.items()):
        classifiers[label] = classifier
    order = [('59d638667bfe0b5f22bd6420: Motek - Erebus Unmount',
              "Erebus Unmount"),
             ('59d638667bfe0b5f22bd6443: Motek - White Part Mount Tilted',
              "Motek - White Part Mount Tilted"),
             ('59d638667bfe0b5f22bd6446: Pitasc-Sub - White Part Mount Tilted',
              "Pitasc-Sub - White Part Mount Tilted"),
             ('59d638667bfe0b5f22bd6449: Pitasc - Insert Upright',
              "Pitasc - Insert Upright")]
    j = 0
    for i, (label, title) in enumerate(order):
        if classifiers.has_key(label):
            classifier = classifiers[label]
            axs[j].set_title(title)
            lines.update(
                plot_shapelet(axs[j], classifier.shapelet, classifier.dim_s))
            axs[j].set_xlim(0, 50)
            plt.setp(axs[j].get_yticklabels(), visible=False)
            j += 1
    classifiers = sorted(lines.items(), key=lambda x: x[0])
    plt.figlegend([x[1] for x in classifiers], [x[0] for x in classifiers],
                  loc='lower center',
                  ncol=5,
                  labelspacing=0.)
    plt.subplots_adjust(left=.05,
                        bottom=.15,
                        right=.95,
                        top=.93,
                        wspace=.12,
                        hspace=.51)
    plt.show()
예제 #25
0
def fig_pca(directory, outfn, coords, model, title, sample_population=None):
    import pylab

    if sample_population is None:
        sample_population = df_samples.population.values
    populations = list(sample_population.unique())
    colordict = randColor(len(populations), populations)

    # plot coords for PCs 1 vs 2, 3 vs 4
    figData = pylab.figure(figsize=(10, 6))
    #fig = pylab.figure()
    #figlegend = pylab.figure(figsize=(3,2))
    #ax = fig.add_subplot(121)
    #lines = ax.plot(range(10), pylab.randn(10), range(10), pylab.randn(10))

    ax = figData.add_subplot(1, 2, 1)
    plot_pca_coords(coords, model, 0, 1, ax, sample_population, populations,
                    colordict)
    ax = figData.add_subplot(1, 2, 2)
    plot_pca_coords(coords, model, 2, 3, ax, sample_population, populations,
                    colordict)

    # create a second figure for the legend
    figLegend = pylab.figure(figsize=(5, 10))

    # produce a legend for the objects in the other figure
    pylab.figlegend(*ax.get_legend_handles_labels(), loc='upper left')

    # save the two figures to files
    #figData.savefig("plot.png")
    figLegend.savefig(directory + "graphics/" + outfn + "_pca_legend.jpg")

    #ax.legend(bbox_to_anchor=(1, 1), loc='center')
    figData.suptitle(title, y=1.02)
    #fig.tight_layout()

    figData.savefig(directory + "graphics/" + outfn + "_pca.jpg")
예제 #26
0
파일: figures.py 프로젝트: n1Ray/tamkin
def do_plot(data, legend, fn_png):
    pylab.clf()
    pylab.figure(0, (6,8))
    #pylab.axes([left, bottom, width, height])
    # in fractional coordinates
    pylab.axes([0.20, 0.070, 0.76, 0.84])
    handles = []
    labels = []
    for key, symbol, label in legend:
        x = []
        y = []
        for index, lot in enumerate(lots_list):
            av = data.get((key, lot.key))
            if av is not None:
                x.append(av)
                y.append(-index)
        handle = pylab.plot(x, y, symbol)
        handles.append(handle)
        labels.append(label)
    pos = -numpy.arange(len(lots_list))
    ylabels = [lot.key for lot in lots_list]
    pylab.yticks(pos, ylabels, size="small")
    pylab.xticks(size="small")
    #pylab.xlabel(r"Geometric mean of $k_{\theory}/k_{\exp}$", size="small")
    pylab.xlabel(r"$\exp(RMSD(\ln(k_{\theory}) - \ln($k_{\exp})))$", size="small")
    pylab.semilogx()
    pylab.axvline(0.1, color="k", zorder=-5)
    pylab.axvline(1.0, color="k", zorder=-5)
    pylab.axvline(10.0, color="k", zorder=-5)
    for y in 0, -5, -10, -15, -20, -25, -30, -35, -40:
        pylab.axhline(y, color="k", alpha=0.2, lw=10, zorder=-5)
    if len(legend) > 1:
        legend = pylab.figlegend(handles, labels, (0.22,0.915), numpoints=1, handlelength=0, prop={"size":"small"})
        frame = legend.get_frame()
        frame.set_lw(0.0)
    #pylab.xlim(1e-8, 1e5)
    pylab.xlim(1e0, 1e5)
    pylab.ylim(pos[-1]-0.5, 0.5)
    pylab.savefig(fn_png)
예제 #27
0
def wise_psf(bands = [1,2,3,4], fftcentral=None, noplots=False, bw=False):
    colors = ['b', 'g', 'r', 'm']

    dbs = []

    slices = []
    centrals = []
    
    for band in bands:
        if band in [1,2,3]:
            # im = fitsio.read('wise-psf-w%i-507.5-507.5.fits' % band)
            im = fitsio.read('wise-psf-w%i-507.5-507.5-bright.fits' % band)
        else:
            # im = fitsio.read('wise-psf-w%i-253.5-253.5.fits' % band)
            im = fitsio.read('wise-psf-w%i-253.5-253.5-bright.fits' % band)
        im /= im.max()
        print 'Image', im.shape, im.dtype
        H,W = im.shape
        assert(H == W)
        print 'center / max:', im[H/2, W/2]
        assert(im[H/2, W/2] == 1.0)
        c = W/2

        # plt.clf()
        # plt.imshow(np.log10(im), interpolation='nearest', origin='lower',
        #            vmin=-8, vmax=0)
        # ps.savefig()

        nclip = (W - 251) / 2
        slc = slice(nclip, -nclip)
        central = im[slc,slc]
        centrals.append(central)
        ch,cw = central.shape
        cc = ch/2
        slices.append((central[cc,:], central[:,cc]))

        if fftcentral is not None:
            nclip = (W - fftcentral) / 2
            im = im[nclip:-nclip, nclip:-nclip]
            H,W = im.shape

        Y = im[H/2, :]
        X = im[:, W/2]
        Fy = np.fft.fft(Y)
        Fx = np.fft.fft(X)
        Sy = np.fft.fftshift(Fy)
        Sx = np.fft.fftshift(Fx)
        Px = Sx.real**2 + Sx.imag**2
        Py = Sy.real**2 + Sy.imag**2

        freqs1 = np.fft.fftfreq(len(Y))
        sfreqs1 = np.fft.fftshift(freqs1)
        s0 = np.flatnonzero(sfreqs1 == 0)
        s0 = s0[0]

        Px = Px[s0:]
        Py = Py[s0:]
        sf = sfreqs1[s0:]
        Sx = Sx[s0:]
        Sy = Sy[s0:]
        dbs.append((Sx, Sy, Px, Py, sf))

        # plt.clf()
        # plt.plot(sf, 10.*np.log10(Px / Px[0]), 'g-')
        # plt.plot(sf, 10.*np.log10(Py / Py[0]), 'b-')
        # plt.ylabel('dB')
        # plt.ylim(-40, 3)
        # plt.title('PSF models: W%i' % band)
        # ps.savefig()

    if noplots:
        return dbs

    cmap = 'jet'
    ps.basefn = 'wisepsf'
    if bw:
        ps.pattern = 'wisepsf-%s-bw.%s'
        #cmap = 'gray'
        cmap = antigray
        colors = ['k']*4

    plt.figure(figsize=(4,4))
    if True:
        vmin=-7
        vmax=0
        # bot = 0.05
        # top = 0.02
        # left = 0.1
        # right = 0.02
        bot = 0.12
        top = 0.03
        left = 0.12
        right = 0.03
        
        # colorbar cbar
        plt.figure(figsize=(0.75,4))
        plt.clf()
        ax = plt.gca()
        print 'ax pos', ax.get_position()
        ax.set_position([0.01, bot, 0.35, 1.-bot-top])
        #cax,kw = matplotlib.colorbar.make_axes(ax, fraction=0.9)
        #fraction 0.15; fraction of original axes to use for colorbar
        #pad 0.05 if vertical, 0.15 if horizontal; fraction of original axes between colorbar and new image axes
        #shrink 1.0; fraction by which to shrink the colorbar
        #aspect 20; ratio of long to short dimensions
        cb = matplotlib.colorbar.ColorbarBase(ax, cmap=cmap,
            norm=matplotlib.colors.Normalize(vmin=vmin, vmax=vmax))
        tv = np.arange(vmin, vmax+1)
        cb.set_ticks(tv, update_ticks=False)
        tt = ['$10^{%i}$' % t for t in tv]
        tt[-1] = '$1$'
        cb.set_ticklabels(tt, update_ticks=False)
        cb.update_ticks()
        ps.savefig()

        plt.figure(figsize=(4,4))
        plt.subplots_adjust(left=left, right=1.-right, bottom=bot, top=1.-top)
        for central in centrals:
            ch,cw = central.shape
            cc = ch/2
            plt.clf()
            plt.imshow(np.log10(np.maximum(10.**(vmin-1), central)),
                       interpolation='nearest', origin='lower',
                       vmin=vmin, vmax=vmax, cmap=cmap,
                       extent=[-cc,cc,-cc,cc])
            ps.savefig()



    if True:
        left = 0.17
        right = 0.03
        bot = 0.12
        top = 0.03
        plt.subplots_adjust(left=left, right=1.-right, bottom=bot, top=1.-top)

        for i,(sx,sy) in enumerate(slices):
            band = bands[i]
            plt.clf()
            S = len(sx)
            mid = S/2
            #vmin,vmax = -6,0
            #vmin,vmax = -3,0
            x = np.arange(len(sx)) - mid
            pa = plt.semilogy(x, sx, '-', color=colors[i])
            pb = plt.plot(x, sy, '-', color=colors[i], lw=3, alpha=0.5)
            xlo,xhi = -10, 10
            xx = np.linspace(xlo, xhi, 500)
            scale = np.pi / 2.
            yy = (2. * scipy.special.j1(xx * scale) / (xx * scale))**2
            yy[xx == 0] = 1.
            pc = plt.plot(xx, yy, '--', color='k')
    
            plt.figlegend((pb[0], pa[0], pc[0]),
                       ('W%i(y)' % band, 'W%i(x)' % band, 'Airy*'),
                       loc='upper right', fontsize=fontsize)
    
            plt.xlim(xlo, xhi)
            plt.ylim(1e-3/1.1, 1.1)
            plt.xlabel('Pixel')
            plt.ylabel('PSF profile')
            ps.savefig()


    if True:
        left = 0.17
        right = 0.03
        bot = 0.12
        top = 0.03
        plt.subplots_adjust(left=left, right=1.-right, bottom=bot, top=1.-top)
    
        # How much to compress the Airy profile so that it is just
        # well-sampled (with unit pixels)
        scale = np.pi / 2.
        # Its half-width at half-max
        hwhm = (1./scale * 1.61633)
        # Gaussian with same HWHM
        sigma = 2. * hwhm / 2.35
        print 'sigma', sigma
        x = np.linspace(-10, 10, 1000)
        dx = x[1]-x[0]
        g = np.exp(-0.5 * x**2 / sigma**2)

        airy = (2. * scipy.special.j1(x * scale) / (x * scale))**2

        Fg = np.fft.fft(g)
        Sg = np.fft.fftshift(Fg)
        Pg = Sg.real**2 + Sg.imag**2
        freqs1 = np.fft.fftfreq(len(g))
        sfg = np.fft.fftshift(freqs1)
        s0 = np.flatnonzero(sfg == 0)

        Fa = np.fft.fft(airy)
        Sa = np.fft.fftshift(Fa)
        Pa = Sa.real**2 + Sa.imag**2
        
        Pg = Pg[s0:]
        Pa = Pa[s0:]
        sfg = sfg[s0:]
        print 'dx', dx
        sfg /= dx
        
        for i,(nil,nil,Px,Py,sf) in enumerate(dbs):
            band = bands[i]

            plt.clf()
            pa = plt.plot(sf, 10.*np.log10(Px / Px[0]), '-', color=colors[i])
            pb = plt.plot(sf, 10.*np.log10(Py / Py[0]), '-', color=colors[i],
                          lw=3, alpha=0.5)
            pc = plt.plot(sfg, 10.*np.log10(Pg / Pg[0]), 'k--')
            pd = plt.plot(sfg, 10.*np.log10(Pa / Pa[0]), 'k:')

            lp = (pb[0], pa[0], pc[0], pd[0])
            lt = ('W%i (y)' % band, 'W%i (x)' % band,
                  'Gaussian*', 'Airy*')

            if band == 1:
                pitch = 0.001
                lan3 = lanczos_test(noplots=True, pitch=pitch)
                (Slan,Plan,sflan) = lan3[0]
                sflan /= pitch
                pe = plt.plot(sflan, 10.*np.log10(Plan / Plan[0]), 'k-.')
                lp = (pe[0],) + lp
                lt = ('Lanczos-3',) + lt

            plt.ylabel('Power in Fourier component (dB)')
            plt.ylim(-40, 3)
            plt.xlabel('Frequency (cycles/pixel)')
            plt.legend(lp, lt, loc='lower left', fontsize=fontsize)
            plt.xlim(0, 0.5)
            ps.savefig()
예제 #28
0
def plot_icd_vs_mass():
    galaxies = pickle.load(open('galaxies.pickle', 'rb'))

    arrow_up = [[0, 0], [-1, -1], [0, 0], [0, -2], [0, 0], [1, -1], [0, 0]]

    # Make figure
    f1 = pyl.figure(1, figsize=(4, 6))
    f1s1 = f1.add_subplot(211)
    f1s2 = f1.add_subplot(212)

    for galaxy in galaxies:
        if galaxy.ston_I > 30. and galaxy.ICD_IH != None:
            f1s1.scatter(galaxy.z,
                         galaxy.ICD_IH * 100,
                         c='0.8',
                         marker='o',
                         s=25,
                         edgecolor='0.8')

        if galaxy.ston_V > 30. and galaxy.ICD_VJ != None:
            f1s2.scatter(galaxy.z,
                         galaxy.ICD_VJ * 100,
                         c='0.8',
                         marker='o',
                         s=25,
                         edgecolor='0.8')

    # Add the box and whiskers
    galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.z for galaxy in galaxies2]
    ll = 1.5
    ul = 4
    bins_x = pyl.arange(ll, ul, 0.5)
    grid = []

    for i in range(bins_x.size - 1):
        xmin = bins_x[i]
        xmax = bins_x[i + 1]
        cond = [cond1 and cond2 for cond1, cond2 in zip(x >= xmin, x < xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_IH * 100 for galaxy in grid[i]])
    from boxplot_percentile import percentile_box_plot as pbp
    pbp(f1s1,
        icd,
        indexer=list(pyl.delete(bins_x, -1) + 0.25),
        whisker_top=None,
        whisker_bottom=None)

    # Add the box and whiskers
    galaxies = pickle.load(open('galaxies.pickle', 'rb'))
    galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.z for galaxy in galaxies2]
    ll = 1.5
    ul = 4
    bins_x = pyl.arange(ll, ul, 0.5)
    grid = []

    for i in range(bins_x.size - 1):
        xmin = bins_x[i]
        xmax = bins_x[i + 1]
        cond = [cond1 and cond2 for cond1, cond2 in zip(x >= xmin, x < xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_VJ * 100 for galaxy in grid[i]])
    pbp(f1s2,
        icd,
        indexer=list(pyl.delete(bins_x, -1) + 0.25),
        whisker_top=None,
        whisker_bottom=None)

    f1s1.set_xlim(1.5, 3.5)
    f1s2.set_xlim(1.5, 3.5)

    f1s1.set_ylim(-1, 10)
    f1s2.set_ylim(-1, 10)

    f1s1.set_xticklabels([])
    f1s2.set_xticks([1.5, 2, 2.5, 3, 3.5])

    f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)")
    f1s2.set_xlabel("Redshift")
    f1s2.set_ylabel(r"$\xi[V_{606},J_{125}]$ (%)")

    import matplotlib.font_manager
    line1 = pyl.Line2D([], [],
                       marker='o',
                       mfc='0.8',
                       mec='0.8',
                       markersize=8,
                       linewidth=0)
    line2 = pyl.Line2D([], [],
                       marker='s',
                       mec='#348ABD',
                       mfc='None',
                       markersize=10,
                       linewidth=0,
                       markeredgewidth=2)
    line3 = pyl.Line2D([], [], color='#A60628', linewidth=2)
    prop = matplotlib.font_manager.FontProperties(size='small')
    pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles', 'Medians'),
                  'lower center',
                  prop=prop,
                  ncol=3)

    pyl.show()
예제 #29
0
ax.grid()
if I > 1:
    ax.set_xscale('log')
    ax.set_yscale('log')

if I == 0:
    # x = We ** aWe / Ca ** aCa
    ax.set_xlabel(r'$We^{{{0:0.3f}}} Ca^{{{1:0.3f}}}$'.format(aWe, -aCa))
if I == 1:
    # x = We ** aWe * Re ** aRe
    ax.set_xlabel(r'$We^{{{0:0.3f}}} We^{{{1:0.3f}}}$'.format(aWe, aRe))
elif I == 2:
    # x = St ** aSt * (Ca ** aCa * c1 + c2)
    ax.set_xlabel(r'$St^{{{0:0.3f}}} Ca^{{{1:0.3f}}}$'.format(aSt, aCa))
elif I == 3:
    # x = Re * St ** aSt / We ** aWe
    ax.set_xlabel(r'$St^{{{0:0.3f}}} We^{{{1:0.3f}}} Re$'.format(aSt, -aWe))

fig.suptitle('C' + repr(I + 1))
plt.subplots_adjust(top=0.8)
ax.plot(x, y, color='black')
plt.savefig('validationData/plots/C' + repr(I + 1) + '.pgf',
            bbox_inches='tight')

figLegend = pylab.figure(figsize=(1.8, 0.3))
pylab.figlegend(*ax.get_legend_handles_labels(), loc='upper left')
plt.savefig('validationData/plots/legend.pgf', bbox_inches='tight')

plt.show()
예제 #30
0
파일: kintab.py 프로젝트: n1Ray/tamkin
    pylab.semilogy()
    pylab.fill(
        numpy.concatenate((invtemps, invtemps[::-1])),
        numpy.concatenate((experimental_k*10, experimental_k[::-1]/10)),
        "k", alpha=0.2, lw=0,
    )
    pylab.xticks(
        1/numpy.array([300, 350, 400, 450, 500, 550, 600], float),
        ["300", "350", "400", "450", "500", "550", "600"],
    )
    pylab.title(title)
    pylab.xlabel("T [K]")
    pylab.ylabel("k [(m**3/mol)/s]")
    pylab.ylim(1e-8,1e7)
    legend = pylab.figlegend(
        lines, labels, (0.07,0.06), ncol=3, prop={"size":11},
        handlelength=3, labelspacing=0.1, columnspacing=1
    )
    #legend.get_frame().set_linewidth(0)
    legend.get_frame().set_fill(True)
    legend.get_frame().set_alpha(0.5)
    pylab.savefig(fn_img % "rates")

    pylab.clf()
    lines = []
    labels = []
    line = pylab.plot([experimental_Ea], [experimental_A], color="k", marker="o", ms=11, mew=2, lw=0, ls=" ")
    lines.append(line)
    labels.append("experiment")
    for lot in lots_list:
        if lot.spin == "ROS":
            label = "ro" + lot.label
예제 #31
0
        P.close()

    # do AUC bar-chart
    #P.rcParams['xtick.direction'] = 'out'
    def x(a, b, m):
        return b*(len(methods)+1) + m
    P.figure(figsize=(14,6))
    for a, auc in enumerate(('AUC', 'AUC50')):
        ax = P.subplot(1, 2, a+1)
        xlocs = []
        xlabels = []
        for b, bg in enumerate(harness.options.backgrounds):
            rects = P.bar(
                [x(a, b, m) for m in xrange(len(methods))],
                [aucs[bg][auc][method] for method in methods],
                color=[colors[method] for method in methods],
                width=1.
            )
            xlocs.append(x(a, b, len(methods)/2.))
            xlabels.append(bg)
        P.xticks(xlocs, xlabels)
        P.ylim(0,1)
        P.title(auc)
        for tl in ax.get_xticklines():
            tl.set_visible(False)
    #P.text(.5, .9, '%s' % fragment, horizontalalignment='center')
    P.figlegend(rects, methods, loc='upper right')
    P.savefig(os.path.join(options.results_dir, 'AUC-%s.png' % fragment))
    P.savefig(os.path.join(options.results_dir, 'AUC-%s.eps' % fragment))
    P.close()
예제 #32
0
import pylab as plt

A = fits_table('Almanac_2016-03-31.fits')
A.cut(A.ra_offset < 99)
T = fits_table('db.fits')
T.cut(np.array([e in A.expnum for e in T.expnum]))
print(len(A), len(T))
from camera_mosaic import dradec_to_ref_chip

refdra, refddec = dradec_to_ref_chip(T)

plt.clf()
plt.subplot(2, 1, 1)
p1 = plt.plot(A.expnum, A.ra_offset, 'b.')
p2 = plt.plot(T.expnum, refdra, 'r.')
plt.figlegend([p1[0], p2[0]], ('Mosstat', 'Copilot'), 'upper right')
plt.ylabel('dRA (arcsec)')
plt.subplot(2, 1, 2)
plt.plot(A.expnum, A.dec_offset, 'b.')
plt.plot(T.expnum, refddec, 'r.')
plt.ylabel('dDec (arcsec)')
plt.xlabel('Expnum')
plt.suptitle('Copilot and Mosstat, 2016-03-31')
plt.savefig('astrom.png')

plt.clf()
p1 = plt.plot(A.expnum, A.ra_offset - refdra, 'b.')
p2 = plt.plot(A.expnum, A.dec_offset - refddec, 'r.')
plt.figlegend([p1[0], p2[0]], ('RA', 'Dec'), 'upper right')
plt.ylabel('Mosstat - Copilot (arcsec)')
plt.xlabel('Expnum')
예제 #33
0
def plot_icd_vs_mass():
    galaxies = pickle.load(open('galaxies.pickle','rb'))

    # Make figure
    f1 = pyl.figure(1, figsize=(6,4))
    f1s1 = f1.add_subplot(221)
    f1s2 = f1.add_subplot(222)
    f1s3 = f1.add_subplot(223)
    f1s4 = f1.add_subplot(224)

    #Upper and Lower limit arrow verts
    arrowup_verts = [[0.,0.], [-1., -1], [0.,0.],
        [0.,-2.], [0.,0.], [1,-1], [0,0]]
    #arrowdown_verts = [[0.,0.], [-1., 1], [0.,0.],
    #    [0.,2.], [0.,0.], [1, 1]]

    for galaxy in galaxies:
        if galaxy.ston_I > 30. and galaxy.ICD_IH != None:
            # Add arrows first
            if galaxy.ICD_IH > 0.5:
                f1s1.scatter(galaxy.Mass, 0.5*100, s=100, marker=None,
                    verts=arrowup_verts)
            else:
                f1s1.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8',
                    marker='o', s=25, edgecolor='0.8')
                f1s2.scatter(galaxy.Mass, galaxy.ICD_IH * 100, c='0.8',
                    marker='o', s=25, edgecolor='0.8')

        if galaxy.ston_J > 30. and galaxy.ICD_JH != None:
            # Add arrows first
            if galaxy.ICD_JH > 0.12:
                f1s3.scatter(galaxy.Mass, 12, s=100, marker=None,
                    verts=arrowup_verts)
            else:
                f1s3.scatter(galaxy.Mass, galaxy.ICD_JH * 100, c='0.8',
                    marker='o', s=25, edgecolor='0.8')
                f1s4.scatter(galaxy.Mass, galaxy.ICD_JH * 100, c='0.8',
                    marker='o', s=25, edgecolor='0.8')

    # Add the box and whiskers
    galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.Mass for galaxy in galaxies2]
    ll = 8.5
    ul= 12
    #bins_x =pyl.arange(8.5, 12.5, 0.5)
    bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.])
    grid = []

    for i in range(bins_x.size-1):
        xmin = bins_x[i]
        xmax = bins_x[i+1]
        cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]])

    from boxplot_percentile import percentile_box_plot as pbp
    #bp1 = f1s1.boxplot(icd, positions=pyl.delete(bins_x,-1)+0.25, sym='')
    pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))
    pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))


    # Add the box and whiskers
    galaxies2 = filter(lambda galaxy: galaxy.ston_J > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.Mass for galaxy in galaxies2]
    ll = 8.5
    ul= 12
    #bins_x =pyl.linspace(ll, ul, 7)
    #bins_x =pyl.arange(8.5, 12.5, 0.5)
    bins_x =pyl.array([8.5, 9., 9.5, 10., 10.5, 11., 12.])
    grid = []

    for i in range(bins_x.size-1):
        xmin = bins_x[i]
        xmax = bins_x[i+1]
        cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_JH*100 for galaxy in grid[i]])
    #bp2 = f1s2.boxplot(icd, positions=pyl.delete(bins_x,-1)+0.25, sym='')
    pbp(f1s3, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))
    pbp(f1s4, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))

    # Finish Plot
    # Tweak colors on the boxplot
    #pyl.setp(bp1['boxes'], lw=2)
    #pyl.setp(bp1['whiskers'], lw=2)
    #pyl.setp(bp1['medians'], lw=2)
    #pyl.setp(bp2['boxes'], lw=2)
    #pyl.setp(bp2['whiskers'], lw=2)
    #pyl.setp(bp2['medians'], lw=2)
    #pyl.setp(bp['fliers'], color='#8CFF6F', marker='+')

    #f1s1.axvspan(7.477, 9, facecolor='#FFFDD0', ec='None', zorder=0)
    #f1s1.axvspan(11, 12, facecolor='#FFFDD0', ec='None', zorder=0)
    #f1s2.axvspan(7.477, 9, facecolor='#FFFDD0', ec='None', zorder=0)
    #f1s2.axvspan(11, 12, facecolor='#FFFDD0', ec='None', zorder=0)

    f1s1.set_xlim(8,12)
    f1s2.set_xlim(8,12)
    f1s3.set_xlim(8,12)
    f1s4.set_xlim(8,12)

    f1s1.set_ylim(-10,50)
    f1s2.set_ylim(0,15)
    f1s3.set_ylim(-5,12)
    f1s4.set_ylim(-1,3)

    f1s1.set_xticks([8,9,10,11,12])
    f1s1.set_xticklabels([])
    f1s2.set_xticks([8,9,10,11,12])
    f1s2.set_xticklabels([])
    f1s3.set_xticks([8,9,10,11,12])
    f1s4.set_xticks([8,9,10,11,12])

    f1s4.set_yticks([-1, 0, 1, 2, 3])

    f1s3.set_xlabel(r"Log Mass ($M_{\odot})$")
    f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)")
    f1s4.set_xlabel(r"Log Mass ($M_{\odot})$")
    f1s3.set_ylabel(r"$\xi[J_{125},H_{160}]$ (%)")

    import matplotlib.font_manager
    line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8,
        linewidth=0)
    line2 = pyl.Line2D([], [], marker='s', mec='blue', mfc='None',
        markersize=10, linewidth=0, markeredgewidth=2)
    line3 = pyl.Line2D([], [], color='r', linewidth=2)
    prop = matplotlib.font_manager.FontProperties(size='small')
    pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles',
        'Medians'), 'lower center', prop=prop, ncol=3)

    from matplotlib.patches import ConnectionPatch
    xy = (12, 15)
    xy2 = (8, 15)
    con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
        axesA=f1s1, axesB=f1s2)
    xy = (12, 0)
    xy2 = (8, 0)
    con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
        axesA=f1s1, axesB=f1s2)
    f1s1.add_artist(con)
    f1s1.add_artist(con2)

    xy = (12, 3)
    xy2 = (8, 3)
    con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
        axesA=f1s3, axesB=f1s4)
    xy = (12, -1)
    xy2 = (8, -1)
    con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
        axesA=f1s3, axesB=f1s4)
    f1s3.add_artist(con)
    f1s3.add_artist(con2)

    pyl.draw()
    pyl.show()
예제 #34
0
def main():

    parser = E.OptionParser(
        version="%prog version: $Id: plot_histogram.py 2782 2009-09-10 11:40:29Z andreas $", usage=globals()["__doc__"])

    parser.add_option("-l", "--legend", dest="legend", type="string",
                      help="legend for plot [default=%default].")
    parser.add_option("-t", "--title", dest="title", type="string",
                      help="title for plot [default=%default].")
    parser.add_option("-p", "--hardcopy", dest="hardcopy", type="string",
                      help="filename for hardcopy of plot. The extension defines the format. Known extensions are: 'emf, eps, jpeg, jpg, pdf, png, ps, raw, rgba, svg, svgz' [default=%default].", metavar="FILE")
    parser.add_option("", "--xrange", dest="xrange", type="string",
                      help="x viewing range of plot [default=%default].")
    parser.add_option("", "--yrange", dest="yrange", type="string",
                      help="y viewing range of plot[default=%default].")
    parser.add_option("-o", "--logscale", dest="logscale", type="string",
                      help="use logscale on x, y or xy [default=%default]")
    parser.add_option("-x", "--xtitle", dest="xtitle", type="string",
                      help="title for x axis [default=%default]")
    parser.add_option("-y", "--ytitle", dest="ytitle", type="string",
                      help="title for y axis [default=%default]")
    parser.add_option("-d", "--dpi", dest="dpi", type="int",
                      help="dpi of images [default=%default]")
    parser.add_option("-n", "--normalize", dest="normalize", action="store_true",
                      help="normalize histograms [default=%default]")
    parser.add_option("--cumulate", dest="cumulate", action="store_true",
                      help="calculate cumulative histogram [default=%default].")
    parser.add_option("--reverse-cumulate", dest="reverse_cumulate", action="store_true",
                      help="calculate cumulative histogram in reverse order [default=%default].")
    parser.add_option("--legend-location", dest="legend_location", type="choice",
                      choices=("upper left", "upper right", "lower left",
                               "lower right", "center", "center right", "center left", "none"),
                      help="location of legend [default=%default]")
    parser.add_option("--backend", dest="backend", type="string",
                      help="backend to use [Agg|SVG|PS] [default=%default]")
    parser.add_option("--symbols", dest="symbols", type="string",
                      help="symbols to use for each histogram [steps|...] [default=%default].")
    parser.add_option("--dump", dest="dump", action="store_true",
                      help="dump data for debug purposes [default=%default].")
    parser.add_option("-c", "--columns", dest="columns", type="string",
                      help="columns to use for plotting [default=%default].")
    parser.add_option("--truncate", dest="truncate", action="store_true",
                      help="truncate date within x range. If not set, xrange is simply a viewing range [default=%default].")
    parser.add_option("--as-lines", dest="as_lines", action="store_true",
                      help="plot only lines, no symbols [default=%default].")
    parser.add_option("--noheaders", dest="headers", action="store_false",
                      help="do not take first input line as header [default=%default].")
    parser.add_option("--stacked", dest="stacked", action="store_true",
                      help="do a stacked plot [default=%default].")
    parser.add_option("--add-function", dest="function", type="string",
                      help="add a function to the plot [default=%default].")
    parser.add_option("--add-error-bars", dest="error_bars", type="choice",
                      choices=("interleaved", "blocked"),
                      help="add error bars. The input format is 'interleaved' or 'blocked'. In the interleaved format the error follows each column. I the blocked format first the data, then the errors in the same order [default=%default].")

    parser.set_defaults(
        legend=None,
        title=None,
        hardcopy=None,
        logscale=None,
        xtitle=None,
        ytitle=None,
        xrange=None,
        yrange=None,
        normalize=None,
        columns="all",
        headers=True,
        legend_location="upper right",
        backend="cairo",
        symbols="g-D,b-h,r-+,c-+,m-+,y-+,k-o,g-^,b-<,r->,c-D,m-h",
        dump=False,
        truncate=False,
        cumulate=False,
        reverse_cumulate=False,
        function=None,
        add_error_bars=None,
        as_lines=False,
        stacked=False,
        dpi=80,
    )

    (options, args) = E.Start(parser)

    # import matplotlib/pylab. Has to be done here
    # for batch scripts without GUI.
    import matplotlib
    if options.hardcopy:
        matplotlib.use("cairo")
    import pylab

    # put this method here (because it requires pylab)
    def doStackedPlot(data, legend):

        colors = ["red",
                  "blue",
                  "green",
                  "cyan",
                  "magenta",
                  "yellow",
                  "brown",
                  "silver",
                  "purple",
                  "lightyellow",
                  "black",
                  "ivory",
                  "pink",
                  "orange",
                  "gray",
                  "teal"]

        ax = data[:, 0]
        xvals = numpy.concatenate((ax, ax[::-1]))
        y_top = numpy.zeros(len(ax))

        min_y = min(data[:, 1:].flat)
        max_y = min_y
        new_legend, dummy_lines = [], []

        for i in range(1, len(legend)):
            new_y_top = y_top + data[:, i]
            yvals = numpy.concatenate((new_y_top, y_top[::-1]))
            p = pylab.fill(xvals,
                           yvals,
                           colors[i % len(colors)])

            y_top = new_y_top
            max_y = max(y_top)

            dummy_lines.append(pylab.plot(xvals,
                                          yvals,
                                          colors[i % len(colors)]))

            new_legend.append(legend[i])

        if not options.xrange:
            options.xrange = min(data[:, 0]), max(data[:, 0])

        if not options.yrange:
            options.yrange = 0, max_y

        return dummy_lines, new_legend

    if options.as_lines:
        options.symbols = []
        for y in ("-", ":", "--"):
            for x in "gbrcmyk":
                options.symbols.append(y + x)
    else:
        options.symbols = options.symbols.split(",")

    if options.xrange:
        options.xrange = map(float, options.xrange.split(","))
    if options.yrange:
        options.yrange = map(float, options.yrange.split(","))

    # Added support for (inclusive) range format: "1,3,5,7-100"  (Gerton
    # 13/12/06)
    if options.columns != "all":
        cols = []
        for d in options.columns.split(','):
            colopts = d.split('-')
            if len(colopts) == 2:
                cols += range(int(colopts[0]), int(colopts[1]) + 1)
            else:
                cols += [int(d) - 1]
        options.columns = cols

    if args:
        if args[0] == "-":
            infile = sys.stdin
        else:
            infile = open(args[0], "r")
    else:
        infile = sys.stdin

    if options.truncate:
        xr = options.xrange
    else:
        xr = None

    data, legend = IOTools.readTable(infile,
                                     numeric_type=numpy.float,
                                     take=options.columns,
                                     headers=options.headers,
                                     truncate=xr)

    if infile != sys.stdin:
        infile.close()
    if len(data) == 0:  # or data is None:
        E.info("empty table: no plot")
        E.Stop()
        return

    nrows, ncols = data.shape

    # note: because of MA, iteration makes copy of slices
    # Solution: inplace edits.
    if options.cumulate:
        if options.add_error_bars:
            raise "can not add error bars to cumulative histogram."
        if data.mask.any():
            # cumsum does not work with masked arrays, so do it manually
            for y in range(1, ncols):
                c = 0
                for x in range(0, nrows):
                    if not data.mask[x, y]:
                        data[x, y] += c
                        c = data[x, y]
        else:
            for x in range(1, ncols):
                data[:, x] = data[:, x].cumsum()

    elif options.reverse_cumulate:
        if options.add_error_bars:
            raise "can not add error bars to cumulative histogram."
        if data.mask.any():
            l = [0] * ncols
            for x in range(nrows - 1, -1, -1):
                for y in range(1, ncols):
                    if not data.mask[x, y]:
                        data[x, y] += l[y]
                        l[y] = data[x, y]
        else:
            l = [0] * ncols
            for x in range(nrows - 1, -1, -1):
                for y in range(1, ncols):
                    data[x, y] += l[y]
                    l[y] = data[x, y]

    if options.normalize:
        if options.add_error_bars:
            raise "can not add error bars to normalized histogram."
        if data.mask.any():
            m = [0] * ncols
            for x in range(nrows):
                for y in range(1, ncols):
                    if not data.mask[x, y]:
                        m[y] = max(m[y], float(data[x, y]))

            for y in range(1, ncols):
                if m[y] == 0:
                    m[y] = 1.0

            for x in range(nrows):
                for y in range(1, ncols):
                    data[x, y] = data[x, y] / m[y]
        else:
            for x in range(1, ncols):
                m = float(data[:, x].max())
                data[:, x] /= m

    if options.legend:
        legend = options.legend.split(",")

    if options.dump:
        for d in data:
            print d

    if options.title:
        pylab.title(options.title)

    if options.xtitle:
        pylab.xlabel(options.xtitle)
    else:
        pylab.xlabel(legend[0])

    if options.ytitle:
        pylab.ylabel(options.ytitle)

    lines = []
    # use dummy_lines to workaround a bug in errorbars that
    # causes the line styles to be set incorrectly.
    dummy_lines = []
    new_legend = []

    if options.error_bars:
        if options.error_bars == "interleaved":
            step_size = 2
            max_size = len(legend)
        elif options.error_bars == "blocked":
            step_size = 1
            max_size = (len(legend) - 1) / 2
    else:
        step_size = 1
        max_size = len(legend)

    if options.stacked:
        dummy_lines, new_legend = doStackedPlot(data, legend)
    else:
        nplotted = 0
        nskipped = 0
        for x in range(1, max_size, step_size):

            s = options.symbols[nplotted % len(options.symbols)]

            yvals = data[:, x]

            xvals = numpy.ma.masked_array(data[:, 0], numpy.ma.getmask(yvals))

            xvals = xvals.compressed()
            yvals = yvals.compressed()

            if len(xvals) == 0:
                E.warn("skipped empty column %i: %s" % (x, legend[x]))

            if options.error_bars == "interleaved":
                yerr = data[:, x + 1]
                yerr = yerr.compressed()
            else:
                yerr = None

            lines.append(pylab.errorbar(xvals,
                                        yvals,
                                        yerr=yerr,
                                        fmt=s))

            dummy_lines.append(pylab.plot(xvals,
                                          yvals,
                                          s))

            new_legend.append(legend[x])

            nplotted += 1

        E.info("nplotted=%i, nskipped=%i" % (nplotted, nskipped))

    if len(lines) == 0:
        E.Stop()
        return

    if options.legend_location != "none":
        pylab.figlegend(dummy_lines,
                        new_legend,
                        options.legend_location)

    if options.logscale:
        if "x" in options.logscale:
            pylab.gca().set_xscale('log')
        if "y" in options.logscale:
            pylab.gca().set_yscale('log')

    if options.xrange:
        pylab.xlim(options.xrange)

    if options.yrange:
        pylab.ylim(options.yrange)

    if options.function:
        xstart, xend = pylab.gca().get_xlim()
        increment = (xend - xstart) / 100.0
        exec("f = lambda x: %s" % options.function) in locals()
        xvals, yvals = [], []
        for x in range(0, 100):
            xvals.append(xstart)
            yvals.append(f(xstart))
            xstart += increment
        xvals.append(xstart)
        yvals.append(f(xstart))

        pylab.plot(xvals, yvals)

    if options.hardcopy:
        pylab.savefig(os.path.expanduser(options.hardcopy), dpi=options.dpi)
    else:
        pylab.show()

    E.Stop()
예제 #35
0
    # calculate the nets output for train and the test data
    trnSequenceOutput = net.extrapolate(trnSequenceWashout, len(trnSequenceTarget))
    tstSequenceOutput = net.extrapolate(tstSequenceWashout, len(tstSequenceTarget))

    # plot training data
    sp = subplot(211)  # switch to the first subplot
    cla()  # clear the subplot
    title("Training Set")  # set the subplot's title
    sp.set_autoscale_on(True)  # enable autoscaling
    targetline = plot(trnSequenceTarget, "r-")  # plot the targets
    sp.set_autoscale_on(False)  # disable autoscaling
    outputline = plot(trnSequenceOutput, "b-")  # plot the actual output

    # plot test data
    sp = subplot(212)
    cla()
    title("Test Set")
    sp.set_autoscale_on(True)
    plot(tstSequenceTarget, "r-")
    sp.set_autoscale_on(False)
    plot(tstSequenceOutput, "b-")

    # create a legend
    figlegend((targetline, outputline), ("target", "output"), ("upper right"))

    # draw everything
    draw()


show()
예제 #36
0
def interaction_plot(df, val, xaxis, 
                     seplines=None, sepxplots=None, sepyplots=None,
                     xmin='AUTO', xmax='AUTO', ymin='AUTO', ymax='AUTO',
                     where=None, fname=None, output_dir='',
                     quality='low', yerr=None):
    """
    makes an interaction plot

       args:
          df:
             a pyvttbl.DataFrame object
    
          val:
             the label of the dependent variable
    
          xaxis:
             the label of the variable to place on the xaxis of each subplot

       kwds:
          seplines:
             label specifying seperate lines in each subplot
               
          sepxplots:
             label specifying seperate horizontal subplots
                
          sepyplots:
             label specifying separate vertical subplots
                
          xmin:
             ('AUTO' by default) minimum xaxis value across subplots
    
          xmax:
             ('AUTO' by default) maximum xaxis value across subplots
    
          ymin:
             ('AUTO' by default) minimum yaxis value across subplots
    
          ymax:
             ('AUTO' by default) maximum yaxis value across subplots
    
          where:
             a string, list of strings, or list of tuples
             applied to the DataFrame before plotting
            
          fname:
             output file name
    
          quality:
             {'low' | 'medium' | 'high'} specifies image file dpi
    
          yerr:
             {float, 'ci', 'stdev', 'sem'} designates errorbars across
             datapoints in all subplots
    """

    ##############################################################
    # interaction_plot programmatic flow                         #
    ##############################################################
    #  1. Check to make sure a plot can be generated with the    # 
    #     specified arguments and parameter                      #
    #  2. Set yerr aggregate                                     #
    #  3. Figure out ymin and ymax if 'AUTO' is specified        #
    #  4. Figure out how many subplots we need to make and the   #
    #     levels of those subplots                               #
    #  5. Initialize pylab.figure and set plot parameters        #
    #  6. Build and set main title                               #
    #  7. loop through the the rlevels and clevels and make      #
    #     subplots                                               #
    #      7.1 Create new axes for the subplot                   #
    #      7.2 Add subplot title                                 #
    #      7.3 Format the subplot                                #
    #      7.4 Iterate plotnum counter                           #
    #  8. Place yerr text in bottom right corner                 #
    #  9. Save the figure                                        #
    # 10. return the test dictionary                             #
    ##############################################################

    #  1. Check to make sure a plot can be generated with the    
    #     specified arguments and parameter
    ##############################################################
    # pylab doesn't like not being closed. To avoid starting
    # a plot without finishing it, we do some extensive checking
    # up front

    if where == None:
        where = []

    # check for data
    if df == {}:
        raise Exception('Table must have data to plot marginals')

    # check to see if data columns have equal lengths
    if not df._are_col_lengths_equal():
        raise Exception('columns have unequal lengths')

    # check to make sure arguments are column labels
    if val not in df.keys():
        raise KeyError(val)

    if xaxis not in df.keys():
        raise KeyError(xaxis)
    
    if seplines not in df.keys() and seplines != None:
        raise KeyError(seplines)

    if sepxplots not in df.keys() and sepxplots != None:
        raise KeyError(sepxplots)
    
    if sepyplots not in df.keys() and sepyplots != None:
        raise KeyError(sepyplots)

    # check for duplicate names
    dup = Counter([val, xaxis, seplines, sepxplots, sepyplots])
    del dup[None]
    if not all([count == 1 for count in dup.values()]):
        raise Exception('duplicate labels specified as plot parameters')

    # check fname
    if not isinstance(fname, _strobj) and fname != None:
        raise TypeError('fname must be None or string')

    if isinstance(fname, _strobj):
        if not (fname.lower().endswith('.png') or \
                fname.lower().endswith('.svg')):
            raise Exception('fname must end with .png or .svg')                

    # check cell counts
    cols = [f for f in [seplines, sepxplots, sepyplots] if f in df.keys()]
    counts = df.pivot(val, rows=[xaxis], cols=cols,
                      where=where, aggregate='count')

    # unpack conditions DictSet from counts PyvtTbl
    # we need to do it this way instead of using df.conditions
    # because the counts PyvtTbl reflects the where exclusions
    conditions = counts.conditions

    # flatten counts to MaskedArray
    counts = counts.flatten()

    for count in counts:
        if count < 1:
            raise Exception('cell count too low to calculate mean')

    #  2. Initialize test dictionary
    ##############################################################
    # To test the plotting a dict with various plot parameters
    # is build and returned to the testing module. In this
    # scenario our primary concern is that the values represent
    # what we think they represent. Whether they match the plot
    # should be fairly obvious to the user. 
    test = {}
    
    #  3. Set yerr aggregate so sqlite knows how to calculate yerr
    ##############################################################
    
    # check yerr
    aggregate = None
    if yerr == 'sem':
        aggregate = 'sem'
        
    elif yerr == 'stdev':
        aggregate = 'stdev'
        
    elif yerr == 'ci':
        aggregate = 'ci'

    for count in counts:
        if aggregate != None and count < 2:
            raise Exception('cell count too low to calculate %s'%yerr)

    test['yerr'] = yerr
    test['aggregate'] = aggregate

    #  4. Figure out ymin and ymax if 'AUTO' is specified
    ##############################################################            
    desc = df.descriptives(val)
    
    if ymin == 'AUTO':
        # when plotting postive data always have the y-axis go to 0
        if desc['min'] >= 0.:
            ymin = 0. 
        else:
            ymin = desc['mean'] - 3.*desc['stdev']
    if ymax == 'AUTO':
        ymax = desc['mean'] + 3.*desc['stdev']

    if any([math.isnan(ymin), math.isinf(ymin), math.isnan(ymax), math.isinf(ymax)]):
        raise Exception('calculated plot bounds nonsensical')

    test['ymin'] = ymin
    test['ymax'] = ymax

    #  5. Figure out how many subplots we need to make and the
    #     levels of those subplots
    ##############################################################      
    numrows = 1
    rlevels = [1]
    if sepyplots != None:
        rlevels = copy(conditions[sepyplots]) # a set
        numrows = len(rlevels) # a int
        rlevels = sorted(rlevels) # set -> sorted list
            
    numcols = 1
    clevels = [1]            
    if sepxplots != None:
        clevels = copy(conditions[sepxplots])
        numcols = len(clevels)
        clevels = sorted(clevels) # set -> sorted list

    test['numrows']  = numrows
    test['rlevels']  = rlevels
    test['numcols']  = numcols
    test['clevels']  = clevels
    
    #  6. Initialize pylab.figure and set plot parameters
    ##############################################################  
    fig = pylab.figure(figsize=(6*numcols, 4*numrows+1))
    fig.subplots_adjust(wspace=.05, hspace=0.2)
    
    #  7. Build and set main title
    ##############################################################  
    maintitle = '%s by %s'%(val,xaxis)
    
    if seplines:
        maintitle += ' * %s'%seplines
    if sepxplots:
        maintitle += ' * %s'%sepxplots
    if sepyplots:
        maintitle += ' * %s'%sepyplots
        
    fig.text(0.5, 0.95, maintitle,
             horizontalalignment='center',
             verticalalignment='top')

    test['maintitle']  = maintitle
    
    #  8. loop through the the rlevels and clevels and make
    #     subplots
    ##############################################################
    test['y'] = []
    test['yerr'] = []
    test['subplot_titles'] = []
    test['xmins'] = []
    test['xmaxs'] = []
    
    plotnum = 1 # subplot counter
    axs = []

    for r, rlevel in enumerate(rlevels):
        for c, clevel in enumerate(clevels):
            where_extension = []
            if sepxplots!=None:
                where_extension.append((sepxplots, '=', [clevel]))
            if sepyplots!=None:
                where_extension.append((sepyplots, '=', [rlevel]))
            
            #  8.1 Create new axes for the subplot
            ######################################################
            axs.append(pylab.subplot(numrows, numcols, plotnum))

            ######## If separate lines are not specified #########
            if seplines == None:
                y = df.pivot(val, cols=[xaxis],
                               where=where+where_extension,
                               aggregate='avg')
                
                cnames = y.cnames
                y = y.flatten()

                if aggregate != None:
                    yerr = df.pivot(val, cols=[xaxis],
                                      where=where+where_extension,
                                      aggregate=aggregate).flatten()
                
                x = [name for [(label, name)] in cnames]
                
                if _isfloat(yerr):
                    yerr = np.array([yerr for a in x])

                if all([_isfloat(a) for a in x]):
                    axs[-1].errorbar(x, y, yerr)
                    if xmin == 'AUTO' and xmax == 'AUTO':
                        xmin, xmax = axs[-1].get_xlim()
                        xran = xmax - xmin
                        xmin = xmin - 0.05*xran
                        xmax = xmax + 0.05*xran

                    axs[-1].plot([xmin, xmax], [0., 0.], 'k:')
                    
                else : # categorical x axis
                    axs[-1].errorbar(_xrange(len(x)), y, yerr)
                    pylab.xticks(_xrange(len(x)), x)
                    xmin = - 0.5
                    xmax = len(x) - 0.5
                    
                    axs[-1].plot([xmin, xmax], [0., 0.], 'k:')

            ########## If separate lines are specified ###########
            else:                       
                y = df.pivot(val, rows=[seplines], cols=[xaxis],
                               where=where+where_extension,
                               aggregate='avg')
                
                if aggregate != None:
                    yerrs = df.pivot(val,
                                       rows=[seplines],
                                       cols=[xaxis],
                                       where=where+where_extension,
                                       aggregate=aggregate)
                    
                x = [name for [(label, name)] in y.cnames]

                if _isfloat(yerr):
                    yerr = np.array([yerr for a in x])

                plots = []
                labels = []
                for i, name in enumerate(y.rnames):
                    if aggregate != None:
                        yerr = yerrs[i].flatten()
                    
                    labels.append(name[0][1])

                    if all([_isfloat(a) for a in x]):
                        plots.append(
                            axs[-1].errorbar(x, y[i].flatten(), yerr)[0])
                        
                        if xmin == 'AUTO' and xmax == 'AUTO':
                            xmin , xmax = axs[-1].get_xlim()
                            xran = xmax - xmin
                            xmin = xmin - .05*xran
                            xmax = xmax + .05*xran
                            
                        axs[-1].plot([xmin, xmax], [0.,0.], 'k:')
                        
                    else : # categorical x axis
                        plots.append(
                            axs[-1].errorbar(
                                _xrange(len(x)), y[i].flatten(), yerr)[0])
                        
                        pylab.xticks(_xrange(len(x)), x)
                        xmin = - 0.5
                        xmax = len(x) - 0.5
                        
                        axs[-1].plot([xmin, xmax], [0., 0.], 'k:')

                pylab.figlegend(plots, labels, loc=1,
                                labelsep=.005,
                                handlelen=.01,
                                handletextsep=.005)

            test['y'].append(y)
            if yerr == None:
                test['yerr'].append([])
            else:
                test['yerr'].append(yerr)
            test['xmins'].append(xmin)
            test['xmaxs'].append(xmax)

            #  8.2 Add subplot title
            ######################################################
            if rlevels == [1] and clevels == [1]:
                title = ''
                
            elif rlevels == [1]:
                title = _str(clevel)
                
            elif clevels == [1]:
                title = _str(rlevel)
                
            else:
                title = '%s = %s, %s = %s' \
                        % (sepyplots, _str(rlevel),
                           sepxplots, _str(rlevel))
                
            pylab.title(title, fontsize='medium')
            test['subplot_titles'].append(title)

            #  8.3 Format the subplot
            ######################################################
            pylab.xlim(xmin, xmax)
            pylab.ylim(ymin, ymax)

            # supress tick labels unless subplot is on the bottom
            # row or the far left column
            if r != (len(rlevels) - 1):
                pylab.setp(axs[-1].get_xticklabels(), visible=False)
                
            if c != 0:
                pylab.setp(axs[-1].get_yticklabels(), visible=False)

            # Set the aspect ratio for the subplot
            Dx = abs(axs[-1].get_xlim()[0] - axs[-1].get_xlim()[1])
            Dy = abs(axs[-1].get_ylim()[0] - axs[-1].get_ylim()[1])
            axs[-1].set_aspect(.75*Dx/Dy)
            
            #  8.4 Iterate plotnum counter
            ######################################################
            plotnum += 1

    #  9. Place yerr text in bottom right corner
    ##############################################################
    if aggregate != None:
        if aggregate == 'ci':
            aggregate = '95% ci' 
            
        pylab.xlabel('\n\n                '
                     '*Error bars reflect %s'\
                     %aggregate.upper())

    # 10. Save the figure
    ##############################################################
    if fname == None:
        fname = 'interaction_plot(%s'%val
        factors = [xaxis,seplines,sepxplots,sepyplots]
        fname += '~' + '_X_'.join([str(f) for f in factors if f != None])
        if aggregate != None:
            fname += ',yerr=' + aggregate
        elif yerr != None:
            fname += ',yerr=' + str(yerr[0])
        fname += ').png'

    fname = os.path.join(output_dir, fname)
    
    if quality == 'low' or fname.endswith('.svg'):
        pylab.savefig(fname)
        
    elif quality == 'medium':
        pylab.savefig(fname, dpi=200)
        
    elif quality == 'high':
        pylab.savefig(fname, dpi=300)
        
    else:
        pylab.savefig(fname)

    pylab.close()

    test['fname'] = fname

    # 11. return the test dictionary
    ##############################################################
    if df.TESTMODE:
        return test
예제 #37
0
    sp = subplot(211) # switch to the first subplot
    cla() # clear the subplot
    title("Training Set") # set the subplot's title
    sp.set_autoscale_on( True ) # enable autoscaling
    targetline = plot(trnSequenceTarget,"r-") # plot the targets
    sp.set_autoscale_on( False ) # disable autoscaling
    outputline = plot(trnSequenceOutput,"b-") # plot the actual output


    # plot test data
    sp = subplot(212)
    cla()
    title("Test Set")
    sp.set_autoscale_on( True )
    plot(tstSequenceTarget,"r-")
    sp.set_autoscale_on( False )
    plot(tstSequenceOutput,"b-")

    # create a legend
    figlegend((targetline, outputline),('target','output'),('upper right'))

    # draw everything
    draw()


show()




예제 #38
0
def _plot_psf_models(fn, ps):
    plt.clf()
    plt.subplots_adjust(top=0.92, bottom=0.1, left=0.1, right=0.9,
                        hspace=0.25)
    lp,lt = [],[]
    for band in [1,2,3,4]:
        print
        print 'Band W%i' % band
        
        #plt.clf()
        plt.subplot(2,2, band)
        plt.title('W%i' % band)
        
        pix = fitsio.read('wise-psf-avg-pix-bright.fits', ext=band-1)
        h,w = pix.shape
        scale = 1.
        vscale = 1
        if band == 4:
            scale = 0.5
            vscale = 2.
        # Slices along axes
        xx = (np.arange(w)-w/2) / scale
        p1 = plt.plot(xx, pix[h/2, :] * vscale, 'b-', lw=2, alpha=0.5)
        xx = (np.arange(h)-h/2) / scale
        p2 = plt.plot(xx, pix[:, w/2] * vscale, 'b--', lw=2, alpha=0.5)
        lp.append(p1[0])
        lt.append('Meisner+')
        yy = pix[:,w/2]
        print 'Meisner max:', pix.max(), 'sum', pix.sum()
        
        pix = reduce(np.add,
                     [fitsio.read('wise-w%i-psf-wpro-09x09-%02ix%02i.fits' %
                                  (band, 1+(i/9), 1+(i%9)))
                                  for i in range(9*9)])
        pix /= 81.
        
        h,w = pix.shape
        scale = 8.
        vscale = scale**2
        if band == 4:
            scale = 4.
            
        xx = (np.arange(w)-w/2) / scale
        p3 = plt.plot(xx, pix[h/2, :] * vscale, 'r-')
        xx = (np.arange(h) - h/2) / scale
        p4 = plt.plot(xx, pix[:, w/2] * vscale, 'r--')
        lp.append(p3[0])
        lt.append('AllWISE')
        print 'AllWISE max:', pix.max(), 'sum', pix.sum()


        T = fits_table(fn, hdu=band)
        # T.about()
        print 'Amp sum:', np.sum(T.amp)
        print 'Amp', T.amp
        # print 'Mean', T.mean
        print 'Var', T.var[:,0,0]

        vscale = 1
        if band == 4:
            vscale = 4.

        #xlo,xhi = -22, 22
        xlo,xhi = -16, 16
            
        xx = np.linspace(xlo, xhi, 201)
        yy = reduce(np.add,
                    [amp * 1./(2.*np.pi*var) * np.exp(-0.5 * xx**2 / var)
                     for amp,var in zip(T.amp, T.var[:,0,0])])
        p5 = plt.plot(xx, yy * vscale, 'k-')

        lp.append(p5[0])
        lt.append('Model')

        
        # for amp,var in zip(T.amp, T.var[:,0,0]):
        #     yy = amp * 1./(2.*np.pi*var) * np.exp(-0.5 * xx**2 / var)
        #     if amp < 0:
        #         plt.plot(xx, np.abs(yy) * vscale, 'k--', alpha=0.5)
        #     else:
        #         plt.plot(xx, yy * vscale, 'k-', alpha=0.5)

        print 'Mid:', yy[len(yy)/2]
        print 'Model max:', yy.max()

        xx,yy = np.meshgrid(np.arange(w)-w/2, np.arange(h)-h/2)
        zz = reduce(np.add,
                    [amp * 1./(2.*np.pi*var) * np.exp(-0.5 * (xx**2 + yy**2) / var)
                     for amp,var in zip(T.amp, T.var[:,0,0])])
        print 'Model sum:', zz.sum()

        
        #plt.yscale('symlog', linthreshy=1e-6)
        plt.yscale('log')
        plt.ylim(2.5e-6, 0.25)
        
        #plt.xscale('symlog', linthreshx=10, linscalex=3)
        #plt.xlim(-100, 100)
        plt.xticks([-20,-10,0,10,20])
        plt.xlim(xlo, xhi)
        
        if band == 1:
            plt.figlegend(lp, lt, 'upper right')

        if band in [1,3]:
            plt.ylabel('PSF profile')
        if band in [3,4]:
            plt.xlabel('Pixel')
            
            
    ps.savefig()
예제 #39
0
파일: helper.py 프로젝트: arsenovic/hftools
def twin_fig_legend(axes=None, loc="best", **kw):
    """Skapa figur legend for alla linjer i en plot som skapats med
    dubbla y-axlar (twinx)
    """
    (lines, labels) = build_figlegend(axes)
    pylab.figlegend(lines, labels, loc=loc, **kw)
예제 #40
0
def plot_fingerprint(data, alpha=0.7, \
    show_legend=True, graph_name='fingerprint.png', has_mean=True,
    which_blocks='quartets', multiple=False, graph_grid='t', prob_axes=True, \
    edge_colors='k', **kwargs):
    """Outputs a bubble plot of four-codon amino acid blocks
    labeled with the colors from Sueoka 2002.

    takes: data:  array-elements in the col order x, y, r of
           each of the four codon Amino Acids in the row order:
           ALA, ARG4, GLY, LEU4, PRO, SER, THR, VAL
           (for traditional fingerprint), or:
           UU -> GG (for 16-block fingerprint).
           last row is the mean (if has_mean is set True)

        **kwargs passed on to init_graph_display (these include 
        graph_shape, graph_grid, x_label, y_label, dark, with_parens).
                 
           title: will be printed on graph (default: 'Unknown Species')
           
           num_genes (number of genes contributing to graph: default None)
           NOTE: will not print if None.)
        
           size: of graph in inches (default = 8.0)

           alpha: transparency of bubbles
           (ranges from 0, transparent, to 1, opaque; default 0.7)
           
           show_legend: bool, default True, whether to print legend

           graph_name: name of file to write (default 'fingerprint.png')

           has_mean: whether the data contain the mean (default: True)

           which_blocks: which codon blocks to print (default is 'quartets'
           for the 4-codon amino acid blocks, but can also use 'all' for all 
           quartets or 'split' for just the split quartets.)

           multiple: if False (the default), assumes it got a single block
           of data. Otherwise, assumes multiple blocks of data in a list or
           array.

           edge_colors: if multiple is True (ignored otherwise), uses this
           sequence of edge color strings to hand out edge colors to successive
           series. Will iterate over this, so can be a string of 1-letter
           color codes or a list of color names.

    note: that the data are always expected to be in the range (0,1)
    since we're plotting frequencies. axes, gid, etc. are hard-coded
    to these values. 
    """
    #figure out which type of fingerprint plot we're doing, and get the
    #right colors
    if which_blocks == 'quartets':
        blocks = CodonUsage.SingleAABlocks
    elif which_blocks == 'split':
        blocks = CodonUsage.SplitBlocks
    else:
        blocks = CodonUsage.Blocks

    colors = [doublets_to_colors[i] for i in blocks]
      
    #formatting the labels in latex
    x_label="$G_3/(G_3+C_3)$"
    y_label="$A_3/(A_3+T_3)$"

    #initializing components of the graph
    font,label_font_size=init_graph_display(graph_shape='sqr', \
        graph_grid=graph_grid, x_label=x_label, \
        y_label=y_label, prob_axes=prob_axes, **kwargs)

    if not multiple:
        data = [data]
 
    alpha = broadcast(alpha, len(data))
    edge_colors = broadcast(edge_colors, len(data))
  
    for al, d, edge_color in zip(alpha, data, edge_colors):
        #skip this series if no data
        if d is None or not d.any():
            continue
        for i, color in enumerate(colors):
            j = i+1
            #note: doing these as slices because scatter_classic needs the
            #extra level of nesting
            patches = scatter_classic(d[i:j,0], d[i:j,1],
                        s=(d[i:j,2]/2), c=color)
            #set alpha for the patches manually
            for p in patches:
                p.set_alpha(al)
                p.set_edgecolor(edge_color)
        
        #plot mean as its own point -- can't do cross with scatter
        if has_mean:
            mean_index = len(blocks)    #next index after the blocks
            plot([d[mean_index,0]], [d[mean_index,1]],
                 '-k+',markersize=label_font_size, alpha=al)
               

    abbrev = CodonUsage.BlockAbbreviations

    a = gca()
    #if show_legend is True prints a legend in the right center area
    if show_legend:
        legend_key = [abbrev[b] for b in blocks]
        #copy legend font properties from the x axis tick labels
        legend_font_props = \
            a.xaxis.get_label().get_fontproperties().copy()
        legend_font_scale_factor = 0.7
        curr_size = legend_font_props.get_size()
        legend_font_props.set_size(curr_size*legend_font_scale_factor)
        l = figlegend(a.patches[:len(blocks)],
                  legend_key,
                  prop=legend_font_props,
                  loc='center right',borderpad=0.1,labelspacing=0.5,
                  handlelength=1.0,handletextpad=0.5, borderaxespad=0.0)
        #fix transparency of patches
        for p in l.get_patches():
            p.set_alpha(1)

    #initialize the ticks
    set_axis_to_probs()
    init_ticks(a, label_font_size)
    a.set_xticks([0, 0.5, 1])
    a.set_yticks([0,0.5,1])
    
    #output the figure
    if graph_name is not None:
        savefig(graph_name)
예제 #41
0
파일: bpt_legend.py 프로젝트: bajoshi/taffy
    pylab.errorbar(0.5, 0.5, xerr=0.1, yerr=0.1, \
        color='None', markersize=7, markeredgecolor='darkorange', fmt='o', capsize=0, elinewidth=0.4, \
        label=r'$\left< \mathrm{West\ Bridge} \right>$')

    pylab.errorbar(np.arange(10), np.arange(10), xerr=np.arange(10), yerr=np.arange(10), \
        color='darkgreen', markersize=3.5, markeredgecolor='None', fmt='o', capsize=0, elinewidth=0.2, \
        label='Taffy-N')
    pylab.errorbar(np.arange(10), np.arange(10), xerr=np.arange(10), yerr=np.arange(10), \
        color='darkgreen', markersize=6, markeredgecolor='darkgreen', fmt='+', zorder=5, capsize=0, elinewidth=0.2, \
        label='West Taffy-N')

    pylab.errorbar(np.arange(10), np.arange(10), xerr=np.arange(10), yerr=np.arange(10), \
        color='midnightblue', markersize=3.5, markeredgecolor='None', fmt='o', capsize=0, elinewidth=0.2, \
        label='Taffy-S')
    pylab.errorbar(np.arange(10), np.arange(10), xerr=np.arange(10), yerr=np.arange(10), \
        color='midnightblue', markersize=4.5, markeredgecolor='midnightblue', fmt='d', zorder=5, capsize=0, elinewidth=0.2, \
        label='Taffy-S nuclear region')
    #pylab.errorbar(np.arange(10), np.arange(10), xerr=np.arange(10), yerr=np.arange(10), \
    #    color='None', markersize=5, markeredgecolor='limegreen', fmt='o', zorder=5, capsize=0, elinewidth=0.2, \
    #    label='Taffy-S nuclear minor axis')

    # create a second figure for the legend
    figLegend = pylab.figure()
    
    # produce a legend for the objects in the other figure
    pylab.figlegend(*ax.get_legend_handles_labels(), loc='center', frameon=False)
    
    # save the two figures to files
    figLegend.savefig(taffy_extdir + "emissionline_diagnostic_legend.png", dpi=300)

    sys.exit(0)
예제 #42
0
for k in tests:
    print k, objective(results[k]['sol'])


import pylab
lines = []
colors = ('b', 'g', 'r', 'c', 'm', 'y', 'k')
styles = ['-', ':']

count = 0
for label in tests.keys():
    count += 1
    color = colors[count % len(colors)]
    style = styles[count % len(styles)]
    res_pri = results[label]['res_pri']
    res_dual = results[label]['res_dual']
    #errs = results[label]['errs']
    pylab.subplot(2, 1, 1)
    line = pylab.semilogy(range(runs), res_pri, style, color=color)
    lines.append(line[0])
    pylab.ylabel('primal residual')

    pylab.subplot(2, 1, 2)
    pylab.semilogy(range(runs), res_dual, style, color=color)
    pylab.xlabel('iteration')
    pylab.ylabel('dual residual')

pylab.figlegend(lines, tests.keys(), loc='upper center', shadow=True,
                fancybox=True, ncol=3)
pylab.show()
예제 #43
0
    #pylab.ylabel('Correctly classified loci [%]')
    pylab.yticks(range(50,101,10), [])
    pylab.text(0.07, 1.02, 'C', transform = ax.transAxes, horizontalalignment='right', fontsize=12)
    ######### HGDP WinSize ################
    ax=pylab.subplot(1,4,4)
    with open(OUTPUT_TWO_POP_SVM_WIN,'r') as fp: g=cPickle.load(fp)
    success=np.asarray(g.success)
    lines=[]; labels=[]
    for i in range(7):
        l=pylab.semilogx(g.fst[:8], success[i*8:(i+1)*8,0], '-o', linewidth=.5, markersize=3)
        lines.append(l)
        labels.append('-'.join([l.capitalize() for l in  g.files[i*8]]))
    pylab.axis([8.5, 5500, 50, 100.5])
    pylab.xticks(np.take(g.fst, [0,1,2,3,4,6,7]), np.take(g.fst, [0,1,2,3,4,6]), fontsize=6); pylab.yticks(range(50,101,10), [])
    pylab.xlabel('Window size [# SNPs]')
    pylab.figlegend(lines, labels, "lower right", ncol=4, numpoints=1, borderaxespad=1)
    pylab.subplots_adjust(left=.078, bottom=.3, right=.98, top=.9, hspace=.19, wspace=.05)
    pylab.text(0.07, 1.02, 'D', transform = ax.transAxes, horizontalalignment='right', fontsize=12)
    pylab.savefig('fig1.'+FILETYPE,format=FILETYPE) 

    ################################
    # Supplemental Figure 1 - Structure+all chroms
    ################################
    pylab.figure(figsize=(8.8, 10.8))
    ####### SupportMix Plots ############x##    
    startPos=.95
    hSpace=0.001
    for i in range(1,23):
        CHR='chr%i' %i
        chrIdx=qatarPositions[:,0]==CHR
        height=.77*sum(chrIdx)/nQatarWins
예제 #44
0
def barplot_neighbors(Nrange=2 ** np.arange(1, 11),
                      Drange=2 ** np.arange(7),
                      krange=2 ** np.arange(10),
                      N=1000,
                      D=64,
                      k=5,
                      leaf_size=30,
                      dataset='digits'):
    algorithms = ('kd_tree', 'brute', 'ball_tree')
    fiducial_values = {'N': N,
                       'D': D,
                       'k': k}

    #------------------------------------------------------------
    # varying N
    N_results_build = dict([(alg, np.zeros(len(Nrange)))
                            for alg in algorithms])
    N_results_query = dict([(alg, np.zeros(len(Nrange)))
                            for alg in algorithms])

    for i, NN in enumerate(Nrange):
        print("N = %i (%i out of %i)" % (NN, i + 1, len(Nrange)))
        X = get_data(NN, D, dataset)
        for algorithm in algorithms:
            nbrs = neighbors.NearestNeighbors(n_neighbors=min(NN, k),
                                              algorithm=algorithm,
                                              leaf_size=leaf_size)
            t0 = time()
            nbrs.fit(X)
            t1 = time()
            nbrs.kneighbors(X)
            t2 = time()

            N_results_build[algorithm][i] = (t1 - t0)
            N_results_query[algorithm][i] = (t2 - t1)

    #------------------------------------------------------------
    # varying D
    D_results_build = dict([(alg, np.zeros(len(Drange)))
                            for alg in algorithms])
    D_results_query = dict([(alg, np.zeros(len(Drange)))
                            for alg in algorithms])

    for i, DD in enumerate(Drange):
        print("D = %i (%i out of %i)" % (DD, i + 1, len(Drange)))
        X = get_data(N, DD, dataset)
        for algorithm in algorithms:
            nbrs = neighbors.NearestNeighbors(n_neighbors=k,
                                              algorithm=algorithm,
                                              leaf_size=leaf_size)
            t0 = time()
            nbrs.fit(X)
            t1 = time()
            nbrs.kneighbors(X)
            t2 = time()

            D_results_build[algorithm][i] = (t1 - t0)
            D_results_query[algorithm][i] = (t2 - t1)

    #------------------------------------------------------------
    # varying k
    k_results_build = dict([(alg, np.zeros(len(krange)))
                            for alg in algorithms])
    k_results_query = dict([(alg, np.zeros(len(krange)))
                            for alg in algorithms])

    X = get_data(N, DD, dataset)

    for i, kk in enumerate(krange):
        print("k = %i (%i out of %i)" % (kk, i + 1, len(krange)))
        for algorithm in algorithms:
            nbrs = neighbors.NearestNeighbors(n_neighbors=kk,
                                              algorithm=algorithm,
                                              leaf_size=leaf_size)
            t0 = time()
            nbrs.fit(X)
            t1 = time()
            nbrs.kneighbors(X)
            t2 = time()

            k_results_build[algorithm][i] = (t1 - t0)
            k_results_query[algorithm][i] = (t2 - t1)

    pl.figure(figsize=(8, 11))

    for (sbplt, vals, quantity,
         build_time, query_time) in [(311, Nrange, 'N',
                                      N_results_build,
                                      N_results_query),
                                     (312, Drange, 'D',
                                      D_results_build,
                                      D_results_query),
                                     (313, krange, 'k',
                                      k_results_build,
                                      k_results_query)]:
        ax = pl.subplot(sbplt, yscale='log')
        pl.grid(True)

        tick_vals = []
        tick_labels = []

        bottom = 10 ** np.min([min(np.floor(np.log10(build_time[alg])))
                               for alg in algorithms])

        for i, alg in enumerate(algorithms):
            xvals = 0.1 + i * (1 + len(vals)) + np.arange(len(vals))
            width = 0.8

            c_bar = pl.bar(xvals, build_time[alg] - bottom,
                           width, bottom, color='r')
            q_bar = pl.bar(xvals, query_time[alg],
                           width, build_time[alg], color='b')

            tick_vals += list(xvals + 0.5 * width)
            tick_labels += ['%i' % val for val in vals]

            pl.text((i + 0.02) / len(algorithms), 0.98, alg,
                    transform=ax.transAxes,
                    ha='left',
                    va='top',
                    bbox=dict(facecolor='w', edgecolor='w', alpha=0.5))

            pl.ylabel('Time (s)')

        ax.xaxis.set_major_locator(ticker.FixedLocator(tick_vals))
        ax.xaxis.set_major_formatter(ticker.FixedFormatter(tick_labels))

        for label in ax.get_xticklabels():
            label.set_rotation(-90)
            label.set_fontsize(10)

        title_string = 'Varying %s' % quantity

        descr_string = ''

        for s in 'NDk':
            if s == quantity:
                pass
            else:
                descr_string += '%s = %i, ' % (s, fiducial_values[s])

        descr_string = descr_string[:-2]

        pl.text(1.01, 0.5, title_string,
                transform=ax.transAxes, rotation=-90,
                ha='left', va='center', fontsize=20)

        pl.text(0.99, 0.5, descr_string,
                transform=ax.transAxes, rotation=-90,
                ha='right', va='center')

        pl.gcf().suptitle("%s data set" % dataset.capitalize(), fontsize=16)

    pl.figlegend((c_bar, q_bar), ('construction', 'N-point query'),
                 'upper right')
예제 #45
0
def stage3(cat=None,
           variances=None,
           T=None,
           bands=None,
           ps=None,
           targetwcs=None,
           T2=None,
           M=None,
           **kwargs):

    ccmap = dict(g='g', r='r', i='m')

    for band in bands:
        mag = M.get('mag_%s' % band)
        # sdss
        smag = M.get('smag_%s' % band)
        ok = np.flatnonzero(mag != smag)
        mag = mag[ok]
        smag = smag[ok]

        cc = ccmap[band]
        plt.clf()
        plt.subplot(2, 1, 1)
        plt.plot(smag, mag, '.', color=cc, alpha=0.5)
        lo, hi = 16, 24
        plt.plot([lo, hi], [lo, hi], 'k-')
        plt.xlabel('SDSS mag')
        plt.ylabel('CFHT mag')
        plt.axis([lo, hi, lo, hi])

        plt.subplot(2, 1, 2)
        plt.plot(smag, mag - smag, '.', color=cc, alpha=0.5)
        lo, hi = 16, 24
        plt.plot([lo, hi], [0, 0], 'k-')
        plt.xlabel('SDSS mag')
        plt.ylabel('CFHT - SDSS mag')
        plt.axis([lo, hi, -1, 1])

        plt.suptitle('%s band' % band)
        ps.savefig()

    plt.clf()
    lp, lt = [], []
    for band in bands:
        sn = (T2.get('decam_%s_nanomaggies' % band) *
              np.sqrt(T2.get('decam_%s_nanomaggies_invvar' % band)))
        mag = T2.get('decam_%s_mag' % band)
        cc = ccmap[band]
        p = plt.semilogy(mag, sn, '.', color=cc, alpha=0.5)
        lp.append(p[0])
        lt.append('%s band' % band)
    plt.xlabel('mag')
    plt.ylabel('Flux Signal-to-Noise')
    #tt = [1,2,3,4,5,10,20,30,40,50]
    tt = [
        1, 3, 5, 10, 30, 50, 100, 300, 500, 1000, 3000, 5000, 10000, 30000,
        50000
    ]
    plt.yticks(tt, ['%i' % t for t in tt])
    plt.axhline(5., color='k')
    #plt.axis([21, 26, 1, 20])
    plt.legend(lp, lt, loc='upper right')
    plt.title('CFHT depth')
    lo, hi = 16, 27
    plt.xlim(lo, hi)
    ps.savefig()

    # zps = {}
    # plt.clf()
    # lp,lt = [],[]
    # for band in bands:
    #     sn = (T2.get('decam_%s_nanomaggies' % band) *
    #           np.sqrt(T2.get('decam_%s_nanomaggies_invvar' % band)))
    #     mag = T2.get('decam_%s_mag' % band)
    #     cc = ccmap[band]
    #     I = (np.isfinite(mag) * (T2.type == 'S') * np.isfinite(sn))
    #     Ti = T2[I]
    #     Ti.mag = mag[I]
    #     Ti.sn = sn[I]
    #     zps[band] = np.median(np.log10(Ti.sn) + 0.4*Ti.mag)
    #     plt.plot(Ti.mag, np.log10(Ti.sn) + 0.4*Ti.mag, '.', color=cc)
    # ps.savefig()

    metadata = dict(
        g=(1080946, 634),
        r=(1617879, 445),
        i=(1080306, 411),
    )

    zps = {}
    lo, hi = 16, 27
    plt.clf()
    lp, lt = [], []
    for band in bands:
        sn = (T2.get('decam_%s_nanomaggies' % band) *
              np.sqrt(T2.get('decam_%s_nanomaggies_invvar' % band)))
        mag = T2.get('decam_%s_mag' % band)
        cc = ccmap[band]
        I = (np.isfinite(mag) * (T2.type == 'S') * np.isfinite(sn))
        Ti = T2[I]
        Ti.mag = mag[I]
        Ti.sn = sn[I]

        zp = np.median(np.log10(Ti.sn) + 0.4 * Ti.mag)
        zps[band] = zp
        print('zp', zp)
        xx = np.array([lo, hi])
        plt.plot(xx, 10.**(zp - 0.4 * xx), '-', alpha=0.4, color=cc)
        plt.plot(xx, 10.**(zp - 0.4 * xx), 'k-', alpha=0.4)

        p = plt.semilogy(Ti.mag, Ti.sn, '.', color=cc, alpha=0.5)

        (expnum, exptime) = metadata[band]
        depth = (zp - np.log10(5.)) / 0.4
        lp.append(p[0])
        lt.append('%s band: exptime %i, depth %.2f (exp %i)' %
                  (band, exptime, depth, expnum))
        plt.plot([depth, depth], [1, 5], color=cc, alpha=0.5)

    plt.xlabel('mag')
    plt.ylabel('Flux Signal-to-Noise')
    #tt = [1,2,3,4,5,10,20,30,40,50]
    tt = [
        1, 3, 5, 10, 30, 50, 100, 300, 500, 1000, 3000, 5000, 10000, 30000,
        50000
    ]
    plt.yticks(tt, ['%i' % t for t in tt])
    plt.axhline(5., color='k')
    #plt.axis([21, 26, 1, 20])
    plt.xlim(lo, hi)
    #ylo,yhi = plt.ylim()
    #plt.ylim(1, yhi)
    plt.ylim(1, 1e5)
    plt.legend(lp, lt, loc='upper right')
    plt.title('CFHT depth (point sources)')
    ps.savefig()

    plt.clf()
    lp, lt = [], []
    for band in bands:
        sn = (T2.get('decam_%s_nanomaggies' % band) *
              np.sqrt(T2.get('decam_%s_nanomaggies_invvar' % band)))
        mag = T2.get('decam_%s_mag' % band)
        cc = ccmap[band]
        I = (np.isfinite(mag) * (T2.type != 'S') * np.isfinite(sn))
        Ti = T2[I]
        Ti.mag = mag[I]
        Ti.sn = sn[I]
        xx = np.array([lo, hi])
        zp = zps[band]
        plt.plot(xx, 10.**(zp - 0.4 * xx), '-', alpha=0.4, color=cc)
        plt.plot(xx, 10.**(zp - 0.4 * xx), 'k-', alpha=0.4)

        p = plt.semilogy(Ti.mag, Ti.sn, '.', color=cc, alpha=0.5)

        lp.append(p[0])
        lt.append('%s band' % band)

    plt.xlabel('mag')
    plt.ylabel('Flux Signal-to-Noise')
    tt = [
        1, 3, 5, 10, 30, 50, 100, 300, 500, 1000, 3000, 5000, 10000, 30000,
        50000
    ]
    plt.yticks(tt, ['%i' % t for t in tt])
    plt.axhline(5., color='k')
    plt.xlim(lo, hi)
    # ylo,yhi = plt.ylim()
    # plt.ylim(1, yhi)
    plt.ylim(1, 1e5)
    plt.legend(lp, lt, loc='upper right')
    plt.title('CFHT depth (extended sources)')
    ps.savefig()

    plt.subplots_adjust(hspace=0.)
    plt.clf()
    lp, lt = [], []
    for i, band in enumerate(bands):
        plt.subplot(3, 1, i + 1)
        sn = (T2.get('decam_%s_nanomaggies' % band) *
              np.sqrt(T2.get('decam_%s_nanomaggies_invvar' % band)))
        mag = T2.get('decam_%s_mag' % band)
        cc = ccmap[band]
        I = (np.isfinite(mag) * (T2.type != 'S') * np.isfinite(sn))
        Ti = T2[I]
        Ti.mag = mag[I]
        Ti.sn = sn[I]
        xx = np.array([lo, hi])
        zp = zps[band]
        plt.plot(xx, [1., 1.], '-', alpha=0.4, color=cc)
        plt.plot(xx, [1., 1.], 'k-', alpha=0.4)

        plt.plot(xx, [0.4, 0.4], 'k-', alpha=0.1)
        plt.plot(xx, [0.16, 0.16], 'k-', alpha=0.1)

        p = plt.semilogy(Ti.mag,
                         Ti.sn / 10.**(zp - 0.4 * Ti.mag),
                         '.',
                         color=cc,
                         alpha=0.5)
        lp.append(p[0])
        lt.append('%s band' % band)

        if i == 1:
            plt.ylabel('Flux Signal-to-Noise vs Point Source')
        if i != 2:
            plt.xticks([])

        plt.ylim(0.08, 1.2)
        plt.xlim(lo, hi)

    plt.xlabel('mag')
    #tt = [1,3,5,10,30,50,100,300,500,1000,3000,5000,10000,30000,50000]
    #plt.yticks(tt, ['%i' % t for t in tt])
    plt.figlegend(lp, lt, loc='upper right')
    plt.suptitle('CFHT depth (extended sources)')
    ps.savefig()

    plt.clf()
    lp, lt = [], []
    for i, band in enumerate(bands):
        sn = (T2.get('decam_%s_nanomaggies' % band) *
              np.sqrt(T2.get('decam_%s_nanomaggies_invvar' % band)))
        mag = T2.get('decam_%s_mag' % band)
        cc = ccmap[band]
        I = (np.isfinite(mag) * (T2.type != 'S') * np.isfinite(sn))
        Ti = T2[I]
        Ti.mag = mag[I]
        Ti.sn = sn[I]
        zp = zps[band]
        snloss = Ti.sn / 10.**(zp - 0.4 * Ti.mag)

        for J, name, style, deV in [
            (np.flatnonzero(Ti.type == 'D'), 'deV', 'x', True),
            (np.flatnonzero(Ti.type == 'E'), 'exp', 'o', False),
            (np.flatnonzero(
                (Ti.type == 'C') * (Ti.fracDev > 0.5)), 'comp/deV', 's', True),
            (np.flatnonzero((Ti.type == 'C') * (Ti.fracDev <= 0.5)),
             'comp/exp', '^', False),
        ]:
            print(len(J), name)
            if deV:
                size = Ti.shapeDev[:, 0]
            else:
                size = Ti.shapeExp[:, 0]

            ylo = 5e-2
            p = plt.loglog(np.clip(size[J], 1e-2, 1e3),
                           np.clip(snloss[J], ylo, 10.),
                           style,
                           color=cc,
                           mfc='none',
                           mec=cc,
                           alpha=0.5)

            if i == 0:
                lp.append(p[0])
                lt.append(name)

    plt.axhline(0.4**0, color='k', alpha=0.1)
    plt.axhline(0.4**1, color='k', alpha=0.1)
    plt.axhline(0.4**2, color='k', alpha=0.1)
    plt.axhline(0.4**3, color='k', alpha=0.1)

    plt.xlim(0.9 * 1e-2, 1.1 * 1e3)
    plt.ylim(0.9 * ylo, 1.2)
    plt.xlabel('Galaxy effective radius (arcsec)')
    plt.ylabel('S/N loss vs point source')
    plt.legend(lp, lt, loc='upper right')
    plt.title('CFHT extended sources: S/N vs size')
    ps.savefig()
예제 #46
0
    def plot_multiple(pairs_names,
                      num_instances,
                      performances_array,
                      y_title,
                      project_name,
                      dir_path,
                      file_name,
                      y_lim,
                      b_anch,
                      legend_loc,
                      col_num,
                      zip_size,
                      color_set,
                      z_orders,
                      step=1,
                      print_legend=True,
                      datasetName=None,
                      dataName=None):

        # x = []
        # y = []
        # for i in range(0, len(pairs_names)):
        #     y.append([])
        # for i in range(0, num_instances):
        #     if i % zip_size == 0 or i == num_instances - 1:
        #         x.append((i / num_instances) * 100)
        #         for j in range(0, len(pairs_names)):
        #             y[j].append(performances_array[j][i])

        fig = plt.figure()

        ax = fig.add_subplot(111)

        # LaTeX rendering case. You may use the next line if you have LaTeX installed.
        # ax.set_title(r'\textsc{' + project_name.title() + '}', fontsize=14)
        ax.set_title(project_name.title(), fontsize=14)

        # ax.set_xlim(0, 100)
        # if y_lim is not None:
        #     ax.set_ylim(y_lim[0], y_lim[1])
        # else:
        #     y1_y2 = performances_array[0] + performances_array[1]
        #     y_lim = [min(y1_y2)-1.0, 1.05]
        #     print(y_lim)
        #     ax.set_ylim(y_lim[0], y_lim[1])
        if y_lim is not None:
            ax.set_ylim(y_lim[0], y_lim[1])
        else:
            interval = 0.1
            top = 1.05
            bottom = -0.05
            if datasetName == 'prsa_data':
                interval = 0.01
                top = 1.01
                bottom = 0.79
            elif datasetName == 'movie_data':
                if dataName == 'movie_lens':
                    interval = 0.1
                    top = 1.05
                    bottom = 0.29
                elif dataName == 'netflix':
                    interval = 0.1
                    top = 1.05
                    bottom = 0.45
                elif dataName == 'rotten_tomatoes':
                    interval = 0.05
                    top = 1.04
                    bottom = 0.66
                elif dataName == 'twitter':
                    interval = 0.05
                    top = 1.04
                    bottom = 0.56
            y_major_locator = MultipleLocator(interval)
            ax.yaxis.set_major_locator(y_major_locator)
            ax.set_ylim(bottom, top)

        ax.set_ylabel(y_title, fontsize=14)
        ax.set_xlabel("Step = {}".format(step), fontsize=14)
        ax.grid()

        for i in range(0, len(pairs_names)):
            ax.plot([i for i in range(len(performances_array[i]))],
                    performances_array[i],
                    label=pairs_names[i],
                    color=color_set[i],
                    linewidth=1,
                    zorder=z_orders[i])

        # LaTeX rendering case. You may use the next line if you have LaTeX installed.
        # ax.xaxis.set_major_formatter(FuncFormatter(lambda ix, _: '%1.0f' % ix + '\%'))
        # ax.xaxis.set_major_formatter(FuncFormatter(lambda ix, _: '%1.0f' % ix + '%'))

        file_path = (dir_path + file_name + "_" + y_title).lower()

        if print_legend is True:
            leg = ax.legend(bbox_to_anchor=b_anch,
                            loc=legend_loc,
                            ncol=col_num,
                            fontsize=8,
                            framealpha=1)
            for leg_obj in leg.legendHandles:
                leg_obj.set_linewidth(1)
            fig.savefig(file_path + ".pdf", dpi=150, bbox_inches='tight')
            fig.savefig(file_path + ".png", dpi=150, bbox_inches='tight')
        else:
            fig_leg = pylab.figure(figsize=(13.5, 3.5), dpi=150)
            leg = pylab.figlegend(*ax.get_legend_handles_labels(),
                                  loc='center',
                                  ncol=col_num,
                                  fontsize=14,
                                  framealpha=1)
            for leg_obj in leg.legendHandles:
                leg_obj.set_linewidth(3.0)
            fig_leg.savefig(file_path + "_legend.pdf", dpi=150)
            fig_leg.savefig(file_path + "_legend.png", dpi=150)
            fig.savefig(file_path + ".pdf", dpi=150, bbox_inches='tight')
            fig.savefig(file_path + ".png", dpi=150, bbox_inches='tight')
예제 #47
0
파일: plot_icd_z_box.py 프로젝트: boada/ICD
def plot_icd_vs_mass():
    galaxies = pickle.load(open('galaxies.pickle','rb'))

    arrow_up = [[0,0], [-1,-1], [0,0], [0,-2], [0,0], [1,-1], [0,0]]

    # Make figure
    f1 = pyl.figure(1, figsize=(6,4))
    f1s1 = f1.add_subplot(221)
    f1s2 = f1.add_subplot(222)
    f1s3 = f1.add_subplot(223)
    f1s4 = f1.add_subplot(224)

    for galaxy in galaxies:
        if galaxy.ston_I > 30. and galaxy.ICD_IH != None:
            if galaxy.ICD_IH*100 < 50:
                f1s1.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8',
                    marker='o', s=25, edgecolor='0.8')
            else:
                f1s1.scatter(galaxy.z, 50, s=100, marker=None, verts=arrow_up)

            f1s2.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8',
                marker='o', s=25, edgecolor='0.8')

        if galaxy.ston_V > 30. and galaxy.ICD_VJ != None:
            f1s3.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8',
                marker='o', s=25, edgecolor='0.8')
            f1s4.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8',
                marker='o', s=25, edgecolor='0.8')

    # Add the box and whiskers
    galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.z for galaxy in galaxies2]
    ll = 1.5
    ul= 4
    bins_x =pyl.arange(ll, ul, 0.5)
    grid = []

    for i in range(bins_x.size-1):
        xmin = bins_x[i]
        xmax = bins_x[i+1]
        cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]])
    from boxplot_percentile import percentile_box_plot as pbp
    pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))
    pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))

    # Add the box and whiskers
    galaxies = pickle.load(open('galaxies.pickle','rb'))
    galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.z for galaxy in galaxies2]
    ll = 1.5
    ul= 4
    bins_x =pyl.arange(ll, ul, 0.5)
    grid = []

    for i in range(bins_x.size-1):
        xmin = bins_x[i]
        xmax = bins_x[i+1]
        cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_VJ*100 for galaxy in grid[i]])
    pbp(f1s3, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))
    pbp(f1s4, icd, indexer=list(pyl.delete(bins_x,-1)+0.25))

    f1s1.set_xlim(1.5, 3.5)
    f1s2.set_xlim(1.5, 3.5)
    f1s3.set_xlim(1.5, 3.5)
    f1s4.set_xlim(1.5, 3.5)

    f1s1.set_ylim(-5, 50)
    f1s2.set_ylim(-1, 10)
    f1s3.set_ylim(-5, 50)
    f1s4.set_ylim(-1, 10)

    f1s1.set_xticks([1.5,2,2.5,3,3.5])
    f1s2.set_xticks([1.5,2,2.5,3,3.5])
    f1s1.set_xticklabels([])
    f1s2.set_xticklabels([])
    f1s3.set_xticks([1.5,2,2.5,3,3.5])
    f1s4.set_xticks([1.5,2,2.5,3,3.5])

    f1s1.axhline(0.0, lw=2, c='b', zorder=0)
    f1s2.axhline(0.0, lw=2, c='b', zorder=0)
    f1s3.axhline(0.0, lw=2, c='b', zorder=0)
    f1s4.axhline(0.0, lw=2, c='b', zorder=0)

    f1s3.set_xlabel("Redshift")
    f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)")
    f1s4.set_xlabel("Redshift")
    f1s3.set_ylabel(r"$\xi[V_{606},J_{125}]$ (%)")

    import matplotlib.font_manager
    line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8,
        linewidth=0)
    line2 = pyl.Line2D([], [], marker='s', mec='blue', mfc='None',
        markersize=10, linewidth=0, markeredgewidth=2)
    line3 = pyl.Line2D([], [], color='r', linewidth=2)
    prop = matplotlib.font_manager.FontProperties(size='small')
    pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles',
        'Medians'), 'lower center', prop=prop, ncol=3)

    from matplotlib.patches import ConnectionPatch
    xy = (3.5, 10)
    xy2 = (1.5, 10)
    con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
            axesA=f1s1, axesB=f1s2)
    xy = (3.5, -1)
    xy2 = (1.5, -1)
    con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
            axesA=f1s1, axesB=f1s2)
    f1s1.add_artist(con)
    f1s1.add_artist(con2)

    xy = (3.5, 10)
    xy2 = (1.5, 10)
    con = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
            axesA=f1s3, axesB=f1s4)
    xy = (3.5, -1)
    xy2 = (1.5, -1)
    con2 = ConnectionPatch(xyA=xy, xyB=xy2, coordsA='data', coordsB='data',
            axesA=f1s3, axesB=f1s4)
    f1s3.add_artist(con)
    f1s3.add_artist(con2)

    pyl.draw()
    pyl.show()
예제 #48
0
def plot_fingerprint(data, alpha=0.7, \
    show_legend=True, graph_name='fingerprint.png', has_mean=True,
    which_blocks='quartets', multiple=False, graph_grid='t', prob_axes=True, \
    edge_colors='k', **kwargs):
    """Outputs a bubble plot of four-codon amino acid blocks
    labeled with the colors from Sueoka 2002.

    takes: data:  array-elements in the col order x, y, r of
           each of the four codon Amino Acids in the row order:
           ALA, ARG4, GLY, LEU4, PRO, SER, THR, VAL
           (for traditional fingerprint), or:
           UU -> GG (for 16-block fingerprint).
           last row is the mean (if has_mean is set True)

        **kwargs passed on to init_graph_display (these include 
        graph_shape, graph_grid, x_label, y_label, dark, with_parens).
                 
           title: will be printed on graph (default: 'Unknown Species')
           
           num_genes (number of genes contributing to graph: default None)
           NOTE: will not print if None.)
        
           size: of graph in inches (default = 8.0)

           alpha: transparency of bubbles
           (ranges from 0, transparent, to 1, opaque; default 0.7)
           
           show_legend: bool, default True, whether to print legend

           graph_name: name of file to write (default 'fingerprint.png')

           has_mean: whether the data contain the mean (default: True)

           which_blocks: which codon blocks to print (default is 'quartets'
           for the 4-codon amino acid blocks, but can also use 'all' for all 
           quartets or 'split' for just the split quartets.)

           multiple: if False (the default), assumes it got a single block
           of data. Otherwise, assumes multiple blocks of data in a list or
           array.

           edge_colors: if multiple is True (ignored otherwise), uses this
           sequence of edge color strings to hand out edge colors to successive
           series. Will iterate over this, so can be a string of 1-letter
           color codes or a list of color names.

    note: that the data are always expected to be in the range (0,1)
    since we're plotting frequencies. axes, gid, etc. are hard-coded
    to these values. 
    """
    #figure out which type of fingerprint plot we're doing, and get the
    #right colors
    if which_blocks == 'quartets':
        blocks = CodonUsage.SingleAABlocks
    elif which_blocks == 'split':
        blocks = CodonUsage.SplitBlocks
    else:
        blocks = CodonUsage.Blocks

    colors = [doublets_to_colors[i] for i in blocks]
      
    #formatting the labels in latex
    x_label="$G_3/(G_3+C_3)$"
    y_label="$A_3/(A_3+T_3)$"

    #initializing components of the graph
    font,label_font_size=init_graph_display(graph_shape='sqr', \
        graph_grid=graph_grid, x_label=x_label, \
        y_label=y_label, prob_axes=prob_axes, **kwargs)

    if not multiple:
        data = [data]
 
    alpha = broadcast(alpha, len(data))
    edge_colors = broadcast(edge_colors, len(data))
  
    for al, d, edge_color in zip(alpha, data, edge_colors):
        #skip this series if no data
        if d is None or not d.any():
            continue
        for i, color in enumerate(colors):
            j = i+1
            #note: doing these as slices because scatter_classic needs the
            #extra level of nesting
            patches = scatter_classic(d[i:j,0], d[i:j,1],
                        s=(d[i:j,2]/2), c=color)
            #set alpha for the patches manually
            for p in patches:
                p.set_alpha(al)
                p.set_edgecolor(edge_color)
        
        #plot mean as its own point -- can't do cross with scatter
        if has_mean:
            mean_index = len(blocks)    #next index after the blocks
            plot([d[mean_index,0]], [d[mean_index,1]],
                 '-k+',markersize=label_font_size, alpha=al)
               

    abbrev = CodonUsage.BlockAbbreviations

    a = gca()
    #if show_legend is True prints a legend in the right center area
    if show_legend:
        legend_key = [abbrev[b] for b in blocks]
        #copy legend font properties from the x axis tick labels
        legend_font_props = \
            a.xaxis.get_label().get_fontproperties().copy()
        legend_font_scale_factor = 0.7
        curr_size = legend_font_props.get_size()
        legend_font_props.set_size(curr_size*legend_font_scale_factor)
        l = figlegend(a.patches[:len(blocks)],
                  legend_key,
                  prop=legend_font_props,
                  loc='center right',borderpad=0.1,labelspacing=0.5,
                  handlelength=1.0,handletextpad=0.5, borderaxespad=0.0)
        #fix transparency of patches
        for p in l.get_patches():
            p.set_alpha(1)

    #initialize the ticks
    set_axis_to_probs()
    init_ticks(a, label_font_size)
    a.set_xticks([0, 0.5, 1])
    a.set_yticks([0,0.5,1])
    
    #output the figure
    if graph_name is not None:
        savefig(graph_name)
예제 #49
0
    trnSequenceOutput = net.extrapolate(trnSequenceWashout,
                                        len(trnSequenceTarget))
    tstSequenceOutput = net.extrapolate(tstSequenceWashout,
                                        len(tstSequenceTarget))

    # plot training data
    sp = subplot(211)  # switch to the first subplot
    cla()  # clear the subplot
    title("Training Set")  # set the subplot's title
    sp.set_autoscale_on(True)  # enable autoscaling
    targetline = plot(trnSequenceTarget, "r-")  # plot the targets
    sp.set_autoscale_on(False)  # disable autoscaling
    outputline = plot(trnSequenceOutput, "b-")  # plot the actual output

    # plot test data
    sp = subplot(212)
    cla()
    title("Test Set")
    sp.set_autoscale_on(True)
    plot(tstSequenceTarget, "r-")
    sp.set_autoscale_on(False)
    plot(tstSequenceOutput, "b-")

    # create a legend
    figlegend((targetline, outputline), ('target', 'output'), ('upper right'))

    # draw everything
    draw()

show()
예제 #50
0
        fake_vms_end_line = lines.Line2D([jvmBootDays,0], [jvmBootDays,0], label='VMS cache refresh end', color='c')
        ax.add_line(fake_vms_start_line)
        ax.add_line(fake_vms_end_line)

try_to_draw_vms_cache_refresh_lines()

# various chart options
smallEnvDict = getSmallEnvDict()
ax.set_title('gc events over time for %s %s %s %s %s %s' % (smallEnvDict['appname'], smallEnvDict['env'], smallEnvDict['ec2PublicHostname'], smallEnvDict['instanceID'], smallEnvDict['instanceType'], smallEnvDict['asg']))
ax.grid(True)
ax.set_xlabel('gc event start timestamp')
ax.set_ylabel('gc event duration (seconds)')
fig.autofmt_xdate()
plt.ylim([0,maxSTWGCEventDuration])
fig.set_size_inches(21,12)
pylab.figlegend(*ax.get_legend_handles_labels(), loc='lower center', ncol=5)

# BUG: add sar charts, including but not limited to cpu, network
# BUG: plot secs since jvm start
# BUG: visualize the facet data collected in vms-cache-refresh-facet-info.csv. Maybe leave this up to visualize-instance.

savedImageBaseFileName = vmsGCReportDirectory + os.path.sep + smallEnvDict['appname'] + '-gc-events-' + now
savedIamgeFileName = savedImageBaseFileName + '.png'
plt.savefig(savedIamgeFileName)
print "output on %s" % (savedIamgeFileName,)
savedIamgeFileName = savedImageBaseFileName + '.pdf'
plt.savefig(savedIamgeFileName)
print "output on %s" % (savedIamgeFileName,)

# Potential BUG: do we want to issue the plt.show()
plt.show()
예제 #51
0
    pylab.xlabel(xlabel(tex(r'[112]') + ' tilt angle ' + tex(r'\theta')),
                 labelpad=xlabelpad)
    pylab.ylabel(ylabel("Energy " + tex("(J/m^2)")), labelpad=ylabelpad)
offset = 0.9
if args.annotate:
    pylab.annotate(annotate(tex(r'(1\bar{1}0)(1\bar{1}0)')),
                   xycoords=('data', 'axes fraction'),
                   xy=(0 + offset, 1.05),
                   va="bottom",
                   ha="center",
                   rotation=90)

handles, labels = ax.get_legend_handles_labels()
if not args.labels_off:
    if unrelaxed:
        lgd = pylab.figlegend(handles, labels, loc=legend_loc, ncol=2)
    else:
        lgd = pylab.figlegend(handles, labels, loc=legend_loc, ncol=3)

pylab.tight_layout()

if args.annotate:
    pylab.subplots_adjust(bottom=0.175,
                          left=0.075,
                          right=0.98,
                          top=0.875,
                          wspace=0.2,
                          hspace=0.75)
else:
    pylab.subplots_adjust(bottom=0.2,
                          left=0.075,
예제 #52
0
def plot_icd_vs_mass():
    galaxies = pickle.load(open('galaxies.pickle','rb'))

    arrow_up = [[0,0], [-1,-1], [0,0], [0,-2], [0,0], [1,-1], [0,0]]

    # Make figure
    f1 = pyl.figure(1, figsize=(4,6))
    f1s1 = f1.add_subplot(211)
    f1s2 = f1.add_subplot(212)

    for galaxy in galaxies:
        if galaxy.ston_I > 30. and galaxy.ICD_IH != None:
            f1s1.scatter(galaxy.z, galaxy.ICD_IH * 100, c='0.8',
                marker='o', s=25, edgecolor='0.8')

        if galaxy.ston_V > 30. and galaxy.ICD_VJ != None:
            f1s2.scatter(galaxy.z, galaxy.ICD_VJ * 100, c='0.8',
                marker='o', s=25, edgecolor='0.8')

    # Add the box and whiskers
    galaxies2 = filter(lambda galaxy: galaxy.ston_I > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.z for galaxy in galaxies2]
    ll = 1.5
    ul= 4
    bins_x =pyl.arange(ll, ul, 0.5)
    grid = []

    for i in range(bins_x.size-1):
        xmin = bins_x[i]
        xmax = bins_x[i+1]
        cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_IH*100 for galaxy in grid[i]])
    from boxplot_percentile import percentile_box_plot as pbp
    pbp(f1s1, icd, indexer=list(pyl.delete(bins_x,-1)+0.25), whisker_top=None,
            whisker_bottom=None)

    # Add the box and whiskers
    galaxies = pickle.load(open('galaxies.pickle','rb'))
    galaxies2 = filter(lambda galaxy: galaxy.ston_V > 30., galaxies)
    galaxies2 = pyl.asarray(galaxies2)
    x = [galaxy.z for galaxy in galaxies2]
    ll = 1.5
    ul= 4
    bins_x =pyl.arange(ll, ul, 0.5)
    grid = []

    for i in range(bins_x.size-1):
        xmin = bins_x[i]
        xmax = bins_x[i+1]
        cond=[cond1 and cond2 for cond1, cond2 in zip(x>=xmin, x<xmax)]

        grid.append(galaxies2.compress(cond))
    icd = []
    for i in range(len(grid)):
        icd.append([galaxy.ICD_VJ*100 for galaxy in grid[i]])
    pbp(f1s2, icd, indexer=list(pyl.delete(bins_x,-1)+0.25), whisker_top=None,
            whisker_bottom=None)

    f1s1.set_xlim(1.5, 3.5)
    f1s2.set_xlim(1.5, 3.5)

    f1s1.set_ylim(-1, 10)
    f1s2.set_ylim(-1, 10)

    f1s1.set_xticklabels([])
    f1s2.set_xticks([1.5,2,2.5,3,3.5])

    f1s1.set_ylabel(r"$\xi[i_{775},H_{160}]$ (%)")
    f1s2.set_xlabel("Redshift")
    f1s2.set_ylabel(r"$\xi[V_{606},J_{125}]$ (%)")

    import matplotlib.font_manager
    line1 = pyl.Line2D([], [], marker='o', mfc='0.8', mec='0.8', markersize=8,
        linewidth=0)
    line2 = pyl.Line2D([], [], marker='s', mec='#348ABD', mfc='None',
        markersize=10, linewidth=0, markeredgewidth=2)
    line3 = pyl.Line2D([], [], color='#A60628', linewidth=2)
    prop = matplotlib.font_manager.FontProperties(size='small')
    pyl.figlegend((line1, line2, line3), ('Data', 'Quartiles',
        'Medians'), 'lower center', prop=prop, ncol=3)

    pyl.show()
예제 #53
0
         qdo.Task.SUCCEEDED: 'g',
         qdo.Task.FAILED: 'r',
}

for state in [qdo.Task.WAITING, qdo.Task.SUCCEEDED, qdo.Task.PENDING,
              qdo.Task.RUNNING, qdo.Task.FAILED]:
    if not state in state_radec:
        continue
    ra,dec = state_radec[state]

    p = plt.plot(ra, dec, '.', color=cmap.get(state, 'y'))
    lp.append(p[0])
    lt.append(state)

plt.xlim([0, 360])
plt.figlegend(lp, lt, 'upper left')
plt.savefig('status.png')

sys.exit(0)


allra = []
alldec = []
allstate = []
alltasks = []

#     allra.append(ra)
#     alldec.append(dec)
#     allstate.append([state] * len(ra))
#     alltasks.append(tasks)
예제 #54
0
    def __doPlot(self):
        if len(self.groupedPlots) + len(self.plots) == 0:
            print("PlotLayout.plot(): No data to plot!")
            return

        if self.rcParams is not None:
            pylab.rcParams.update(self.rcParams)

        groupedPlotLengths = [
            len(plots) for plots in self.groupedPlots.values()
        ]

        if len(groupedPlotLengths) == 0:
            maxRowLength = self.width
        else:
            maxRowLength = max(groupedPlotLengths)

        numPlots = len(self.plots)

        if numPlots == 0:
            numExcessRows = 0
        elif numPlots <= maxRowLength:
            numExcessRows = 1
        elif numPlots % maxRowLength == 0:
            numExcessRows = numPlots / maxRowLength
        else:
            numExcessRows = (numPlots / maxRowLength) + 1

        numRows = len(self.groupedPlots.keys()) + numExcessRows

        if self.groupOrder is not None:
            keyList = self.groupOrder
        else:
            keyList = self.groupedPlots.keys()

        currentRow = 0

        if self.figdimensions is not None:
            fig = pyplot.figure(figsize=(self.figdimensions[0],
                                         self.figdimensions[1]))
        elif self.dimensions is not None:
            fig = pyplot.figure(figsize=(self.dimensions[0] * maxRowLength,
                                         self.dimensions[1] * numRows))
        else:
            (figWidth, figHeight) = getGoldenRatioDimensions(8.0)
            figWidth *= maxRowLength
            figHeight *= numRows
            fig = pyplot.figure(figsize=(figWidth, figHeight))
            # figWidth = fig.get_figwidth()
            # print figWidth
            # print fig.get_figheight()
            # (goldenWidth, goldenHeight) = getGoldenRatioDimensions(figWidth)
            # fig.set_figheight(goldenHeight)
            # print fig.get_figheight()

        plotHandles = []
        plotLabels = []

        for grouping in keyList:
            currentColumn = 1
            plots = self.groupedPlots[grouping]

            numPlots = len(plots)

            for plot in plots:
                myRows = numRows
                myCols = numPlots
                myPos = (currentRow * numPlots) + currentColumn

                (currPlotHandles,
                 currPlotLabels) = plot.subplot(myRows, myCols, myPos)

                for i in range(len(currPlotHandles)):
                    if currPlotLabels[i] in plotLabels:
                        continue

                    if isinstance(currPlotHandles[i], list):
                        plotHandles.append(currPlotHandles[i][0])
                    else:
                        plotHandles.append(currPlotHandles[i])

                    plotLabels.append(currPlotLabels[i])

#                plotHandles.extend(currPlotHandles)
#                plotLabels.extend(currPlotLabels)

                currentColumn += 1
            currentRow += 1

        ungroupedPlotsRemaining = len(self.plots)
        if ungroupedPlotsRemaining > 0:
            currentColumn = 1

            for plot in self.plots:
                if currentColumn == 1:
                    numColumns = min(maxRowLength, ungroupedPlotsRemaining)

                myRows = numRows
                myCols = numColumns
                myPos = (currentRow * numColumns) + currentColumn

                (currPlotHandles,
                 currPlotLabels) = plot.subplot(myRows, myCols, myPos)
                for i in range(len(currPlotHandles)):
                    if currPlotLabels[i] in plotLabels:
                        continue

                    if isinstance(currPlotHandles[i], list):
                        plotHandles.append(currPlotHandles[i][0])
                    else:
                        plotHandles.append(currPlotHandles[i])

                    plotLabels.append(currPlotLabels[i])

                currentColumn += 1

                if currentColumn > numColumns:
                    currentRow += 1
                    currentColumn = 1

                ungroupedPlotsRemaining -= 1

        if self.figLegendLoc is not None:
            figLegendKeywords = {}

            if self.figLegendCols is not None:
                versionPieces = [
                    int(x) for x in matplotlib.__version__.split('.')
                ]

                (superMajor, major, minor) = versionPieces[0:3]

                if superMajor == 0 and major < 98:
                    print >> sys.stderr, "Number of columns support not available in versions of matplotlib prior to 0.98"
                else:
                    figLegendKeywords["ncol"] = self.figLegendCols

            pylab.figlegend(plotHandles, plotLabels, self.figLegendLoc,
                            **figLegendKeywords)

        if self.plotParams is not None:
            pylab.subplots_adjust(left=self.plotParams["left"],
                                  bottom=self.plotParams["bottom"],
                                  right=self.plotParams["right"],
                                  top=self.plotParams["top"],
                                  wspace=self.plotParams["wspace"],
                                  hspace=self.plotParams["hspace"])
예제 #55
0
파일: plot_tb.py 프로젝트: mrahtz/dotfiles
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('log_dir', nargs='*')
    parser.add_argument('--n_cols', type=int, default=4)
    parser.add_argument('--smoothing', type=float, default=0.6)
    args = parser.parse_args()

    if not args.log_dir:
        raise Exception("Specify at least one log directory")
    for d in args.log_dir:
        if not os.path.exists(d):
            raise Exception(f"Directory {d} does not exist")

    print("Reading events...")
    events_by_log_dir = read_all_events(args.log_dir)

    i = len(os.path.commonprefix(args.log_dir))
    len_longest_suffix = len(os.path.commonprefix([s[::-1] for s in args.log_dir]))
    if len_longest_suffix > 0:
        j = -len_longest_suffix
    else:
        j = None

    print("Plotting...")
    lines, labels = [], []
    plot_n = 1
    axes = {}
    n_graphs = len(set([key for events in events_by_log_dir.values() for key in events.keys()])) + 1
    subplot_dims = (int(ceil(n_graphs / args.n_cols)), args.n_cols)
    figure(figsize=(6 * subplot_dims[1], 4 * subplot_dims[0]))
    for log_dir_n, (log_dir, events) in enumerate(events_by_log_dir.items()):
        print(log_dir)
        color = f"C{log_dir_n % 10}"
        label = log_dir[i:j]
        for key, events in events.items():
            if key not in axes:
                axes[key] = subplot(*(subplot_dims + (plot_n,)))
                plot_n += 1
            ax = axes[key]
            timestamps, values = list(zip(*events))
            relative_timestamps = [t - timestamps[0] for t in timestamps]
            relative_timestamps_hours = [t / 3600 for t in relative_timestamps]
            line = plot_values(ax, values, relative_timestamps_hours, 'Value', 'Time (hours)', key,
                               label=label, color=color, smoothing=args.smoothing)
            lines.append(line)
            labels.append(label)

    unique_lines = []
    unique_labels = []
    for line, label in zip(lines, labels):
        if label not in unique_labels:
            unique_labels.append(label)
            unique_lines.append(line)
    figlegend(unique_lines, unique_labels, loc='upper center')
    tight_layout(rect=[0, 0, 1, 0.90])

    print("Saving figure...")
    # Removing trailing slashes
    normed_log_dirs = [os.path.normpath(d) for d in args.log_dir]
    if len(args.log_dir) == 1:
        out_filename = os.path.basename(normed_log_dirs[0])
    else:
        longest_common_prefix = os.path.commonprefix(normed_log_dirs)
        longest_common_suffix = os.path.commonprefix([d[::-1] for d in normed_log_dirs])[::-1]
        out_filename = longest_common_prefix + '*' + longest_common_suffix + '.png'
    savefig(out_filename)
예제 #56
0
    
    rr,dd = wcs.pixelxy2radec(np.array([1,W,W,1,1]),
                              np.array([1,1,H,H,1]))

    print 'included:', np.sum(T.included)

    T.use = (T.use == 1)
    T.included = (T.included == 1)

    print 'included:', len(np.flatnonzero(T.included))
    
    plt.clf()
    plt.plot(T.ra, T.dec, 'r.')
    I = (T.qual_frame == 0)
    p1 = plt.plot(T.ra[I], T.dec[I], 'mx')
    p2 = plt.plot(T.ra[T.use], T.dec[T.use], 'b.')

    I = (T.npixrchi > (T.npixoverlap * 0.01))
    p3 = plt.plot(T.ra[I], T.dec[I], 'r+')

    I = T.included
    p4 = plt.plot(T.ra[I], T.dec[I], 'go')
    plt.plot(rr, dd, 'k-')

    plt.figlegend((p[0] for p in (p1,p2,p3,p4)),
                  ('Bad qual_frame', 'Used', 'Bad rchi', 'Included'),
        loc='upper right')

    ps.savefig()