Пример #1
0
        def serialize(dataset):
            fix_map_attributes(dataset)
            fig = Figure(figsize=figsize, dpi=dpi)
            fig.figurePatch.set_alpha(0.0)
            ax = fig.add_axes([0.05, 0.05, 0.45, 0.85])
            ax.axesPatch.set_alpha(0.5)

            # Plot requested grids.
            layers = [layer for layer in query.get('LAYERS', '').split(',')
                    if layer] or [var.id for var in walk(dataset, GridType)]
            layer = layers[0]
            names = [dataset] + layer.split('.')
            grid = reduce(operator.getitem, names)

            actual_range = self._get_actual_range(grid)
            norm = Normalize(vmin=actual_range[0], vmax=actual_range[1])
            cb = ColorbarBase(ax, cmap=get_cmap(cmap), norm=norm,
                    orientation='vertical')
            for tick in cb.ax.get_yticklabels():
                tick.set_fontsize(14)
                tick.set_color('white')
                #tick.set_fontweight('bold')

            # Save to buffer.
            canvas = FigureCanvas(fig)
            output = StringIO() 
            canvas.print_png(output)
            if hasattr(dataset, 'close'): dataset.close()
            return [ output.getvalue() ]
Пример #2
0
 def print_xcf(self, filename_or_obj, *args, **kwargs):
     "Writes the figure to a GIMP XCF image file"
     # If filename_or_obj is a file-like object we need a temporary file for
     # GIMP's output too...
     if is_string(filename_or_obj):
         out_temp_handle, out_temp_name = None, filename_or_obj
     else:
         out_temp_handle, out_temp_name = tempfile.mkstemp(suffix='.xcf')
     try:
         # Create a temporary file and write the "layer" to it as a PNG
         in_temp_handle, in_temp_name = tempfile.mkstemp(suffix='.png')
         try:
             FigureCanvasAgg.print_png(self, in_temp_name, *args, **kwargs)
             run_gimp_script(
                 SINGLE_LAYER_SCRIPT.format(
                     input=quote_string(in_temp_name),
                     output=quote_string(out_temp_name)))
         finally:
             os.close(in_temp_handle)
             os.unlink(in_temp_name)
     finally:
         if out_temp_handle:
             os.close(out_temp_handle)
             # If we wrote the XCF to a temporary file, write its content to
             # the file-like object we were given (the copy is chunked as
             # XCF files can get pretty big)
             with open(out_temp_name, 'rb') as source:
                 for chunk in iter(lambda: source.read(131072), ''):
                     filename_or_obj.write(chunk)
             os.unlink(out_temp_name)
Пример #3
0
def plot(title='title',xlab='x',ylab='y',mode='plot',
         data={'xxx':[(0,0),(1,1),(1,2),(3,3)],
               'yyy':[(0,0,.2,.2),(2,1,0.2,0.2),(2,2,0.2,0.2),(3,3,0.2,0.3)]}):
    fig=Figure()
    fig.set_facecolor('white')
    ax=fig.add_subplot(111)
    if title: ax.set_title(title)
    if xlab: ax.set_xlabel(xlab)
    if ylab: ax.set_ylabel(ylab)
    legend=[]
    keys=sorted(data)
    for key in keys:
        stream = data[key]
        (x,y)=([],[])
        for point in stream:
            x.append(point[0])
            y.append(point[1])
        if mode=='plot':
            ell=ax.plot(x, y)
            legend.append((ell,key))
        if mode=='hist':
            ell=ax.hist(y,20)            
    if legend:
        ax.legend([x for (x,y) in legend], [y for (x,y) in legend], 
                  'upper right', shadow=True)
    canvas=FigureCanvas(fig)
    stream=cStringIO.StringIO()
    canvas.print_png(stream)
    return stream.getvalue()
Пример #4
0
Файл: views.py Проект: LS80/FFL
def team_wp(request, manager):    
    manager = manager.replace('-', ' ')
    try:
        team = Team.objects.get(manager__iexact=manager)
    except Team.DoesNotExist:
        raise Http404()
    
    week = WeekPoints.objects.latest_week()
    
    points = team.weekly_points.values_list('points', flat=True)
    weeks = range(1, week + 1)
    
    teams = list(WeekPoints.objects.values_list('points', flat=True))
    shape = (Team.objects.count(), week)
    avgs = mean(reshape(array(teams), shape), axis=0)
    
    fig = Figure(figsize=(7, 3), dpi=100, facecolor='white')
    ax = fig.add_subplot(111)
    rects = ax.bar(weeks, points, align='center', linewidth=1, color='#008ad1', width=1)
    ax.set_xlabel("Week")
    ax.set_ylabel("Points")
    ax.set_xticks(weeks) # add a tick for every week
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x() + rect.get_width() / 2., height / 2., str(height),
                fontsize=10, color="#ffffff", ha='center')
    ax.set_xlim((0.5, max(10, week) + 0.5))
    ax.plot(weeks, avgs, color='blue', marker='*', label='Week average score')
    ax.legend(markerscale=0, handlelength=3)

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response
Пример #5
0
 def __get_column_width(self):
 
   max_length = 0
   max_column_text = ''
   flag = self.prefs.get('legend_numbers',True)
   unit = self.prefs.get('legend_unit',False)
   for label,num in self.labels:      
     if not flag: num = None
     if num is not None:
       column_length = len(str(label)+str(num)) + 1
     else:
       column_length = len(str(label)) + 1  
     if column_length > max_length:
       max_length = column_length
       if flag:
         if type(num) == types.IntType or type(num) == types.LongType:
           numString = str(num)
         else:
           numString = "%.1f" % float(num)  
         max_column_text = '%s  %s' % (str(label),numString)
         if unit:
           max_column_text += "%"
       else:
         max_column_text = '%s   ' % str(label)  
                        
   figure = Figure()   
   canvas = FigureCanvasAgg(figure) 
   dpi = self.prefs['dpi']
   figure.set_dpi( dpi ) 
   l_size,l_padding = self.__get_legend_text_size()    
   self.text_size = pixelToPoint(l_size,dpi)           
   text = Text(0.,0.,text=max_column_text,size=self.text_size)
   text.set_figure(figure)
   bbox = text.get_window_extent(canvas.get_renderer())
   self.column_width = bbox.width+6*l_size
Пример #6
0
def graph_prices(x, y, gname):
    
    '''make a plot of the prices over time for a specific game'
    x is be the dates of the bins
    y is the prices
    gname is the name of the game
    '''
    x_list = list(x)
    x_dt =  [datetime.fromtimestamp(xx) for xx in x_list]
    fig=Figure(facecolor='white')
    ax=fig.add_subplot(111)
    ax.plot(x_dt,y,'r-')    
    ax.set_ylim([0,np.max(y) + np.max(y) * 0.10])
    #ax.set_title(gname)
    #ax.set_axis_bgcolor('red')

    formatter = FuncFormatter(money_format)
    ax.yaxis.set_major_formatter(formatter)
    #fig.autofmt_xdate()
    #xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
    #ax.xaxis.set_major_formatter(xfmt)
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    
    canvas=FigureCanvas(fig)
    png_output = StringIO.StringIO()
    canvas.print_png(png_output)
    response=make_response(png_output.getvalue())
    response.headers['Content-Type'] = 'image/png'
    return response
Пример #7
0
def payee_boxplot(request, payee):

    data_parameters = ytd + ['-w', '-E', '-F', '%(amount)\n', '-M', 'reg', 'Expenses', 'and', 'payee', payee]

    output = runledger(data_parameters)
    values = [float(sanatize(line)) for line in output.split('\n') if line != ""]
    if(len(values) <= 0): values.append(0)

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

    boxplot(values)

    title('Boxplot of ' + payee)
    ax.yaxis.set_major_formatter(moneyFmt)
    ax.format_ydata = price

    ax.set_xticklabels([prettyname(payee)])
    ax.set_ylim(min(values + [-1.0]) * 1.1, max(values + [1.0]) * 1.1)

    fig.autofmt_xdate()


    canvas=FigureCanvas(fig)
    response=HttpResponse(content_type='image/png')
    canvas.print_png(response)
    plt.close(fig)

    return response
Пример #8
0
def simple():
#    import datetime
#    import StringIO
    #get the polyline map using api:
    get_activity_map()
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
#    from matplotlib.dates import DateFormatter
    import polyline
    m = session['map']
    summary_lat_lon = polyline.decode(m.summary_polyline)
    fig=Figure()
    ax=fig.add_subplot(111)
    lats = [i[0] for i in summary_lat_lon]
    lons = [i[1] for i in summary_lat_lon]
#    x=[]
#    y=[]
#    now=datetime.datetime.now()
#    delta=datetime.timedelta(days=1)
#    for i in range(10):
#        x.append(now)
#        now+=delta
#        y.append(random.randint(0, 1000))
#    ax.plot_date(x, y, '-')
#    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    ax.scatter(lons,lats)
    fig.autofmt_xdate()
    canvas=FigureCanvas(fig)
    png_output = StringIO.StringIO()
    canvas.print_png(png_output)
    response=make_response(png_output.getvalue())
    response.headers['Content-Type'] = 'image/png'
    return response
Пример #9
0
    def getImage(self):
        ddict=self.fitresult
        try:
            fig = Figure(figsize=(6,3)) # in inches
            canvas = FigureCanvas(fig)
            ax = fig.add_axes([.15, .15, .8, .8])
            ax.set_axisbelow(True)
            logplot = self.plotDict.get('logy', True)
            if logplot:
                axplot = ax.semilogy
            else:
                axplot = ax.plot
            axplot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5)
            axplot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5)
            legendlist = ['spectrum', 'continuum', 'fit']
            axplot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5)
            fontproperties = FontProperties(size=8)
            if ddict['result']['config']['fit']['sumflag']:
                axplot(ddict['result']['energy'],
                       ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5)
                legendlist.append('pileup')
            if matplotlib_version < '0.99.0':
                legend = ax.legend(legendlist,0,
                                   prop = fontproperties, labelsep=0.02)
            else:
                legend = ax.legend(legendlist,0,
                                   prop = fontproperties, labelspacing=0.02)
        except ValueError:
            fig = Figure(figsize=(6,3)) # in inches
            canvas = FigureCanvas(fig)
            ax = fig.add_axes([.15, .15, .8, .8])
            ax.set_axisbelow(True)
            ax.plot(ddict['result']['energy'], ddict['result']['ydata'], 'k', lw=1.5)
            ax.plot(ddict['result']['energy'], ddict['result']['continuum'], 'g', lw=1.5)
            legendlist = ['spectrum', 'continuum', 'fit']
            ax.plot(ddict['result']['energy'], ddict['result']['yfit'], 'r', lw=1.5)
            fontproperties = FontProperties(size=8)
            if ddict['result']['config']['fit']['sumflag']:
                ax.plot(ddict['result']['energy'],
                            ddict['result']['pileup'] + ddict['result']['continuum'], 'y', lw=1.5)
                legendlist.append('pileup')
            if matplotlib_version < '0.99.0':
                legend = ax.legend(legendlist,0,
                               prop = fontproperties, labelsep=0.02)
            else:
                legend = ax.legend(legendlist,0,
                               prop = fontproperties, labelspacing=0.02)

        ax.set_xlabel('Energy')
        ax.set_ylabel('Counts')
        legend.draw_frame(False)

        outfile = self.outdir+"/"+self.outfile+".png"
        try:
            os.remove(outfile)
        except:
            pass

        canvas.print_figure(outfile)
        return self.__getFitImage(self.outfile+".png")
Пример #10
0
def simple(request):
    import random
    import django
    import datetime
    
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter
    print "hello"
    
    #print form['subject'].value()
    fig=Figure()
    ax=fig.add_subplot(111)
    x=[]
    y=[]
    now=datetime.datetime.now()
    delta=datetime.timedelta(days=1)
    for i in range(10):
        x.append(now)
        now+=delta
        y.append(random.randint(0, 1000))
    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas=FigureCanvas(fig)
    response=django.http.HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response
Пример #11
0
def figure_to_response(f):
    """ Creates a png image to be displayed in an html file """
    canvas = FigureCanvasAgg(f)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    matplotlib.pyplot.close(f)
    return response
Пример #12
0
Файл: views.py Проект: LS80/FFL
def player_wp(request, code):
    try:
        player = Player.objects.get(code=code)
    except Player.DoesNotExist:
        raise Http404

    points = player.weekly_points.values_list('points', flat=True)
    response = HttpResponse(content_type='image/png')
    
    if points:
        weeks = list(player.weekly_points.values_list('week', flat=True))
        fig = Figure(figsize=(0.4 * min(max(10, weeks[-1]), 22), 3),
                     dpi=100, facecolor='white')
        ax = fig.add_subplot(111)
        rects = ax.bar(weeks, points, align='center', linewidth=1, color='#008ad1', width=1)
        ax.set_xlabel("Week")
        ax.set_ylabel("Points")
        ax.set_xticks(weeks) # add a tick for every week
        for p, rect in zip(points, rects):
            if p != 0:
                if p < 0:
                    h = p * 2 - 1
                elif p > 0:
                    h = p + 1
                ax.text(rect.get_x() + rect.get_width() / 2., h, str(p),
                        fontsize=10, color='black', ha='center')
        ax.set_xlim((0.5, max(10, weeks[-1]) + 0.5))
    else:
        fig = Figure(figsize=(1, 1), dpi=1, facecolor='white') # return one white pixel

    canvas = FigureCanvas(fig)
    canvas.print_png(response)

    return response
Пример #13
0
def plot_activity(values):

	daysFmt = DateFormatter("%d-%B %H:00")

	fig=Figure()
	ax=fig.add_subplot(111)

	times = values.keys()
	times.sort()

	number_found = [values[key] for key in times]

	ax.plot_date(times, number_found, '-')
	
	#assert 0, '%s'%(values)

	# format the ticks
	ax.xaxis.set_major_locator(HourLocator(byhour=range(0,24,4)))
	ax.xaxis.set_major_formatter(daysFmt)
	ax.autoscale_view()
	ax.grid(True)
	ax.set_title('All devices')

	fig.autofmt_xdate()
	canvas=FigureCanvas(fig)
	response=HttpResponse(content_type='image/png')
	canvas.print_png(response)
	return response
 def draw(self):
     """
     Draw the figure using the agg renderer
     """
     self.canvas.clear()
     FigureCanvasAgg.draw(self)
     if self.blitbox is None:
         l, b, w, h = self.figure.bbox.bounds
         w, h = int(w), int(h)
         buf_rgba = self.get_renderer().buffer_rgba()
     else:
         bbox = self.blitbox
         l, b, r, t = bbox.extents
         w = int(r) - int(l)
         h = int(t) - int(b)
         t = int(b) + h
         reg = self.copy_from_bbox(bbox)
         buf_rgba = reg.to_string()
     texture = Texture.create(size=(w, h))
     texture.flip_vertical()
     with self.canvas:
         Color(1.0, 1.0, 1.0, 1.0)
         self.img_rect = Rectangle(texture=texture, pos=self.pos, size=(w, h))
     texture.blit_buffer(bytes(buf_rgba), colorfmt="rgba", bufferfmt="ubyte")
     self.img_texture = texture
Пример #15
0
    def save(self, name, log=False, vrange=None):
        if self.imdict['X'].sum() == 0.0 and log:
            warn("can't plot {}, in log mode".format(name), RuntimeWarning,
                 stacklevel=2)
            return
        fig = Figure(figsize=(8,6))
        canvas = FigureCanvas(fig)
        ax = fig.add_subplot(1,1,1)
        divider = make_axes_locatable(ax)
        cax = divider.append_axes("right", "5%", pad="1.5%")
        if log:
            norm=LogNorm()
        else:
            norm=Normalize()
        if vrange:
            self.imdict['vmin'], self.imdict['vmax'] = vrange
        im = ax.imshow(norm=norm,**self.imdict)
        cb_dict = {'cax':cax}
        if log:
            cb_dict['ticks'] = LogLocator(10, np.arange(0.1,1,0.1))
            cb_dict['format'] = LogFormatterMathtext(10)

        try:
            cb = plt.colorbar(im, **cb_dict)
        except ValueError:
            print self.imdict['X'].sum()
            raise
        ax.set_xlabel(self.x_label, x=0.98, ha='right')
        ax.set_ylabel(self.y_label, y=0.98, ha='right')
        if self.cb_label:
            cb.set_label(self.cb_label, y=0.98, ha='right')
        canvas.print_figure(name, bbox_inches='tight')
def plot_demo():
    import numpy as np
    import matplotlib
    matplotlib.use('Agg')
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    import matplotlib.mlab as mlab
    import matplotlib.pyplot as plt

    mu, sigma = 100, 15
    x = mu + sigma*np.random.randn(10000)

    # the histogram of the data
    n, bins, patches = plt.hist(x, 50, normed=1, facecolor='green', alpha=0.75)

    # add a 'best fit' line
    y = mlab.normpdf( bins, mu, sigma)
    l = plt.plot(bins, y, 'r--', linewidth=1)

    plt.xlabel('Smarts')
    plt.ylabel('Probability')
    plt.title(r'$\mathrm{Histogram\ of\ IQ:}\ \mu=100,\ \sigma=15$')
    plt.axis([40, 160, 0, 0.03])
    plt.grid(True)

    # Write to the canvas
    fig = plt.gcf()
    fig.set_size_inches(6,5)
    canvas = FigureCanvas(fig)
    output = StringIO.StringIO()
    canvas.print_png(output)
    response = make_response(output.getvalue())
    response.mimetype = 'image/png'
    return response
Пример #17
0
def plot_sfh(model_sfh, mock_sfh, plot_path):
    labels = {'lewis': r'ACS-MS', 'oir_all': r'OIR-ALL'}
    colors = {'lewis': 'dodgerblue', 'oir_all': 'maroon'}

    fig = Figure(figsize=(3.5, 3.5), frameon=False)
    canvas = FigureCanvas(fig)
    gs = gridspec.GridSpec(1, 1,
                           left=0.18, right=0.95, bottom=0.15, top=0.95,
                           wspace=None, hspace=None,
                           width_ratios=None, height_ratios=None)
    ax = fig.add_subplot(gs[0])
    for plane_key in ['lewis', 'oir_all']:
        if plane_key not in model_sfh['sfh'].keys():
            continue
        plot_single_sfh_line(ax, model_sfh['sfh'][plane_key],
                             label=labels[plane_key],
                             color=colors[plane_key],
                             drawstyle='steps-mid')
        _plot_mean_age(ax, model_sfh['sfh'][plane_key].attrs['mean_age'],
                       c=colors[plane_key])

    # plot_single_sfh_line(ax, model_sfh, label='Model', color='k')
    # print model_sfh['sfr']
    _plot_mock_sfh(ax, mock_sfh, lw=1.5, c='k', label='Mock')
    _plot_mean_age(ax, mock_sfh.attrs['mean_age'])

    ax.legend(loc='lower left', fontsize=8, frameon=True)

    gs.tight_layout(fig, pad=1.08, h_pad=None, w_pad=None, rect=None)
    canvas.print_figure(plot_path + ".pdf", format="pdf")
Пример #18
0
def bar_plot(d, labels):
    colors = itertools.cycle(['b', 'g', 'r', 'c', 'm', 'y', 'k'])
    fig=Figure(figsize=(8, 6), dpi=200)
    fig.set_facecolor('white')
    fig.subplots_adjust(bottom=0.30)
    ax=fig.add_subplot(111)
    ax.set_title("")
    ax.set_ylabel('Factor values')
    #ax.grid(which='major')
    bottom = None
    for col in d.columns:
	if bottom is None:
	    bottom = 0*d[col]
	ax.bar(range(len(d[col])), d[col], align='center', bottom=bottom,
		label=labels[col], color=colors.next(), alpha=0.6)
	bottom += d[col]
    ax.set_xticks(range(len(d[col])))
    ax.set_xlim([-0.5, len(d[col])])
    ax.set_xticklabels([unicode(el) for el in d[col].index], size='x-small',
	    rotation='vertical')
    leg = ax.legend(loc='best', fancybox=True, prop={'size':9})
    leg.get_frame().set_alpha(0.5)

    canvas=FigureCanvas(fig)
    stream=cStringIO.StringIO()
    canvas.print_png(stream, bbox_inches='tight')
    return stream.getvalue()
def plot_hist(spec="10_100_500"):
    import numpy as np
    import matplotlib
    matplotlib.use('Agg')
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    import matplotlib.pyplot as plt

    spec = request.args.get("spec", spec, type=str).split("_")
    assert(len(spec) == 3)

    k = int(spec[0])
    n = int(spec[1])
    p = float(spec[2])/1000.

    res = get_coin_ensemble(k, n, p)
    avgs = [np.average(i) for i in res]

    plt.clf()
    fig = plt.figure()
    l = plt.hist(avgs)

    fig.set_size_inches(5,4)
    canvas = FigureCanvas(fig)
    output = StringIO.StringIO()
    canvas.print_png(output)
    response = make_response(output.getvalue())
    response.mimetype = 'image/png'
    return response
def save_plotSpectrum(y,Fs,image_name):
    """
    Plots a Single-Sided Amplitude Spectrum of y(t)
    """
    fig = Figure(linewidth=0.0)
    fig.set_size_inches(fig_width,fig_length, forward=True)
    Figure.subplots_adjust(fig, left = fig_left, right = fig_right, bottom = fig_bottom, top = fig_top, hspace = fig_hspace)
    n = len(y) # length of the signal

    _subplot = fig.add_subplot(2,1,1)        
    print "Fi"
    _subplot.plot(arange(0,n),y)
    xlabel('Time')
    ylabel('Amplitude')
    _subploti_2=fig.add_subplot(2,1,2)
    k = arange(n)
    T = n/Fs
    frq = k/T # two sides frequency range
    frq = frq[range(n/2)] # one side frequency range

    Y = fft(y)/n # fft computing and normalization
    Y = Y[range(n/2)]

    _subplot_2.plot(frq,abs(Y),'r') # plotting the spectrum
    xlabel('Freq (Hz)')
    ylabel('|Y(freq)|')
    print "here"
    canvas = FigureCanvasAgg(fig)
    if '.eps' in outfile_name:
        canvas.print_eps(outfile_name, dpi = 110)
    if '.png' in outfile_name:
        canvas.print_figure(outfile_name, dpi = 110)
Пример #21
0
def plotSolarRadiationAgainstMonth(filename):
    trainRowReader = csv.reader(open(filename, 'rb'), delimiter=',')
    month_most_common_list = []
    Solar_radiation_64_list = []
    for row in trainRowReader:
        month_most_common = row[3]
        Solar_radiation_64 = row[6]
        month_most_common_list.append(month_most_common)
        Solar_radiation_64_list.append(Solar_radiation_64)   
     
    #convert all elements in the list to float while skipping the first element for the 1st element is a description of the field.
    month_most_common_list = [float(i) for i in prepareList(month_most_common_list)[1:] ]
    Solar_radiation_64_list = [float(i) for i in prepareList(Solar_radiation_64_list)[1:] ]

    fig=Figure()
    ax=fig.add_subplot(111)
    title='Scatter Diagram of solar radiation against month of the year'
    ax.set_xlabel('Most common month')
    ax.set_ylabel('Solar Radiation')
    fig.suptitle(title, fontsize=14)
    try:
        ax.scatter(month_most_common_list, Solar_radiation_64_list)
        #it is possible to make other kind of plots e.g bar charts, pie charts, histogram
    except ValueError:
        pass
    canvas = FigureCanvas(fig)
    canvas.print_figure('solarRadMonth.png',dpi=500)
Пример #22
0
def make_1d_overlay(in_file_name, out_dir, ext, subset, b_effs=[0.1, 0.2]):
    textsize = _text_size - 2
    b_eff_styles = _b_eff_styles

    taggers = {x:{} for x in b_effs}
    with h5py.File(in_file_name, 'r') as in_file:
        for b_eff in taggers:
            for tag in (subset or _default_overlay_1d):
                taggers[b_eff][tag] = get_c_vs_u_const_beff(
                    in_file, tag, b_eff=b_eff)

    fig = Figure(figsize=_fig_size)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(1,1,1)
    for b_eff, linestyle in zip(b_effs, b_eff_styles):
        for tname, (vc, vu) in taggers[b_eff].items():
            label, color = leg_labels_colors.get(tname, (tname, 'k'))
            lab = '$1 / \epsilon_{{ b }} = $ {rej:.0f}, {tname}'.format(
                rej=1/b_eff, tname=label)
            ax.plot(vc, vu, label=lab, color=color, linewidth=_line_width,
                    linestyle=linestyle)
    ax.set_xlim(0.1, 0.5)
    legprops = {'size':textsize}
    leg = ax.legend(prop=legprops)
    leg.get_title().set_fontsize(textsize)

    setup_1d_ctag_legs(ax, textsize)

    fig.tight_layout(pad=0, h_pad=0, w_pad=0)
    if not isdir(out_dir):
        os.mkdir(out_dir)
    file_name = '{}/ctag-1d-brej-overlay{}'.format(
        out_dir, ext)
    canvas.print_figure(file_name, bbox_inches='tight')
Пример #23
0
def draw_simple_rejrej(in_file, out_dir, ext='.pdf', tagger='gaia',
                       official=False, approval='Internal', points=[]):
    """
    Draw iso-efficiency contours for one tagger (no colors).
    """
    fig = Figure(figsize=_fig_size)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(1,1,1)
    ds = in_file[tagger + '/all']

    label_rejrej_axes(ax, ds)
    levels = np.linspace(0.2, 0.5, 7)
    add_contour(ax, ds, opts=dict(levels=levels, textsize=10))
    zax = long_particle_names[ds.attrs['xyz'][2]]
    if official:
        _add_rejrej_official(ax, approval, zax=zax, size=9)

    for y, x, z in points:
        ax.scatter(x, y, s=20, c='yellow')
        ax.annotate(
            r'$\epsilon_{{c}}$ = {:.2f}'.format(z), (x,y), xytext=(-8,0),
            textcoords='offset points', size='x-small',
            bbox = dict(boxstyle = 'round', fc = 'yellow', alpha = 1),
            # arrowprops = dict(arrowstyle = '->'),
            ha='right', va='top')

    out_name = '{}/rejrej-simple{}'.format(out_dir, ext)
    canvas.print_figure(out_name, bbox_inches='tight')
Пример #24
0
def startup_cost():
    import datetime
    import StringIO
    import random
    import base64

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter

    fig=Figure(facecolor='#ffffff')
    ax=fig.add_subplot(211)
    ax2=fig.add_subplot(212, axisbg='y')
    x=[]
    y=[]
    now=datetime.datetime.now()
    delta=datetime.timedelta(days=1)
    for i in range(10):
        x.append(now)
        now+=delta
        y.append(random.randint(0, 1000))
    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    ax2.plot_date(x, y, '-')
    ax2.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas=FigureCanvas(fig)
    png_output = StringIO.StringIO()
    canvas.print_png(png_output)
    image=make_response(png_output.getvalue())
    response.headers['Content-Type'] = 'image/png'
    return response
Пример #25
0
def draw_ctag_rejrej(in_file, out_dir, ext='.pdf'):
    """
    Basic heatmap of efficiency vs two rejections.
    """
    fig = Figure(figsize=_fig_size)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(1,1,1)
    ds = in_file['gaia/all']

    eff_array, extent = _get_arr_extent(ds)
    label_rejrej_axes(ax, ds)
    im = ax.imshow(eff_array.T, extent=extent,
                   origin='lower', aspect='auto')
    ax.set_xscale('log')
    ax.set_yscale('log')
    ax.grid(which='both')

    # add_contour(ax,ds)

    divider = make_axes_locatable(ax)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    cb = Colorbar(ax=cax, mappable=im)

    out_name = '{}/rejrej{}'.format(out_dir, ext)
    # ignore complaints about not being able to log scale images
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        canvas.print_figure(out_name, bbox_inches='tight')
Пример #26
0
def plot_normprob(d, snrs, outroot):
    """ Normal quantile plot compares observed SNR to expectation given frequency of occurrence.
    Includes negative SNRs, too.
    """

    outname = os.path.join(d["workdir"], "plot_" + outroot + "_normprob.png")

    # define norm quantile functions
    Z = lambda quan: n.sqrt(2) * erfinv(2 * quan - 1)
    quan = lambda ntrials, i: (ntrials + 1 / 2.0 - i) / ntrials

    # calc number of trials
    npix = d["npixx"] * d["npixy"]
    if d.has_key("goodintcount"):
        nints = d["goodintcount"]
    else:
        nints = d["nints"]
    ndms = len(d["dmarr"])
    dtfactor = n.sum([1.0 / i for i in d["dtarr"]])  # assumes dedisperse-all algorithm
    ntrials = npix * nints * ndms * dtfactor
    logger.info("Calculating normal probability distribution for npix*nints*ndms*dtfactor = %d" % (ntrials))

    # calc normal quantile
    if len(n.where(snrs > 0)[0]):
        snrsortpos = n.array(sorted(snrs[n.where(snrs > 0)], reverse=True))  # high-res snr
        Zsortpos = n.array([Z(quan(ntrials, j + 1)) for j in range(len(snrsortpos))])
        logger.info("SNR positive range = (%.1f, %.1f)" % (snrsortpos[-1], snrsortpos[0]))
        logger.info("Norm quantile positive range = (%.1f, %.1f)" % (Zsortpos[-1], Zsortpos[0]))

    if len(n.where(snrs < 0)[0]):
        snrsortneg = n.array(sorted(n.abs(snrs[n.where(snrs < 0)]), reverse=True))  # high-res snr
        Zsortneg = n.array([Z(quan(ntrials, j + 1)) for j in range(len(snrsortneg))])
        logger.info("SNR negative range = (%.1f, %.1f)" % (snrsortneg[-1], snrsortneg[0]))
        logger.info("Norm quantile negative range = (%.1f, %.1f)" % (Zsortneg[-1], Zsortneg[0]))

    # plot
    fig3 = plt.Figure(figsize=(10, 10))
    ax3 = fig3.add_subplot(111)
    if len(n.where(snrs < 0)[0]) and len(n.where(snrs > 0)[0]):
        logger.info("Plotting positive and negative cands")
        ax3.plot(snrsortpos, Zsortpos, "k.")
        ax3.plot(snrsortneg, Zsortneg, "kx")
        refl = n.linspace(
            min(snrsortpos.min(), Zsortpos.min(), snrsortneg.min(), Zsortneg.min()),
            max(snrsortpos.max(), Zsortpos.max(), snrsortneg.max(), Zsortneg.max()),
            2,
        )
    elif len(n.where(snrs > 0)[0]):
        logger.info("Plotting positive cands")
        refl = n.linspace(min(snrsortpos.min(), Zsortpos.min()), max(snrsortpos.max(), Zsortpos.max()), 2)
        ax3.plot(snrsortpos, Zsortpos, "k.")
    elif len(n.where(snrs < 0)[0]):
        logger.info("Plotting negative cands")
        refl = n.linspace(min(snrsortneg.min(), Zsortneg.min()), max(snrsortneg.max(), Zsortneg.max()), 2)
        ax3.plot(snrsortneg, Zsortneg, "kx")
    ax3.plot(refl, refl, "k--")
    ax3.set_xlabel("SNR")
    ax3.set_ylabel("Normal quantile SNR")
    canvas = FigureCanvasAgg(fig3)
    canvas.print_figure(outname)
Пример #27
0
def plot_lm(d, snrs, l1s, m1s, outroot):
    """ Plot the lm coordinates (relative to phase center) for all candidates.
    """

    outname = os.path.join(d["workdir"], "plot_" + outroot + "_impeak.png")

    snrmin = 0.8 * min(d["sigma_image1"], d["sigma_image2"])
    fig4 = plt.Figure(figsize=(10, 10))
    ax4 = fig4.add_subplot(111)

    # plot positive
    good = n.where(snrs > 0)
    sizes = (snrs[good] - snrmin) ** 5  # set scaling to give nice visual sense of SNR
    xarr = 60 * n.degrees(l1s[good])
    yarr = 60 * n.degrees(m1s[good])
    ax4.scatter(xarr, yarr, s=sizes, facecolor="none", alpha=0.5, clip_on=False)
    # plot negative
    good = n.where(snrs < 0)
    sizes = (n.abs(snrs[good]) - snrmin) ** 5  # set scaling to give nice visual sense of SNR
    xarr = 60 * n.degrees(l1s[good])
    yarr = 60 * n.degrees(m1s[good])
    ax4.scatter(xarr, yarr, s=sizes, marker="x", edgecolors="k", alpha=0.5, clip_on=False)

    ax4.set_xlabel("Dec Offset (amin)")
    ax4.set_ylabel("RA Offset (amin)")
    fov = n.degrees(1.0 / d["uvres"]) * 60.0
    ax4.set_xlim(fov / 2, -fov / 2)
    ax4.set_ylim(-fov / 2, fov / 2)
    canvas4 = FigureCanvasAgg(fig4)
    canvas4.print_figure(outname)
Пример #28
0
def make_1d_plots(in_file_name, out_dir, ext, b_eff=0.1, reject='U'):
    textsize=_text_size
    taggers = {}
    with h5py.File(in_file_name, 'r') as in_file:
        for tag in ['gaia', mv1uc_name, 'jfc', 'jfit']:
            taggers[tag] = get_c_vs_u_const_beff(
                in_file, tag, b_eff=b_eff, reject=reject)

    fig = Figure(figsize=_fig_size)
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(1,1,1)
    for tname, (vc, vu) in taggers.items():
        label, color = leg_labels_colors.get(tname, (tname, 'k'))
        ax.plot(vc, vu, label=label, color=color, linewidth=_line_width)
    leg = ax.legend(title='$b$-rejection = {}'.format(1/b_eff),
                    prop={'size':textsize})
    leg.get_title().set_fontsize(textsize)

    setup_1d_ctag_legs(ax, textsize, reject=reject)

    fig.tight_layout(pad=0, h_pad=0, w_pad=0)
    if not isdir(out_dir):
        os.mkdir(out_dir)
    file_name = '{}/{rej}Rej-vs-cEff-brej{}{}'.format(
        out_dir, int(1.0/b_eff), ext, rej=reject.lower())
    canvas.print_figure(file_name, bbox_inches='tight')
Пример #29
0
def simple():
    import datetime
    import StringIO
    import random

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter

    fig=Figure()
    ax=fig.add_subplot(111)
    x=[]
    y=[]
    now=datetime.datetime.now()
    delta=datetime.timedelta(days=1)
    for i in range(10):
        x.append(now)
        now+=delta
        y.append(random.randint(0, 1000))
    ax.plot_date(x, y, '-')
    ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
    fig.autofmt_xdate()
    canvas=FigureCanvas(fig)
    png_output = StringIO.StringIO()
    canvas.print_png(png_output)
    data = png_output.getvalue().encode('base64')
    data_url = 'data:image/png;base64,{}'.format(urllib.quote(data.rstrip('\n')))
    response=make_response(png_output.getvalue())
    response.headers['Content-Type'] = 'image/png'
    return response
Пример #30
0
    def _plot_baseline_subtracted(self, x, y, raw=True, baseline=True):
        """Plot the baseline-subtracted data"""
        from matplotlib.figure import Figure
        from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

        figure = Figure()
        canvas = FigureCanvas(figure)
        axes1 = figure.add_subplot(1, 1, 1, axisbg='whitesmoke')

        # Points for fit
        axes1.plot(x, y, 'o', color='deepskyblue', markersize=2, alpha=1, label='Baseline-subtracted data')
        axes1.set_xlabel('time (s)')
        axes1.set_ylabel(r' corr. differential power ($\mu$cal / s)')
        axes1.legend(loc='upper center', bbox_to_anchor=(0.2, 0.95), ncol=1, fancybox=True, shadow=True, markerscale=3,
                     prop={'size': 6})

        if raw:
            axes2 = axes1.twinx()
            axes2.plot(x, self.differential_power, 'o', color='gray', markersize=2, alpha=.3, label='Raw data')
            axes2.set_ylabel(r'raw differential power ($\mu$cal / s)')
            axes2.legend(loc='upper center', bbox_to_anchor=(0.8, 0.95), ncol=1, fancybox=True, shadow=True,
                         markerscale=3,
                         prop={'size': 6})
            if baseline:
                axes2.plot(x, self.baseline_power, '-', color='black', alpha=.3, label='baseline')

        axes1.set_title(self.data_filename)
        canvas.print_figure(self.name + '-subtracted.png', dpi=500)
Пример #31
0
    def plot_sample_preds(self, images, labels, calnet_preds, pred_dist,
                          gt_dist, n_preds, dataset):

        n_plotted_preds = 5 if n_preds > 5 else n_preds

        n_cols = n_plotted_preds + 4
        n_rows = pred_dist.shape[1]

        fig = plt.figure(figsize=(n_cols + 2, n_rows + 2))
        canvas = FigureCanvasAgg(fig)

        if dataset == "LIDC":
            lidc_norm = matplotlib.colors.Normalize(vmin=0, vmax=1)

        # plot sample predictions
        for i in range(n_rows):

            # plot input
            plt.subplot(n_rows, n_cols, i * n_cols + 1)
            plottable_images = move_color_channel(de_torch(
                (images[i] + 1) / 2))
            if plottable_images.shape[-1] == 1:
                plottable_images = plottable_images.squeeze()
            plt.imshow(plottable_images, interpolation="none")
            if i == 0: plt.title("Input")
            plt.xticks([])
            plt.yticks([])

            for j in range(n_cols - 4):
                plottable_pred = _recolour_label(de_torch(
                    _1hot_2_2d(pred_dist[j, i, :, :], sample=True)),
                                                 dataset=dataset)

                plt.subplot(n_rows, n_cols, i * n_cols + j + 2)
                if dataset == "LIDC":
                    plt.imshow(plottable_pred,
                               norm=lidc_norm,
                               interpolation="none")
                else:
                    plt.imshow(plottable_pred, interpolation="none")
                if i == 0: plt.title(f"Pred {j + 1}")
                plt.xticks([])
                plt.yticks([])

            # plot average predictions
            plt.subplot(n_rows, n_cols, i * n_cols + n_cols - 2)
            plottable_avg_pred = _recolour_label(de_torch(
                _1hot_2_2d(pred_dist[:, i, :, :].mean(0), sample=True)),
                                                 dataset=dataset)
            if dataset == "LIDC":
                plt.imshow(plottable_avg_pred,
                           norm=lidc_norm,
                           interpolation="none")
            else:
                plt.imshow(plottable_avg_pred, interpolation="none")
            if i == 0: plt.title(f"Avg Pred\nN = {pred_dist.shape[0]}")
            plt.xticks([])
            plt.yticks([])

            # plot calibration net predictions
            plt.subplot(n_rows, n_cols, i * n_cols + n_cols - 1)
            plottable_calnet_pred = _recolour_label(de_torch(
                _1hot_2_2d(calnet_preds[i], sample=True)),
                                                    dataset=dataset)

            if dataset == "LIDC":
                plt.imshow(plottable_calnet_pred,
                           norm=lidc_norm,
                           interpolation="none")
            else:
                plt.imshow(plottable_calnet_pred, interpolation="none")
            if i == 0: plt.title("CalNet Pred")
            plt.xticks([])
            plt.yticks([])

            # plot actual predictions
            plt.subplot(n_rows, n_cols, i * n_cols + n_cols)

            if gt_dist is None:
                if labels.shape[1] != LABELS_CHANNELS:
                    label = torch.eye(
                        LABELS_CHANNELS)[labels[:, 1, :, :].long()].permute(
                            0, 3, 1, 2)[i]  # convert rgb label to one-hot
                else:
                    label = labels[i]
                plottable_label = _recolour_label(de_torch(
                    _1hot_2_2d(label, sample=True)),
                                                  dataset=dataset)
            else:
                pad = lambda x: np.pad(x.cpu().numpy(),
                                       pad_width=2,
                                       mode='constant',
                                       constant_values=1)
                glued_top = np.concatenate(
                    (pad(gt_dist[i, 0]), pad(gt_dist[i, 1])), axis=1)
                glued_bottom = np.concatenate(
                    (pad(gt_dist[i, 2]), pad(gt_dist[i, 3])), axis=1)
                plottable_label = np.concatenate([glued_top, glued_bottom],
                                                 axis=0)

            if dataset == "LIDC":
                plt.imshow(plottable_label,
                           norm=lidc_norm,
                           interpolation="none")
            else:
                plt.imshow(plottable_label, interpolation="none")
            if i == 0: plt.title("Label 0")
            plt.xticks([])
            plt.yticks([])

        fig.suptitle('Sample predictions')

        # convert figure to array
        canvas.draw()
        _, (width, height) = canvas.print_to_buffer()
        s = canvas.tostring_rgb()

        plt.close(fig)

        return np.fromstring(s, dtype='uint8').reshape((height, width, 3))
Пример #32
0
def get_plot(params):
    time_min = dt.datetime.strptime(params['time_min'],
                                    '%Y-%m-%dT%H:%M:00.000Z')
    time_max = dt.datetime.strptime(params['time_max'],
                                    '%Y-%m-%dT%H:%M:00.000Z')
    barbs = 'barbs' in params.keys() and params['barbs'] == 'True'
    pbl = 'pbl' in params.keys() and params['pbl'] == 'True'
    var = params['var']
    lgd = None
    has_barbs = var == 'barbs' or barbs
    if var == 'barbs':
        lidars = get_barb_data(params)
    else:
        lidars = get_lidar_data(params)
    if barbs:
        ds_barbs = get_barb_data(params)
    nds = len(lidars)
    figsize = (10, 3.5 * nds)
    sharey = False
    f, axarr = plt.subplots(nds, #sharex=True,
                            sharey=sharey,
                            squeeze=False,
                            figsize=figsize)
    cbar_kwargs = {'label': cb_dict[var]}
    is_centered = centered_dict[var]
    graph_center = 0 if is_centered else False
    cmap = 'coolwarm' if is_centered else 'jet'

    for i, ds in enumerate(lidars):
        ax = axarr[i][0]
        title = ds.attrs['lidar'] + ' (' + ds.attrs['scan'] + ')'
        if has_barbs: # get nice range intervals
            # find the scan range_res
            ranges = ds.coords['Range'].values
            ydelta = ranges[1] - ranges[0]
            # get the desired average barb height locations
            ysteps = np.arange(1, 10)
            yloc = MaxNLocator(nbins=barb_heights, steps=ysteps)
            heights = yloc._raw_ticks(0, len(ranges))
            dheight = (heights[1] - heights[0]) * ydelta
            
        if var == 'barbs':
            ds['windspeed'].rasp.plot_barbs(x='Time', y='Range',
                                            components=['x', 'y'],
                                            # resample=barb_resample,
                                            # resampley=barb_bins,
                                            resampley=dheight,
                                            ax=ax,
                                            length=barb_length)
            ax.set_title(title)
            ax.set_xlim([time_min, time_max])
            ax.set_xlabel('Time (UTC)')
            ax.set_ylabel('Range (km)')
        else:
            vmin = vmin_dict[var] if var in vmin_dict.keys() else None
            vmax = vmax_dict[var] if var in vmax_dict.keys() else None
            try:
                da = ds[var]
                da.plot.pcolormesh(x='Time', y='Range', center=graph_center,
                                   robust=True, cmap=cmap, ax=ax,
                                   vmin=vmin, vmax=vmax,
                                   cbar_kwargs=cbar_kwargs)
            except:
                # this happens occasionally when all data is NA, or
                # there's only one profile of data
                Z = np.zeros((ds.dims['Time'], ds.dims['Range'])).transpose()
                # Z = da.values.transpose()
                Zm = np.ma.masked_where(np.isnan(Z), Z)
                ax.pcolormesh(ds.coords['Time'].values,
                              ds.coords['Range'].values, Zm)

            if pbl:
                # add pbl
                ds['pbl'] = estimate_residual_layer(ds['cnr_whole'])
                ax.scatter(ds.coords['Time'].values, ds['pbl'], 50, marker='.', color='red',
                           edgecolor='gray', lw=.5, label='PBL (Residual Layer)')
                ax.legend(loc=1)
            
            ax.set_title(title)
            ax.set_xlim([time_min, time_max])
            ax.set_xlabel('Time (UTC)')
            ax.set_ylabel('Range (km)')
            if barbs and ds_barbs[i] is not None:
                ds = ds_barbs[i]
                ds['windspeed'].rasp.plot_barbs(x='Time', y='Range',
                                                components=['x', 'y'],
                                                # resample=barb_resample,
                                                # resampley=barb_bins,
                                                resampley=dheight,
                                                ax=ax,
                                                length=barb_length,
                                                lw=barb_lw)
    png_output = BytesIO()
    if lgd is None:
        plt.tight_layout()
        canvas = FigureCanvas(plt.gcf())
        canvas.print_png(png_output)
    else:
        plt.savefig(png_output, format='png',
                    bbox_extra_artists=(lgd,),
                    bbox_inches='tight')
    response = png_output.getvalue()
    # have to make sure to close things duh
    plt.close()
    png_output.close()
    return response
Пример #33
0
    def get(self, request):

        f = matplotlib.figure.Figure()

        FigureCanvasAgg(f)
        A = io.BytesIO()
        df = pd.read_csv("D:\heart-disease-uci\heart.csv")
        response = HttpResponse('data unfetched')
        try:
            image_type = request.GET['image_name']
            print(image_type)
            if image_type.lower() == "heartdiagpostive":
                df['Age_Category'] = pd.cut(df['age'],
                                            bins=list(np.arange(25, 85, 5)))
                df[df['target'] == 1].groupby(
                    'Age_Category')['age'].count().plot(kind='bar')
                plt.title(
                    'Age Distribution of Patients with +ve Heart Diagonsis')
                A = io.BytesIO()
                plt.savefig(A, format='png')
                plt.close(f)
                response = HttpResponse(A.getvalue(), content_type='image/png')
            elif image_type.lower() == "heartdiagnegative":
                df['Age_Category'] = pd.cut(df['age'],
                                            bins=list(np.arange(25, 85, 5)))
                df[df['target'] == 0].groupby(
                    'Age_Category')['age'].count().plot(kind='bar')
                plt.title(
                    'Age Distribution of Patients with -ve Heart Diagonsis')
                B = io.BytesIO()
                plt.savefig(B, format='png')
                plt.close(f)
                response = HttpResponse(B.getvalue(), content_type='image/png')
            elif image_type.lower() == "blood":
                sns.countplot(x='fbs', data=df, hue='target')
                plt.xlabel('< 120mm/Hg                   >120 mm/Hg')
                plt.ylabel('Fasting blood sugar')
                plt.legend(['No disease', ' disease'])
                c = io.BytesIO()
                plt.savefig(c, format='png')
                plt.close(f)
                response = HttpResponse(c.getvalue(), content_type='image/png')
            elif image_type.lower() == "excercise":
                sns.countplot(x='exang', data=df, hue='target')
                plt.xlabel(
                    'No ex                                     Exercise')
                plt.title(' Excercise effect on Heart diease')
                plt.legend(['No disease', ' disease'])
                d = io.BytesIO()
                plt.savefig(d, format='png')
                plt.close(f)
                response = HttpResponse(d.getvalue(), content_type='image/png')
            elif image_type.lower() == "default":
                sns.countplot(x='target', data=df, hue='sex')
                plt.legend(['Female ', 'Male'])
                plt.xlabel('No Heart disease                 Heart Disease')
                E = io.BytesIO()
                plt.savefig(E, format='png')
                plt.close(f)
                response = HttpResponse(E.getvalue(), content_type='image/png')
        except KeyError:
            response = HttpResponse('unable fetch')

        return response
In this example, the contents of the agg canvas are extracted to a
string, which can in turn be passed off to PIL or put in a numeric
array


"""
from matplotlib.backends.backend_agg import FigureCanvasAgg
from matplotlib.figure import Figure
from matplotlib.mlab import normpdf
from numpy.random import randn
import numpy

fig = Figure(figsize=(5, 4), dpi=100)
ax = fig.add_subplot(111)

canvas = FigureCanvasAgg(fig)

mu, sigma = 100, 15
x = mu + sigma * randn(10000)

# the histogram of the data
n, bins, patches = ax.hist(x, 50, normed=1)

# add a 'best fit' line
y = normpdf(bins, mu, sigma)
line, = ax.plot(bins, y, 'r--')
line.set_linewidth(1)

ax.set_xlabel('Smarts')
ax.set_ylabel('Probability')
ax.set_title(r'$\mathrm{Histogram of IQ: }\mu=100, \sigma=15$')
Пример #35
0
    def plot(self, title, scol, logT=False, ylim=False, pw=3, ph=4):
        """ Return boxplot from the given tables.

        """
        self.xtickrotation, self.xtickalign = 0, "center"
        if len(self.group_tags) < 2:
            ticklabelsize = pw * 1.5
        else:
            ticklabelsize = pw * 6
        tw = len(self.group_tags) * pw
        th = ph

        f, axarr = plt.subplots(1,
                                len(self.group_tags),
                                dpi=300,
                                sharey=scol,
                                figsize=(tw, th))
        # f, axarr = plt.subplots(1, len(self.group_tags), dpi=300, sharey = scol)

        # nm = len(self.group_tags) * len(self.color_tags) * len(self.sort_tags)
        # if nm > 30:
        # f.set_size_inches(nm * 0.25 ,nm * 0.15)
        # legend_x = 1.2
        # self.xtickrotation, self.xtickalign = 70,"right"

        canvas = FigureCanvas(f)
        canvas.set_window_title(title)
        try:
            axarr = axarr.reshape(-1)
        except:
            axarr = [axarr]
        # plt.subplots_adjust(bottom=0.3)
        if logT:
            if self.df:
                axarr[0].set_ylabel("Read number difference (log)",
                                    fontsize=ticklabelsize + 1)
            else:
                axarr[0].set_ylabel("Read number (log)",
                                    fontsize=ticklabelsize + 1)
        else:
            if self.df:
                axarr[0].set_ylabel("Read number difference",
                                    fontsize=ticklabelsize + 1)
            else:
                axarr[0].set_ylabel("Read number", fontsize=ticklabelsize + 1)

        for i, g in enumerate(self.sortDict.keys()):
            # if self.df:
            #     axarr[i].set_title(g + "_df", y=1.02, fontsize=ticklabelsize + 2)
            # else:
            axarr[i].set_title(g, y=1.02, fontsize=ticklabelsize + 2)

            if logT and not self.df:
                axarr[i].set_yscale('log')
            else:
                axarr[i].locator_params(axis='y', nbins=4)

            axarr[i].tick_params(axis='y', direction='out')
            axarr[i].yaxis.tick_left()
            axarr[i].yaxis.grid(True,
                                linestyle='-',
                                which='major',
                                color='lightgrey',
                                alpha=0.7,
                                zorder=1)
            if ylim:
                axarr[i].set_ylim([-ylim, ylim])
            d = []  # Store data within group
            color_t = []  # Store tag for coloring boxes
            x_ticklabels = []  # Store ticklabels
            for j, a in enumerate(self.sortDict[g].keys()):
                # if len(a) > 10:
                # print(a)
                self.xtickrotation = 70
                self.xtickalign = "right"
                for k, c in enumerate(self.sortDict[g][a].keys()):
                    if not numpy.any(
                            self.sortDict[g][a]
                        [c]):  # When there is no matching data, skip it
                        continue
                    else:
                        if self.df:
                            d.append(self.sortDict[g][a][c])
                        else:
                            d.append([x + 1 for x in self.sortDict[g][a][c]])
                        color_t.append(self.colors[k])
                        x_ticklabels.append(a)  # + "." + c

            # Fine tuning boxplot
            # print(d)
            bp = axarr[i].boxplot(d,
                                  notch=False,
                                  sym='o',
                                  vert=True,
                                  whis=1.5,
                                  positions=None,
                                  widths=None,
                                  patch_artist=True,
                                  bootstrap=None)
            z = 10  # zorder for boxplot
            plt.setp(bp['whiskers'],
                     color='black',
                     linestyle='-',
                     linewidth=0.8,
                     zorder=z)
            plt.setp(bp['fliers'],
                     markerfacecolor='gray',
                     color='white',
                     alpha=0.3,
                     markersize=1.8,
                     zorder=z)
            plt.setp(bp['caps'], color='white', zorder=z)
            plt.setp(bp['medians'], color='black', linewidth=1.5, zorder=z + 1)
            legends = []
            for patch, color in zip(bp['boxes'], color_t):
                patch.set_facecolor(
                    color
                )  # When missing the data, the color patch will exceeds
                patch.set_edgecolor("none")
                patch.set_zorder(z)
                legends.append(patch)

            # Fine tuning subplot
            axarr[i].set_xticks([
                len(self.color_tags) * n + 1 + (len(self.color_tags) - 1) / 2
                for n, s in enumerate(self.sortDict[g].keys())
            ])
            # plt.xticks(xlocations, sort_tags, rotation=90, fontsize=10)
            axarr[i].set_xticklabels(self.sortDict[g].keys(),
                                     rotation=self.xtickrotation,
                                     ha=self.xtickalign)
            # axarr[i].set_xticklabels(self.sortDict[g].keys(), rotation=70, ha=self.xtickalign, fontsize=10)

            # axarr[i].set_ylim(bottom=0.95)
            for spine in ['top', 'right', 'left', 'bottom']:
                axarr[i].spines[spine].set_visible(False)
            axarr[i].tick_params(axis='x',
                                 which='both',
                                 bottom=False,
                                 top=False,
                                 labelbottom=True)
            axarr[i].tick_params(labelsize=ticklabelsize + 1)
            if scol:
                # plt.setp(axarr[i].get_yticklabels(),visible=False)
                axarr[i].minorticks_off()

                # axarr[i].tick_params(axis='y', which='both', left='off', right='off', labelbottom='off')
            else:
                plt.setp(axarr[i].get_yticklabels(), visible=True)
                axarr[i].tick_params(axis='y',
                                     which='both',
                                     left=True,
                                     right=False,
                                     labelbottom=True)
                # plt.setp(axarr[i].get_yticks(),visible=False)

        axarr[-1].legend(legends[0:len(self.color_tags)],
                         self.color_tags,
                         loc='center left',
                         handlelength=1,
                         handletextpad=1,
                         columnspacing=2,
                         borderaxespad=0.,
                         prop={'size': ticklabelsize + 1},
                         bbox_to_anchor=(1.05, 0.5))
        # f.tight_layout(pad=2, h_pad=None, w_pad=None)
        # f.tight_layout()
        self.fig = f
Пример #36
0
    def doChooch(self, elt, edge, scan_directory, archive_directory, prefix):
        symbol = "_".join((elt, edge))
        scan_file_prefix = os.path.join(scan_directory, prefix)
        archive_file_prefix = os.path.join(archive_directory, prefix)

        if os.path.exists(scan_file_prefix + ".raw"):
            i = 1
            while os.path.exists(scan_file_prefix + "%d.raw" % i):
                i = i + 1
            scan_file_prefix += "_%d" % i
            archive_file_prefix += "_%d" % i

        scan_file_raw_filename = \
            os.path.extsep.join((scan_file_prefix, "raw"))
        archive_file_raw_filename = \
            os.path.extsep.join((archive_file_prefix, "raw"))
        scan_file_efs_filename = \
            os.path.extsep.join((scan_file_prefix, "efs"))
        archive_file_efs_filename = \
            os.path.extsep.join((archive_file_prefix, "efs"))
        scan_file_png_filename = \
            os.path.extsep.join((scan_file_prefix, "png"))
        archive_file_png_filename = \
            os.path.extsep.join((archive_file_prefix, "png"))

        try:
            if not os.path.exists(scan_directory):
                os.makedirs(scan_directory)
            if not os.path.exists(archive_directory):
                os.makedirs(archive_directory)
        except:
            logging.getLogger("HWR").exception(\
                 "EMBLEnergyScan: could not create results directory.")
            self.store_energy_scan()
            self.emit("energyScanFailed", ())
            return

        try:
            scan_file_raw = open(scan_file_raw_filename, "w")
            archive_file_raw = open(archive_file_raw_filename, "w")
        except:
            logging.getLogger("HWR").exception(\
                 "EMBLEnergyScan: could not create results raw file")
            self.store_energy_scan()
            self.emit("energyScanFailed", ())
            return
        else:
            scanData = []
            for i in range(len(self.scan_data)):
                x = float(self.scan_data[i][0])
                x = x < 1000 and x * 1000.0 or x
                y = float(self.scan_data[i][1])
                scanData.append((x, y))
                scan_file_raw.write("%f,%f\r\n" % (x, y))
                archive_file_raw.write("%f,%f\r\n" % (x, y))
            scan_file_raw.close()
            archive_file_raw.close()
            self.scan_info["scanFileFullPath"] = str(scan_file_raw_filename)

        pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, chooch_graph_data = \
             PyChooch.calc(scanData, elt, edge, scan_file_efs_filename)

        rm = (pk + 30) / 1000.0
        pk = pk / 1000.0
        savpk = pk
        ip = ip / 1000.0
        comm = ""
        #IK TODO clear this
        self.scan_info['edgeEnergy'] = 0.1
        self.th_edge = self.scan_info['edgeEnergy']
        logging.getLogger("HWR").info(\
              "th. Edge %s ; chooch results are pk=%f, ip=%f, rm=%f" % \
              (self.th_edge, pk, ip, rm))

        #should be better, but OK for time being
        """
        self.th_edgeThreshold = 0.01
        if math.fabs(self.th_edge - ip) > self.thEdgeThreshold:
          pk = 0
          ip = 0
          rm = self.th_edge + 0.03
          comm = "Calculated peak (%f) is more that 10eV away from the " + \
                 "theoretical value (%f). Please check your scan" % \
                 (savpk, self.th_edge)

          logging.getLogger("HWR").warning("EnergyScan: calculated peak " + \
                  "(%f) is more that 20eV %s the theoretical value (%f). " + \
                  "Please check your scan and choose the energies manually" % \
                   (savpk, (self.th_edge - ip) > 0.02 and "below" or "above", self.thEdge))
        """

        try:
            fi = open(scan_file_efs_filename)
            fo = open(archive_file_efs_filename, "w")
        except:
            self.store_energy_scan()
            self.emit("energyScanFailed", ())
            return
        else:
            fo.write(fi.read())
            fi.close()
            fo.close()

        self.scan_info["peakEnergy"] = pk
        self.scan_info["inflectionEnergy"] = ip
        self.scan_info["remoteEnergy"] = rm
        self.scan_info["peakFPrime"] = fpPeak
        self.scan_info["peakFDoublePrime"] = fppPeak
        self.scan_info["inflectionFPrime"] = fpInfl
        self.scan_info["inflectionFDoublePrime"] = fppInfl
        self.scan_info["comments"] = comm
        self.scan_info["choochFileFullPath"] = scan_file_efs_filename
        self.scan_info["filename"] = archive_file_raw_filename
        self.scan_info["workingDirectory"] = archive_directory

        chooch_graph_x, chooch_graph_y1, chooch_graph_y2 = zip(
            *chooch_graph_data)
        chooch_graph_x = list(chooch_graph_x)
        for i in range(len(chooch_graph_x)):
            chooch_graph_x[i] = chooch_graph_x[i] / 1000.0

        #logging.getLogger("HWR").info("EMBLEnergyScan: Saving png" )
        # prepare to save png files
        title = "%s  %s  %s\n%.4f  %.2f  %.2f\n%.4f  %.2f  %.2f" % \
              ("energy", "f'", "f''", pk, fpPeak, fppPeak, ip, fpInfl, fppInfl)
        fig = Figure(figsize=(15, 11))
        ax = fig.add_subplot(211)
        ax.set_title("%s\n%s" % (scan_file_efs_filename, title))
        ax.grid(True)
        ax.plot(*(zip(*scanData)), **{"color": 'black'})
        ax.set_xlabel("Energy")
        ax.set_ylabel("MCA counts")
        ax2 = fig.add_subplot(212)
        ax2.grid(True)
        ax2.set_xlabel("Energy")
        ax2.set_ylabel("")
        handles = []
        handles.append(ax2.plot(chooch_graph_x, chooch_graph_y1, color='blue'))
        handles.append(ax2.plot(chooch_graph_x, chooch_graph_y2, color='red'))
        canvas = FigureCanvasAgg(fig)

        self.scan_info["jpegChoochFileFullPath"] = str(
            archive_file_png_filename)
        try:
            logging.getLogger("HWR").info("Rendering energy scan and Chooch " + \
                 "graphs to PNG file : %s", scan_file_png_filename)
            canvas.print_figure(scan_file_png_filename, dpi=80)
        except:
            logging.getLogger("HWR").exception("could not print figure")
        try:
            logging.getLogger("HWR").info("Saving energy scan to archive " +\
                 "directory for ISPyB : %s", archive_file_png_filename)
            canvas.print_figure(archive_file_png_filename, dpi=80)
        except:
            logging.getLogger("HWR").exception("could not save figure")

        self.store_energy_scan()

        logging.getLogger("HWR").info("<chooch> returning")
        self.emit('choochFinished',
                  (pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, rm,
                   chooch_graph_x, chooch_graph_y1, chooch_graph_y2, title))
        return pk, fppPeak, fpPeak, ip, fppInfl, fpInfl, rm, chooch_graph_x, \
                 chooch_graph_y1, chooch_graph_y2, title
#TODO: remove all the ticks (both axes), and tick labels on the Y axis

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg

fig = plt.figure()
canvas = FigureCanvasAgg(fig)

languages = np.array(['Python','SQL','Java','C++','JavaScript'])
pos = np.arange(len(languages))
popularity = [56,39,34,34,29]
plt.bar(pos, popularity, align = 'center')
plt.xticks(pos, languages)
plt.ylabel('% popularity')
plt.title('Top 5 Languages for Math & Data \n by % popularity on Stack Overflow', alpha=0.8)

plt.tick_params(top=False, bottom=False, left=False, right=False, labelleft=False, labelbottom=True)

canvas.print_figure('images/week2_finaltest1.png')

Пример #38
0
def analyse_couleurs():
    import io
    import urllib
    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas

    global progress
    progress = 10
    n_colors = int(request.form['nbbins'])
    nbsamples = int(request.form['nbsamples'])
    opacity = float(request.form['opacity'])
    satlum = request.form['satlum']
    ct = float(request.form['sizecoef'])
    srcimage = request.form['srcimage']
    if (srcimage[:4] == "http"):
        srcimage = io.StringIO.StringIO(urllib.urlopen(srcimage).read())

    # Opens image to an array of RGB colors
    img = Image.open(srcimage)

    # Convert to floats instead of the default 8 bits integer coding. Dividing by
    # 255 is important so that plt.imshow behaves works well on float data (need to
    # be in the range [0-1])
    img = np.array(img, dtype=np.uint8)

    # Convert to HSV color space
    imgHSV = color.rgb2hsv(img)

    # Load Image and transform to a 2D numpy array.
    w, h, d = original_shape = tuple(imgHSV.shape)
    assert d == 3
    image_array = np.reshape(imgHSV, (w * h, d))

    # print("Fitting model on a small sub-sample of the data")
    t0 = time()
    image_array_sample = shuffle(image_array,
                                 random_state=0,
                                 n_samples=nbsamples)[:1000]
    kmeans = KMeans(n_clusters=n_colors,
                    random_state=0,
                    n_jobs=1,
                    max_iter=200,
                    init='k-means++',
                    algorithm='elkan',
                    precompute_distances=True).fit(image_array_sample)
    #print("done in %0.3fs." % (time() - t0))
    progress = 50

    # Get labels for all points
    #print("Predicting color indices on the full image (k-means)")
    t0 = time()
    labels = kmeans.predict(image_array)
    couleurs = kmeans.cluster_centers_
    print("done in %0.3fs." % (time() - t0))
    # Counting pixels in color bins, for sorting and circles dimensioning
    unique, counts = np.unique(labels, return_counts=True)
    progress = 60

    ro = math.pi / 2
    maxc = max(counts)
    rcmc = math.sqrt(maxc)
    fig = plt.figure()
    ax = plt.subplot(111, projection='polar')
    ax.set_theta_zero_location("N")
    ax.set_rmax(1)
    ax.set_rticks([0.25, 0.5, 0.75, 1])  # less radial ticks
    ax.set_rlabel_position(-22.5)  # get radial labels away from plotted line
    ax.grid(True)

    cp = dict(zip(range(0, n_colors), counts))
    co = sorted(cp.items(), key=operator.itemgetter(1), reverse=True)

    # Figure-drawing loop
    for i in range(0, n_colors):
        ht = -((math.pi * 2) *
               couleurs[co[i][0]][0]) + ro  # Color hue coefficient
        if (satlum == "sat"):  # Radial axis : sat or lum component
            ar = couleurs[co[i][0]][1]
        else:
            ar = couleurs[co[i][0]][2]
        x = ar * math.cos(ht)  # Conversion of ht to X pos
        y = ar * math.sin(ht)  # Conversion of ht to Y pos
        va = counts[co[i][0]]  # Raw size of color bin
        ra = ((math.sqrt(va) / rcmc) /
              2) * ct  # Adapted size for display, relative to the max size
        at = (np.array([[couleurs[co[i][0]]]]) * 255).astype(np.uint8)
        c = color.hsv2rgb(at)  # Retro-conversion to RGB for plotting
        tc = plt.Circle((x, y),
                        ra,
                        transform=ax.transProjectionAffine + ax.transAxes,
                        color=c[0][0].astype(np.float),
                        alpha=opacity,
                        clip_on=False)  # Circle drawing
        ax.add_artist(tc)

    # Figure to PNG export
    progress = 70
    canvas = FigureCanvas(fig)
    png_output = io.StringIO.StringIO()
    canvas.print_png(png_output)
    progress = 80
    data = png_output.getvalue().encode('base64')
    data_url = 'data:image/png;base64,{}'.format(
        urllib.quote(data.rstrip('\n')))
    progress = 100
    return data_url
Пример #39
0
def figure_response(fig, request, adjust=None, **kwargs):
    canvas = FigureCanvasAgg(fig)
    figdata = io.BytesIO()
    canvas.print_png(figdata, bbox_inches='tight', pad_inches=0.1, **kwargs)
    response = HttpResponse(figdata.getvalue(), content_type='image/png')
    return response
Пример #40
0
"""
Example of matplotlib colormaps
http://matplotlib.org/api/figure_api.html#matplotlib.figure.Figure.colorbar
"""

import numpy as np

import matplotlib
from matplotlib import figure
from matplotlib.backends.backend_agg import (FigureCanvasAgg as FigureCanvas)

data = np.random.randn(50, 50)

fig = figure.Figure()

canvas = FigureCanvas(fig)

ax1 = fig.add_subplot(1, 1, 1)
plt1 = ax1.imshow(data, cmap='RdBu')
cbar = fig.colorbar(plt1, ax=ax1, fraction=0.045)
cbar.set_label("Random Data")

canvas.print_figure('../figures/cmapaxis.png', facecolor='lightgray')
def plot_flu_prediction8(fig_root_dir, images, gt_maps, pr_maps, cut_folder, unet_fl1_dir, unet_fl2_dir, image_ids, nb_images, rand_seed = 3, colorbar = True):
	import matplotlib.pyplot as plt
	from matplotlib.backends.backend_agg import FigureCanvasAgg
	from matplotlib.figure import Figure
	from skimage import io
	import random
	seed = rand_seed #3
	random.seed(seed)
	font_size = 28; label_size = 18
	#indices = random.sample(range(gt_maps.shape[0]),nb_images)
	rows, cols, size = nb_images,4,6
	
	pre_select_fnames = ['f0_t0_i0_ch0_c2_r3_z1_mhilbert.png', 'f0_t4_i0_ch0_c4_r1_z0_mhilbert.png', 
				'f0_t4_i0_ch0_c2_r2_z1_mhilbert.png', 'f0_t4_i0_ch0_c1_r0_z1_mhilbert.png']
	
	indices = [image_ids.index(fname) for fname in pre_select_fnames]
	
	for i in range(len(indices)):
		example_folder = os.path.join(fig_root_dir, 'example_{:03d}'.format(i)); generate_folder(example_folder)
		cols, rows = 1, 1
		fig1 = Figure(tight_layout=True,figsize=(size-1.0, size-1.2))
		fig2 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig3 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig4 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig5 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig6 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig7 = Figure(tight_layout=True,figsize=(size, size-1.2))
		ax = fig1.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		bx = fig2.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		cx = fig3.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		dx = fig4.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		ex = fig5.subplots(nrows=rows,ncols=cols);
		fx = fig6.subplots(nrows=rows,ncols=cols);
		gx = fig7.subplots(nrows=rows,ncols=cols);
		idx = indices[i]
		image, gt_map, pr_map = images[idx,:].squeeze(), gt_maps[idx,:].squeeze(), pr_maps[idx,:].squeeze()
		image = np.uint8((image-image.min())/(image.max()-image.min())*255)
		err_map = np.abs(gt_map-pr_map)*255; err_map_rgb = np.zeros(image.shape).astype(np.uint8); err_map_rgb[:,:,:-1] = err_map
		gt_map_rgb = np.zeros(image.shape,dtype=np.uint8); # gt_map_rgb[:,:,:-1]=np.uint8((gt_map-gt_map.min())/(gt_map.max()-gt_map.min())*255)
		gt_map_rgb[:,:,:-1]=np.uint8(gt_map*255)
		pr_map_rgb = np.zeros(image.shape,dtype=np.uint8); # pr_map_rgb[:,:,:-1]=np.uint8((pr_map-pr_map.min())/(pr_map.max()-pr_map.min())*255)
		pr_map_rgb[:,:,:-1]=np.uint8(pr_map*255)
		unpaired_map = io.imread(cut_folder+'/{}'.format(image_ids[idx]))
		unet_fl1 = io.imread(unet_fl1_dir+'/pred_{}'.format(image_ids[idx].replace('png','tif'))); unet_fl2 = io.imread(unet_fl2_dir+'/pred_{}'.format(image_ids[idx].replace('png','tif')))
		unet_fl1 = unet_fl1/unet_fl1.max(); unet_fl2 = unet_fl2/unet_fl2.max()
		unet_map = np.stack([unet_fl1, unet_fl2], axis = -1); print(unet_map.min(), unet_map.max())
		unet_err_map = np.abs(gt_map-unet_map)*255; unet_err_map_rgb = np.zeros(image.shape).astype(np.uint8); unet_err_map_rgb[:,:,:-1] = unet_err_map
		unet_map_rgb = np.zeros(image.shape,dtype=np.uint8);
		unet_map_rgb[:,:,:-1]=np.uint8(unet_map*255)

		cax = ax.imshow(image); #cbar = fig1.colorbar(cax, ax = ax, shrink = 0.68); cbar.ax.tick_params(labelsize=label_size)
		cbx = bx.imshow(gt_map_rgb); cbar = fig2.colorbar(cbx, ax = bx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		ccx = cx.imshow(pr_map_rgb); cbar = fig3.colorbar(ccx, ax = cx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		cdx = dx.imshow(err_map_rgb); cbar = fig4.colorbar(cdx, ax = dx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		cex = ex.imshow(unpaired_map); cbar = fig5.colorbar(cdx, ax = ex, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		cfx = fx.imshow(unet_map_rgb); cbar = fig6.colorbar(cdx, ax = fx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		cgx = gx.imshow(unet_err_map_rgb); cbar = fig7.colorbar(cdx, ax = gx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		ax.tick_params(axis = 'x', labelsize = label_size); ax.tick_params(axis = 'y', labelsize = label_size);
		bx.tick_params(axis = 'x', labelsize = label_size); bx.tick_params(axis = 'y', labelsize = label_size);
		cx.tick_params(axis = 'x', labelsize = label_size); cx.tick_params(axis = 'y', labelsize = label_size);
		dx.tick_params(axis = 'x', labelsize = label_size); dx.tick_params(axis = 'y', labelsize = label_size);
		ex.tick_params(axis = 'x', labelsize = label_size); ex.tick_params(axis = 'y', labelsize = label_size);
		fx.tick_params(axis = 'x', labelsize = label_size); fx.tick_params(axis = 'y', labelsize = label_size);
		gx.tick_params(axis = 'x', labelsize = label_size); gx.tick_params(axis = 'y', labelsize = label_size);
		canvas = FigureCanvasAgg(fig1); canvas.print_figure(example_folder+'/Image.png', dpi=120)
		canvas = FigureCanvasAgg(fig2); canvas.print_figure(example_folder+'/Ground_truth.png', dpi=120)
		canvas = FigureCanvasAgg(fig3); canvas.print_figure(example_folder+'/EfficientNet_Prediction.png', dpi=120)
		canvas = FigureCanvasAgg(fig4); canvas.print_figure(example_folder+'/EfficientNet_Error_map.png', dpi=120)
		canvas = FigureCanvasAgg(fig5); canvas.print_figure(example_folder+'/Unpaired_pred.png', dpi=120)
		canvas = FigureCanvasAgg(fig6); canvas.print_figure(example_folder+'/UNet_Prediction.png', dpi=120)
		canvas = FigureCanvasAgg(fig7); canvas.print_figure(example_folder+'/UNet_Error_map.png', dpi=120)
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_agg import FigureCanvasAgg
import pandas as pd

linear_data = np.array([1, 2, 3, 4, 5, 6, 7, 8])
quadratic_data = linear_data**2

fig = plt.figure()
canvas = FigureCanvasAgg(fig)

observation_dates = np.arange('2017-01-01',
                              '2017-01-09',
                              dtype='datetime64[D]')
observation_dates = list(map(pd.to_datetime, observation_dates))

plt.plot(observation_dates, linear_data, '-o', observation_dates,
         quadratic_data, '-o')

x = plt.gca().xaxis

for item in x.get_ticklabels():
    item.set_rotation(45)  #rotates the tick labels for readablility

plt.subplots_adjust(
    bottom=0.25)  #adjusts the image bc its running off of the screen

canvas.print_figure('images/week2_14.png')
Пример #43
0
def airmassplot(request, transient_id, obs_id, telescope_id):
    import random
    import django
    import datetime
    from astroplan.plots import plot_airmass

    from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
    from matplotlib.figure import Figure
    from matplotlib.dates import DateFormatter
    from matplotlib import rcParams
    rcParams['figure.figsize'] = (7, 7)

    transient = Transient.objects.get(pk=transient_id)
    if int(obs_id):
        obsnight = ClassicalObservingDate.objects.get(pk=obs_id)
        obs_date = obsnight.obs_date
    else:
        obs_date = datetime.date.today()  #time.now()

    telescope = Telescope.objects.get(pk=telescope_id)

    target = SkyCoord(transient.ra, transient.dec, unit=u.deg)
    time = Time(str(obs_date).split('+')[0], format='iso')

    location = EarthLocation.from_geodetic(telescope.longitude * u.deg,
                                           telescope.latitude * u.deg,
                                           telescope.elevation * u.m)
    tel = Observer(location=location, name=telescope.name, timezone="UTC")

    fig = Figure()
    ax = fig.add_subplot(111)
    canvas = FigureCanvas(fig)

    ax.set_title("%s, %s, %s" %
                 (telescope.tostring(), transient.name, obs_date))

    night_start = tel.twilight_evening_astronomical(time, which="previous")
    night_end = tel.twilight_morning_astronomical(time, which="previous")
    if night_end < night_start:
        night_end = tel.twilight_morning_astronomical(time, which="next")
    delta_t = night_end - night_start
    observe_time = night_start + delta_t * np.linspace(0, 1, 75)
    plot_airmass(target, tel, observe_time, ax=ax)

    yr, mn, day, hr, minu, sec = night_start.iso.replace(':', ' ').replace(
        '-', ' ').split()
    starttime = datetime.datetime(int(yr), int(mn), int(day), int(hr),
                                  int(minu))
    if int(hr) == 0:
        xlow = datetime.datetime(int(yr), int(mn), int(day) - 1, 23, int(minu))
    else:
        xlow = datetime.datetime(int(yr), int(mn), int(day),
                                 int(hr) - 1, int(minu))
    yr, mn, day, hr, minu, sec = night_end.iso.replace(':', ' ').replace(
        '-', ' ').split()
    endtime = datetime.datetime(int(yr), int(mn), int(day), int(hr), int(minu))
    xhi = datetime.datetime(int(yr), int(mn), int(day), int(hr) + 1, int(minu))
    ax.axvline(starttime, color='r',
               label='18 deg twilight')  #night_start.iso)
    ax.axvline(endtime, color='r')
    ax.legend(loc='lower right')
    ax.set_xlim([xlow, xhi])

    response = django.http.HttpResponse(content_type='image/png')
    canvas.print_jpg(response)
    return response
def plot_flu_prediction2(fig_root_dir, images, gt_maps, pr_maps, nb_images, rand_seed = 3, colorbar = True):
	import matplotlib.pyplot as plt
	from matplotlib.backends.backend_agg import FigureCanvasAgg
	from matplotlib.figure import Figure
	import random
	from skimage import io
	seed = rand_seed #3
	random.seed(seed)
	font_size = 28; label_size = 18
	indices = random.sample(range(gt_maps.shape[0]),nb_images)
	rows, cols, size = nb_images,4,6

	for i in range(len(indices)):
		example_folder = os.path.join(fig_root_dir, 'example_{:03d}'.format(i)); generate_folder(example_folder)
		cols, rows = 1, 1
		fig1 = Figure(tight_layout=True,figsize=(size-1.0, size-1.2))
		fig2 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig3 = Figure(tight_layout=True,figsize=(size, size-1.2))
		fig4 = Figure(tight_layout=True,figsize=(size, size-1.2))
		ax = fig1.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		bx = fig2.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		cx = fig3.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		dx = fig4.subplots(nrows=rows,ncols=cols); #ax.set_xticks([]);ax.set_yticks([])
		idx = indices[i]
		image, gt_map, pr_map = images[idx,:].squeeze(), gt_maps[idx,:].squeeze(), pr_maps[idx,:].squeeze()
		image = np.uint8((image-image.min())/(image.max()-image.min())*255)
		err_map = np.abs(gt_map-pr_map)*255; err_map_rgb = np.zeros(image.shape).astype(np.uint8); err_map_rgb[:,:,:-1] = err_map
		gt_map_rgb = np.zeros(image.shape,dtype=np.uint8); # gt_map_rgb[:,:,:-1]=np.uint8((gt_map-gt_map.min())/(gt_map.max()-gt_map.min())*255)
		gt_map_rgb[:,:,:-1]=np.uint8(gt_map*255)
		pr_map_rgb = np.zeros(image.shape,dtype=np.uint8); # pr_map_rgb[:,:,:-1]=np.uint8((pr_map-pr_map.min())/(pr_map.max()-pr_map.min())*255)
		pr_map_rgb[:,:,:-1]=np.uint8(pr_map*255)
		cax = ax.imshow(image); #cbar = fig1.colorbar(cax, ax = ax, shrink = 0.68); cbar.ax.tick_params(labelsize=label_size)
		cbx = bx.imshow(gt_map_rgb); cbar = fig2.colorbar(cbx, ax = bx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		ccx = cx.imshow(pr_map_rgb); cbar = fig3.colorbar(ccx, ax = cx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		cdx = dx.imshow(err_map_rgb); cbar = fig4.colorbar(cdx, ax = dx, shrink = 0.97); cbar.ax.tick_params(labelsize=label_size)
		ax.tick_params(axis = 'x', labelsize = label_size); ax.tick_params(axis = 'y', labelsize = label_size);
		bx.tick_params(axis = 'x', labelsize = label_size); bx.tick_params(axis = 'y', labelsize = label_size);
		cx.tick_params(axis = 'x', labelsize = label_size); cx.tick_params(axis = 'y', labelsize = label_size);
		dx.tick_params(axis = 'x', labelsize = label_size); dx.tick_params(axis = 'y', labelsize = label_size);
		canvas = FigureCanvasAgg(fig1); canvas.print_figure(example_folder+'/Image.png', dpi=120)
		canvas = FigureCanvasAgg(fig2); canvas.print_figure(example_folder+'/Ground_truth.png', dpi=120)
		canvas = FigureCanvasAgg(fig3); canvas.print_figure(example_folder+'/Prediction.png', dpi=120)
		canvas = FigureCanvasAgg(fig4); canvas.print_figure(example_folder+'/Error_map.png', dpi=120)
Пример #45
0
def satellite_track_variability_plot(request,
                                     track_id=0,
                                     width=800,
                                     type='fold'):
    track = SatelliteTracks.objects.get(id=track_id)
    records = track.satelliterecords_set.order_by('time')

    if track.satellite.catalogue == 2:
        assert_permission(request, 'fweb.access_satellites_all')

    fig = Figure(facecolor='white',
                 figsize=(width / 72, width * 0.5 / 72),
                 tight_layout=True)
    ax = fig.add_subplot(111)
    ax.autoscale()

    time_start = satellite_track_start(track)
    time_end = satellite_track_end(track)
    T = (time_end - time_start).total_seconds()
    if track.variability == 2:
        P = track.variability_period
    else:
        P = None

    xs = []
    ys = []
    fs = []

    for r in records:
        x = (r.time - time_start).total_seconds()

        y = r.stdmag

        color = favor2_filters[
            r.filter_id] if r.filter_id else favor2_filters[0]
        if r.penumbra:
            f = color + [0.3]
        else:
            f = color + [1.0]

        xs.append(x)
        ys.append(y)
        fs.append(f)

    x = np.array(xs)
    y = np.array(ys)

    fit = np.polyfit(x, y, 2)
    y0 = np.polyval(fit, x)

    # ax.plot((0,0), (min(ys), max(ys)), c='lightgray')
    # ax.plot((track.variability_period,track.variability_period), (min(ys), max(ys)), c='lightgray')

    #ax.scatter(xs, ys, c=fs, marker='.', edgecolors=f)

    if type == 'fold' and P:
        #P = refine_period(x, y-y0, P, detrend=False)

        if int(request.GET.get('refine', 0)):
            P = refine_track_period(track)

        px = np.mod(x, P)

        ax.scatter(px, y - y0, c=fs, marker='.', edgecolors=fs)
        ax.scatter(px - P, y - y0, c=fs, marker='.', edgecolors=fs)
        ax.scatter(px + P, y - y0, c=fs, marker='.', edgecolors=fs)

        ax.axvline(0, c='gray', alpha=0.5)
        ax.axvline(P, c='gray', alpha=0.5)

        if P != track.variability_period:
            ax.set_xlabel(
                "Time, seconds, folded to period %g (original %g) s" %
                (P, track.variability_period))
        else:
            ax.set_xlabel("Time, seconds, folded to period %g s" % (P))

        ax.set_ylabel("Standard Magnitude")
        ax.invert_yaxis()
    elif type == 'ls':
        freq = np.arange(1.0 / T, 5.0, 1.0 / T)
        ls = signal.lombscargle(x, y, 2 * np.pi * freq)

        #ax.semilogx(freq, ls)
        ax.loglog(freq, ls, drawstyle='steps')

        if P:
            ax.axvline(1.0 / P, c='gray', alpha=0.5)
        ax.set_xlabel("Frequency, Hz")
        ax.set_ylabel("Lomb-Scargle periodogram")
    elif type == 'pdm':
        pp = np.arange(P * 0.9, P * 1.1, P * 0.001)
        pdm = PDM(x, y - y0, 1.0 / pp)

        ax.plot(pp, pdm, drawstyle='steps')
        ax.set_xlabel("Period, seconds")
        ax.set_ylabel("Phase Dispersion Minimization")

    ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

    #fig.autofmt_xdate()

    # 10% margins on both axes
    ax.margins(0.0, 0.1)

    time_start_str = time_start.strftime(
        '%Y-%m-%d %H:%M:%S') + '.%03d' % np.round(
            1e-3 * time_start.microsecond)

    ax.set_title(
        "%s %d / %s, track at %s" %
        (satellite_catalogue(track.satellite.catalogue),
         track.satellite.catalogue_id, track.satellite.name, time_start_str))

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)

    return response
def plot_flu_prediction7(file_name, images, gt_maps, pr_maps, cut_folder, unet_fl1_dir, unet_fl2_dir, image_ids, nb_images, rand_seed = 3, colorbar = True):
	import matplotlib.pyplot as plt
	from matplotlib.backends.backend_agg import FigureCanvasAgg
	from matplotlib.figure import Figure
	from skimage import io
	import random
	seed = rand_seed #3
	random.seed(seed)
	font_size = 28; label_size = 20
	indices = random.sample(range(gt_maps.shape[0]),nb_images)

	pre_select_fnames = ['f0_t0_i0_ch0_c2_r3_z1_mhilbert.png', 'f0_t4_i0_ch0_c4_r1_z0_mhilbert.png', 
				'f0_t4_i0_ch0_c2_r2_z1_mhilbert.png', 'f0_t4_i0_ch0_c1_r0_z1_mhilbert.png']
	
	indices = [image_ids.index(fname) for fname in pre_select_fnames]

	rows, cols, size = nb_images,7,6
	widths = [0.8, 1, 1, 1, 1,1,1]; heights = [1, 1, 1, 1]; gs_kw = dict(width_ratios=widths, height_ratios=heights)

	fig = Figure(tight_layout=True,figsize=(size*cols, size*rows)); ax = fig.subplots(nrows=rows,ncols=cols, gridspec_kw=gs_kw)
	for i in range(len(indices)):
		idx = indices[i]
		image, gt_map, pr_map = images[idx,:].squeeze(), gt_maps[idx,:].squeeze(), pr_maps[idx,:].squeeze()
		image = np.uint8((image-image.min())/(image.max()-image.min())*255)
		err_map = np.abs(gt_map-pr_map)*255; err_map_rgb = np.zeros(image.shape).astype(np.uint8); err_map_rgb[:,:,:-1] = err_map
		gt_map_rgb = np.zeros(image.shape,dtype=np.uint8); # gt_map_rgb[:,:,:-1]=np.uint8((gt_map-gt_map.min())/(gt_map.max()-gt_map.min())*255)
		gt_map_rgb[:,:,:-1]=np.uint8(gt_map*255)
		pr_map_rgb = np.zeros(image.shape,dtype=np.uint8); # pr_map_rgb[:,:,:-1]=np.uint8((pr_map-pr_map.min())/(pr_map.max()-pr_map.min())*255)
		pr_map_rgb[:,:,:-1]=np.uint8(pr_map*255)
		cut_map = io.imread(cut_folder+'/{}'.format(image_ids[idx]))
		cx0 = ax[i,0].imshow(image); cx1 = ax[i,1].imshow(gt_map_rgb); 
		cx2 = ax[i,2].imshow(pr_map_rgb); cx3 = ax[i,3].imshow(err_map_rgb); cx4 = ax[i,6].imshow(cut_map);
		unet_fl1 = io.imread(unet_fl1_dir+'/pred_{}'.format(image_ids[idx].replace('png','tif'))); unet_fl2 = io.imread(unet_fl2_dir+'/pred_{}'.format(image_ids[idx].replace('png','tif')))
# 		unet_fl2 = np.zeros(unet_fl1.shape)
		unet_fl1 = unet_fl1/unet_fl1.max(); unet_fl2 = unet_fl2/unet_fl2.max()
		unet_map = np.stack([unet_fl1, unet_fl2], axis = -1); print(unet_map.min(), unet_map.max())
		#unet_map = unet_map/unet_map.max(); print(unet_map.min(), unet_map.max())
		unet_err_map = np.abs(gt_map-unet_map)*255; unet_err_map_rgb = np.zeros(image.shape).astype(np.uint8); unet_err_map_rgb[:,:,:-1] = unet_err_map
		unet_map_rgb = np.zeros(image.shape,dtype=np.uint8);
		unet_map_rgb[:,:,:-1]=np.uint8(unet_map*255)
		cx5 = ax[i,4].imshow(unet_map_rgb); cx6 = ax[i,5].imshow(unet_err_map_rgb); 
		ax[i,0].set_xticks([]);ax[i,0].set_yticks([]);
		#ax[i,0].tick_params(axis = 'x', labelsize = label_size);ax[i,0].tick_params(axis = 'y', labelsize = label_size);
		ax[i,1].set_xticks([]);ax[i,2].set_xticks([]);ax[i,3].set_xticks([]); ax[i,4].set_xticks([]); ax[i,5].set_xticks([]); ax[i,6].set_xticks([]);
		ax[i,1].set_yticks([]);ax[i,2].set_yticks([]);ax[i,3].set_yticks([]); ax[i,4].set_yticks([]); ax[i,5].set_yticks([]); ax[i,6].set_yticks([]);
		if colorbar:
			#fig.colorbar(cx0, ax = ax[i,0], shrink = 0); 
			cbar = fig.colorbar(cx1, ax = ax[i,1], shrink = 0.68); cbar.ax.tick_params(labelsize=label_size) 
			cbar = fig.colorbar(cx2, ax = ax[i,2], shrink = 0.68); cbar.ax.tick_params(labelsize=label_size) 
			cbar = fig.colorbar(cx3, ax = ax[i,3], shrink = 0.68); cbar.ax.tick_params(labelsize=label_size)
			cbar = fig.colorbar(cx4, ax = ax[i,4], shrink = 0.68); cbar.ax.tick_params(labelsize=label_size)
			cbar = fig.colorbar(cx5, ax = ax[i,5], shrink = 0.68); cbar.ax.tick_params(labelsize=label_size) 
			cbar = fig.colorbar(cx6, ax = ax[i,6], shrink = 0.68); cbar.ax.tick_params(labelsize=label_size) 
		if i == 0:
			ax[i,0].set_title('Image',fontsize=font_size);
			ax[i,1].set_title('Ground Truth',fontsize=font_size);
			ax[i,2].set_title('E-Net (Paired)',fontsize=font_size);
			ax[i,3].set_title('Err Map (E-Net)',fontsize=font_size);
			ax[i,4].set_title('U-Net (Paired)',fontsize=font_size);
			ax[i,5].set_title('Err Map (U-Net)',fontsize=font_size);
			ax[i,6].set_title('Unpaired Method',fontsize=font_size);
	fig.tight_layout(pad=-2)
	canvas = FigureCanvasAgg(fig); canvas.print_figure(file_name, dpi=120)
Пример #47
0
def satellite_track_plot(request, track_id=0, width=800, type='stdmag'):
    if type in ['fold', 'ls', 'pdm']:
        return satellite_track_variability_plot(request, track_id, width, type)

    track = SatelliteTracks.objects.get(id=track_id)
    records = track.satelliterecords_set.order_by('time')

    if track.satellite.catalogue == 2:
        assert_permission(request, 'fweb.access_satellites_all')

    fig = Figure(facecolor='white',
                 figsize=(width / 72, width * 0.5 / 72),
                 tight_layout=True)
    ax = fig.add_subplot(111)
    ax.autoscale()

    #time_start = track.satelliterecords_set.aggregate(Min('record__time')).get('record__time__min')
    time_start = satellite_track_start(track)

    for channel_id in [
            r.channel_id
            for r in track.satelliterecords_set.distinct('channel_id')
    ]:
        records = track.satelliterecords_set.filter(
            channel_id=channel_id).order_by('time')

        xs = []
        ys = []
        x_prev = None
        f_prev = None
        channel_prev = None

        for r in records:
            x = (r.time - time_start).total_seconds()

            if type == 'stdmag':
                y = r.stdmag
            elif type == 'mag':
                y = r.mag
            elif type == 'distance':
                y = r.distance
            elif type == 'phase':
                y = r.phase
            else:
                y = 0

            color = favor2_filters[
                r.filter_id] if r.filter_id else favor2_filters[0]
            if r.penumbra:
                f = color + [0.3]
            else:
                f = color + [1.0]

            channel = r.channel_id

            if (x_prev and x - x_prev > 0.5) or (f_prev and f != f_prev) or (
                    channel_prev and channel != channel_prev):
                ax.plot(xs, ys, marker='.', color=f_prev, lw=0.1, ms=3)
                xs = []
                ys = []

            xs.append(x)
            ys.append(y)

            x_prev = x
            f_prev = f
            channel_prev = channel

        # Finalize
        if x and y and f_prev:
            ax.plot(xs, ys, marker='.', color=f_prev, lw=0.1, ms=3)

    #ax.scatter(x, y, c=f, marker='.', edgecolors=f)
    #ax.plot(x, y, marker='.')

    #ax.xaxis.set_major_formatter(DateFormatter('%Y.%m.%d %H:%M:%S'))
    #ax.xaxis.set_major_formatter(DateFormatter('%H:%M:%S'))
    ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

    ax.set_xlabel("Time since track start, seconds")
    if type == 'stdmag':
        ax.set_ylabel("Standard Magnitude")
        ax.invert_yaxis()
    elif type == 'mag':
        ax.set_ylabel("Apparent Magnitude")
        ax.invert_yaxis()
    elif type == 'distance':
        ax.set_ylabel("Distance, km")
    elif type == 'phase':
        ax.set_ylabel("Phase angle, degrees")

    #fig.autofmt_xdate()

    # 10% margins on both axes
    ax.margins(0.1, 0.1)

    time_start_str = time_start.strftime(
        '%Y-%m-%d %H:%M:%S') + '.%03d' % np.round(
            1e-3 * time_start.microsecond)

    ax.set_title(
        "%s %d / %s, track at %s" %
        (satellite_catalogue(track.satellite.catalogue),
         track.satellite.catalogue_id, track.satellite.name, time_start_str))

    canvas = FigureCanvas(fig)
    if request.GET.get('format', 'png') == 'pdf':
        response = HttpResponse(content_type='application/pdf')
        canvas.print_figure(response, format='pdf')
    elif request.GET.get('format', 'png') == 'eps':
        response = HttpResponse(content_type='application/postscript')
        canvas.print_figure(response, format='eps')
    else:
        response = HttpResponse(content_type='image/png')
        canvas.print_png(response)

    return response
Пример #48
0
from mpl_toolkits.basemap import Basemap
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.collections import LineCollection
import csv

# requires pyshapelib from Thuban (http://thuban.intevation.org/). 
# cd to libraries/pyshapelib in Thuban source distribution, run 
# 'python setup.py install'. 

# Lambert Conformal map of lower 48 states. 
m = Basemap(llcrnrlon=-119,llcrnrlat=22,urcrnrlon=-64,urcrnrlat=49, 
            projection='lcc',lat_1=33,lat_2=45,lon_0=-95) 
fig=p.figure(figsize=(10,m.aspect*8))

canvas = FigureCanvas(fig)

# draw state boundaries. 
shp_info = m.readshapefile('statesp020','states',drawbounds=True) 

#generate empty lats and longs lists
latsList=[]
lonsList=[]

#Import previously generated CSV File, Write to a new list of lats and longs
with open('MapPlaces.csv') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=',')
    for row in spamreader:
        list(row)
        lons = float(row[0])
        lonsList.append(lons)
Пример #49
0
    def spectrum_command_finished(self):
        """
        Descript. :
        """
        with cleanup(self.ready_event.set):
            self.spectrum_info['endTime'] = time.strftime("%Y-%m-%d %H:%M:%S")
            self.spectrum_running = False

            xmin = 0
            xmax = 0
            mca_data = []
            calibrated_data = []

            try:
                spectrum_file_raw = open(
                    self.spectrum_info["scanFileFullPath"], "w")
                archive_file_raw = open(self.spectrum_info["scanFilePath"],
                                        "w")
            except:
                logging.getLogger("HWR").exception(
                  "XRFSpectrum: could not create spectrum result raw file %s" % \
                  self.spectrum_info["scanFileFullPath"])

            for n, value in enumerate(self.spectrum_data):
                energy = (self.mca_calib[2] + \
                          self.mca_calib[1] * n + \
                          self.mca_calib[0] * n * n) / 1000
                if energy < 20:
                    if energy > xmax:
                        xmax = value
                    if energy < xmin:
                        xmin = value
                    calibrated_data.append([energy, value])
                    mca_data.append((n / 1000.0, value))
                    if spectrum_file_raw:
                        spectrum_file_raw.write("%f,%f\r\n" % (energy, value))
                    if archive_file_raw:
                        archive_file_raw.write("%f,%f\r\n" % (energy, value))
            if spectrum_file_raw:
                spectrum_file_raw.close()
            if archive_file_raw:
                archive_file_raw.close()
            calibrated_array = numpy.array(calibrated_data)

            if self.transmission_hwobj is not None:
                self.spectrum_info["beamTransmission"] = \
                   self.transmission_hwobj.getAttFactor()
            self.spectrum_info["energy"] = self.get_current_energy()
            if self.beam_info_hwobj is not None:
                beam_size = self.beam_info_hwobj.get_beam_size()
                self.spectrum_info["beamSizeHorizontal"] = int(beam_size[0] *
                                                               1000)
                self.spectrum_info["beamSizeVertical"] = int(beam_size[1] *
                                                             1000)

            mca_config = {}
            mca_config["legend"] = self.spectrum_info["filename"]
            mca_config["file"] = self.config_filename
            mca_config["min"] = xmin
            mca_config["max"] = xmax
            mca_config["htmldir"] = self.spectrum_info["htmldir"]
            self.spectrum_info.pop("htmldir")
            self.spectrum_info.pop("scanFilePath")

            fig = Figure(figsize=(15, 11))
            ax = fig.add_subplot(111)
            ax.set_title(self.spectrum_info["jpegScanFileFullPath"])
            ax.grid(True)

            ax.plot(*(zip(*calibrated_array)), **{"color": 'black'})
            ax.set_xlabel("Energy")
            ax.set_ylabel("Counts")
            canvas = FigureCanvasAgg(fig)
            logging.getLogger().info("XRFSpectrum: Rendering spectrum to PNG file : %s", \
                                     self.spectrum_info["jpegScanFileFullPath"])
            canvas.print_figure(self.spectrum_info["jpegScanFileFullPath"],
                                dpi=80)
            #logging.getLogger().debug("Copying .fit file to: %s", a_dir)
            #tmpname=filename.split(".")
            #logging.getLogger().debug("finished %r", self.spectrum_info)
            self.store_xrf_spectrum()
            self.emit('xrfSpectrumFinished',
                      (mca_data, self.mca_calib, mca_config))
Пример #50
0
def satellite_plot(request, id=0, width=800, type='stdmag'):
    satellite = Satellites.objects.get(id=id)

    if satellite.catalogue == 2:
        assert_permission(request, 'fweb.access_satellites_all')

    x = []
    y = []
    f = []

    if type == 'period':
        tracks = satellite.satellitetracks_set.all()
        for t in tracks:
            if t.variability == 2:
                x.append(t.satelliterecords_set.all()[0].time)
                y.append(t.variability_period)
    else:
        records = satellite.satelliterecords_set.all()
        for r in records:
            if type == 'phase_stdmag':
                if r.penumbra:
                    continue
                x.append(r.phase)
            else:
                x.append(r.time)

            if type == 'stdmag' or type == 'phase_stdmag':
                y.append(r.stdmag)
            elif type == 'mag':
                y.append(r.mag)
            elif type == 'distance':
                y.append(r.distance)
            elif type == 'phase':
                y.append(r.phase)
            else:
                y.append(0)

            color = favor2_filters[
                r.filter_id] if r.filter_id else favor2_filters[0]
            if r.penumbra:
                f.append(color + [0.3])
            else:
                f.append(color + [1.0])

    fig = Figure(facecolor='white',
                 figsize=(width / 72, width * 0.5 / 72),
                 tight_layout=True)
    ax = fig.add_subplot(111)
    ax.autoscale()

    if type == 'period':
        ax.scatter(x, y, c='r', marker='o')
    else:
        ax.scatter(x, y, c=f, marker='.', edgecolors=f, s=1)

    if type == 'phase_stdmag':
        ax.xaxis.set_major_formatter(ScalarFormatter(useOffset=False))
        ax.set_xlabel("Phase angle, degrees")
    else:
        ax.xaxis.set_major_formatter(DateFormatter('%Y.%m.%d'))
        ax.set_xlabel("Time, UT")

    ax.yaxis.set_major_formatter(ScalarFormatter(useOffset=False))

    if type == 'stdmag' or type == 'phase_stdmag':
        ax.set_ylabel("Standard Magnitude")
        ax.invert_yaxis()
    elif type == 'mag':
        ax.set_ylabel("Apparent Magnitude")
        ax.invert_yaxis()
    elif type == 'distance':
        ax.set_ylabel("Distance, km")
    elif type == 'phase':
        ax.set_ylabel("Phase angle, degrees")
    elif type == 'period':
        ax.set_ylabel("Light curve period, s")

    fig.autofmt_xdate()

    # 10% margins on both axes
    ax.margins(0.1, 0.1)

    ax.set_title("%s %d / %s" % (satellite_catalogue(
        satellite.catalogue), satellite.catalogue_id, satellite.name))

    canvas = FigureCanvas(fig)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)

    return response
Пример #51
0
def visualize_object(x,
                     y,
                     z,
                     transformation,
                     obj='stick',
                     ax=None,
                     fix_axes=True,
                     plot_origin=False,
                     plot_axis=False,
                     no_plot=False):
    if obj == 'cube':
        obj = CUBE
    elif obj == 'stick':
        obj = STICK
    else:
        raise Exception('No such object: Use either "stick" or "cube".')

    line_width = 6. if ax == None else 2.

    if ax == None:
        fig = plt.figure()
        canvas = FigureCanvas(fig)
        ax = fig.add_subplot(111,
                             projection='3d',
                             aspect='equal',
                             proj_type='ortho')
    else:
        fig = ax.figure
        canvas = FigureCanvas(fig)

    axis = transformation[:-1]
    axis /= np.linalg.norm(axis)
    quaternion = Quaternion(*axis, transformation[-1])

    rot_obj = []

    for i, p in enumerate(obj):
        new_point = quaternion.rotate(p)
        new_point += [x, y, z]
        rot_obj.append(new_point)

    rot_obj = np.array(rot_obj)

    for (fx, fy, fz), (tx, ty, tz) in zip(rot_obj[:-1], rot_obj[1:]):
        ax.plot([fx, tx], [fy, ty], [fz, tz], linewidth=line_width)
    ax.axis('off')
    if fix_axes:
        ax.set_xlim((-1.6, 1.6))
        ax.set_ylim((-1.6, 1.6))
        ax.set_zlim((-1.6, 1.6))
    if plot_origin:
        ax.scatter([0.], [0.], [0.], c='b', marker=',')
    if plot_axis:
        ax.quiver([0], [0], [0], [axis[0]], [axis[1]], [axis[2]])

    canvas.draw()
    s, (width, height) = canvas.print_to_buffer()

    im = np.fromstring(s, np.uint8).reshape(height, width, 4)
    min_col = np.min(np.where((im[:, :, 3] != 0.).any(axis=0)))
    max_col = np.max(np.where((im[:, :, 3] != 0.).any(axis=0)))
    min_row = np.min(np.where((im[:, :, 3] != 0.).any(axis=1)))
    max_row = np.max(np.where((im[:, :, 3] != 0.).any(axis=1)))
    im = im[min_row:max_row, min_col:max_col]
    im = cv2.resize(im,
                    tuple((np.array(im.shape)[[1, 0]] * 0.3).astype(np.int32)))

    if no_plot:
        plt.close()

    return im
Пример #52
0
 def print_figure(fig, *args, **kwargs):
     canvas = FigureCanvasAgg(fig)
     canvas.print_figure(*args, **kwargs)
Пример #53
0
def htmlReport(S,repFmt,headline,afterHead='',figures=[],dialect=None,figFmt=None,svgEmbed=False,show=False):
	'''
	Generate (X)HTML report for simulation.

	:param S: :obj:`Scene` object; must contain :obj:`Scene.pre`
	:param repFmt: format for the output file; will be expanded using :obj:`Scene.tags`; it should end with `.html` or `.xhtml`.
	:param headline: title of the report, used as HTML title and the first ``<h1>`` heading.
	:param afterHead: contents (XHTML fragment) to be added verbatim after the header (title, woo config table, preprocessor parameters)
	:param figs: figures included in the report; they are either embedded (SVG, optionally ) or saved to files in the same directory as the report, using its name as prefix, and referenced via relative links from the XHTML. Figures are given as list of 2- or 3-tuples, where each item contains:
	   
		 1. name of the figure (will generate a ``<h2>`` heading)
		 2. figure object (`Matplotlib <http://matplotlib.org>`__ figure object)
		 3. optionally, format to save the figure to (`svg`, `png`, ...)

	:param figFmt: format of figures, if the format is not specified by the figure itself; if None, choose format appropriate for given dialect (png for html4, svg for html5 and xhtml)
	:param dialect: one of "xhtml", "html5", "html4"; if not given (*None*), selected from file suffix (``.html`` for ``html4``, ``.xhtml`` for ``xhtml``). ``html4`` will save figures as PNG (even if something else is specified) so that the resulting file is easily importable into LibreOffice (which does not import HTML with SVG correctly now).
	:param svgEmbed: don't save SVG as separate files, embed them in the report instead. 
	:param show: open the report in browser via the webbrowser module, unless running in batch.
	:return: (filename of the report, list of external figures)

	'''
	import codecs, re, os.path
	import woo,woo.batch
	dialects=set(['html5','html4','xhtml'])
	if not dialect:
		if repFmt.endswith('.xhtml'): dialect='xhtml'
		elif repFmt.endswith('.html'): dialect='html4'
		else: raise ValueError("Unable to guess dialect (not given) from *repFmt* '%s' (not ending in .xhtml or .html)")
	if dialect not in dialects: raise ValueError("Unknown dialect '%s': must be one of "+', '.join(dialects)+'.')
	if figFmt==None: figFmt={'html5':'png','html4':'png','xhtml':'svg'}[dialect]
	if dialect in ('html5',) and svgEmbed and figFmt=='svg':
		svgEmbed=False
		warnings.warn("Dialect '%s' does not support embedded SVG -- will not be embedded."%dialect)
	if dialect in ('html4',) and not figFmt=='png':
		warnings.warn("Dialect '%s' will save images as 'png', not '%s'"%(dialect,figFmt))
		figFmt='png'
	repName=unicode(repFmt).format(S=S,**(dict(S.tags)))
	rep=codecs.open(repName,'w','utf-8','replace')
	print 'Writing report to file://'+os.path.abspath(repName)
	repBase=re.sub('\.x?html$','',repName)
	s=htmlReportHead(S,headline,dialect=dialect,repBase=repBase)
	s+=afterHead


	if figures: s+='<h2>Figures</h2>\n'
	extFiles=[]
	from matplotlib.backends.backend_agg import FigureCanvasAgg
	for ith,figspec in enumerate(figures):
		if len(figspec)==2: figspec=(figspec[0],figspec[1],figFmt)
		figName,figObj,figSuffix=figspec
		if not figName: figName='Figure %i'%(ith+1)
		s+='<h3>'+figName+'</h3>'
		canvas=FigureCanvasAgg(figObj)
		if figSuffix=='svg' and svgEmbed:
			figFile=woo.master.tmpFilename()+'.'+figSuffix
			figObj.savefig(figFile)
			s+=svgFileFragment(figFile)
		else: 
			figFile=repBase+'.'+re.sub('[^a-zA-Z0-9_-]','_',figName)+'.'+figSuffix
			figObj.savefig(figFile)
			s+='<img src="%s" alt="%s"/>'%(os.path.basename(figFile),figName)
			extFiles.append(os.path.abspath(figFile))
	s+='</body></html>'
	rep.write(s)
	rep.close() # flushed write buffers

	# attempt conversion to ODT, not under Windows
	if False and sys.platform!='win32':
		# TODO TODO TODO: have timeout on the abiword call; some versions stall forever
		def convertToOdt(html,odt):
			try:
				import subprocess
				# may raise OSError if the executable is not found
				out=subprocess.check_output(['abiword','--to=ODT','--to-name='+odt,html])
				print 'Report converted to ODT via Abiword: file://'+os.path.abspath(odt)
			except subprocess.CalledProcessError as e:
				print 'Report conversion to ODT failed (error when running Abiword), returning %d; the output was: %s.'%(e.returncode,e.output)
			except:
				print 'Report conversion to ODT not done (Abiword not installed?).'
		# if this is run in the background, main process may finish before that thread, leading potentially to some error messages??
		import threading
		threading.Thread(target=convertToOdt,args=(repName,repBase+'.odt')).start()
				
	if show and not woo.batch.inBatch():
		import webbrowser
		webbrowser.open('file://'+os.path.abspath(repName))
	return repName,extFiles
Пример #54
0
def postsubmit(request):
    if request.method == 'POST':
        df2 = request.POST['loicfile']
        df = pd.read_csv(str(df2), header=None)
        start = timeit.default_timer()

        y1 = df.iloc[0:100, 4].values
        y1 = np.where(y1 == 'Iris-setosa', -1, 1)
        x = df.iloc[0:100, [0, 2]].values
        pn = Perceptron(0.1, 10)
        pn.fit(x, y1)
        predictions = pn.predict(x)

        print(pn.score(x, y1))

        def plot_decision_regions(x, y1, classifier, resolution=0.02):
            markers = ('s', 'x', 'o', '^', 'v')
            colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
            cmap = ListedColormap(colors[:len(np.unique(y1))])
            x1_min, x1_max = x[:, 0].min() - 1, x[:, 0].max() + 1
            x2_min, x2_max = x[:, 1].min() - 1, x[:, 1].max() + 1
            xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                                   np.arange(x2_min, x2_max, resolution))
            Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
            Z = Z.reshape(xx1.shape)
            plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
            plt.xlim(xx1.min(), xx1.max())
            plt.ylim(xx2.min(), xx2.max())

            for idx, cl in enumerate(np.unique(y1)):
                plt.scatter(x=x[y1 == cl, 0],
                            y=x[y1 == cl, 1],
                            alpha=0.8,
                            c=cmap(idx),
                            marker=markers[idx],
                            label=cl)

        plot_decision_regions(x, y1, classifier=pn)
        plt.xlabel('sepal length')
        plt.ylabel('petal length')

        stop = timeit.default_timer()

        print("CPU Utilization: ")
        print(str(psutil.cpu_percent()))

        process = psutil.Process(os.getpid())
        mem = process.memory_percent()
        print("Memory Utilization for this process: ")
        print(mem)

        print("Running time: ")
        print(stop - start)

        s0 = "sepal-length"
        n = '\n'
        sd = psutil.cpu_percent()
        s1 = "CPU Utilization: "
        s2 = str(sd)
        s3 = "Memory Utilization: "
        s4 = str(mem)
        s5 = "Running time: "
        s6 = str(stop - start)

        s = s1 + s2 + n + s3 + s4 + "," + s5 + s6
        plt.title(s)
        #plt.figure(figsize=(20, 10))
        canvas = FigureCanvas(plt.figure(1))
        response = HttpResponse(content_type='image/png')
        canvas.print_png(response)
        return response
Пример #55
0
def prepare_args_multiprocessing(cubes,
                                 last_day_idx=None,
                                 regions=REGIONS,
                                 bounds=BOUNDS,
                                 figh=FIGH):
    """Initiate data for multiprocessing
    
    Initiate all data such that plotting jobs using :func:`plot_all_days` can 
    be performed parallel, where each job corresponds to a certain region
    (e.g. EUROPE) and parameter (e.g. od550aer).
    
    Loops over all cubes found in :param:`cubes` (e.g. N=5) and over all 
    regions in dictionary :param:`regions` (e.g. M=10) and crops cube 
    correspondingly within defined lon / lat region and between first and last 
    day (defined using :param:`last_day_idx`). That makes a list of length 
    MxN containing the prepared cubes for each species and region. Further, 
    corresponding lists of all other required input parameters for 
    :func:`plot_all_days` are created for each of the cropped cubes, that is, 
    lists of length MxN of matplotlib figure instances, longitude range, 
    latitude range, lower and upper range of colorbar and the corresponding ID 
    of the region). 
    
    In order to prepared the input
    
    
    Parameters
    ----------
    cubes : iris.cube.CubeList
        list containing cubes of different species
    last_day_idx : :obj:`int`, optional
        index of last day that is supposed to be plotted
    regions : dict
        dictionary specifying region IDs (keys) and corresponding lon / lat
        ranges
    bounds : dict
        dictionary specifying min / max ranges (values) for plotting of 
        different species (keys). The latter must correspond to the species 
        IDs as defined in the :attr:`var_name` of the different
        :class:`iris.cube.Cube` instances in :param:`cubes`. 
    
    Returns
    -------
    tuple
        7-element tuple, containing the following lists of length MxN
        
        - list: cropped cubes for each species (n (m $$\in{N}$$)) and\
            region (m $$\in{M}$$)
        - list: corresponding Figure instances for plotting
        - list: corresponding longitude ranges (tuple containing min / max)
        - list: corresponding latitude ranges (tuple containing min / max)
        - list: lower value for color-range (depends on species)
        - list: upper value for color-range (depends on species)
        - list: string ID of corresponding region
    """
    args = []
    if last_day_idx is None:
        last_day_idx = cubes[0].coord("time").points[-1]
    for cube in cubes:
        print(cube.var_name)
        for region, val in regions.items():
            print(region)
            lon_range, lat_range = val
            t0 = time()
            dat = funs.crop_cube_lonlat(cube, lon_range, lat_range)
            dat = dat[:last_day_idx]
            print("Elapsed time crop: %.4f" % (time() - t0))
            wfac = dat.coord("longitude").shape[0] / dat.coord(
                "latitude").shape[0]
            figw = figh * wfac + 2
            t0 = time()
            fig = Figure(figsize=(figw, figh), dpi=100)
            # create canvas to draw onto
            FigureCanvasAgg(fig)
            print("Elapsed time figure: %.4f" % (time() - t0))
            vmin, vmax = bounds[cube.var_name]

            args.append((dat, fig, lon_range, lat_range, float(vmin),
                         float(vmax), region))

    return tuple(args)
Пример #56
0
def fig_to_png(fig):
    """ Converts a matplotlib figure to a png (byte stream). """
    canvas = FigureCanvas(fig)
    output = io.BytesIO()
    canvas.print_png(output)
    return output
Пример #57
0
"""
使用matploamlib进行数据绘图
"""
from matplotlib.figure import Figure
from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas
# matplotlib命令与格式包括:画布和图像  Figure and Axis
# 创建自定义图像
# figure(num=None,figsize=None,dpi=None,facecolor=None,edgecolor=None,edgecolor=None,frameON=tRUE)
# NUM:图像编号或名称,数字为编号,字符串为名称
# figsize:指定figure的宽和高,单位为英寸
# dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80
# facecolor 背景颜色
# edgecolor 边框颜色
# frameon 是否显示边框
fig = Figure()
# 获得绘图对象
canvas = FigureCanvas(fig)
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
line, = ax.plot([0, 2], [0, 2])
# 图表标题
ax.set_title("a straight line ")
# x和 y 轴的标签
ax.set_xlabel("x label")
ax.set_ylabel("y label")
# 指定位置绘制图片
canvas.print_figure('./语法基础/res/image/chatpic1.jpg')
Пример #58
0
    def saveGraph(self):
        """Saves the data as a picture,
            after creating it"""

        colors = []
        'Creates the color array'
        for i in range(len(self.datasets)):
            colors.append([])
            for j in range(0, len(self.diskServers)):
                'Checks if there will be a divide by 0 error'
                if not self.accessesInDataset[i] == 0:
                    '''Divides the accesses by the number in its dataset,
                        to get the amount'''
                    amount = float(self.accesses[i][j]) / float(
                        self.accessesInDataset[i])
                else:
                    'else sets the amount to 0'
                    amount = 0

                if self.noOfFiles[i][j] == 0 and not (i == 0 and j == 0):
                    '''If the noOfFiles is 0, uses green as the color,
                        due to the colors not diplaying if all rgb values.
                        The green color cannot be the first in the array,
                        as this also causes an error'''
                    colors[i].append('g')
                else:
                    if not amount == 0:
                        'Calculates a log value for the amount'
                        amount = (math.log10(amount) / 4) + 1
                    colors[i].append([1.0, 1.0 - amount, 1.0 - amount])

        my_dpi = 100
        '''Create the size of the image.
            This is for ffmpeg (see animation.py) as it requires images
            with even heights and widths.

            As the backend is being used directly,
            a figure and a canvas is needed'''
        fig = Figure(figsize=(8, 6), dpi=my_dpi)
        canvas = FigureCanvas(fig)

        'matplotlib requires numpy arrays'
        data_array = np.array(self.noOfFiles)
        colors = np.array(colors)

        'Add a 3d subplot to the image'
        ax = fig.add_subplot(111, projection='3d')

        for i in range(len(self.diskServers)):
            'Gets rid of the start and end, so they will fit on the graph'
            self.diskServers[i] = self.diskServers[i].replace('gdss', '')
            self.diskServers[i] = self.diskServers[i].replace(
                '.gridpp.rl.ac.uk', '')

        'Sets the number of ticks on the x axis, the their names'
        ax.set_xticks(range(len(self.diskServers)))
        ax.set_xticklabels(self.diskServers, fontsize=6, rotation='vertical')

        'Adds the date to the graph'
        ax.text2D(0.05,
                  0.95,
                  "Date: " + str(date.today()) + ' ' +
                  str(datetime.now().hour) + ':00',
                  transform=ax.transAxes)

        'Set the axes labels'
        ax.set_xlabel('Disk server')
        ax.set_ylabel('Datasets')
        ax.set_zlabel('Count')

        'Create two arrays for the x and y positions of the bar'
        xpos, ypos = np.meshgrid(np.arange(data_array.shape[1]),
                                 np.arange(data_array.shape[0]))

        'matplotlib also requires flattened arrays'
        xpos = xpos.flatten()
        ypos = ypos.flatten()

        colors = colors.flatten()
        dz = data_array.flatten()
        zpos = np.zeros(len(dz))
        'set the bar width and height to 0.2'
        dx = 0.2 * np.ones_like(zpos)
        dy = dx.copy()

        'create the histogram'
        ax.bar3d(xpos, ypos, zpos, dx, dy, dz, colors)

        'create a color map for the sidebar'
        cdict2 = {
            'red': ((0.0, 1.0, 1.0), (1.0, 1.0, 1.0)),
            'green': ((0.0, 1.0, 1.0), (1.0, 0.0, 0.0)),
            'blue': ((0.0, 1.0, 1.0), (1.0, 0.0, 0.0))
        }

        red2 = LinearSegmentedColormap('Red2', cdict2)
        'Register the colormap'
        plt.register_cmap(cmap=red2)

        norm = mpl.colors.Normalize(vmin=0, vmax=100)
        'Get the colormap back again'
        cmap = plt.get_cmap('Red2')

        'Create some axes for the sidebar'
        cax = fig.add_axes([0.905, 0.2, 0.02, 0.6])
        'Create the colorbar'
        cb = mpl.colorbar.ColorbarBase(cax,
                                       cmap=cmap,
                                       norm=norm,
                                       spacing='proportional')
        cb.set_label('% of accesses to diskserver in dataset')

        'Set where the ticks are'
        cb.set_ticks([0, 25, 50, 75, 100])
        'Set the ticks on the colorbar'
        cb.set_ticklabels(['0', '0.1', '1', '10', '100'])
        'Update what the ticks show'
        cb.update_ticks()

        'Save the image'
        canvas.print_figure('images/graph_%s_%02d.png' %
                            (str(date.today()), datetime.now().hour),
                            bbox_inches='tight')
        '''This close the picture.
            Needed so the program does not use all the computers memory'''
        fig.clf()
        plt.close()
Пример #59
0
def graph1():
    # 【AWS IAM関連情報】
    #  IAMユーザーに割り当てたポリシー
    #   AmazonDynamoDBFullAccess
    #   AWSLambdaDynamoDBExecutionRole
    #  profile名
    #   環境編巣にAWS_DEFAULT_PROFILE, AWS_PROFILEとして定義した。
    dynamodb = boto3.resource("dynamodb")

    # TODO 2回DBにアクセスする理由はないので後で修正する
    # 1日前の時間表記(yyyyMMddhhmmss)を取得する
    yesterday = (datetime.now(timezone(timedelta(hours=+9), 'JST')) -
                 timedelta(days=1)).strftime('%Y%m%d%H%M%S')

    # 1日前までのデータをDynamoDBより取得する
    entries = Entry.scan(Entry.MeasureDateTime >= yesterday)
    entries = sorted(entries, key=lambda x: x.MeasureDateTime)

    df = pd.DataFrame(columns=["人数"])
    for entry in entries:
        df.loc[entry.MeasureDateTime] = entry.value

    #table = dynamodb.Table(os.environ['table_name'])
    #response = table.scan()

    #df = pd.json_normalize(response["Items"])

    # YYYYMMDDHHMMSS形式を日付として認識させる
    df.index = pd.to_datetime(df.index)
    #df.MeasureDateTime = pd.to_datetime(df.MeasureDateTime)

    # data["value"]はDecimalで入っているが。
    # Decimalは直接表示できないので、floatに変換
    df = df.astype({"人数": float})
    #df = df.astype({"value": float})

    # 日付でソートする。戻り値を受け取らないとソートされないので注意
    #df = df.sort_values(by='MeasureDateTime')

    fig, ax = plt.subplots()

    # x軸の目盛りは1時間ごとにする(set_major_locatorで目盛りを打つ場所を決める)
    ax.xaxis.set_major_locator(mdates.HourLocator(interval=4))
    # x軸の目盛りの表示形式を設定する(set_major_formatterで目盛りに書く内容を決める)
    ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%d\n%H:%M'))
    # 補助目盛線を付加する
    ax.xaxis.set_minor_locator(mdates.HourLocator())

    plt.plot(df.index, df["人数"])
    #plt.plot(df.MeasureDateTime, df.value)
    # ax.plot(df.MeasureDateTime, df.value)というのもある

    plt.title('時間と人数の推移')
    # plt.xlabel('X軸ラベル')
    # plt.ylabel('人数')
    plt.ylabel("人数", rotation=0, labelpad=20)
    # 縦書きにする場合
    # ax.set_ylabel("人\n数", rotation=0, va='center')

    plt.grid()
    # 補助目盛は点線にする
    plt.grid(True, which="minor", linestyle="--")
    # 主目盛は実線
    # plt.grid(True, which="major", linestyle="-")

    canvas = FigureCanvasAgg(fig)
    png_output = BytesIO()
    canvas.print_png(png_output)
    data = png_output.getvalue()

    response = make_response(data)
    response.headers['Content-Type'] = 'image/png'
    response.headers['Content-Length'] = len(data)
    return response
Пример #60
0
def plot_day_logspacing(cube,
                        fig,
                        day_idx=0,
                        lon_range=(-180, 180),
                        lat_range=(-90, 90),
                        vmin=None,
                        vmax=None,
                        cmap_id=CMAP):
    """Plot variable on map using pcolormesh
    
    Parameters
    ----------
    cube : iris.cube.Cube
        cube containing data. Note that the input cube must correspond 2D cube
        containing data from one time stamp (i.e. ndim=2).
    day_idx : int
        index of timestamp that is to be plotted
    lon_range : tuple
        tuple specifying plotted longitude range 
    lat_range : tuple
        tuple specifying plotted latitude range
    vmin : float
        lower limit for AOD display
    vmax : float
        upper limit for AOD display
    cmap_id : str
        string ID of matplotlib colormap supposed to be used
    """
    if fig.canvas is None:
        FigureCanvasAgg(fig)
    if cube.ndim != 3:
        msg = ("Invalid dimension %d of input cube, need ndim=2 (i.e. data "
               "corresponding to one timestamp" % cube.ndim)
        raise ValueError(msg)
    dat = cube[day_idx]

    geo_ax = fig.add_axes([0.1, .1, .8, .8], projection=ccrs.PlateCarree())
    ax_cbar = fig.add_axes([0.85, .1, .02, .8])

    lvls = funs.init_cmap_levels(vmin, vmax)
    cmap = get_cmap(cmap_id)
    norm = BoundaryNorm(lvls, ncolors=cmap.N, clip=True)

    lons, lats = dat.coord("longitude").points, dat.coord("latitude").points
    X, Y = np.meshgrid(lons, lats)
    mesh = geo_ax.pcolormesh(X, Y, dat.data, cmap=cmap,
                             norm=norm)  #, figure=fig)
    #mesh = iplt.pcolormesh(cube,cmap=cmap, norm=norm, figure=fig)
    geo_ax.coastlines(color=COASTLINE_COLOR)
    #things that do not need to be done when day is updated

    ticks = funs.get_cmap_ticks(lvls)
    # =============================================================================
    #     try:
    #         ax_cbar = fig.axes[1]
    #         [x.remove for x in ax_cbar.artists]
    # =============================================================================
    cbar = fig.colorbar(mesh, norm=norm, boundaries=lvls, cax=ax_cbar)
    # =============================================================================
    #     except:
    #         cbar = fig.colorbar(mesh, norm=norm, boundaries=lvls)
    # =============================================================================
    # Set some suitable fixed "logarithmic" colourbar tick positions.
    cbar.set_ticks(ticks)
    cbar.set_label(funs.var_str(dat))
    # Modify the tick labels so that the centre one shows "+/-<minumum-level>".
    #tick_levels[3] = r'$\pm${:g}'.format(minimum_log_level)
    cbar.set_ticklabels(["%.3f" % x for x in ticks])

    geo_ax.set_xlim([lon_range[0], lon_range[1]])
    geo_ax.set_ylim([lat_range[0], lat_range[1]])

    geo_ax.set_xticks(np.linspace(lon_range[0], lon_range[1], 7),
                      crs=ccrs.PlateCarree())
    geo_ax.set_yticks(np.linspace(lat_range[0], lat_range[1], 7),
                      crs=ccrs.PlateCarree())

    lon_formatter = LongitudeFormatter(number_format='.1f',
                                       degree_symbol='',
                                       dateline_direction_label=True)
    lat_formatter = LatitudeFormatter(number_format='.1f', degree_symbol='')

    geo_ax.xaxis.set_major_formatter(lon_formatter)
    geo_ax.yaxis.set_major_formatter(lat_formatter)

    geo_ax.set_xlabel("Longitude", fontsize=12)
    geo_ax.set_ylabel("Latitude", fontsize=12)
    # Label the colourbar to show the units.
    #bar.set_label('[{}, log scale]'.format(anomaly.units))
    tit = ("%s %s mean: %.3f" %
           (funs.var_str(dat), funs.daystring(
               cube, day_idx), funs.area_weighted_mean(dat)))
    geo_ax.set_title(tit)
    return fig