def plot_response(data, plate_name, save_folder = 'Figures/'):
    """
    """
    if not os.path.isdir(save_folder):
        os.makedirs(save_folder)

    for block in data:
        #
        group = group_similar(data[block].keys())
        names = data[block].keys()
        names.sort()
        #
        plt.figure(figsize=(16, 4 + len(names)/8), dpi=300)
        #
        for i, name in enumerate(names):
            a, b, c = get_index(group, name)
            color, pattern = color_shade_pattern(a, b, c, group)
            mean = data[block][name]['mean'][0]
            std = data[block][name]['std'][0]

            plt.barh([i], [mean], height=1.0, color=color, hatch=pattern)
            plt.errorbar([mean], [i+0.5], xerr=[std], ecolor = [0,0,0], linestyle = '')

        plt.yticks([i+0.5 for i in xrange(len(names))], names, size = 8)
        plt.title(plate_name)
        plt.ylim(0, len(names))
        plt.xlabel('change')
        plt.tight_layout()

        plt.savefig(save_folder + 'response_' + str(block + 1))
    #
    return None
Esempio n. 2
0
def plot_comfort(fingers_org=range(1, 6, 1),
                 fingers_dst=range(1, 6, 1),
                 jumps=range(-12, 13, 1)):

    import seaborn
    from mpl_toolkits.mplot3d import Axes3D
    from pylab import plt

    xs, ys, zs, cs = calculate_comforts(fingers_org, fingers_dst, jumps)

    fig = plt.figure()
    ax = fig.add_subplot(111, projection='3d')
    ax.scatter(xs, ys, zs, c=cs)
    ax.set_zlabel("Interval (half steps)", fontsize=15)
    ax.set_zlim(jumps[0], jumps[-1])
    # ax.set_zticks(jumps)

    plt.xticks(fingers_org)
    plt.xlim(fingers_org[0], fingers_org[-1])
    plt.xlabel("From finger", fontsize=15)

    plt.yticks(fingers_dst)
    plt.ylim(fingers_dst[0], fingers_dst[-1])
    plt.ylabel("To finger", fontsize=15)

    plt.title("Difficulty of finger passages", fontsize=25)

    plt.savefig('./figures/image.png', figsize=(16, 12), dpi=300)
    plt.show()
Esempio n. 3
0
def serve_css(name, length, keys, values):
    from pylab import plt, mpl
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    from matplotlib.font_manager import FontProperties
    # font = FontProperties(fname="d:\Users\ll.tong\Desktop\msyh.ttf", size=12)
    font = FontProperties(fname="/usr/share/fonts/msyh.ttf", size=11)
    plt.xlabel(u'')
    plt.ylabel(u'出现次数',fontproperties=font)
    plt.title(u'词频统计',fontproperties=font)
    plt.grid()
    keys = keys.decode("utf-8").split(' ')
    values = values.split(' ')
    valuesInt = []
    for value in values:
        valuesInt.append(int(value))

    plt.xticks(range(int(length)), keys)
    plt.plot(range(int(length)), valuesInt)
    plt.xticks(rotation=defaultrotation, fontsize=9,fontproperties=font)
    plt.yticks(fontsize=10,fontproperties=font)
    name = name + str(datetime.now().date()).replace(':', '') + '.png'
    imgUrl = 'static/temp/' + name
    fig = matplotlib.pyplot.gcf()
    fig.set_size_inches(12.2, 2)
    plt.savefig(imgUrl, bbox_inches='tight', figsize=(20,4), dpi=100)
    plt.close()
    tempfile = static_file(name, root='./static/temp/')
    #os.remove(imgUrl)
    return tempfile
Esempio n. 4
0
 def _plot(self):
     p = plt.pcolor(self.matrix, cmap=self.cmap, vmin=self.vmin, vmax=self.vmax)
     plt.colorbar(p)
     plt.xlim((0, self.matrix.shape[0]))
     plt.ylim((0, self.matrix.shape[1]))
     if self.labels is not None:
         plt.xticks(numpy.arange(0.5, len(self.labels) + 0.5), self.labels, fontsize=self.fontsize, rotation=90)
         plt.yticks(numpy.arange(0.5, len(self.labels) + 0.5), self.labels, fontsize=self.fontsize)
Esempio n. 5
0
 def plot_smoothed_alpha_comparison(self,rmsval,suffix=''):
     plt.plot(self.f,self.alpha,'ko',label='data set')
     plt.plot(self.f,self.salpha,'c-',lw=2,label='smoothed angle $\phi$')
     plt.xlabel('frequency in Hz')
     plt.ylabel('angle $\phi$ in coordinates of circle')
     plt.legend()
     ylims=plt.axes().get_ylim()
     plt.yticks((arange(9)-4)*0.5*pi, ['$-2\pi$','$-3\pi/2$','$-\pi$','$-\pi/2$','$0$','$\pi/2$','$\pi$','$3\pi/2$','$2\pi$'])
     plt.ylim(ylims)
     plt.title('RMS offset from smooth curve: {:.4f}'.format(rmsval))
     if self.show: plt.show()
     else: plt.savefig(join(self.sdc.plotpath,'salpha','c{}_salpha_on_{}_circle'.format(self.sdc.case,self.ZorY)+self.sdc.suffix+self.sdc.outsuffix+suffix+'.png'), dpi=240)
     plt.close()
Esempio n. 6
0
    def run(self):

        plt.xticks([]), plt.yticks([])

        if self.mode == Mode.DISPLAY:
            self.animation = matplotlib.animation.FuncAnimation(self.fig, self.time_step, interval=60)

        elif self.mode == Mode.ON_KEY_PRESS:
            self.fig.canvas.mpl_connect('key_press_event', self.time_step)

        else:
            print("Creating video! Could need some time to complete!")

            ffmpeg_writer = matplotlib.animation.writers['ffmpeg']
            metadata = dict(title=self.video_name.split(".")[0], artist='Matplotlib',
                            comment='')
            writer = ffmpeg_writer(fps=15, metadata=metadata)

            n_frames = len(self.data)

            with writer.saving(self.fig, self.video_name, n_frames):
                for i in range(n_frames):
                    self.time_step()
                    writer.grab_frame()

        if self.mode != Mode.SAVE:
            plt.show()
def plot_baseline(data, plate_name, save_folder = r'Figures/'):
    """
    """
    colors = ((0.2, 0.2, 0.2),
              (0.5, 0.5, 0.5),
              (0.7, 0.7, 0.7),
              (0.3, 0.3, 0.3))

    names = data.keys()
    names.sort()
    fig, axs = plt.subplots(figsize=(8,3))
    for index, name in enumerate(names):
        for value in data[name]['original_data']:
            plot_color = colors[index % len(colors)]
            
            if abs(value - data[name]['mean'][0]) > data[name]['std'][0] * 2.0:
                axs.plot([value], [index], 'ko', markerfacecolor = [1,1,1])
            else:
                axs.plot([value], [index], 'ko', color = plot_color)

        axs.plot([data[name]['mean'][0] for _ in xrange(2)],
                 [index-0.25, index+0.25],
                  'k-')
        axs.plot([data[name]['mean'][0] - data[name]['std'][0] for _ in xrange(2)],
                 [index-0.25, index+0.25],
                 'k--')
        axs.plot([data[name]['mean'][0] + data[name]['std'][0] for _ in xrange(2)],
                 [index-0.25, index+0.25],
                 'k--')

    plt.yticks([i for i in xrange(len(names))], names, size = 10)
    plt.title(plate_name)
    plt.ylim(-0.5,len(names)-0.5)
    plt.xlabel('Fluorescent intensity')
    plt.tight_layout()

    save_filename = save_folder + 'baseline_average'

    pdf = PdfPages(save_filename.split('.')[0] + '.pdf')
    pdf.savefig(fig)
    pdf.close()

    plt.savefig(save_filename)
    #
    return None
Esempio n. 8
0
def dynamic_img_show(img,title_str='',fig_size=[14,8],hide_axes=True):
    '''Show image <img>. If called repeatedly within a cycle will dynamically redraw image.
    #DEMO
    import time

    for i in range(10):
        img = np.zeros([50,50])
        img[:i*5]=1
        dynamic_img_show(img,'iter=%s'%i)
        time.sleep(0.1)
    '''
    plt.clf()
    plt.title(title_str)
    plt.imshow(img)
    plt.xticks([]); plt.yticks([]);
    plt.gcf().set_size_inches(fig_size)
    display.display(plt.gcf())
    display.clear_output(wait=True)   
def pos_firmA_over_pos_firmB(file_name, folder=None):

    if folder is None:
        folder = "data/figures"

    os.makedirs(folder, exist_ok=True)

    bkp = backup.RunBackup.load(file_name=file_name)

    pos = bkp.positions[-1000:]

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.scatter(pos[:, 0], pos[:, 1], color="black", alpha=0.05)
    ax.axvline(0.5, color="white", linewidth=0.5, linestyle="--")
    ax.axhline(0.5, color="white", linewidth=0.5, linestyle="--")

    plt.xlim(-1, bkp.parameters.n_positions)
    plt.ylim(-1, bkp.parameters.n_positions)
    plt.xticks(
        range(0, bkp.parameters.n_positions + 1,
              round(bkp.parameters.n_positions / 5)))
    plt.yticks(
        range(0, bkp.parameters.n_positions + 1,
              round(bkp.parameters.n_positions / 5)))

    plt.xlabel("Position A")
    plt.ylabel("Position B")

    plt.text(0.005,
             0.005,
             file_name,
             transform=fig.transFigure,
             fontsize='x-small',
             color='0.5')

    plt.title("$r={:.2f}$".format(bkp.field_of_view / 2))
    ax.set_aspect(1)
    plt.tight_layout()
    plt.savefig("{}/{}_evo_positions.pdf".format(folder, file_name))
    plt.show()
def show_prediction_result(image, label_image, clf):
    size = (8, 8)
    plt.figure(figsize=(15, 10))
    plt.imshow(image, cmap='gray_r')
    candidates = []
    predictions = []
    for region in regionprops(label_image):
        # skip small images
        #     if region.area < 100:
        #         continue
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        # make regions square
        maxwidth = np.max([maxr - minr, maxc - minc])
        minr, maxr = int(0.5 * ((maxr + minr) - maxwidth)) - 3, int(0.5 * ((maxr + minr) + maxwidth)) + 3
        minc, maxc = int(0.5 * ((maxc + minc) - maxwidth)) - 3, int(0.5 * ((maxc + minc) + maxwidth)) + 3
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2, alpha=0.2)
        plt.gca().add_patch(rect)
        # predict digit
        candidate = image[minr:maxr, minc:maxc]
        candidate = np.array(imresize(candidate, size), dtype=float)
        # invert
        # candidate = np.max(candidate) - candidate
        #     print im
        # rescale to 16 in integer
        candidate = (candidate - np.min(candidate))
        if np.max(candidate) == 0:
            continue
        candidate /= np.max(candidate)
        candidate[candidate < 0.2] = 0.0
        candidate *= 16
        candidate = np.array(candidate, dtype=int)
        prediction = clf.predict(candidate.reshape(-1))
        candidates.append(candidate)
        predictions.append(prediction)
        plt.text(minc - 10, minr - 10, "{}".format(prediction), fontsize=50)
    plt.xticks([], [])
    plt.yticks([], [])
    plt.tight_layout()
    plt.show()
    return candidates, predictions
Esempio n. 11
0
def show_prediction_result(image, label_image, clf):
    size = (8, 8)
    plt.figure(figsize=(15, 10))
    plt.imshow(image, cmap='gray_r')
    candidates = []
    predictions = []
    for region in regionprops(label_image):
        # skip small images
        #     if region.area < 100:
        #         continue
        # draw rectangle around segmented coins
        minr, minc, maxr, maxc = region.bbox
        # make regions square
        maxwidth = np.max([maxr - minr, maxc - minc])
        minr, maxr = int(0.5 * ((maxr + minr) - maxwidth)) - 3, int(0.5 * ((maxr + minr) + maxwidth)) + 3
        minc, maxc = int(0.5 * ((maxc + minc) - maxwidth)) - 3, int(0.5 * ((maxc + minc) + maxwidth)) + 3
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2, alpha=0.2)
        plt.gca().add_patch(rect)
        # predict digit
        candidate = image[minr:maxr, minc:maxc]
        candidate = np.array(imresize(candidate, size), dtype=float)
        # invert
        # candidate = np.max(candidate) - candidate
        #     print im
        # rescale to 16 in integer
        candidate = (candidate - np.min(candidate))
        if np.max(candidate) == 0:
            continue
        candidate /= np.max(candidate)
        candidate[candidate < 0.2] = 0.0
        candidate *= 16
        candidate = np.array(candidate, dtype=int)
        prediction = clf.predict(candidate.reshape(-1))
        candidates.append(candidate)
        predictions.append(prediction)
        plt.text(minc - 10, minr - 10, "{}".format(prediction), fontsize=50)
    plt.xticks([], [])
    plt.yticks([], [])
    plt.tight_layout()
    plt.show()
    return candidates, predictions
def pos_firmA_over_pos_firmB(backup, fig_name):

    os.makedirs(os.path.dirname(fig_name), exist_ok=True)

    position_max = backup.parameters.n_positions - 1

    pos = backup.positions[-1000:] / position_max

    fig = plt.figure()
    ax = fig.add_subplot(111)

    ax.scatter(pos[:, 0], pos[:, 1], color="black", alpha=0.05, zorder=10)
    ax.axvline(0.5, color="0.5", linewidth=0.5, linestyle="--", zorder=1)
    ax.axhline(0.5, color="0.5", linewidth=0.5, linestyle="--", zorder=1)

    plt.xlim(0, 1)
    plt.ylim(0, 1)
    plt.xticks((0, 0.5, 1))
    plt.yticks((0, 0.5, 1))

    plt.xlabel("Position $a$")
    plt.ylabel("Position $b$")

    for tick in ax.get_xticklabels():
        tick.set_fontsize("small")
    for tick in ax.get_yticklabels():
        tick.set_fontsize("small")

    plt.title("$r={:.2f}$".format(backup.parameters.r))
    ax.set_aspect(1)

    # Cut margins
    plt.tight_layout()

    # Save fig
    plt.savefig(fig_name)

    plt.close()
Esempio n. 13
0
plt.plot([0.119,0.119],[0,18.402],color='k',linestyle='--',linewidth=1, dashes = (10,10))
#Plot the W1 5 sigma line in this color space
W1,=plt.plot(col,ch2prime, color='k',linestyle='-.',linewidth=1, dashes = [8,4,2,4])
#SpIES 5sigma line (CH2)
plt.axhline(22.0, linestyle='--',linewidth=1, color='b', dashes = (10,10),label=r'SpIES 5$\sigma$')

plt.xlabel(r'[3.6]$-$[4.5] Color')
plt.ylabel('[4.5]')
first_legend = plt.legend([WISE,W1],['Assef et al. 2013 limits',r'WISE W1 5$\sigma$'],loc=1)
ax = plt.gca().add_artist(first_legend)
plt.legend(loc=2,markerscale=2, scatterpoints=1)
fig.set_size_inches(10.0,10.0)
ax2.minorticks_on()

ax2.yaxis.set_major_locator(majorLocator)
ax2.yaxis.set_major_formatter(majorFormatter)
ax2.yaxis.set_minor_locator(minorLocator)
#ax2.yaxis.set_minor_formatter(majorFormatter)
label = ax2.get_yticks()
plt.yticks(label,rotation=90)

plt.xlim(-4,4)
plt.ylim(13,23)


plt.gca().invert_yaxis()

#plt.savefig('Group_Plotting2.pdf')

plt.show()
Esempio n. 14
0
def runAnalysis( caseDir ):

    # User info 
    print "Plotting hydrogren bonds"

    # Output file name and location
    dataDir = caseDir+"/analysis/data/"
    plotDir = caseDir+"/analysis/plots/"

    # Do a histogram plot of Hbond information
    pp = PdfPages( plotDir+"hbonds.pdf" )
    
    # Get the data to plot
    names, fractions, avgDists, avgAngles = [],[],[],[]
    qbfile = open(dataDir+"hbond.avg","r")
    n = 0
    for aline in qbfile:
        if n > 1 and n < 17:
            if aline:
                values = aline.split()
                names.append( values[0]+" - "+values[1] )                      
                fractions.append( float(values[4]) )
                avgDists.append( float(values[5]) )
                avgAngles.append( float(values[6]) )
        n = n + 1

    # Create an array for the interactions
    y_pos = np.arange(len(names))
    
    # Set the plotting font and default size 'family' : 'Arial',
    font = {
            'weight' : 'normal',
            'size'   : 10}
        
    # Color of the bars
    color = plt.rcParams['axes.color_cycle'][0]        
        
    # Do a bar plot of fractions
    fig = plt.figure(figsize=(16,5))
    plt.barh(y_pos, fractions, align='center', color=color)   
    plt.yticks(y_pos, names)
    ax = fig.gca()
    ax.set_xlabel("Occupied Fraction", fontsize=12)
    ax.set_ylabel("H-bond Interaction", fontsize=12)
    plt.title( "Hydrogen Bonds: Occupancy" )
    plt.rc('font', **font)        
    plt.savefig(pp, format="pdf",dpi=300)

    # Do a bar plot of avg dists
    fig = plt.figure(figsize=(16,5))
    plt.barh(y_pos, avgDists, align='center', color=color)   
    plt.yticks(y_pos, names)
    ax = fig.gca()
    ax.set_xlabel("Average Distance", fontsize=12)
    ax.set_ylabel("H-bond Interaction", fontsize=12)
    plt.title( "Hydrogen Bonds: Average Distance" )
    plt.rc('font', **font)        
    plt.savefig(pp, format="pdf",dpi=300)
    
    # Do a bar plot of avg angles
    fig = plt.figure(figsize=(16,5))
    plt.barh(y_pos, avgAngles, align='center', color=color)   
    plt.yticks(y_pos, names)
    ax = fig.gca()
    ax.set_xlabel("Average Angle", fontsize=12)
    ax.set_ylabel("H-bond Interaction", fontsize=12)
    plt.title( "Hydrogen Bonds: Average Angle" )
    plt.rc('font', **font)        
    plt.savefig(pp, format="pdf",dpi=300)
    
    # Close the figure
    pp.close()
Esempio n. 15
0
def runAnalysis( caseDir , eps, minPoints, timeFactor ):

    # User information
    print "Now Doing dbscan Cluster"
    titlePostpend = "eps: "+str(eps)+", minPoints: "+str(minPoints)
    
    ## KDIST PLOT
    #############

    if os.path.isfile( caseDir+"/analysis/data/Kdist.1.dat" ):
        myPlot.plotData( 
            caseDir+"/analysis/plots" , 
            "Kdist Plots", 
            ["1-dist","2-dist","3-dist","4-dist","5-dist","6-dist"], 
            [caseDir+"/analysis/data/Kdist.1.dat", 
             caseDir+"/analysis/data/Kdist.2.dat", 
             caseDir+"/analysis/data/Kdist.3.dat", 
             caseDir+"/analysis/data/Kdist.4.dat", 
             caseDir+"/analysis/data/Kdist.5.dat", 
             caseDir+"/analysis/data/Kdist.6.dat"] , 
            "k-dist",
            xUnit = "points",
            skipLines = 1,
            xLimits=[0,100])    
    
    # Instantiate the class
    if os.path.isfile(caseDir+"/analysis/data/cluster_dbscan_out"):
        
        # Start the handler
        handler = cluster.clusterBase( caseDir+"/analysis/data/cluster_dbscan_out" )
        
        ## RMSd PLOT
        ############
        numRmsdDataSets = handler.separateDataSet( 
            caseDir+"/analysis/data/backbone.rmsd",
            caseDir+"/analysis/data/cluster_dbscan_rmsd_"
        )
        
        # User info
        print "Number of datasets for RMSd: "+str(numRmsdDataSets) 
        
        # Create lists of labels and files for plotting
        clusterLabels = []
        clusterFiles = []
        for i in range( 0, numRmsdDataSets):
            clusterLabels.append( "Cluster "+str(i) )
            clusterFiles.append( caseDir+"/analysis/data/cluster_dbscan_rmsd_d1_c"+str(i) )
        
        # First one is noise
        clusterLabels[0] = "Noise"   
        
        # Do the plottin
        myPlot.plotData( 
            caseDir+"/analysis/plots" , 
            "RMSd DBscan Cluster Plot", 
            clusterLabels, 
            clusterFiles , 
            "RMSd ($\AA$)",
            xFactor = timeFactor,
            scatter = True,
            legendLoc = 4
        )
        
        ## OCCUPANCY PLOT
        #################
        
        # Get the data to plot
        names, fractions = [],[]
        with open(caseDir+"/analysis/data/cluster_dbscan_summary.dat","r") as fi:
            next(fi)
            for aline in fi:
                if aline:
                    values = aline.split()
                    names.append( "Cluster "+values[0] )                      
                    fractions.append( float(values[2]) )
                    
        # Create an array for the interactions
        y_pos = np.arange(len(names))
        
        # Do a bar plot of fractions 'family' : 'Arial',
        pp = PdfPages( caseDir+"/analysis/plots/ClusterDBscanOccupancy.pdf" )
        font = {
                'weight' : 'normal',
                'size'   : 10}    
        fig = plt.figure(figsize=(16,5))
        plt.barh( y_pos, fractions, align = 'center', color = plt.rcParams['axes.color_cycle'][0]  )   
        plt.yticks(y_pos, names)
        ax = fig.gca()
        ax.set_xlabel("Occupied Fraction", fontsize=12)
        ax.set_ylabel("", fontsize=12)
        plt.title( "Cluster DBscan Occupancy Fraction, "+titlePostpend )
        plt.rc('font', **font)        
        plt.savefig(pp, format="pdf",dpi=100)
        pp.close()    
        
        ## PCA PLOT
        ###########
        
        # Separate the dataset
        numPCAdataSets = handler.separateDataSet( 
            caseDir+"/analysis/data/pca12-ca", 
            caseDir+"/analysis/data/cluster_dbscan_pca_",
            xColumn = 1
        )    
        
        # User info
        print "Number of datasets for PCA: "+str(numPCAdataSets)    
        
        # Go through each PCA component
        for n in range(2,4):    
        
            # Create lists of labels and files for plotting
            clusterLabels = []
            clusterFiles = []
            for i in range( 0, numPCAdataSets):
                clusterLabels.append( "Cluster "+str(i) )
                clusterFiles.append( caseDir+"/analysis/data/cluster_dbscan_pca_d"+str(n)+"_c"+str(i) )
            
            # First one is noise
            clusterLabels[0] = "Noise"        
            
            myPlot.plotData( 
                caseDir+"/analysis/plots" , 
                "PCA DBscan Cluster, 1vs"+str(n), 
                clusterLabels, 
                clusterFiles , 
                "PC"+str(n),
                xUnit = "PC1",
                scatter = True,
                legendLoc = 4,
                figWidth = 8,
                figHeight = 8,
                tightXlimits = False,
                legendFrame = 1,
                legendAlpha = 1
            )
Esempio n. 16
0
def runAnalysis( caseDir, timeFactor ):

    # User information
    print "Now Doing hier Cluster"
    
    # Instantiate the class
    handler = cluster.clusterBase( caseDir+"/analysis/data/cluster_hier_out" )    
      
    # RMSd PLOT
    numRmsdDataSets = handler.separateDataSet( 
        caseDir+"/analysis/data/backbone.rmsd",
        caseDir+"/analysis/data/cluster_hier_rmsd_"
    )
    
    # User info
    print "Number of datasets for RMSd: "+str(numRmsdDataSets) 
    
    # Create lists of labels and files for plotting
    clusterLabels = []
    clusterFiles = []
    for i in range( 1, numRmsdDataSets+1):
        clusterLabels.append( "Cluster "+str(i) )
        clusterFiles.append( caseDir+"/analysis/data/cluster_hier_rmsd_d1_c"+str(i) )
    
    # Do the plottin
    myPlot.plotData( 
        caseDir+"/analysis/plots" , 
        "RMSd Hier Cluster Plot", 
        clusterLabels, 
        clusterFiles , 
        "RMSd ($\AA$)",
        xFactor = timeFactor,
        scatter = True,
        legendLoc = 4
    )
    
    ## OCCUPANCY PLOT
    #################
    
    # Get the data to plot
    names, fractions = [],[]
    with open(caseDir+"/analysis/data/cluster_hier_summary.dat","r") as fi:
        next(fi)
        for aline in fi:
            if aline:
                values = aline.split()
                names.append( "Cluster "+values[0] )                      
                fractions.append( float(values[2]) )
                
    # Create an array for the interactions
    y_pos = np.arange(len(names))
    
    # Do a bar plot of fractions #'family' : 'Arial',
    pp = PdfPages( caseDir+"/analysis/plots/ClusterHierOccupancy.pdf" )
    font = {
            'weight' : 'normal',
            'size'   : 10}    
    fig = plt.figure(figsize=(16,5))
    plt.barh( y_pos, fractions, align = 'center', color = plt.rcParams['axes.color_cycle'][0]  )   
    plt.yticks(y_pos, names)
    ax = fig.gca()
    ax.set_xlabel("Occupied Fraction", fontsize=12)
    ax.set_ylabel("", fontsize=12)
    plt.title( "Cluster Hier Occupancy Fraction" )
    plt.rc('font', **font)        
    plt.savefig(pp, format="pdf",dpi=100)
    pp.close()
    
    ## PCA PLOT
    ###########
    
    # Separate the dataset
    numPCAdataSets = handler.separateDataSet( 
        caseDir+"/analysis/data/pca12-ca", 
        caseDir+"/analysis/data/cluster_hier_pca_",
        xColumn = 1
    )       
    
    # User info
    print "Number of datasets for PCA: "+str(numPCAdataSets)    
    
    # Go through each PCA component
    for n in range(2,4):    
    
        # Create lists of labels and files for plotting
        clusterLabels = []
        clusterFiles = []
        for i in range( 1, numPCAdataSets+1):
            clusterLabels.append( "Cluster "+str(i) )
            clusterFiles.append( caseDir+"/analysis/data/cluster_hier_pca_d"+str(n)+"_c"+str(i) )
        
        print "Calling plot"
        print "labels", clusterLabels  
        print "files", clusterFiles  
        
        myPlot.plotData( 
            caseDir+"/analysis/plots" , 
            "PCA Hier Cluster, 1vs"+str(n), 
            clusterLabels, 
            clusterFiles , 
            "PC"+str(n),
            xUnit = "PC1",
            scatter = True,
            legendLoc = 4,
            figWidth = 8,
            figHeight = 8,
            tightXlimits = False,
            legendFrame = 1,
            legendAlpha = 1
        )
Esempio n. 17
0
def weatherstat(df, destguid=None):
    dfduplicates = df.groupby('date').apply(
        lambda d: tuple(d.index) if len(d.index) > 1 else None).dropna()
    log.info(dfduplicates)
    df.drop_duplicates(inplace=True)  # 去重,去除可能重复的天气数据记录,原因可能是邮件重复发送等
    # print(df.head(30))
    df['date'] = df['date'].apply(lambda x: pd.to_datetime(x))
    # 日期做索引,去重并重新排序
    df.index = df['date']
    df = df[~df.index.duplicated()]
    df.sort_index(inplace=True)
    # print(df)
    df.dropna(how='all', inplace=True)  # 去掉空行,索引日期,后面索引值相同的行会被置空,需要去除
    # print(len(df))
    # df['gaowen'] = df['gaowen'].apply(lambda x: np.nan if str(x).isspace() else int(x))   #处理空字符串为空值的另外一骚
    df['gaowen'] = df['gaowen'].apply(lambda x: int(x)
                                      if x else None)  # 数字串转换成整数,如果空字符串则为空值
    df['diwen'] = df['diwen'].apply(lambda x: int(x) if x else None)
    df['fengsu'] = df['fengsu'].apply(lambda x: int(x) if x else None)
    df['shidu'] = df['shidu'].apply(lambda x: int(x) if x else None)
    # df['gaowen'] = df['gaowen'].astype(int)
    # df['diwen'] = df['diwen'].astype(int)
    # df['fengsu'] = df['fengsu'].astype(int)
    # df['shidu'] = df['shidu'].astype(int)
    df.fillna(method='ffill', inplace=True)  # 向下填充处理可能出现的空值,bfill是向上填充
    df['wendu'] = (df['gaowen'] + df['diwen']) / 2
    df['richang'] = df['sunoff'] - df['sunon']
    df['richang'] = df['richang'].astype(int)
    df['wendu'] = df['wendu'].astype(int)

    # print(df.tail(30))

    df_recent_year = df.iloc[-365:]
    # print(df_recent_year)
    # print(df[df.gaowen == df.iloc[-364:]['gaowen'].max()])
    # df_before_year = df.iloc[:-364]

    plt.figure(figsize=(16, 20))
    ax1 = plt.subplot2grid((4, 2), (0, 0), colspan=2, rowspan=2)
    ax1.plot(df['gaowen'], lw=0.3, label=u'日高温')
    ax1.plot(df['diwen'], lw=0.3, label=u'日低温')
    ax1.plot(df['wendu'], 'g', lw=0.7, label=u'日温度(高温低温平均)')
    quyangtianshu = 10
    ax1.plot(df['wendu'].resample('%dD' % quyangtianshu).mean(),
             'b',
             lw=1.2,
             label='日温度(每%d天平均)' % quyangtianshu)
    ax1.plot(df[df.fengsu > 5]['fengsu'], '*', label='风速(大于五级)')
    plt.legend(loc=2)
    #  起始统计日
    kedu = df.iloc[0]
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--', lw=0.4)
    ax1.scatter([
        kedu['date'],
    ], [kedu['wendu']], 50, color='Wheat')
    fsize = 8
    txt = str(kedu['wendu'])
    ax1.annotate(txt,
                 xy=(kedu['date'], kedu['wendu']),
                 xycoords='data',
                 xytext=(-(len(txt) * fsize), +20),
                 textcoords='offset points',
                 fontsize=fsize,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))

    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -20),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))
    # 去年今日,如果数据不足一年,取今日
    if len(df) >= 366:
        locqnjr = -365
    else:
        locqnjr = -1
    kedu = df.iloc[locqnjr]
    # kedu = df.iloc[-364]
    print(kedu)
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--')
    ax1.scatter([
        kedu['date'],
    ], [kedu['wendu']], 50, color='Wheat')
    ax1.annotate(str(kedu['wendu']),
                 xy=(kedu['date'], kedu['wendu']),
                 xycoords='data',
                 xytext=(-5, +20),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -35),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))
    # 今日
    kedu = df.iloc[-1]
    # print(kedu)
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['wendu']], 'c--')
    ax1.scatter([
        kedu['date'],
    ], [kedu['gaowen']], 50, color='BlueViolet')
    ax1.annotate(str(kedu['gaowen']),
                 xy=(kedu['date'], kedu['gaowen']),
                 xycoords='data',
                 xytext=(-10, +20),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    ax1.scatter([
        kedu['date'],
    ], [kedu['wendu']], 50, color='BlueViolet')
    ax1.annotate(str(kedu['wendu']),
                 xy=(kedu['date'], kedu['wendu']),
                 xycoords='data',
                 xytext=(10, +5),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    ax1.scatter([
        kedu['date'],
    ], [kedu['diwen']], 50, color='BlueViolet')
    ax1.annotate(str(kedu['diwen']),
                 xy=(kedu['date'], kedu['diwen']),
                 xycoords='data',
                 xytext=(-10, -20),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -35),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))

    # 最近一年最高温
    kedu = df_recent_year[df_recent_year.gaowen == df_recent_year.iloc[-364:]
                          ['gaowen'].max()].iloc[0]
    # print(kedu)
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['gaowen']], 'c--')
    ax1.scatter([
        kedu['date'],
    ], [kedu['gaowen']], 50, color='Wheat')
    ax1.annotate(str(kedu['gaowen']),
                 xy=(kedu['date'], kedu['gaowen']),
                 xycoords='data',
                 xytext=(-20, +5),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -20),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))
    # 最近一年最低温
    kedu = df_recent_year[df_recent_year.diwen == df_recent_year.iloc[-364:]
                          ['diwen'].min()].iloc[0]
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['diwen']], 'c--')
    ax1.scatter([
        kedu['date'],
    ], [kedu['diwen']], 50, color='Wheat')
    ax1.annotate(str(kedu['diwen']),
                 xy=(kedu['date'], kedu['diwen']),
                 xycoords='data',
                 xytext=(-20, +5),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -20),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))
    # 最高温
    kedu = df[df.gaowen == df['gaowen'].max()].iloc[0]
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['gaowen']], 'c--')
    ax1.scatter([
        kedu['date'],
    ], [kedu['gaowen']], 50, color='Wheat')
    ax1.annotate(str(kedu['gaowen']),
                 xy=(kedu['date'], kedu['gaowen']),
                 xycoords='data',
                 xytext=(-20, +5),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -20),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))
    # 最低温
    kedu = df[df.diwen == df['diwen'].min()].iloc[0]
    ax1.plot([kedu['date'], kedu['date']], [0, kedu['diwen']], 'c--')
    ax1.scatter([
        kedu['date'],
    ], [kedu['diwen']], 50, color='Wheat')
    ax1.annotate(str(kedu['diwen']),
                 xy=(kedu['date'], kedu['diwen']),
                 xycoords='data',
                 xytext=(-20, +5),
                 textcoords='offset points',
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=.2",
                                 color='Purple'))
    dates = "%02d-%02d" % (kedu['date'].month, kedu['date'].day)
    ax1.annotate(dates,
                 xy=(kedu['date'], 0),
                 xycoords='data',
                 xytext=(-10, -20),
                 textcoords='offset points',
                 fontsize=8,
                 arrowprops=dict(arrowstyle="->",
                                 connectionstyle="arc3,rad=0"))
    ax1.set_ylabel(u'(摄氏度℃)')
    ax1.grid(True)
    ax1.set_title(u'最高气温、最低气温和均值温度图')

    ax3 = plt.subplot2grid((4, 2), (2, 0), colspan=2, rowspan=2)
    # print(type(ax3))
    ax3.plot(df_recent_year['shidu'], 'c.', lw=0.3, label=u'湿度')
    ax3.plot(df_recent_year['shidu'].resample('15D').mean(), 'g', lw=1.5)
    ax3.set_ylabel(u'(百分比%)')
    ax3.set_title(u'半月平均湿度图')

    img_wenshifeng_path = dirmainpath / "img" / 'weather' / 'wenshifeng.png'
    img_wenshifeng_path_str = str(img_wenshifeng_path)
    touchfilepath2depth(img_wenshifeng_path)
    plt.legend(loc='lower left')
    plt.savefig(img_wenshifeng_path_str)

    imglist = list()
    imglist.append(img_wenshifeng_path_str)
    plt.close()

    plt.figure(figsize=(16, 10))
    fig, ax1 = plt.subplots()
    plt.plot(df['date'], df['sunon'], lw=0.8, label=u'日出')
    plt.plot(df['date'], df['sunoff'], lw=0.8, label=u'日落')
    ax = plt.gca()
    # ax.yaxis.set_major_formatter(FuncFormatter(min_formatter)) # 主刻度文本用pi_formatter函数计算
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: "%02d:%02d" %
                      (int(x / 60), int(x % 60))))  # 主刻度文本用pi_formatter函数计算
    plt.ylim((0, 24 * 60))
    plt.yticks(np.linspace(0, 24 * 60, 25))
    plt.xlabel(u'日期')
    plt.ylabel(u'时刻')
    plt.legend(loc=6)
    plt.title(u'日出日落时刻和白天时长图')
    plt.grid(True)
    ax2 = ax1.twinx()
    print(ax2)
    plt.plot(df_recent_year['date'],
             df_recent_year['richang'],
             'r',
             lw=1.5,
             label=u'日长')
    ax = plt.gca()
    # ax.yaxis.set_major_formatter(FuncFormatter(min_formatter)) # 主刻度文本用pi_formatter函数计算
    ax.yaxis.set_major_formatter(
        FuncFormatter(lambda x, pos: "%02d:%02d" %
                      (int(x / 60), int(x % 60))))  # 主刻度文本用pi_formatter函数计算
    # ax.set_xticklabels(rotation=45, horizontalalignment='right')
    plt.ylim((3 * 60, 12 * 60))
    plt.yticks(np.linspace(3 * 60, 15 * 60, 13))
    plt.ylabel(u'时分')
    plt.legend(loc=5)
    plt.grid(True)

    # plt.show()
    img_sunonoff_path = dirmainpath / 'img' / 'weather' / 'sunonoff.png'
    img_sunonoff_path_str = str(img_sunonoff_path)
    touchfilepath2depth(img_sunonoff_path)
    plt.savefig(img_sunonoff_path_str)
    imglist.append(img_sunonoff_path_str)
    plt.close()

    imglist2note(get_notestore(), imglist, destguid, '武汉天气图')