Esempio n. 1
2
def home(request):
    import matplotlib.pyplot as plt, mpld3

    # Graph
    fig = plt.figure()
    x = [4, 12, 14, 4, 22, 18]
    y = [3, 10, 16, 3, 20, 17]
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16], mec='w', mew=5, ms=20)
    graph1 = mpld3.fig_to_html(fig)

    # Graph 2
    fig = plt.figure()
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16], 'ro')
    graph2 = mpld3.fig_to_html(fig)


    # Histogram
    fig = plt.figure()
    ax = fig.add_subplot(111, axisbg='#EEEEEE')
    ax.grid(color='white', linestyle='solid')

    x = np.random.normal(size=1000)
    ax.hist(x, 30, histtype='stepfilled', fc='lightblue', alpha=0.5);
    histogram = mpld3.fig_to_html(fig)

    return render_to_response('election_prediction_site/index.html', {'graph1': graph1,
                                                                      'graph2': graph2,
                                                                      'histogram': histogram})
Esempio n. 2
0
def show_d3(figure=None):
    """
    Show matplotlib fig using d3.js.
    """
    if figure:
        img = mpld3.fig_to_html(figure, d3_url=D3_URL)
    else:
        img = mpld3.fig_to_html(fig, d3_url=D3_URL)
    dat = img.replace('\n', '\r')
    print 'embed:', dat
Esempio n. 3
0
def render_webfigure(var_dict):

  userlon = float(var_dict['userlon'])
  userlat = float(var_dict['userlat'])
  data = var_dict['data']

  fig, ax = plt.subplots(figsize=(15,10))

  #return antlons, antlats, scss, cats, separations, geodesics, contour_lons, contour_lats
  antlons = data[0]
  antlats = data[1]
  scss = data[2]
  cats = data[3]
  contour_lons = data[6]
  contour_lats = data[7]

  #print userlon
  plt.plot(userlon, userlat, 'g*', ms=40)
  plt.scatter(antlons, antlats, c='r')
  for i in xrange(len(antlons)):
    label = '{} ({})'.format(scss[i], cats[i])
    plt.text(antlons[i]+.05, antlats[i], label)
    plt.plot(contour_lons[i], contour_lats[i])
    #print contour_lons[i], contour_lats[i]

  import json
  execfile("/Users/mwoods/Work/OldJobs/JobSearch/Pre-Insight/plotUSA.py")
  jdata = json.load(open("/Users/mwoods/Work/OldJobs/JobSearch/Pre-Insight/states.json"))
  i=0
  for state in jdata['geometries']:
    i+=1
    j=0
    # State only has one border, do not enter into 'for each border' loop.
    if len(state['coordinates'][0]) != 1:
      x,y = np.array(state['coordinates']).T
      plt.plot(x,y, 'b', lw=3)
      continue
    # There is a list with multiple borders (islands)
    for shape in state['coordinates']:
      x,y = np.array(shape[0]).T
      plt.plot(x,y, 'b', lw=3)
  plt.xlim(userlon-1, userlon+1)
  plt.ylim(userlat-1, userlat+1)
  plt.xlabel("Longitude")
  plt.ylabel("Latitude")
  #plt.gca().set_aspect('equal')


  #plt.hist(np.random.random(100))
  #plt.scatter(np.random.random(100), np.random.random(100))
  fig_html = mpld3.fig_to_html(fig)

  return mpld3.fig_to_html(fig)
Esempio n. 4
0
def d3():
    rtimes, rt, rp = np.loadtxt("data/data.txt").T
    mask = np.logical_and(rtimes>1391000000, rtimes<1393000000)
    rtimes = rtimes[mask]
    rt = rt[mask]
    rtimes = map(datetime.datetime.fromtimestamp, rtimes)
    rtimes = matplotlib.dates.date2num(rtimes)
    fig, axis = plt.subplots()

    axis.xaxis_date()
    fig.autofmt_xdate()
    axis.plot_date(rtimes, rt, "-",linewidth=3, color="black")

    forecast_list = []
    for fname in glob.glob("data/forecast.1391*.txt"):
        stamp = fname.split(".")[1]
        times, tempa = np.loadtxt(fname).T
        times = map(datetime.datetime.fromtimestamp, times)
        times = matplotlib.dates.date2num(times)

        points = np.array([times, tempa]).T.reshape(-1, 1, 2)
        segments = np.concatenate([points[:-1], points[1:]], axis=1)
        lc = LineCollection(segments, cmap=plt.get_cmap("jet"),
                            norm=plt.Normalize(0, 1))
        lc.set_array(np.linspace(0,1,len(times)))
        lc.set_linewidth(1)
        axis.add_collection(lc)

        axis.plot_date(times, tempa, "-", linewidth=2)



    return fig_to_html(fig)
Esempio n. 5
0
def csuppfb():
    if request.args['df'] :
        dataf1 =request.args['df']
        dataf = pd.read_csv(app.config['UPLOAD_FOLDER']+'/'+dataf1)        
        index_list=["Case Owner","Case Number","Support Survey - Service rating","Support Survey - Service feedback"]
        value_list = ["Age (Hours)"]
        pvt = pd.pivot_table(dataf, index=index_list, values=value_list,
                           aggfunc=[np.sum, np.mean, np.std], fill_value=0, columns=None)          
        
    agent_df = []
    for agent in pvt.index.get_level_values(0).unique():
        agent_df.append([agent, pvt.xs(agent, level=0).to_html()])
    env = Environment(loader=FileSystemLoader('.'))
    template = env.get_template("csupp.html")

    plt=mpld3.fig_to_html(pvt)

    template_vars={"title": "CSUPP FB REPORT", 
                "Excellent": get_summary_stats(dataf,"Excellent"),
                "Good": get_summary_stats(dataf,"Good"),
                "csupp_pivot_table": pvt.to_html(),
                "agent_detail": agent_df,   
                "csupp_pivot_graph": plt.to_html()}

    html_out = template.render(template_vars)

    return html_out
 def salaries_stats_regression_plot(self):
     """
     This function is to plot the regression results.
     
     Note:
     By calling this function, you will get a scatter plot on predicted salaries (y axis) and true salaries (x axis).
     
     Return:
     html: a string of html of regression plot.
     """
     regdata = self.df.reset_index(1)
     regdata = regdata[regdata['POS'].isin(['C','SF','PF','PG','SG'])]
     pos={'C':2,'PF':3,'PG':4,'SF':5,'SG':1}
     posdata = [pos[regdata['POS'][j]] for j in xrange(0,len(regdata.index))] #convert position information into categorical numbers
     
     fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'), figsize=(10,6))
     scatter = ax.scatter(regdata['SALARY'],
                          regdata['Predicted'], 
                          c=posdata,
                          s=10*regdata['PPG'],
                          color=['skyblue', 'yellowgreen', 'lightcoral', 'mediumpurple','gold'])
     ax.grid(color='white', linestyle='solid')
     labels = [regdata.index[i] for i in xrange(0,len(regdata.index))]#add player names as labels
     tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
     mpld3.plugins.connect(fig, tooltip)
     reference = np.arange(self.df['SALARY'].min(),self.df['SALARY'].max(),1000) 
     plt.plot(reference,reference,'white',alpha=0.7) #a reference line y = x
     #set x and y axis labels and their locations to avoid overlapping with x and y axis ticks
     ax.set_xlabel('True Salaries', fontsize=16)
     ax.set_ylabel('Predicted Salaries', fontsize=16)
     ax.xaxis.set_label_coords(0.5,-0.08)
     ax.yaxis.set_label_coords(-0.14,0.5)
     html = mpld3.fig_to_html(fig)
     plt.close()
     return html
Esempio n. 7
0
def generate_histogram(dates, values):
    """
    Generate a barchart from the given information.

    This function receives a list of floats and a list of labels
    and uses them to generates a barchart histogram.

    Keyword arguments:
    values -- a list of values for each bar of the chart.
    labels -- a list of labels for each bar of the chart.
    """
    #n_groups = len(values)


    fig, axes = plt.subplots()
    axes.plot(dates, values, 'o--')
    plt.ylim([0, 5])
    one_mon_rel = relativedelta(months=1)
    plt.xlim([min(dates), max(dates) + one_mon_rel])
    fig.set_size_inches(12, 5)
    plt.xlabel('Videos through time')
    plt.ylabel('Mean video scores')
    plt.title('Channel\'s video progress through time')
    plt.legend()
    plt.tight_layout()

    return mpld3.fig_to_html(fig)
Esempio n. 8
0
    def plot(self, data, notebook=False, show=True, savename=None):

        fig = pyplot.figure()
        ncenters = len(self.centers)

        colorizer = Colorize()
        colorizer.get = lambda x: self.colors[int(self.predict(x)[0])]

        # plot time series of each center
        # TODO move into a time series plotting function in viz.plots
        for i, center in enumerate(self.centers):
            ax = pyplot.subplot2grid((ncenters, 3), (i, 0))
            ax.plot(center, color=self.colors[i], linewidth=5)
            fig.add_axes(ax)

        # make a scatter plot of the data
        ax2 = pyplot.subplot2grid((ncenters, 3), (0, 1), rowspan=ncenters, colspan=2)
        ax2, h2 = scatter(data, colormap=colorizer, ax=ax2)
        fig.add_axes(ax2)

        plugins.connect(fig, HiddenAxes())

        if show and notebook is False:
            mpld3.show()

        if savename is not None:
            mpld3.save_html(fig, savename)

        elif show is False:
            return mpld3.fig_to_html(fig)
Esempio n. 9
0
    def plot_weight_distributions(self, simulation):

        # Plot weight distribution at given intervals.
        plots = []
        for n in simulation['neurons']:
            frame = 1
            frames = 5
            rows = frames + 1
            cols = 1
            f = pylab.figure()
            for dist in n['weight_distributions']:
                # Plot a histogram of the values.
                p = pylab.subplot(rows, cols, frame)

                # Only add title to first plot.
                if frame == 1:
                    pylab.title('Weight Distribution Over Time')

                p.axes.get_xaxis().set_visible(False)
                p.axes.get_yaxis().set_ticks([])
                p.bar(np.arange(0, 1, 0.1), dist, 0.1)

                # Only show if plot is complete.
                if rows * cols == frame:
                    pylab.xlabel("Weight Value")
                    p.axes.get_xaxis().set_visible(True)
                frame += 1
            plots.append(mpld3.fig_to_html(f))
            pylab.close(f)
        return plots
Esempio n. 10
0
    def mpld3_to_html(self):
        """This require to call a plotting figure before hand"""
        from gdsctools import gdsctools_data
        # This copy the full path and therefore HTML cannot
        # be moved in another directory. to be fixed.
        js_path1 = gdsctools_data('d3.v3.min.js', where='javascript')
        js_path2 = gdsctools_data('mpld3.v0.2.js', where='javascript')
        try:
            # mpld3 is great but there are a couple of issues
            # 1 - legend zorder is not used so dots may be below the legend,
            #     hence we set the framealpha =0.5
            # 2 - % character even though there well interpreted in matploltib
            #     using \%, they are not once parsed by mpld3. So, here
            #     we remove the \ character
            axl = pylab.legend(loc='best', framealpha=0.8, borderpad=1)
            axl.set_zorder(10) # in case there is a circle behind the legend.
            texts = [this.get_text() for this in axl.get_texts()]

            for i, text in enumerate(texts):
                text = text.replace("\\%", "%")
                text += "  "
                axl.get_texts()[i].set_text(text)
            import mpld3
            htmljs = mpld3.fig_to_html(self.current_fig,
                            d3_url=js_path1,
                            mpld3_url=js_path2)
        except:
            htmljs = ""
        return """<div class="jsimage"> """ + htmljs + "</div>"
Esempio n. 11
0
    def to_html(self):
        import matplotlib.pyplot as plt
        html = fig_to_html(self.content, figid="generatedchart")
        #closes fig element (refresh)
        plt.close()

        return html
Esempio n. 12
0
def display_figure(fig, message=None, max_width='100%'):
    "Display widgets applicable to the specified element"
    if OutputMagic.options['fig'] == 'repr': return None

    figure_format = OutputMagic.options['fig']
    dpi = OutputMagic.options['dpi']
    backend = OutputMagic.options['backend']

    if backend == 'nbagg' and new_figure_manager_given_figure is not None:
        manager = new_figure_manager_given_figure(OutputMagic.nbagg_counter, fig)
        # Need to call mouse_init on each 3D axis to enable rotation support
        for ax in fig.get_axes():
            if isinstance(ax, Axes3D):
                ax.mouse_init()
        OutputMagic.nbagg_counter += 1
        manager.show()
        return ''
    elif backend == 'd3' and mpld3:
        fig.dpi = dpi
        mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fontsize=14))
        html = "<center>" + mpld3.fig_to_html(fig) + "<center/>"
    else:
        renderer = Store.renderer.instance(dpi=dpi)
        figdata = renderer.figure_data(fig, figure_format)
        if figure_format=='svg':
            figdata = figdata.encode("utf-8")
        b64 = base64.b64encode(figdata).decode("utf-8")
        (mime_type, tag) = MIME_TYPES[figure_format], HTML_TAGS[figure_format]
        src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64)
        html = tag.format(src=src)
    plt.close(fig)
    return html if (message is None) else '<b>%s</b></br>%s' % (message, html)
Esempio n. 13
0
def get_heatmap(ds):
    plt.close('all')
    corrmat = ds.corr()
    fig, ax = plt.subplots()
    sns.heatmap(corrmat, vmax=.8, square=True)
    heatmap_html = mpld3.fig_to_html(fig)
    return heatmap_html
Esempio n. 14
0
def plot(groups,fig,ax,cluster_names,cluster_colors):
	for name, group in groups:
	    points = ax.plot(group.x, group.y, marker='o', linestyle='', ms=10, 
	                     label=cluster_names[name], mec='none', 
	                     color=cluster_colors[name])
	    ax.set_aspect('auto')
	    labels = [i for i in group.title]
	    
	    #set tooltip using points, labels and the already defined 'css'
	    tooltip = mpld3.plugins.PointHTMLTooltip(points[0], labels,
	                                       voffset=5, hoffset=5, css=css)
	    #connect tooltip to fig
	    mpld3.plugins.connect(fig, tooltip, TopToolbar())    
	    
	    #set tick marks as blank
	    ax.axes.get_xaxis().set_ticks([])
	    ax.axes.get_yaxis().set_ticks([])
	    
	    #set axis as blank
	    ax.axes.get_xaxis().set_visible(False)
	    ax.axes.get_yaxis().set_visible(False)

	    
	ax.legend(numpoints=1) #show legend with only one dot
	file_ = open('clustering.html', 'w')
	file_.write(mpld3.fig_to_html(fig))
	file_.close()
	# mpld3.show() #show the plot
Esempio n. 15
0
def draw_fig(fig_type):
    """Returns html equivalent of matplotlib figure

    Parameters
    ----------
    fig_type: string, type of figure
            one of following:
                    * line
                    * bar

    Returns
    --------
    d3 representation of figure
    """

    with lock:
        fig, ax = plt.subplots()
        if fig_type == "line":
            ax.plot(x, y)
        elif fig_type == "bar":
            ax.bar(x, y)
        elif fig_type == "pie":
            ax.pie(pie_fracs, labels=pie_labels)
        elif fig_type == "scatter":
            ax.scatter(x, y)
        elif fig_type == "hist":
            ax.hist(y, 10, normed=1)
        elif fig_type == "area":
            ax.plot(x, y)
            ax.fill_between(x, 0, y, alpha=0.2)

    
    return mpld3.fig_to_html(fig)
    def pos_salaries_distribution(self):
        """
        This is a function to analyze and plot salaries distribution by positions.

        Return:
        html: a string of html of by-position salaries distribution plot.
        pos: a dataframe with salaries statistics by positions.
        """
        salaries_pos_year = self.df[[self.year,'POS']].dropna()
        positions = ['C','PF','SF','SG','PG']
        salaries_pos_year = salaries_pos_year[salaries_pos_year['POS'].isin(positions)]
        pos = [salaries_pos_year[salaries_pos_year['POS'] == position].describe().rename(columns={self.year:position}) for position in positions] #store by-position salaries statistics into a list
        pos = pd.concat(pos, axis=1) #merge all by-position dataframes
        #convert all elements in dataframe into integers
        for position in positions:
            pos[position] = pos[position].apply(lambda x: int(x))

        ax = salaries_pos_year.boxplot(by='POS',sym='r*',figsize=(10,6))
        ax.set_axis_bgcolor('#EEEEEE')
        ax.grid(color='white', linestyle='solid')

        #add positions name text into the boxplot
        loc = salaries_pos_year.groupby('POS').median()
        for i in xrange(0,5):
            ax.text(i+1, 
                    loc[self.year][i], 
                    loc.index[i], 
                    ha='center',
                    va='bottom')
        plt.title('')
        ax.set_xlabel('Positions', fontsize=16)
        fig = ax.get_figure()
        html = mpld3.fig_to_html(fig)
        plt.close()
        return html, pos
Esempio n. 17
0
def save_and_or_show_plot(show=None, savefig="None", **kwargs):
    """ Save and/or show current matplotlib figure

    Parameters
    ----------
    show: bool or None
        Show plot, when None only show when savefig is not used
        default: None
    savefig: string
        path to output file of figure. If extension is html, mpld3
        will be used to generate a d3 backed html output.
    \*\*kwargs:
        keyword arguments passed on to ``matplotlib.pyplot.savefig``
    """
    if savefig is not None and savefig != "None":
        if savefig.endswith(".html"):
            # Export using mpld3
            import mpld3

            open(savefig, "wt").write(mpld3.fig_to_html(plt.gcf()))
        else:
            plt.savefig(savefig, **kwargs)

        if show is None:
            show = False
    else:
        if show is None:
            show = True

    if show:
        plt.show()
Esempio n. 18
0
def plot_to_html( name = None ):
    """Converts a matplotlib figure, such that it can be displayed in an html page
        either using an embedded svg tag (default)
        or by using mpld3 
    """
    result = ""
    if mpld3_available and use_mpld3 :
        html = mpld3.fig_to_html( plt.gcf() )
        result = html
    else:
        figStr = io.StringIO()
        plt.savefig( figStr, format='svg', bbox_inches='tight' )
        result = figStr.getvalue()
    
    if write_pdfs and name:
        if not os.path.exists(pdf_output_dir):
            os.makedirs(pdf_output_dir)

        filename = os.path.join(pdf_output_dir, name + '.pdf')
        filepath = os.path.join(filename)
        plt.savefig( filepath, bbox_inches='tight' )
        result = '<a href="%s" > %s </a>' % ( filename, result )
        
    plt.clf()
        
    return result
    def overall_salaries_trend(self):
        """
        This function is to analyze and plot nba salaries trend.

        Return:
        html: a string of html for the salaries trend plot.
        salaries: a dataframe with salaries statistical information (e.g., mean, min, max) in each year. 
        """
        years = xrange(2000,2016)
        salaries = [self.df[year].describe().apply(lambda x: int(x)) for year in years] #store salaries statistical information for each year in a list
        salaries = pd.concat(salaries, axis=1).T.drop(['25%','75%'],1) #merge all salaries statistical information dataframes into a dataframe

        fig = plt.figure(figsize=(10,6))
        ax = fig.add_subplot(111, axisbg='#EEEEEE')
        ax.grid(color='white', linestyle='solid')
        ave_salaries = self.df.mean() #calculate the average salaries for each year
        plt.bar(self.df.columns, ave_salaries, 0.5, color='#0077FF', alpha=0.5)
        ax.set_xlabel('Year', fontsize=16)
        ax.set_ylabel('Average Salaries', fontsize=16)
        ax.xaxis.set_label_coords(0.5,-0.08)
        ax.yaxis.set_label_coords(-0.14,0.5)
        plt.title('2000-2015 NBA Average Salaries Trend')
        html = mpld3.fig_to_html(fig)
        plt.close()
        return html, salaries
 def renderFigure(self, fig):
     def genMarkup(chartFigure):
         return self.env.from_string("""
                 {0}
                 {{%for message in messages%}}
                     <div>{{{{message}}}}</div>
                 {{%endfor%}}
             """.format(chartFigure)
         ).render(messages=self.messages)
         
     if not self.useMpld3:
         import base64
         try:
             from io import BytesIO as pngIO
         except ImportError:
             from StringIO import StringIO as pngIO
         png=pngIO()
         plt.savefig(png, pad_inches=0.05, bbox_inches='tight', dpi=self.getDPI())
         try:
             return( 
                 genMarkup("""
                         <center><img style="max-width:initial !important" src="data:image/png;base64,{0}"  class="pd_save"></center>
                     """.format(base64.b64encode(png.getvalue()).decode("ascii"))
                 )
             )
         finally:
             png.close()
     else:
         mpld3.enable_notebook()
         try:
             return genMarkup(mpld3.fig_to_html(fig))
         finally:
             mpld3.disable_notebook()
def run(data):
    global t0, lines, test
    
    print data
    #override t to be count of seconds
    t=time.time()-t0
    tdata.append(t)

    for sensor in all_data:
        for axis in all_data[sensor]:
            if axis=="x":i=0
            if axis=="y":i=1
            if axis=="z":i=2
            # if test==0: print sensor+" ("+axis+")"
            all_data[sensor][axis].append(data[sensor][i])
            lines[sensor][axis][0].set_data(tdata, all_data[sensor][axis])
    # all_lines=[[axis for axis in sensor.values()] for sensor in lines.values()]
    # test=1
    #MOVING WINDOW
    xmin, xmax = ax[0].get_xlim()
    if t >= xmax-1: #once the line get's 9 10ths of the way...
        #move the window by 5 seconds forward
        
        xmin+=5
        xmax+=5
        for i in range(len(axes)):
            ax[i].set_xlim(xmin, xmax)
        # print 'test'
        ax[0].figure.canvas.draw()
        ax[1].figure.canvas.draw()
        ax[2].figure.canvas.draw()
    html=mpld3.fig_to_html(fig)
    print html
    # mpld3.show(fig)
    return html
Esempio n. 22
0
def display_frame(plot, figure_format, backend, dpi, css, message, **kwargs):
    """
    Display specified element as a figure. Note the plot instance
    needs to be initialized appropriately first.
    """
    if backend == 'nbagg':
        manager = plot.renderer.get_figure_manager(OutputMagic.nbagg_counter, plot)
        if manager is None: return ''
        OutputMagic.nbagg_counter += 1
        manager.show()
        return ''
    elif backend == 'd3' and mpld3:
        plot.state.dpi = dpi
        mpld3.plugins.connect(plot.state, mpld3.plugins.MousePosition(fontsize=14))
        html = "<center>" + mpld3.fig_to_html(plot.state) + "<center/>"
    else:
        renderer = plot.renderer.instance(dpi=dpi)
        figdata = renderer.figure_data(plot, figure_format)
        w,h = renderer.get_size(plot)
        if figure_format=='svg':
            figdata = figdata.encode("utf-8")
        if figure_format == 'pdf' and 'height' not in css:
            css['height'] = '%dpx' % (h*dpi*1.15)
        b64 = base64.b64encode(figdata).decode("utf-8")
        (mime_type, tag) = MIME_TYPES[figure_format], HTML_TAGS[figure_format]
        src = HTML_TAGS['base64'].format(mime_type=mime_type, b64=b64)
        html = tag.format(src=src, css=dict_to_css(css))
    return html if (message is None) else '<b>%s</b></br>%s' % (message, html)
 def draw(self, name=None, old=None, new=None):
     if name is not None and self.DONT_DRAW.match(name):
         return
     
     if self._FREEZE:
         return
     
     plot_and_message = ''
     
     # Better way would be a decorator or something that only goes into draw if not autoupdate
     if self.autoupdate:
         
         # Generate new figure object
         f = plt.figure(figsize=(self.figwidth, self.figheight))
         if PLOTPARSER.is_3d(self.kind):
             projection = '3d'
         else:
             projection = None
         ax = f.add_subplot(111, projection=projection)
         
         if self._color_state or self.kind not in ['spec', 'waterfall', 'contour', 'contour3d']:
             colorkwags = dict(color=self.color)
         else:
             colorkwags = dict(cmap=self.colormap, cbar=self.colorbar)
         
         self.spec_modified.plot(ax=ax,
                                 fig=f,
                                 kind=self.kind,
                                 norm=self.NORMUNITS_REV[self.norm_unit],
                                 **colorkwags
                                 )
         f.tight_layout() #Padding around plot
         lines = ax.get_lines()
         plt.close(f)
                                 
         #http://mpld3.github.io/modules/API.html
         if self.interactive:
             import mpld3
             if self.selectlines:
                 from line_plugin import HighlightLines
                 
                 for idx, col in enumerate(self.spec_modified.columns):
                     name = 'COLUMN(%s): %s' % (idx, col)
                     tooltip = mpld3.plugins.LineLabelTooltip(lines[idx], name)
                     #voffset=10, hoffset=10,  css=css)
                     mpld3.plugins.connect(f, tooltip)
                 
                 mpld3.plugins.connect(f, HighlightLines(lines))
             
             plot_and_message += mpld3.fig_to_html(f)
         else:
             plot_and_message += mpl2html(f)
         
         self.fig_old = f
     
     else:
         plot_and_message += html_figure(self.fig_old)
     
     # VALUE IS WHAT GUI LOOKS UP!!!
     self.value = plot_and_message
Esempio n. 24
0
def get_graph():
    import matplotlib.pyplot as plt, mpld3
    fig = plt.figure()
    x= y= z = np.arange(10)
    plt.plot(x)
    html_str = mpld3.fig_to_html(fig)
    return html_str
Esempio n. 25
0
def map_plot():
    import matplotlib.pyplot as plt, mpld3
    import numpy as np
    from mpl_toolkits.basemap import Basemap

    fig, ax = plt.subplots()
    m = Basemap()
    #m = Basemap(projection='ortho',lat_0=0,lon_0=0,resolution='l')     
    #m = Basemap(projection='moll',lon_0=0,resolution='l')     
    # Shift 'lon' from [0,360] to [-180,180], make numpy array
    #tmp_lon = np.array([lon[n]-360 if l>=180 else lon[n] 
    #                   for n,l in enumerate(lon)])  # => [0,180]U[-180,2.5]
    
    #i_east, = np.where(tmp_lon>=0)  # indices of east lon
    #i_west, = np.where(tmp_lon<0)   # indices of west lon
    #lon = np.hstack((tmp_lon[i_west], tmp_lon[i_east]))  # stack the 2 halves
    
    # Correspondingly, shift the 'air' array
    #tmp_air = np.array(air)
    #air = np.hstack((tmp_air[:,i_west], tmp_air[:,i_east]))
    #poly_paths = m.drawcoastlines().get_paths() # coastline polygon paths
    #X,Y = np.meshgrid(lon,lat)
    
    m.etopo()
    #m.contourf(X, Y, air, 40, alpha=.75, cmap='jet')
    #ticks=range(-90,90,30)
    #ax.set_yticks(ticks)
    
    #ax.set_ylabel("Latitude", fontsize=16)
    #ax.set_xlabel("Longitude", fontsize=16)
    #plt.colorbar()
    
    # D3 Works!
    #mpld3.display(fig)
    return mpld3.fig_to_html(fig,template_type="general")
Esempio n. 26
0
    def plot(self, notebook=False, colormap='polar', scale=1, maptype='points', show=True, savename=None):

        # make a spatial map based on the scores
        fig = pyplot.figure(figsize=(12, 5))
        ax1 = pyplot.subplot2grid((2, 3), (0, 1), colspan=2, rowspan=2)
        if maptype is 'points':
            ax1, h1 = pointmap(self.scores, colormap=colormap, scale=scale, ax=ax1)
        elif maptype is 'image':
            ax1, h1 = imagemap(self.scores, colormap=colormap, scale=scale, ax=ax1)
        fig.add_axes(ax1)

        # make a scatter plot of sampled scores
        ax2 = pyplot.subplot2grid((2, 3), (1, 0))
        ax2, h2, samples = scatter(self.scores, colormap=colormap, scale=scale, thresh=0.01, nsamples=1000, ax=ax2, store=True)
        fig.add_axes(ax2)

        # make the line plot of reconstructions from principal components for the same samples
        ax3 = pyplot.subplot2grid((2, 3), (0, 0))
        ax3, h3, linedata = tsrecon(self.comps, samples, ax=ax3)

        plugins.connect(fig, LinkedView(h2, h3[0], linedata))
        plugins.connect(fig, HiddenAxes())

        if show and notebook is False:
            mpld3.show()

        if savename is not None:
            mpld3.save_html(fig, savename)
        elif show is False:
            return mpld3.fig_to_html(fig)
Esempio n. 27
0
def simple_plot():
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt, mpld3
    fig, ax = plt.subplots()
    plot = ax.plot([3,1,4,1,5], 'ks-', mec='w', mew=5, ms=20)
    return mpld3.fig_to_html(fig, template_type = "general")
Esempio n. 28
0
 def generate_summary_dict(self, with_html=True):
     """
     This method will allow us to seamlessly conform
     our regression results into a Django model,
     making an easy workflow to save results
     """
     try:
         # from https://github.com/statsmodels/statsmodels/blob/master/statsmodels/regression/linear_model.py#L1999
         jb, jbpv, skew, kurtosis = sm.stats.stattools.jarque_bera(self.results.wresid)
         self.summary_dict = {
             'source_file': self.source_file,
             'features': ', '.join(self.features),
             'target': self.target,
             'analysis_type': 'OLS Linear Regression',
             'aic': self.results.aic,
             'bic': self.results.bic,
             'num_observations': self.results.nobs,
             'df_residuals': self.results.df_resid,
             'r_squared': self.results.rsquared,
             'r_squared_adjusted': self.results.rsquared_adj,
             'f_statistic': self.results.fvalue,
             'jarque_bera': jb,
             'jarque_bera_prob': jbpv,
             'skew': skew,
             'kurtosis': kurtosis,
             }
         if with_html:
             base_summary_html = self.results.summary().as_html()
             # some styling magic for Bootstrap CSS
             self.summary_dict['summary_html'] = base_summary_html.replace('simpletable', 'table')
             fig = self.construct_plot()
             self.summary_dict['plot_html'] = mpld3.fig_to_html(fig)
         return self.summary_dict
     except AttributeError:
         raise Exception("You must execute `run_ols_regression` method first")
Esempio n. 29
0
def plot_tweets_histogram(dbname, html_name):
    """ Function to plot a histogram of tweets as a function of time mentioning
    Kobe Bryant or Steph Curry """
    import numpy as np
    from datetime import datetime
    import matplotlib.pyplot as plt
    import seaborn as sns
    from mpld3 import fig_to_html

    df = query_db(dbname)

    # The start of the game in eastern time
    gamestart = datetime(2016, 04, 13, 22, 30)

    df['MinSinceGame'] = (df['CreationTime'] - gamestart) / \
        np.timedelta64(60, 's')

    kobe_list = ['kobe', 'bryant', 'mamba', 'lakers', '60']
    kobe_pattern = '|'.join(kobe_list)
    kobe_idx = df['tweet'].str.contains(kobe_pattern)

    curry_list = ['steph', 'curry', 'warriors', '73']
    curry_pattern = '|'.join(curry_list)
    curry_idx = df['tweet'].str.contains(curry_pattern)

    nba_list = ['nba', 'basketball']
    nba_pattern = '|'.join(nba_list)
    nba_idx = df['tweet'].str.contains(nba_pattern)

    kobe_mention = df['MinSinceGame'][kobe_idx]
    curry_mention = df['MinSinceGame'][curry_idx]
    nba_mention = df['MinSinceGame'][nba_idx]

    fig = plt.figure(figsize=(12, 9))
    # We actually will plot the tweets sent out 30 minutes before the game too
    kobe_mention = kobe_mention[~np.isnan(kobe_mention)]
    curry_mention = curry_mention[~np.isnan(curry_mention)]
    nba_mention = nba_mention[~np.isnan(nba_mention)]

    sns.distplot(kobe_mention[kobe_mention >= -30],
                 kde=False, label='Mention of Kobe Bryant')
    sns.distplot(curry_mention[curry_mention >= -30],
                 kde=False, label='Mention of Steph Curry')
    sns.distplot(nba_mention[nba_mention >= -30],
                 kde=False, label='General mention of Basketball')

    plt.xlabel(
        'Number of minutes since start of both games (10:30pm EDT 04/13/2016)', fontsize=18)
    plt.ylabel('Number of mentions in twitter', fontsize=18)
    plt.legend(loc=2, framealpha=0, fontsize=14)
    plt.title('Number of Tweet Mentions Since Start of Both Games', fontsize=24)

    # In principle I should be able to convert this plot to a bokeh interactive
    # html, but for some reason bokeh seems to consistently have trouble with
    # histograms. I would love to figure out how to do it in bokeh later when I
    # have more time.
    html = fig_to_html(fig)
    with open(html_name, 'w') as f:
        f.write(html)
Esempio n. 30
0
def get_plot(request, plot, grid, cie31, cie64, labels, norm):
	start = time.time()
	log.debug("[%s] Requesting %s/%s/%s/%s/%s/%s - \t\tsID: %s" % (time_now(), plot, grid, cie31, cie64, labels, norm, request.session.session_key))

	#Figure
	fig = plt.figure()

	#Figure size, in inches, 100 dots-per-inch
	dpi = 80
	fig.set_size_inches(8.0, 4.8)
	ax = fig.add_subplot(111)
	plots = request.session['plots']
	results = request.session['results']
	
	optionSet = { 	'grid' 				: int(grid),
            		'cie31' 			: int(cie31),
            		'cie64' 			: int(cie64),
            		'labels' 			: int(labels),
            		'norm'				: bool(int(norm)),   	
    }
    
	updateOptions(optionSet)
    
	if plot == 'xyz':
		webapi.plot.xyz(ax, plots, options)

	elif plot == 'xy':
		webapi.plot.xy(ax, plots, options)

	elif plot == 'lms':
		webapi.plot.lms(ax, plots, options)
	
	elif plot == 'lms_base':
		webapi.plot.lms_base(ax, plots, options)
	
	elif plot == 'bm':
		webapi.plot.bm(ax, plots, options)
	
	elif plot == 'lm':
		webapi.plot.lm(ax, plots, options)
	
	elif plot == 'xyz31':
		webapi.plot.xyz31(ax, plots, options)

	elif plot == 'xyz64':
		webapi.plot.xyz64(ax, plots, options)
	
	elif plot == 'xy31':
		webapi.plot.xy31(ax, plots, options)

	elif plot == 'xy64':
		webapi.plot.xy64(ax, plots, options)
	
	theFig = mark_safe(mpld3.fig_to_html(fig, template_type='general'))
	resulting_plot = theFig
	plt.close(fig)
	stop = time.time()
	log.debug("[%s] Plot %s/%s/%s/%s/%s produced in %s seconds - \t\tsID: %s" % ( time_now(), plot,  grid, cie31, cie64, labels, str(stop - start), request.session.session_key))
	return HttpResponse(resulting_plot);
def banks_problem_analytical_results(r_max, r_min, bailout_cost, discount_rate,
                                     nr_of_periods, R):
    # Bank's problem:

    # bank's problem at the last stage: max(pi) pi*R(pi)/(1-delta*pi)
    f_opt_bank_0 = lambda x: -x * (r_min + (r_max - r_min) *
                                   (1 - x)) / (1 - discount_rate * x)

    # Analytical results:

    # optimal value for period 0
    opt_pi_bank = [0] * nr_of_periods
    v_bank = [0] * nr_of_periods

    opt_pi_bank[0] = optimize.fmin(
        f_opt_bank_0,
        0.5)  # optimization using: Simplex method: the Nelder-Mead
    v_bank[0] = -f_opt_bank_0(opt_pi_bank[0])

    # bank problem in the rest of the periods, eq.(28)
    f_bank = lambda x, y: -((x * (r_min + (r_max - r_min) * (1 - x)) +
                             (1 - x) * (bailout_cost + discount_rate * y)) /
                            (1 - discount_rate * x))
    for k in range(1, nr_of_periods):
        f_opt_bank_0 = lambda x: f_bank(x, v_bank[k - 1])
        opt_pi_bank[k] = optimize.fmin(f_opt_bank_0, 0.7)
        v_bank[k] = -f_opt_bank_0(opt_pi_bank[k])

    # Analytical results:
    # check that analytical solutions coinside with optimal values:
    pi_bank_analytical = [0] * nr_of_periods
    v_bank_analytical = [0] * nr_of_periods
    temp_v_vheck = [0] * nr_of_periods
    pi_bank_analytical[0] = (1 - math.sqrt(1 - r_max * discount_rate /
                                           (r_max - r_min))) / discount_rate
    v_bank_analytical[0] = pi_bank_analytical[0] * R(
        pi_bank_analytical[0]) / (1 - discount_rate * pi_bank_analytical[0])
    for i in range(1, nr_of_periods):
        pi_bank_analytical[i] = (1 - math.sqrt(
            1 - discount_rate *
            (r_max -
             (bailout_cost + discount_rate * v_bank_analytical[i - 1]) *
             (1 - discount_rate)) / (r_max - r_min))) / discount_rate
        v_bank_analytical[i] = (
            pi_bank_analytical[i] * R(pi_bank_analytical[i]) +
            (1 - pi_bank_analytical[i]) *
            (bailout_cost + discount_rate * v_bank_analytical[i - 1])) / (
                1 - discount_rate * pi_bank_analytical[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig1, ax = pyplot.subplots()
    ax.plot(nr_per,
            flipud(opt_pi_bank),
            label='Machine Results',
            color='red',
            linestyle=':')
    ax.plot(nr_per,
            flipud(pi_bank_analytical),
            label='Analytical Results',
            color='blue',
            linestyle='--')
    ax.set_xlabel('Number of Periods')
    ax.set_ylabel('Probabilities')
    ax.set_title('Optimal probabilities for the bank')
    ax.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig1 = mpld3.fig_to_html(fig1)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities_for_the_Bank.pdf',
        format='pdf')
    pyplot.close(fig1)

    return html_text_fig1, opt_pi_bank, pi_bank_analytical
Esempio n. 32
0
def plot(models):
    fig, ax = plt.subplots(figsize=(12, 7))
    h = []
    h = []
    models = models.split("+")
    models_params = {}
    i = 0

    for model_name in models:
        i += 1

        # print model_name
        models_params[model_name] = '\n'.join([
            "%s:\t%s" % (key, val) for key, val in client.get(
                {"params.model_name": model_name}, collection="Models")
            ["params"].iteritems()
        ])

        curves = client.get_learning_curves(
            "" + model_name, val_ref="/mnt/nfs/himl.testing.cs-en.tcs.cs")
        # startTime=client.get_train_start(model_name)
        # endTime = client.get_train_end(model_name)
        #print curves
        for name, points in curves.iteritems():
            if i == 2:
                p = [datetime.datetime(2017, 10, 2, 12, 15, 40, 27000), 21.56]
            else:
                p = [datetime.datetime(2017, 10, 5, 0, 19, 58, 20000), 21.56]
            points = points[:13]
            points.insert(0, p)
            print points
            # print name
            #print points[0][0]
            endTime = None
            #print endTime

            if endTime:
                if points[0][
                        0] > endTime:  # aby evaluace po konce trenovani byly zarovnane s koncovym casem
                    #points[0][0] = endTime
                    print "HA"
            curve = [((date - points[0][0]).total_seconds() / 3600.0, score)
                     for date, score in points]

            c = np.asarray(curve)
            # print c
            # print c
            # fig.autofmt_xdate()
            # max_idx=np.argmax(c[:,1])
            # ax.annotate("test", xy=(c[0],c[1]), xytext=(0, 0),arrowprops=dict(arrowstyle="->"))
            if i == 2: name = "Adam"
            else: name = "SGD"
            p, = plt.plot(c[:, 0], c[:, 1], marker='o', label=name)
            #p, = plt.plot(c[:, 0], c[:, 1], marker='o',
            #              label=' '.join((model_name, str(max(c[:, 1])), " (", name.split("/")[-1], ")")))
            h.append(p)
    plt.legend(
        handles=h, loc=3,
        prop=fontP)  # ,bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    # ax.legend(loc='upper center', bbox_to_anchor=(0.0, -0.05),
    #          fancybox=True, shadow=True)

    plt.xlabel('Hours')
    plt.ylabel('BLEU')
    #plt.ylim([-5,50])
    plt.ylim([19, 24])
    font = {'family': 'normal', 'weight': 'normal', 'size': 22}
    #matplotlib.rc('xtick', labelsize=50)
    #matplotlib.rc('ytick', labelsize=50)
    matplotlib.rc('font', **font)
    #plt.rc('legend', fontsize=25)

    plt.title("Learning curves")
    plt.savefig("fig.png")

    #mpld3.show()

    try:
        models_params.pop("final_script")
        models_params.pop("val_script")
    except:
        pass

    figure = mpld3.fig_to_html(fig)
    #print figure
    #params=json.dumps(params, indent=4, sort_keys=True)
    #if type(models)!=list: models=[models]
    with app.app_context():
        #print models
        return render_template('plot_models.html',
                               figure=figure,
                               models=models_params)
Esempio n. 33
0
def plot_and_info(model_name):
    print model_name
    model = client.get({"params.model_name": model_name}, collection="Models")
    fig, ax = plt.subplots(figsize=(12, 7))
    h = []
    params = model["params"]
    model_name = params["model_name"]
    #print model_name
    curves = client.get_learning_curves(
        "./" + model_name)  #,val_ref="/mnt/nfs/himl.testing.cs-en.tcs.cs")
    #startTime=client.get_train_start(model_name)
    try:
        endTime = client.get_train_end(model_name)
    except:
        endTime = None
    #print curves
    for name, points in curves.iteritems():
        #print points
        #print name
        #print points[0][0]
        #print endTime
        if endTime:
            if points[0][
                    0] > endTime:  # aby evaluace po konce trenovani byly zarovnane s koncovym casem
                points[0][0] = endTime
                #print "HA"
        curve = [((date - points[0][0]).total_seconds() / 3600.0, score)
                 for date, score in points]

        c = np.asarray(curve)
        #print c
        # print c
        # fig.autofmt_xdate()
        # max_idx=np.argmax(c[:,1])
        # ax.annotate("test", xy=(c[0],c[1]), xytext=(0, 0),arrowprops=dict(arrowstyle="->"))
        lines = plt.plot(c[:, 0],
                         c[:, 1],
                         marker='o',
                         label=' '.join((model_name, str(max(c[:, 1])), " (",
                                         name.split("/")[-1], ")")))
        print lines
        mpld3.plugins.connect(
            fig,
            mpld3.plugins.PointLabelTooltip(lines[0], labels=c[:, 1].tolist()))
        h.append(lines[0])
    plt.legend(
        handles=h, loc='lower right',
        prop=fontP)  # ,bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    # ax.legend(loc='upper center', bbox_to_anchor=(0.0, -0.05),
    #          fancybox=True, shadow=True)

    plt.xlabel('Hours')
    plt.ylabel('BLEU')
    plt.ylim([-5, 50])
    plt.title("Learning curves")
    #plugins.connect(fig, plugins.LineLabelTooltip(lines, c[:, 1].tolist()))
    #mpld3.show()
    #plt.savefig("fig.png")
    try:
        params.pop("final_script")
        params.pop("val_script")
    except:
        pass
    try:
        prev_model = client.db.Models.find({
            '_id': {
                '$lt': model["_id"]
            }
        }).sort([('_id', -1)]).limit(1)[0]["params"]["model_name"]
    except:
        prev_model = "asd"
    try:
        next_model = client.db.Models.find({
            '_id': {
                '$gt': model["_id"]
            }
        }).sort([('_id', 1)]).limit(1)[0]["params"]["model_name"]
    except:
        next_model = "asd"

    figure = mpld3.fig_to_html(fig)
    #print figure
    print prev_model
    #params=json.dumps(params, indent=4, sort_keys=True)
    with app.app_context():
        return render_template(
            'model_template.html',
            prev_model=prev_model,
            next_model=next_model,
            model_name=model_name,
            figure=figure,
            params=model["params"]
        )  #"<br>".join(["%s:\t%s"%(key, value) for key, value in params.iteritems()]))
Esempio n. 34
0
def show(name=None):
    # get selected stock name
    if request.method == 'POST':
        name = request.form['stockName']
        showPeriod = request.form['showPeriod']

        import genIndicator as gi

        if showPeriod == 'longTerm':
            # get long term stock data and generate indicators
            df = gi.getAndGen(name)

            import constDaytime as cd
            startShowPoint = cd.startShowing
            endShowPoint = cd.end
        else:
            # get short term intraday data and generate indicators
            import googleFinanceIntraday as gfi

            # get 30 days intraday data, periode 60s
            if name == 'airbus':
                intradayList = gfi.get_google_finance_intraday('AIR', 60, 30)
            elif name == 'sopra':
                intradayList = gfi.get_google_finance_intraday('SOP', 60, 30)
            elif name == 'biomerieux':
                intradayList = gfi.get_google_finance_intraday('BIM', 60, 30)
            elif name == 'oreal':
                intradayList = gfi.get_google_finance_intraday('OR', 60, 30)
            elif name == 'total':
                intradayList = gfi.get_google_finance_intraday('FP', 60, 30)

            dateList = []
            for item in intradayList:
                dateList.append(item.index[0].strftime(
                    '%d/%m/%Y'))  # copy all dates in a list
                print(item.index[0].strftime('%d/%m/%Y'))

            seletedDateTimeStr = showPeriod  # the date and time selected by user
            seletedDateTime = datetime.strptime(
                seletedDateTimeStr,
                '%d/%m/%Y - %Hh%M')  # parse to datetime object
            seletedDateStr = seletedDateTimeStr[0:10]
            # seletedTimeStr = seletedDateTimeStr[13:18]

            # print(seletedDateStr)
            # print(dateList.index(seletedDateStr))
            try:
                df = intradayList[dateList.index(
                    seletedDateStr
                )]  # target dataframe, because intradayList and dateList have the same index
                # gi.genAll(df)   # generate indicators
                # print(df['Close'])

                # set start and end point
                startShowPoint = df.index[0]  # type : timestamp
                # endShowPoint = seletedDateTime.timestamp()
                import pandas as pd
                endShowPoint = pd.Timestamp(seletedDateTime)
                # print(df.loc[startShowPoint:endShowPoint])
            except ValueError:
                df = "Intraday history no more available.</br>"
                startShowPoint = 0  # type : timestamp
                endShowPoint = 0

        # read scores from file
        import readFromFile as rff
        # print(name)
        ssList = rff.analyseScoreFile(
            rff.readScoreFile(name)
        )  # list of scoreStructure by date, containing tables, date, time and final score

        intradayTime = []  # list of datetime when give score
        intradayTimeHtml = []  # list of html code for short term option

        ssToShow = None
        intradayTimeHtml.append('<option value="longTerm">longTerm</option>')
        for ss in ssList:
            if showPeriod != 'longTerm':  # target seleted date's scoreStructure
                if ss.date == seletedDateTimeStr[:10].replace("/", " "):
                    ssToShow = ss
            if not ss.date == 'model':  # generate option list for short term
                tempDateTime = datetime(int(ss.date[6:10]),
                                        int(ss.date[3:5]),
                                        int(ss.date[:2]),
                                        hour=int(ss.time[:2]),
                                        minute=int(ss.time[3:5]))
                intradayTime.append(tempDateTime)
                intradayTimeHtml.append(
                    '<option value="%(val)s">%(text)s</option>' % {
                        'val': tempDateTime.strftime('%d/%m/%Y - %Hh%M'),
                        'text': tempDateTime.strftime('%d/%m/%Y - %Hh%M')
                    })

        if showPeriod == 'longTerm':
            ssToShow = ssList[0]  # for showing finance score in long term page

        if not isinstance(df, str):
            import matplotlib.pyplot as plt, mpld3  # 2D plotting library
            # plotIndicator(df, 'RS')
            # rs = mpld3.fig_to_html(plt.gcf())
            # plt.close()
            plotIndicator(df,
                          'Bollinger',
                          startShowPoint=startShowPoint,
                          endShowPoint=endShowPoint,
                          title=showPeriod)
            bollinger = mpld3.fig_to_html(plt.gcf())
            plt.close()
            plotIndicator(df,
                          'MA',
                          startShowPoint=startShowPoint,
                          endShowPoint=endShowPoint,
                          title=showPeriod)
            ma = mpld3.fig_to_html(plt.gcf())
            plt.close()
            plotIndicator(df,
                          'MACD',
                          startShowPoint=startShowPoint,
                          endShowPoint=endShowPoint,
                          title=showPeriod)
            macd = mpld3.fig_to_html(plt.gcf())
            plt.close()
            plotIndicator(df,
                          'RSI',
                          startShowPoint=startShowPoint,
                          endShowPoint=endShowPoint,
                          title=showPeriod)
            rsi = mpld3.fig_to_html(plt.gcf())
            plt.close()
            plotIndicator(df,
                          'Stochastic',
                          startShowPoint=startShowPoint,
                          endShowPoint=endShowPoint,
                          title=showPeriod)
            stoschastic = mpld3.fig_to_html(plt.gcf())
            plt.close()
            fig = [bollinger, ma, macd, rsi, stoschastic]

            if showPeriod == 'longTerm':  # long term use a different template
                return render_template(
                    'stockLongTerm.html',
                    name=name,
                    fig=fig,
                    shortTermDateTimeOption=intradayTimeHtml,
                    financeScore=ssToShow.finance['Score'].as_matrix())
            else:
                return render_template(
                    'stock.html',
                    name=name,
                    fig=fig,
                    score=ssToShow.graphic['Score'].as_matrix(),
                    raison=ssToShow.graphic['Reason'].as_matrix(),
                    shortTermDateTimeOption=intradayTimeHtml,
                    seletedDateTimeStr=seletedDateTimeStr,
                    criterionScore=ssToShow.criterion['Result'].as_matrix(),
                    finalScore=ssToShow.finalScore)
        elif df == "Intraday history no more available.</br>":
            return render_template(
                'stock.html',
                name=name,
                fig=[df, df, df, df, df],
                score=ssToShow.graphic['Score'].as_matrix(),
                raison=ssToShow.graphic['Reason'].as_matrix(),
                shortTermDateTimeOption=intradayTimeHtml,
                seletedDateTimeStr=seletedDateTimeStr,
                criterionScore=ssToShow.criterion['Result'].as_matrix(),
                finalScore=ssToShow.finalScore)
        else:
            return render_template('readError.html', name=name)
Esempio n. 35
0
    def service_type_plot(self,
                          df,
                          urls_map=None,
                          interactive=True,
                          asl=False):
        fig, ax = plt.subplots(nrows=1, ncols=1)
        years = list(df['Financial Year'].unique())
        service_types = list(df['Service type'].unique())
        width = 0.25
        bars_list = []
        # Draw the bar graphs
        for i, year in enumerate(years):
            group_df = df[df['Financial Year'] == year]
            group = group_df['Sum of ASL'] if asl else group_df['Sum of Total']
            pos = [
                service_types.index(x) for x in group_df['Service type'].values
            ]
            bar_lefts = self._get_lefts(i, len(years), width, pos)
            bars = ax.bar(bar_lefts,
                          group,
                          color=_set_periods_colors(year),
                          width=width,
                          linewidth=4,
                          edgecolor=_set_periods_colors(year),
                          zorder=2)
            bars_list.append({'bars': bars, 'values': group})

        # Set chart properties and formatting
        fig.set_size_inches(12, 6)
        ax.yaxis.set_major_formatter(FuncFormatter(self.format_amount))
        ax.tick_params(axis='y',
                       which='major',
                       labelsize=14,
                       labelcolor='#2F2F31',
                       length=0)

        if interactive:
            ax.tick_params(axis='x', which='major', labelsize=14, length=0)
            xlabels = [x.title() for x in service_types]
            ax.set_xticks(range(len(xlabels)))
            ax.set_xticklabels(xlabels)
        else:
            ax.tick_params(axis='x', which='major', labelsize=9.5, length=0)
            xlabels = [x.replace(' ', '\n') for x in service_types]
            ax.set_xticklabels([], position=(0, -0.04), clip_on=True)

        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.yaxis.grid(color='lightgray', linewidth=2)
        yaxis_label = 'ASL' if asl else 'Dollars'
        ax.set_ylabel(yaxis_label, fontsize=14)

        # Draw the legend
        handles = []
        for i, year in enumerate(years):
            patch = mpatches.Patch(color=_set_periods_colors(year),
                                   label=year,
                                   linewidth=4)
            handles.append(patch)
        if interactive:
            ax.legend(handles=handles, loc='best')
        else:
            ax.legend(handles=handles,
                      frameon=False,
                      loc='center right',
                      bbox_to_anchor=(1.15, 0.5))

        # Data table
        df2 = pd.DataFrame([], columns=service_types)

        def get_value(df, x):
            try:
                if asl:
                    value = df.loc[x]['Sum of ASL']
                else:
                    value = df.loc[x]['Sum of Total']

                return value
            except KeyError:
                return 0

        for i, year in enumerate(years, 1):
            year_df = df[df['Financial Year'] == year]
            year_df.set_index('Service type', inplace=True)
            df2.loc[year] = [get_value(year_df, x) for x in service_types]
        df2['Total Cost'] = [df2.loc[year].sum() for year in df2.index.values]
        if asl:
            for year in years:
                df2.loc[year] = df2.loc[year].apply('{:.2f}'.format)
        else:
            for year in years:
                df2.loc[year] = df2.loc[year].apply('{:.2f}'.format)

        if interactive:
            # Create tooltips for bars
            for bars_group in bars_list:
                for i, bar in enumerate(bars_group['bars'].get_children()):
                    value = list(bars_group['values'])[i]
                    if asl:
                        formatted_value = '{:.2f} ASL'.format(value)
                    else:
                        formatted_value = '${:,.0f}'.format(value)
                    label = "<div style='background: white; padding: 2px; border: solid 1px black;'>%s</div>" % formatted_value
                    tooltip = mpld3.plugins.LineHTMLTooltip(bar,
                                                            label=label,
                                                            hoffset=15,
                                                            voffset=-5)
                    mpld3.plugins.connect(fig, tooltip)
            # Create links in x-axis labels
            mpld3.plugins.connect(fig, TickLabelLink(urls_map))
            # Format the y-axis values
            mpld3.plugins.connect(fig, YTickFormat('dollar thousands'))
            # Create the agency counts table
            html = mpld3.fig_to_html(fig)
            table_json = df2.to_dict('split')
            return html, table_json
        else:
            data_table = ax.table(cellText=df2.as_matrix(),
                                  colLabels=xlabels,
                                  rowLabels=years,
                                  loc='bottom')
            data_table.auto_set_font_size(False)
            data_table.set_fontsize(9.5)

            # Adjust cell heights
            data_table.scale(1, 2)
            cells = data_table.get_celld()
            for x, y in cells:
                cell = cells[(x, y)]
                if x == 0:
                    cell.set_height(0.2)
                cell.set_linewidth(0.5)
                cell.set_linestyle('dashed')

            fig.tight_layout()
            return fig
Esempio n. 36
0
        fig_amps_errs = _gen_amps_errs_plot(amps_orig, singles_or, amps_extr,
                                            singles_det, amps_errs_bins,
                                            amps_errs_range)

        fig_times_errs = _gen_times_errs_plot(pos_orig, singles_or, pos_extr,
                                              singles_det, times_errs_bins,
                                              times_errs_range)

        md = markdown(OUT_TEMPLATE)
        html = Environment(loader=BaseLoader()).from_string(md).render(
            method=metrics_all['method'],
            total=len(amps_orig),
            extr_all=len(amps_extr),
            extr_all_perc=len(amps_extr) / len(amps_orig) * 100,
            extr_fpos=len(false_pos),
            extr_fpos_perc=len(false_pos) / len(amps_extr) * 100,
            extr_ok=len(singles_or),
            extr_ok_perc=len(singles_or) / len(amps_extr) * 100,
            extr_ovlpd=len(mult_or),
            extr_ovlpd_perc=len(mult_or) / len(amps_extr) * 100,
            hist_plot=mpld3.fig_to_html(fig_hist),
            amp_errs_plot=mpld3.fig_to_html(fig_amps_errs),
            times_errs_plot=mpld3.fig_to_html(fig_times_errs))

        fig_hist.show()
        fig_amps_errs.show()
        fig_times_errs.show()
        plt.show()

        serve(html)
def states_problem_min_banks_prob_not_changed(bailout_cost, deadweight_cost,
                                              discount_rate, nr_of_periods,
                                              opt_pi_bank):
    # State's problem: min if Bank's probabilitie is not changed

    # Solve the equation from (20) on:
    v_state_opt_min = [0] * nr_of_periods
    pi_state_tresh_min = [0] * nr_of_periods
    opt_pi_bank_min = opt_pi_bank

    # n=0, pi=0 does not change
    f_state_0 = lambda x: math.pow(
        -deadweight_cost + bailout_cost + discount_rate * deadweight_cost *
        (1 - x) / (1 - discount_rate * x), 2)
    pi_state_tresh_min[0] = optimize.root(f_state_0, 0.5, method='lm').x
    v_state_opt_min[0] = deadweight_cost * (1 - opt_pi_bank_min[0]) / (
        1 - discount_rate * opt_pi_bank_min[0])

    # previous periods, n > 0:
    f_state = lambda x,y: math.pow(- deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * \
                                                             (1 - x) / (1 - discount_rate * x),2)

    n_end = 0

    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt_min[i - 1])
        pi_state_tresh_min[i] = optimize.root(f_state_opt, 0.5, method='lm').x

        if min(deadweight_cost, bailout_cost +
               discount_rate * v_state_opt_min[i - 1]) == deadweight_cost:
            n_end = nr_of_periods - i + 1

        v_state_opt_min[i] = min(deadweight_cost, bailout_cost + discount_rate * v_state_opt_min[i-1]) *\
                             (1 - opt_pi_bank_min[i]) / (1 - discount_rate * opt_pi_bank_min[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig6, ax = pyplot.subplots()
    ax.plot(nr_per,
            flipud(opt_pi_bank_min),
            label='Bank',
            color='red',
            linestyle=':')
    ax.plot(nr_per,
            flipud(pi_state_tresh_min),
            label='State',
            color='blue',
            linestyle='--')
    ax.set_xlabel('Stage of the Game')
    ax.set_ylabel('Probability')
    ax.set_title('Optimal Probabilities')
    ax.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig6 = mpld3.fig_to_html(fig6)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities_Banks_Prob_not_changed.pdf',
        format='pdf')
    pyplot.close(fig6)

    fig7, bx = pyplot.subplots()
    bx.plot(nr_per, flipud(v_state_opt_min), label='Value', color='red')
    bx.set_xlabel('Stage of the Game')
    bx.set_ylabel('Value')
    bx.set_title('State value if it choses whether to save the bank')
    bx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig7 = mpld3.fig_to_html(fig7)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_State_Value_if_choses_whether_to_save_the_Bank.pdf',
        format='pdf')
    pyplot.close(fig7)

    return html_text_fig6, html_text_fig7
    def generateReport(self, user, org, today, lds_df, maps_df,
                       top_5_maps_date, analysis_types_df, analysis_df,
                       dsets_df, tables_sizes, top_5_dsets_date,
                       top_5_dsets_size, sync, private, link, public, geo,
                       none_tbls, points, lines, polys, fig_analysis, fig_lds):
        '''
        Method to generate a HTML report.
        '''

        self.logger.info('Generating HTML template...')

        template = """
            <!DOCTYPE html>
            <html lang="en">
            <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="ie=edge">
            <title>CARTO Database Metrics Report Template</title>
            <link rel="stylesheet" href="https://libs.cartocdn.com/airship-style/v1.0.3/airship.css">
            <script src="https://libs.cartocdn.com/airship-components/v1.0.3/airship.js"></script>
            <style>
            .as-sidebar{
                width: 33.33%;
            }
            .as-box{
                border-bottom: 1px solid #F5F5F5;
            }
            </style>
            </head>                   
            <body class="as-app-body as-app">
            <header class="as-toolbar">
                <div class="as-toolbar__item as-title">
                    CARTO Metrics Report 
                </div>
                <div class="as-toolbar__item as-display--block as-p--12 as-subheader as-bg--complementary">
                    {{ user }} from {{org}} at {{today}}
                </div>
            </header>
            <div class="as-content">
                <aside class="as-sidebar as-sidebar--left">
                <div class="as-container">
                    <h1 class="as-box as-title as-font--medium">
                    Maps and Analysis
                    </h1>
                    <div class="as-box">
                        <h2 class="as-title">
                            Maps
                        </h2>
                        <p class="as-body as-font--medium">Number of maps: {{total_maps}}</p>
                        <div class="as-box" id="maps-table">
                            <h3 class="as-subheader">Top 5 Maps by Date</h3>
                            {{top_5_maps_date.to_html()}}
                        </div>
                    </div>

                    <div class="as-box">
                    <h2 class="as-title">
                        Builder Cached Analysis
                    </h2>
                    <ul class="as-list">
                        <li class="as-list__item">Number of cached analyses: {{total_analysis}}</li>
                        <li class="as-list__item">Cached Analyses Size: {{total_size_analysis}} MB</li>
                    </ul>
                    <div class="as-box" id="analysis-table">
                        {{analysis_types_df.to_html()}}
                    </div>
                    <div class="as-box" id="analysis-fig">
                        {{html_fig_analysis}}
                    </div>
                    </div>
                </div>
                </aside>
                <main class="as-main">
                    <h1 class="as-box as-title as-font--medium">
                        Storage Quota & LDS
                    </h1>
                    <div class="as-box">
                        <h2 class="as-title">
                            Storage Quota
                        </h2>
                        <ul class="as-list">
                            <li class="as-list__item as-font--medium">Account Storage: {{real_storage}} MB</li>
                            <li class="as-list__item as-color--support-01">Used Quota: {{used_storage}} MB, {{pc_used}} %</li>
                            <li class="as-list__item as-color--complementary">Quota Left: {{left_storage}} MB, {{pc_left}} %</li>
                        </ul>
                    </div>
                    <div class="as-box">
                        <h2 class="as-title">
                            Location Data Services
                        </h2>
                        <div class="as-box" id="lds-table">
                            {{lds.to_html()}}
                        </div>
                        <div class="as-box" id="lds-fig">
                            {{html_fig_lds}}
                        </div>
                    </div>
                </main>
                <aside class="as-sidebar as-sidebar--right">
                <div class="as-container">
                    <div class="as-box as-title as-font--medium">
                    Datasets
                    </div>
                    <div class="as-box">
                        <h2 class="as-title">
                            Datasets Summary
                        </h2>
                        <ul class="as-list">
                            <li class="as-list__item as-font--medium">Number of tables: {{total_dsets}}</li>
                            <li class="as-list__item">Sync tables: {{sync}}</li>
                            <li class="as-list__item">Tables Size: {{total_size_tbls}} MB</li>
                        </ul>
                    </div>
                    <div class="as-box">
                    <h2 class="as-title">
                        Privacy
                    </h2>
                    <ul class="as-list">
                        <li class="as-list__item as-color--support-01">🔒 Private: {{private}} tables</li>
                        <li class="as-list__item as-color--support-02">🔗 Shared with link: {{link}} tables</li>
                        <li class="as-list__item as-color--support-03">🔓 Public: {{public}} tables</li>
                    </ul>
                    </div>
                    <div class="as-box">
                    <h2 class="as-title">
                        Geometry
                    </h2>
                    <p class="as-body">
                        Number of geocoded tables: {{geo}}
                    </p>
                    <ul class="as-list">
                        <li class="as-list__item">📌 Points: {{points}} tables</li>
                        <li class="as-list__item">〰️ Lines: {{lines}} tables</span></li>
                        <li class="as-list__item">⬛ Polygons: {{polys}} tables</li>
                    </ul>
                    <p class="as-body">
                        Number of non-geocoded tables: {{none_tbls}}
                    </p>
                    </div>
                    <div class="as-box" id="tables-size">
                        <h3 class="as-subheader">Top 5 Datasets by Size</h3>
                        {{top_5_dsets_size.to_html()}}
                    </div>
                    <div class="as-box" id="tables-date">
                        <h3 class="as-subheader">Top 5 Datasets by Date</h3>
                        {{top_5_dsets_date.to_html()}}
                    </div>
                </div>
                </aside>
            </div>
            <script>
                // add airship class to tables 
                const tableElements = document.querySelectorAll('table');
                tableElements.forEach(element => element.classList.add("as-table"));
            </script>
            </body>
            </html>
        """
        rtemplate = Environment(loader=BaseLoader()).from_string(template)

        self.logger.info('Rendering HTML report...')

        report = rtemplate.render({

            # user and date info
            'user':
            user,
            'org':
            org,
            'today':
            today,

            # lds and storage info
            'lds':
            lds_df,
            'real_storage':
            lds_df.iloc[3]['Monthly Quota'],
            'used_storage':
            lds_df.iloc[3]['Used'],
            'pc_used':
            lds_df.iloc[3]['% Used'],
            'left_storage':
            lds_df.iloc[3]['Left'],
            'pc_left':
            round(lds_df.iloc[3]['% Left'], 2),

            # maps info
            'total_maps':
            len(maps_df),
            'total_analysis':
            len(analysis_df),
            'total_size_analysis':
            analysis_df['size'].sum(),
            'analysis_types_df':
            analysis_types_df,
            'top_5_maps_date':
            top_5_maps_date,

            # datasets info
            'sync':
            sync,
            'total_dsets':
            len(dsets_df),
            'total_size_tbls':
            tables_sizes['size'].sum(),
            'top_5_dsets_size':
            top_5_dsets_size[['size']],
            'top_5_dsets_date':
            top_5_dsets_date[['created']],

            # privacy info
            'private':
            private,
            'link':
            link,
            'public':
            public,

            # geometry info
            'geo':
            geo,
            'points':
            points,
            'lines':
            lines,
            'polys':
            polys,
            'none_tbls':
            none_tbls,

            # figures
            'html_fig_analysis':
            fig_to_html(fig_analysis),
            'html_fig_lds':
            fig_to_html(fig_lds)
        })

        return report
Esempio n. 39
0
    def render(self, idf, filename='output.html'):
        progress_inst = helpers.Progress(idf.opt)
        self.progress = progress_inst.progress

        if idf.opt['outputFilename']:
            filename = idf.opt['outputFilename']

        if idf.opt['outputAs'] == 'html':
            # write matplotlib/d3 plots to html file
            import matplotlib
            import matplotlib.pyplot as plt, mpld3
            import matplotlib.axes

            from mpld3 import plugins
            from jinja2 import Environment, FileSystemLoader
        elif idf.opt['outputAs'] in ['pdf', 'interactive', 'tikz']:
            # show plots in separate matplotlib windows
            import matplotlib
            if idf.opt['outputAs'] == 'pdf':
                from matplotlib.backends.backend_pdf import PdfPages
                pp = PdfPages(filename)
            import matplotlib.pyplot as plt
            import matplotlib.axes
        else:
            print("No proper output method given. Not plotting.")
            return

        font_size = 10
        if idf.opt['outputAs'] in ['pdf', 'tikz']:
            if idf.opt['plotPerJoint']:
                font_size = 30
            else:
                font_size = 12
            matplotlib.rcParams.update({'font.size': font_size})
            matplotlib.rcParams.update({'axes.labelsize': font_size - 5})
            matplotlib.rcParams.update({'axes.linewidth': font_size / 15.})
            matplotlib.rcParams.update({'axes.titlesize': font_size - 2})
            matplotlib.rcParams.update({'legend.fontsize': font_size - 2})
            matplotlib.rcParams.update({'xtick.labelsize': font_size - 5})
            matplotlib.rcParams.update({'ytick.labelsize': font_size - 5})
            matplotlib.rcParams.update({'lines.linewidth': font_size / 15.})
            matplotlib.rcParams.update({'patch.linewidth': font_size / 15.})
            matplotlib.rcParams.update({'grid.linewidth': font_size / 20.})

        # skip some samples so graphs don't get too large/detailed TODO: change skip so that some
        # maximum number of points is plotted (determined by screen etc.)
        skip = 5

        #create figures and plots
        figures = list()
        for ds in self.progress(range(len(self.datasets))):
            group = self.datasets[ds]
            fig, axes = plt.subplots(len(group['dataset']),
                                     sharex=True,
                                     sharey=True)
            # scale unified scaling figures to same ranges and add some margin
            if group['unified_scaling']:
                ymin = 0
                ymax = 0
                for i in range(len(group['dataset'])):
                    ymin = np.min(
                        (np.min(group['dataset'][i]['data']), ymin)) * 1.05
                    ymax = np.max(
                        (np.max(group['dataset'][i]['data']), ymax)) * 1.05

            #plot each group of data
            for d_i in range(len(group['dataset'])):
                d = group['dataset'][d_i]
                if not issubclass(type(axes), matplotlib.axes.SubplotBase):
                    ax = axes[d_i]
                else:
                    ax = axes
                    axes = [axes]
                if idf.opt['outputAs'] != 'tikz':
                    ax.set_title(d['title'])
                if group['unified_scaling']:
                    ax.set_ylim([ymin, ymax])
                for data_i in range(0, len(d['data'])):
                    if len(d['data'][data_i].shape) > 1:
                        #data matrices
                        for i in range(0, d['data'][data_i].shape[1]):
                            l = group['labels'][i] if data_i == 0 else ''
                            if i < 6 and 'contains_base' in group and group[
                                    'contains_base']:
                                ls = 'dashed'
                            else:
                                ls = '-'
                            dashes = ()  # type: Tuple
                            if idf.opt['plotErrors']:
                                if idf.opt['plotPrioriTorques']:
                                    n = 3
                                else:
                                    n = 2
                                if i == n:
                                    ls = 'dashed'
                                    dashes = (3, 0.5)
                            ax.plot(d['time'][::skip],
                                    d['data'][data_i][::skip, i],
                                    label=l,
                                    color=colors[i],
                                    alpha=1 - (data_i / 2.0),
                                    linestyle=ls,
                                    dashes=dashes)
                    else:
                        #data vector
                        ax.plot(d['time'][::skip],
                                d['data'][data_i][::skip],
                                label=group['labels'][d_i],
                                color=colors[0],
                                alpha=1 - (data_i / 2.0))

                ax.grid(which='both', linestyle="dotted", alpha=0.8)
                if 'y_label' in group:
                    ax.set_ylabel(group['y_label'])

            if idf.opt['outputAs'] != 'tikz':
                ax.set_xlabel("Time (s)")

            plt.setp([a.get_xticklabels() for a in axes[:-1]], visible=False)
            #plt.setp([a.get_yticklabels() for a in axes], fontsize=8)

            if idf.opt['plotLegend']:
                handles, labels = ax.get_legend_handles_labels()
                if idf.opt['outputAs'] == 'html':
                    #TODO: show legend properly (see mpld3 bug #274)
                    #leg = fig.legend(handles, labels, loc='upper right', fancybox=True, fontsize=10, title='')
                    leg = axes[0].legend(handles,
                                         labels,
                                         loc='upper right',
                                         fancybox=True,
                                         fontsize=10,
                                         title='',
                                         prop={'size': 8})
                else:
                    leg = plt.figlegend(handles,
                                        labels,
                                        loc='upper right',
                                        fancybox=True,
                                        fontsize=font_size,
                                        title='',
                                        prop={'size': font_size - 3})
                    leg.draggable()

            fig.subplots_adjust(hspace=2)
            fig.set_tight_layout(True)

            if idf.opt['outputAs'] == 'html':
                plugins.clear(fig)
                plugins.connect(fig, plugins.Reset(), plugins.BoxZoom(),
                                plugins.Zoom(enabled=False),
                                plugins.MousePosition(fontsize=14, fmt=".5g"))
                figures.append(mpld3.fig_to_html(fig))
            elif idf.opt['outputAs'] == 'interactive':
                plt.show(block=False)
            elif idf.opt['outputAs'] == 'pdf':
                pp.savefig(plt.gcf())
            elif idf.opt['outputAs'] == 'tikz':
                from matplotlib2tikz import save as tikz_save
                tikz_save('{}_{}_{}.tex'.format(
                    filename, group['dataset'][0]['title'].replace('_', '-'),
                    ds // idf.model.num_dofs),
                          figureheight='\\figureheight',
                          figurewidth='\\figurewidth',
                          show_info=False)

        if idf.opt['outputAs'] == 'html':
            path = os.path.dirname(os.path.abspath(__file__))
            template_environment = Environment(
                autoescape=False,
                loader=FileSystemLoader(os.path.join(path, '../output')),
                trim_blocks=False)

            context = {'figures': figures, 'text': self.text}
            outfile = os.path.join(path, '..', 'output', filename)
            import codecs
            with codecs.open(outfile, 'w', 'utf-8') as f:
                html = template_environment.get_template(
                    "templates/index.html").render(context)
                f.write(html)

            print("Saved output at file://{}".format(outfile))
        elif idf.opt['outputAs'] == 'interactive':
            #keep non-blocking plot windows open
            plt.show()
        elif idf.opt['outputAs'] == 'pdf':
            pp.close()
Esempio n. 40
0
df['x'] = np.random.randn(N)
df['y'] = np.random.randn(N)

labels = []
for i in range(N):
    label = df.ix[[i], :].T
    label.columns = ['point {0}'.format(i)]
    # .to_html() is unicode; so make leading 'u' go away with str()
    labels.append(str(label.to_html()))

points = ax.scatter(df.x,
                    df.y,
                    c=np.random.random(size=N),
                    s=1000 * np.random.random(size=N),
                    alpha=.6)

ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('ScatterPlot', size=20)

tooltip = plugins.PointHTMLTooltip(points,
                                   labels,
                                   voffset=10,
                                   hoffset=10,
                                   css=css)
plugins.connect(fig, tooltip)

mpld3.show()
fig = plt.figure(1, figsize=(9, 6))
print(mpld3.fig_to_html(fig))
Esempio n. 41
0
def graphic(request, funcion, iteraciones, lower_bound, upper_bound):
    
    import matplotlib.pyplot as plt, mpld3
    import matplotlib.figure as fg
    import matplotlib
    import numpy as np
    import mpld3
    from mpld3 import plugins, utils
    from matplotlib.patches import Polygon
    
    from pprint import pprint
    
    
    fig, ax = plt.subplots(figsize=(11,6.5))
    # pprint(vars(fig))
    # print ('#########################################3')
    x = np.linspace(-10, 10, 1000)
    #fig.subplots_adjust(right=2,top=1.1)
    #fig.adjustable='box'
    #fig.figsize=(5,2)
    # print(type(fig))
    # fig.fg.Figure()
    # Ver atributos de un objeto
    # pprint(vars(fig))
    # ax.legend_ = 'Raffo'
    # fig.figurePatch.set_width(3)
    # fig.figurePatch.set_height(3)
    # fig._animated=True
    # print(fig._animated)
    #paso de la funcion a graficar
    y='x**3
    #iteraciones
    n=0
    #limite inferior
    a=-10.0
    #limite superior
    b=10.0
    
    #fig.Figure(figsize=1000)

    """rect=[1,1,1,1]
    fig.add_axes(rect)
    fig.add_axes(rect, frameon=False, facecolor='g')
    fig.add_axes(rect, polar=True)
    fig.add_axes(rect, projection='polar')
    fig.add_axes(ax)"""
    # Propiedades de la linea que es definida por la funcion
    ax.plot(x,y, lw=5, alpha=0.7)
    # Add grid to figure
    ax.grid(True, alpha=0.3)
    
    #ax.set_ylim(-1.2, 1.0)
    #ax.text(5, -1.1, "Here are some curves", size=18)
    
    # Dibujar area bajo la curva
    ix = np.linspace(a, b)
    iy = ix
    verts = [(a, 0)] + list(zip(x, y)) + [(b, 0)]
    poly = Polygon(verts, facecolor='0.9', edgecolor='0.5')
    ax.add_patch(poly)

    


    # For transform it to HTML
    g = mpld3.fig_to_html(fig)
    return HttpResponse(g)
Esempio n. 42
0
    def draw(self,
             category,
             num_top_words_to_annotate=4,
             words_to_annotate=[],
             scores=None,
             transform=percentile_alphabetical):
        '''Outdated.  MPLD3 drawing.

		Parameters
		----------
		category
		num_top_words_to_annotate
		words_to_annotate
		scores
		transform

		Returns
		-------
		pd.DataFrame, html of fgure
		'''
        try:
            import matplotlib.pyplot as plt
        except:
            raise Exception(
                "matplotlib and mpld3 need to be installed to use this function."
            )
        try:
            from mpld3 import plugins, fig_to_html
        except:
            raise Exception("mpld3 need to be installed to use this function.")
        all_categories, other_categories = self._get_category_names(category)
        df = self._term_rank_score_and_frequency_df(all_categories, category,
                                                    other_categories, scores)
        if self.x_coords is None:
            df['x'], df['y'] = self._get_coordinates_from_transform_and_jitter_frequencies \
             (category, df, other_categories, transform)
        df_to_annotate = df[
            (df['not category score rank'] <= num_top_words_to_annotate)
            | (df['category score rank'] <= num_top_words_to_annotate)
            | df['term'].isin(words_to_annotate)]
        words = list(df['term'])

        font = {
            'family': 'sans-serif',
            'color': 'black',
            'weight': 'normal',
            'size': 'large'
        }

        fig, ax = plt.subplots()
        plt.figure(figsize=(10, 10))
        plt.gcf().subplots_adjust(bottom=0.2)
        plt.gcf().subplots_adjust(right=0.2)

        points = ax.scatter(self.x_coords,
                            self.y_coords,
                            c=-df['color_scores'],
                            cmap='seismic',
                            s=10,
                            edgecolors='none',
                            alpha=0.9)
        tooltip = plugins.PointHTMLTooltip(
            points, ['<span id=a>%s</span>' % w for w in words],
            css='#a {background-color: white;}')
        plugins.connect(fig, tooltip)
        ax.set_ylim([-.2, 1.2])
        ax.set_xlim([-.2, 1.2])
        ax.xaxis.set_ticks([0., 0.5, 1.])
        ax.yaxis.set_ticks([0., 0.5, 1.])
        ax.set_ylabel(category.title() + ' Frequency Percentile',
                      fontdict=font,
                      labelpad=20)
        ax.set_xlabel('Not ' + category.title() + ' Frequency Percentile',
                      fontdict=font,
                      labelpad=20)

        for i, row in df_to_annotate.iterrows():
            # alignment_criteria = row['category score rank'] < row['not category score rank']
            alignment_criteria = i % 2 == 0
            horizontalalignment = 'right' if alignment_criteria else 'left'
            verticalalignment = 'bottom' if alignment_criteria else 'top'
            term = row['term']
            ax.annotate(
                term,
                (self.x_coords[i], y_data[i]),
                size=15,
                horizontalalignment=horizontalalignment,
                verticalalignment=verticalalignment,
            )
        # texts.append(
        # ax.text(row['dem freq scaled'], row['rep freq scaled'], row['word'])
        # )
        # adjust_text(texts, arrowprops=dict(arrowstyle="->", color='r', lw=0.5))
        plt.show()
        return df, fig_to_html(fig)
Esempio n. 43
0
        if first_only[i] and fi != 0:
            x[i] = []
            y[i] = []
    if labels:
        lab = labels[fi*len(fields):(fi+1)*len(fields)]
    else:
        lab = fields[:]
    if args.multi:
        col = colors[:]
    else:
        col = colors[fi*len(fields):]
    fig = plotit(x, y, lab, colors=col)
    for i in range(0, len(x)):
        x[i] = []
        y[i] = []
if args.output is None:
    pylab.show()
    pylab.draw()
    input('press enter to exit....')
else:
    fname, fext = os.path.splitext(args.output)
    if fext == '.html':
        import mpld3
        html = mpld3.fig_to_html(fig)
        f_out = open(args.output, 'w')
        f_out.write(html)
        f_out.close()
    else:
        pylab.legend(loc=2,prop={'size':8})
        pylab.savefig(args.output, bbox_inches='tight', dpi=200)
def joint_problem(r_max, r_min, bailout_cost, deadweight_cost, discount_rate,
                  nr_of_periods):
    # Joint problem: after each stage Bank promises pi <= pi_state, but recalculates it's value

    opt_pi_bank_joint = [0] * nr_of_periods
    opt_pi_state_joint = [0] * nr_of_periods
    v_bank_joint = [0] * nr_of_periods
    v_state_joint = [0] * nr_of_periods

    # n=0
    f_opt_bank_0 = lambda x: -x * (r_min + (r_max - r_min) *
                                   (1 - x)) / (1 - discount_rate * x)
    f_state_0 = lambda x: math.pow(
        -deadweight_cost + bailout_cost + discount_rate * deadweight_cost *
        (1 - x) / (1 - discount_rate * x), 2)

    opt_pi_bank_joint[0] = optimize.fmin(
        f_opt_bank_0,
        0.5)  # optimization using: Simplex method: the Nelder-Mead
    opt_pi_state_joint[0] = optimize.root(f_state_0, 0.5, method='lm').x

    # Bank "promises" the threshold pi in order to be saved at period n=1 even it it's optimal pi is lower:
    if opt_pi_bank_joint[0] < opt_pi_state_joint[0]:
        opt_pi_bank_joint[0] = opt_pi_state_joint[0]

    v_bank_joint[0] = -f_opt_bank_0(opt_pi_bank_joint[0])
    v_state_joint[0] = deadweight_cost * (1 - opt_pi_bank_joint[0]) / (
        1 - discount_rate * opt_pi_bank_joint[0])

    # Previous periods:
    f_bank = lambda x, y: -((x * (r_min + (r_max - r_min) * (1 - x)) +
                             (1 - x) * (bailout_cost + discount_rate * y)) /
                            (1 - discount_rate * x))
    f_state = lambda x,y: math.pow(- deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * \
                                                             (1 - x) / (1 - discount_rate * x), 2)

    # Options = optimset('TolFun', 1e-15):
    for i in range(1, nr_of_periods):
        f_opt_bank = lambda x: f_bank(x, v_bank_joint[i - 1])
        f_state_opt = lambda x: f_state(x, v_state_joint[i - 1])

        opt_pi_bank_joint[i] = optimize.fmin(f_opt_bank, 0.7)
        opt_pi_state_joint[i] = optimize.root(f_state_opt, 0.5, method='lm').x

        # BANKS's PROBABILITES CHANGED IF THEY ALE LOWER THAN PI_STATE AND THEN THEY ARE TAKEN FOR V's:
        if opt_pi_bank_joint[i] < opt_pi_state_joint[i]:
            opt_pi_bank_joint[i] = opt_pi_state_joint[i]

        v_bank_joint[i] = -f_opt_bank(opt_pi_bank_joint[i])
        v_state_joint[i] = (bailout_cost + discount_rate * v_state_joint[i-1]) * (1 - opt_pi_bank_joint[i]) /\
                           (1 - discount_rate * opt_pi_bank_joint[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig8, ax = pyplot.subplots()
    ax.plot(nr_per,
            flipud(opt_pi_bank_joint),
            label='Bank',
            color='red',
            linestyle=':')
    ax.plot(nr_per,
            flipud(opt_pi_state_joint),
            label='State',
            color='blue',
            linestyle='--')
    ax.set_xlabel('Stage of the Game')
    ax.set_ylabel('Probability')
    ax.set_title('Optimal probabilities in joint game')
    ax.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig8 = mpld3.fig_to_html(fig8)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities_in_Joint_Game.pdf',
        format='pdf')
    pyplot.close(fig8)

    fig9, bx = pyplot.subplots()
    bx.plot(nr_per, flipud(v_state_joint), color='red')
    bx.set_title('State value in the joint game')
    bx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig9 = mpld3.fig_to_html(fig9)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_State_Value_in_the_Joint_Game.pdf',
        format='pdf')
    pyplot.close(fig9)

    return html_text_fig8, html_text_fig9
Esempio n. 45
0
    def service_profile_plot(self,
                             service_name,
                             df1,
                             df2,
                             deidentified=False,
                             browser='edge',
                             asl=False,
                             selected_agency_name=''):
        # Initialize list that should contain the subplots
        subplots = []
        #---------------------------------------------------
        # Initialize the figure and axes for the scatterplot
        #---------------------------------------------------
        fig = plt.figure(figsize=(7, 5))
        gs = gridspec.GridSpec(2, 1, height_ratios=[6, 1.5])

        unit_type = df1.columns[1]
        ax1 = fig.add_subplot(gs[0, 0])
        profiles = df1[df1['Type'] == 'WofG Agencies']
        dot_size = 100
        if browser == 'edge':
            dot_size = 30
        scatter1 = ax1.scatter(profiles[unit_type],
                               profiles['Unit Rates'],
                               c=colors[0],
                               s=dot_size,
                               zorder=2)
        selected_orgs = df1[df1['Type'] == selected_agency_name]
        scatter2 = None
        if len(selected_orgs):
            scatter2 = ax1.scatter(selected_orgs[unit_type],
                                   selected_orgs['Unit Rates'],
                                   c="#ec4a41",
                                   s=dot_size,
                                   zorder=2)

        ax1.set_ylabel('Unit Rates', fontsize=12)
        ax1.set_xlabel(unit_type, fontsize=12)

        # Grid and labels formatting
        ax1.yaxis.grid(color='lightgray', linewidth=2, zorder=1)
        ax1.xaxis.grid(color='lightgray', linewidth=2, zorder=1)
        ax1.set_title(service_name, position=(0.5, 1.12), fontsize=18)
        ax1.text(0.5,
                 1.1,
                 '(Unit Type = %s)' % unit_type,
                 transform=ax1.transAxes,
                 ha='center',
                 va='center',
                 fontsize=14)

        for axis in ['top', 'bottom', 'left', 'right']:
            ax1.spines[axis].set_linewidth(3)

        # Draw the tooltip for each point
        def format_label(agency, unit_type, x, y):
            style = 'style="background: white; border: 1px solid black; padding: 5px;"'
            if agency:
                label = '<div {}><strong>{}</strong><br>'.format(style, agency)
            else:
                label = '<div {}>'.format(style)
            label += '{}: {:,.0f}<br>Unit Rate: {:,.2f}</div>'.format(
                unit_type, x, y)
            return label

        for sdf, points in [(profiles, scatter1), (selected_orgs, scatter2)]:
            if points:
                labels = zip(sdf['Agency name'], sdf[unit_type],
                             sdf['Unit Rates'])
                labels = [
                    format_label(agency, unit_type, x, y)
                    for agency, x, y in labels
                ]
                tooltip = mpld3.plugins.PointHTMLTooltip(points,
                                                         labels=labels,
                                                         hoffset=15,
                                                         voffset=-5)

                mpld3.plugins.connect(fig, tooltip)

        # Draw and format the legend
        ax2 = fig.add_subplot(gs[1, 0])
        handles = []
        for label, color in [('WofG Agencies', colors[0]),
                             (selected_agency_name, '#ec4a41')]:
            circle = mlines.Line2D(range(1),
                                   range(1),
                                   marker='o',
                                   color='white',
                                   markerfacecolor=color,
                                   markeredgecolor=color,
                                   markersize=9,
                                   label=label)
            handles.append(circle)
        legend = ax2.legend(handles=handles, ncol=2, loc='lower center')
        frame = legend.get_frame()
        frame.set_facecolor('white')
        frame.set_linewidth(2)
        frame.set_edgecolor('black')
        mpld3.plugins.connect(fig, HideSuplotAxes(2))

        html = mpld3.fig_to_html(fig)
        # We can eventually remove the code above
        # but for now, we'll just stop providing it to the template.
        # This is deprecated by a template-driven JS charting library.
        # subplots.append(html)

        #---------------------------------------------------------
        # Initialize the figure and axes for the stacked bar graph
        #---------------------------------------------------------
        fig = plt.figure(figsize=(7, 5))
        gs = gridspec.GridSpec(2, 1, height_ratios=[6, 1.5])
        ax1 = fig.add_subplot(gs[0, 0])

        # Create a color mapping in tuples
        cost_types = list(df2['Cost Type'].dropna().values)
        cost_types.sort()
        cost_type_colors = zip(cost_types, colors[0:len(df2)])
        colors_dict = dict(cost_type_colors)

        df2['Order'] = df2['Cost Type'].dropna().apply(
            lambda x: cost_types.index(x))
        df2['Color'] = df2['Cost Type'].dropna().apply(
            lambda x: colors_dict[x])
        df2 = df2.dropna(subset=['Cost Type']).sort_values('Order',
                                                           ascending=False)

        def create_stacked_bar(pos, values, colors, cost_types):
            width = 0.5
            lastx = 0
            bars_list = []
            for x, color, cost_type in list(zip(values, colors, cost_types)):
                bar = ax1.bar(pos, x, width, bottom=lastx, color=color)
                lastx += x
                bars_list.append({'bar': bar, 'value': x, 'name': cost_type})
            return bars_list

        bar_groups = {
            1: {
                'name': service_name
            },
            2: {
                'name': 'WofG Agencies - %s' % service_name
            }
        }
        df2.sort_values('Cost Type', inplace=True)
        for bg in bar_groups:
            bars = create_stacked_bar(bg, df2[bar_groups[bg]['name']],
                                      df2['Color'], df2['Cost Type'])
            #if interactive:
            for bar in bars:
                style = 'style="background: white; border: 1px solid black; padding: 2px;"'
                label = '<div {}><strong>{}</strong><br>Percentage: {:,.2f}%</div>'
                label = label.format(style, bar['name'], bar['value'])
                tooltip = mpld3.plugins.LineHTMLTooltip(
                    bar['bar'].get_children()[0],
                    label=label,
                    hoffset=15,
                    voffset=-5)
                mpld3.plugins.connect(fig, tooltip)
        ax1.set_title('Compare Your Cost Categories',
                      position=(0.5, 1.05),
                      fontsize=18)
        percentage_by = 'ASL' if asl else 'Dollars'
        ax1.set_ylabel('Percentage - {}'.format(percentage_by), fontsize=12)

        def draw_subplot2_legend(ax):
            # Draw the legend
            handles = []
            for cost_type, color in colors_dict.items():
                if cost_type != 'Unknown':
                    patch = mpatches.Patch(color=color, label=cost_type)
                    handles.append(patch)
            legend = ax.legend(handles=handles, loc='lower center', ncol=3)
            frame = legend.get_frame()
            frame.set_linewidth(2)
            frame.set_facecolor('#f0f3ee')
            frame.set_edgecolor('#dbe0d5')
            return legend

        ax1.xaxis.set_label_coords(0.5, -0.1)
        ax1.yaxis.set_label_coords(-0.1, 0.5)
        ax1.set_xlim(0, 3)
        ax1.set_xticks([1, 2])
        ax1.set_xticklabels(
            [service_name, 'WofG Agencies - %s' % service_name])

        ax2 = fig.add_subplot(gs[1, 0])
        draw_subplot2_legend(ax2)
        mpld3.plugins.connect(fig, HideSuplotAxes(2))

        html = mpld3.fig_to_html(fig)
        subplots.append(html)

        return {
            'subplots': subplots,
            'data': {
                'profiles': profiles.to_dict(),
                'agency': selected_orgs.to_dict()
            }
        }
Esempio n. 46
0
    def service_classification_plot(self,
                                    agency_count_df,
                                    df,
                                    urls_map=None,
                                    interactive=True,
                                    asl=False):
        self.interactive = interactive
        fig, ax = plt.subplots(nrows=1, ncols=1)
        years = list(df['Financial Year'].unique())
        service_classes = list(df['Service classification'].unique())
        width = 0.25
        bars_list = []
        # Draw the bar graphs
        for i, year in enumerate(years):
            group_df = df[df['Financial Year'] == year]
            group = group_df['Sum of ASL'] if asl else group_df['Sum of Total']
            pos = [
                service_classes.index(x)
                for x in group_df['Service classification'].values
            ]
            bar_lefts = self._get_lefts(i, len(years), width, pos)
            bars = ax.bar(bar_lefts,
                          group,
                          color=_set_periods_colors(year),
                          width=width,
                          linewidth=4,
                          edgecolor=_set_periods_colors(year),
                          zorder=2)
            bars_list.append({'values': group, 'bars': bars})

        # Set chart properties and formatting
        fig.set_size_inches(12, 6)
        ax.yaxis.set_major_formatter(FuncFormatter(self.format_amount))
        ax.tick_params(axis='y',
                       which='major',
                       labelsize=14,
                       labelcolor='#2F2F31',
                       length=0)
        ax.tick_params(axis='x',
                       which='major',
                       labelsize=14,
                       labelcolor='#2F2F31',
                       length=0)
        xlabels = df['Service classification'].unique()
        ax.set_xticks([p for p in range(len(xlabels))])
        if interactive:
            labels = ax.set_xticklabels(xlabels)
        else:
            rotation = 0
            halign = 'center'
            # Adjust alignment and rotation if there are more than 4 labels
            if len(xlabels) > 4:
                rotation = 45
                halign = 'right'
            labels = ax.set_xticklabels(xlabels,
                                        fontweight='semibold',
                                        rotation=rotation,
                                        position=(0, -0.04))
            for label in labels:
                label.set_horizontalalignment(halign)
        ax.spines['top'].set_visible(False)
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)
        ax.spines['bottom'].set_visible(False)
        ax.yaxis.grid(color='lightgray', linewidth=2)
        yaxis_label = 'ASL' if asl else 'Dollars'
        ax.set_ylabel(yaxis_label, fontsize=14)

        # Draw the legend
        handles = []
        for i, year in enumerate(years):
            patch = mpatches.Patch(color=_set_periods_colors(year),
                                   label=year,
                                   linewidth=4)
            handles.append(patch)
        if interactive:
            ax.legend(handles=handles, frameon=False, loc='best')
        else:
            ax.legend(handles=handles,
                      frameon=False,
                      loc='center right',
                      bbox_to_anchor=(1.15, 0.5))

        # Prepare the agency counts table
        agency_count_df.fillna(0, inplace=True)

        # Data table
        df1 = pd.DataFrame([], columns=service_classes)

        def get_value(df, x):
            try:
                if asl:
                    value = df.loc[x]['Sum of ASL']
                else:
                    value = df.loc[x]['Sum of Total']

                return value
            except KeyError:
                return 0

        for i, year in enumerate(years, 1):
            year_df = df[df['Financial Year'] == year]
            year_df.set_index('Service classification', inplace=True)
            df1.loc[year] = [get_value(year_df, x) for x in service_classes]
        if asl:
            for year in years:
                df1.loc[year] = df1.loc[year].apply('{:.2f}'.format)
        else:
            for year in years:
                df1.loc[year] = df1.loc[year].apply('{:.2f}'.format)

        if interactive:
            # Create tooltips for bars
            for bars_group in bars_list:
                for i, bar in enumerate(bars_group['bars'].get_children()):
                    value = list(bars_group['values'])[i]
                    if asl:
                        formatted_value = '{:.2f} ASL'.format(value)
                    else:
                        formatted_value = '${:,.0f}'.format(value)
                    label = "<div style='background: white; padding: 2px; border: solid 1px black;'>%s</div>" % formatted_value
                    tooltip = mpld3.plugins.LineHTMLTooltip(bar,
                                                            label=label,
                                                            hoffset=15,
                                                            voffset=-5)
                    mpld3.plugins.connect(fig, tooltip)
            # Create links in x-axis labels
            mpld3.plugins.connect(fig, TickLabelLink(urls_map))
            # Format the y-axis values

            mpld3.plugins.connect(fig, YTickFormat('dollar thousands'))
            # Create the agency counts table
            html = mpld3.fig_to_html(fig)
            agency_count_table_json = agency_count_df.to_dict('split')
            table_json = df1.to_dict('split')
            return html, agency_count_table_json, table_json
        else:
            ax.table(cellText=agency_count_df.as_matrix(),
                     rowLabels=agency_count_df.index,
                     colLabels=agency_count_df.columns,
                     bbox=[0, 1.1, 0.5, 0.7],
                     loc='top')
            fig.tight_layout()
            return fig
def urenderGraph(dataSet):
  """
  This function renders a graph for the use in the specialized word graphs. This portion of the code was not completed at the time of submission, and so this function is currently not being used. 
  """
  tdata=dataSet[0]
  sdata=dataSet[1]
  tsize=len(tdata)
  fig=plt.figure(1)
  fig.set_size_inches(11,(tsize*2.25))
  #fig.set_size_inches(18.5, 10.5, forward=True)
  counter=1
  for word, num in tdata:
    for tword, value in sdata.items():
      tempwords=[]
      tempcounts=[]
      tempucounts=[]
      if(word==tword):
        for sword, counts in value:
          tempwords.append(sword)
          tempcounts.append(counts[0])
          tempucounts.append(counts[1])
        ax=plt.subplot(tsize,1,counter, autoscale_on=True)
        ind = np.arange(len(tempcounts))  # the x locations for the groups
        width = 0.35       # the width of the bars
        rects1 = ax.bar(ind, tempcounts, width, color='r')
        rects2 = ax.bar(ind+.35, tempucounts, width, color='b')
        
        ax.set_ylabel("Occurances")
        ax.set_title(word)
        ax.set_xticks(ind + width / 2)
        ax.set_xticklabels(tempwords)
        
        uautolabel(rects1,ax)
        uautolabel(rects2,ax)
        
        counter+=1
#  data = dataSet[0]
#  title = dataSet[1]
#  ylabel = dataSet[2]
#  xlabels = dataSet[3]
#  data2 = dataSet[4]
  #bool1 = dataSet[5]
#  ind = np.arange(len(data))  # the x locations for the groups
 # width = 0.35       # the width of the bars

  #fig, ax = plt.subplots()
  #print 'Before figure'
  #chart = plt.figure(1)
  #print 'After first figure'

  # add some text for labels, title and axes ticks
#  ax.set_ylabel(ylabel)
#  ax.set_title(title)
#  ax.set_xticks(ind + width / 2)
#  ax.set_xticklabels(xlabels)

  #fig_to_html attempt
  #current error: 'Figure' object has no attribute 'fig_to_html'
  
  #needed to download jinja2 for mpld3 to work
  fig.tight_layout()
  graphOutput = plt.figure() #initialize variable as current graph to be later passed on to HTML code
  graphHTML = fig_to_html(fig) #convert current graph to HTML code
  #print (graphHTML)
  #if(bool1==1):
  #plt.show()
  plt.close(graphOutput) #close the current graph
  mpld3.display(fig)
  return graphHTML
def states_problem_change_banks_pi(bailout_cost, deadweight_cost,
                                   discount_rate, nr_of_periods, opt_pi_bank,
                                   v_state_opt):
    # State's problem: Change Bank's pi if it's less then State's pi

    # solve the equation from (20) on:
    v_state_opt_switch = [0] * nr_of_periods
    pi_state_tresh_switch = [0] * nr_of_periods
    opt_pi_bank_switch = [0] * nr_of_periods

    # n=0
    f_state_0 = lambda x: math.pow(
        -deadweight_cost + bailout_cost + discount_rate * deadweight_cost *
        (1 - x) / (1 - discount_rate * x), 2)
    pi_state_tresh_switch[0] = optimize.fsolve(f_state_0, 0.5)

    # Bank's "promises" the threshold pi in order to be saved at period n=1 even
    # if it's optimal pi is lower:
    if opt_pi_bank[0] < pi_state_tresh_switch[0]:
        opt_pi_bank_switch[0] = pi_state_tresh_switch[0]
    else:
        opt_pi_bank_switch[0] = opt_pi_bank[0]

    v_state_opt_switch[0] = deadweight_cost * (1 - opt_pi_bank_switch[0]) / (
        1 - discount_rate * opt_pi_bank_switch[0])

    # previous periods
    f_state = lambda x,y: math.pow(- deadweight_cost + bailout_cost + discount_rate * (bailout_cost + discount_rate * y) * \
                                                             (1 - x) / (1 - discount_rate * x),2)

    # solve for all the previous periods:
    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt_switch[i - 1])
        pi_state_tresh_switch[i] = optimize.fsolve(f_state_opt, 0.5)

        # BANK's PROBABILITIES CHANGED IF THEY ALE LOWER THAN PI_STATE AND THEN THEY ARE TAKEN FOR V's:
        if opt_pi_bank[i] < pi_state_tresh_switch[i]:
            opt_pi_bank_switch[i] = pi_state_tresh_switch[i]
        else:
            opt_pi_bank_switch[i] = opt_pi_bank[i]

        v_state_opt_switch[i] = (bailout_cost + discount_rate * v_state_opt_switch[i-1]) * (1 - opt_pi_bank_switch[i])\
                                / (1 - discount_rate * opt_pi_bank_switch[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig4, ax = pyplot.subplots()
    ax.plot(nr_per, flipud(opt_pi_bank), label='Bank', color='red')
    ax.plot(nr_per,
            flipud(pi_state_tresh_switch),
            label='State',
            color='blue',
            linestyle=':')
    ax.plot(nr_per,
            flipud(opt_pi_bank_switch),
            label='Bank Switched',
            color='green',
            linestyle='--')
    ax.set_xlabel('Stage of the Game')
    ax.set_ylabel('Probability')
    ax.set_title('Optimal Probabilities')
    ax.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig4 = mpld3.fig_to_html(fig4)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities_Change_Banks_PI.pdf',
        format='pdf')
    pyplot.close(fig4)

    fig5, bx = pyplot.subplots()
    bx.plot(nr_per,
            flipud(v_state_opt_switch),
            label='New value (Bank switches)',
            color='red',
            linestyle=':')
    bx.plot(nr_per,
            flipud(v_state_opt),
            label='Old value',
            color='blue',
            linestyle='--')
    bx.set_title('State value if Bank switches to state pi')
    bx.legend(loc='lower left',
              frameon=True,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig5 = mpld3.fig_to_html(fig5)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_State_Value_if_Bank_switches_to_State_PI.pdf',
        format='pdf')
    pyplot.close(fig5)

    return html_text_fig4, html_text_fig5
Esempio n. 49
0
def generate_plot(tran, ambig, min_read, max_read, master_filepath_dict, lite,
                  offset_dict, ribocoverage, organism, normalize, short_code,
                  background_col, hili_start, hili_stop, comp_uag_col,
                  comp_uga_col, comp_uaa_col, trips_annotation_location,
                  title_size, subheading_size, axis_label_size, marker_size,
                  cds_marker_size, cds_marker_colour, legend_size,
                  transcriptome):
    labels = []
    start_visible = []
    line_collections = []
    all_stops = ["TAG", "TAA", "TGA"]
    returnstr = "Position,"
    y_max = 50
    if normalize == True:
        y_max = 0

    connection = sqlite3.connect(
        '/home/DATA/www/tripsviz/tripsviz/trips.sqlite')
    connection.text_factory = str
    cursor = connection.cursor()
    cursor.execute(
        "SELECT owner FROM organisms WHERE organism_name = '{}' and transcriptome_list = '{}';"
        .format(organism, transcriptome))
    owner = (cursor.fetchone())[0]
    if owner == 1:
        transhelve = sqlite3.connect("{0}{1}/{1}.v2.sqlite".format(
            trips_annotation_location, organism))
    else:
        transhelve = sqlite3.connect(
            "{0}transcriptomes/{1}/{2}/{3}/{2}_{3}.v2.sqlite".format(
                trips_uploads_location, owner, organism, transcriptome))
    cursor = transhelve.cursor()
    cursor.execute(
        "SELECT * from transcripts WHERE transcript = '{}'".format(tran))
    result = cursor.fetchone()
    traninfo = {
        "transcript": result[0],
        "gene": result[1],
        "length": result[2],
        "cds_start": result[3],
        "cds_stop": result[4],
        "seq": result[5],
        "strand": result[6],
        "stop_list": result[7].split(","),
        "start_list": result[8].split(","),
        "exon_junctions": result[9].split(","),
        "tran_type": result[10],
        "principal": result[11]
    }
    traninfo["stop_list"] = [int(x) for x in traninfo["stop_list"]]
    traninfo["start_list"] = [int(x) for x in traninfo["start_list"]]
    if str(traninfo["exon_junctions"][0]) != "":
        traninfo["exon_junctions"] = [
            int(x) for x in traninfo["exon_junctions"]
        ]
    else:
        traninfo["exon_junctions"] = []
    transhelve.close()
    gene = traninfo["gene"]
    tranlen = traninfo["length"]
    cds_start = traninfo["cds_start"]
    cds_stop = traninfo["cds_stop"]
    strand = traninfo["strand"]

    if cds_start == 'NULL' or cds_start == None:
        cds_start = 0
    if cds_stop == 'NULL' or cds_stop == None:
        cds_stop = 0

    all_starts = traninfo["start_list"]
    all_stops = {"TAG": [], "TAA": [], "TGA": []}
    seq = traninfo["seq"].upper()
    for i in range(0, len(seq)):
        if seq[i:i + 3] in all_stops:
            all_stops[seq[i:i + 3]].append(i + 1)
    start_stop_dict = {
        1: {
            "starts": [0],
            "stops": {
                "TGA": [0],
                "TAG": [0],
                "TAA": [0]
            }
        },
        2: {
            "starts": [0],
            "stops": {
                "TGA": [0],
                "TAG": [0],
                "TAA": [0]
            }
        },
        3: {
            "starts": [0],
            "stops": {
                "TGA": [0],
                "TAG": [0],
                "TAA": [0]
            }
        }
    }
    for start in all_starts:
        rem = ((start - 1) % 3) + 1
        start_stop_dict[rem]["starts"].append(start - 1)
    for stop in all_stops:
        for stop_pos in all_stops[stop]:
            rem = ((stop_pos - 1) % 3) + 1
            start_stop_dict[rem]["stops"][stop].append(stop_pos - 1)

    fig = plt.figure(figsize=(23, 12))
    ax_main = plt.subplot2grid((30, 1), (0, 0), rowspan=22)
    if normalize != True:
        label = 'Read count'
    else:
        label = 'Normalized read count'
    ax_main.set_ylabel(label, fontsize=axis_label_size, labelpad=30)
    label = 'Position (nucleotides)'
    ax_main.set_xlabel(label, fontsize=axis_label_size, labelpad=10)

    #if normalize is true work out the factors for each colour
    if normalize == True:
        all_mapped_reads = []
        for color in master_filepath_dict:
            all_mapped_reads.append(
                master_filepath_dict[color]["mapped_reads"])
        min_reads = float(min(all_mapped_reads))
        for color in master_filepath_dict:
            factor = min_reads / float(
                master_filepath_dict[color]["mapped_reads"])
            master_filepath_dict[color]["factor"] = factor

    # So items can be plotted alphabetically
    unsorted_list = []
    for color in master_filepath_dict:
        input_list = [
            color, master_filepath_dict[color]["file_names"],
            master_filepath_dict[color]["file_descs"],
            master_filepath_dict[color]["file_ids"],
            master_filepath_dict[color]["filepaths"],
            master_filepath_dict[color]["file_type"],
            master_filepath_dict[color]["minread"],
            master_filepath_dict[color]["maxread"]
        ]
        if "factor" in master_filepath_dict[color]:
            input_list.append(master_filepath_dict[color]["factor"])
        unsorted_list.append(input_list)

    sorted_list = sorted(unsorted_list, key=lambda x: x[1][0])
    returndict = {}
    for item in sorted_list:
        # needed to make get_reads accept file_paths
        file_paths = {"riboseq": {}}
        for i in range(0, len(item[3])):
            file_paths["riboseq"][item[3][i]] = item[4][i]
        file_names = item[1][0]
        file_descs = item[2]
        if item[5] == "riboseq":
            filename_reads, seqvar_dict = get_reads(ambig, item[6], item[7],
                                                    tran, file_paths, tranlen,
                                                    ribocoverage, organism,
                                                    False, False, "fiveprime",
                                                    "riboseq", 1)
        else:
            filename_reads, seqvar_dict = get_reads(ambig, item[6], item[7],
                                                    tran, file_paths, tranlen,
                                                    True, organism, False,
                                                    False, "fiveprime",
                                                    "riboseq", 1)
        if normalize == False:
            try:
                max_val = max(filename_reads.values()) * 1.1
                if max_val > y_max:
                    y_max = max_val
            except Exception as e:
                print "Error", e
                pass
            labels.append(file_names)
            start_visible.append(True)
            plot_filename = ax_main.plot(filename_reads.keys(),
                                         filename_reads.values(),
                                         alpha=1,
                                         label=labels,
                                         zorder=1,
                                         color=item[0],
                                         linewidth=3)
            line_collections.append(plot_filename)
            returndict[file_names] = {}
            for pos in filename_reads:
                returndict[file_names][pos] = filename_reads[pos]

        else:
            normalized_reads = {}
            for pos in filename_reads:
                normalized_reads[pos] = filename_reads[pos] * item[8]
            try:
                max_val = max(normalized_reads.values()) * 1.1
                if max_val > y_max:
                    y_max = max_val
            except Exception as e:
                print "Error", e
                pass
            labels.append(file_names)
            start_visible.append(True)
            plot_filename = ax_main.plot(normalized_reads.keys(),
                                         normalized_reads.values(),
                                         alpha=1,
                                         label=labels,
                                         zorder=1,
                                         color=item[0],
                                         linewidth=3)

            line_collections.append(plot_filename)
            returndict[file_names] = {}
            for pos in filename_reads:
                returndict[file_names][pos] = normalized_reads[pos]

    for plot_filename in returndict:
        returnstr += "{},".format(plot_filename)
    returnstr += "\n"

    for i in range(0, tranlen):
        returnstr += "{},".format(i)
        for plot_filename in returndict:
            returnstr += "{},".format(returndict[plot_filename][i])
        returnstr += "\n"

    ax_main.set_ylim(0, y_max)
    # draw cds start
    plt.plot((cds_start, cds_start), (0, y_max),
             cds_marker_colour,
             linestyle=':',
             linewidth=cds_marker_size)

    # draw cds end
    plt.plot((cds_stop, cds_stop), (0, y_max),
             cds_marker_colour,
             linestyle=':',
             linewidth=cds_marker_size)
    ax_f1 = plt.subplot2grid((30, 1), (27, 0), rowspan=1, sharex=ax_main)
    ax_f1.set_axis_bgcolor('lightgray')
    ax_f2 = plt.subplot2grid((30, 1), (28, 0), rowspan=1, sharex=ax_main)
    ax_f2.set_axis_bgcolor('lightgray')
    ax_f6 = plt.subplot2grid((30, 1), (29, 0), rowspan=1, sharex=ax_main)
    ax_f6.set_axis_bgcolor('lightgray')
    ax_f6.set_xlabel('Transcript: {}   Length: {} nt'.format(tran, tranlen),
                     fontsize=subheading_size,
                     labelpad=10)

    for axis, frame in ((ax_f1, 1), (ax_f2, 2), (ax_f6, 3)):
        color = color_dict['frames'][frame - 1]
        axis.set_xlim(0, tranlen)
        starts = [(item, 1) for item in start_stop_dict[frame]['starts']]
        axis.broken_barh(starts, (0.5, 1),
                         color='white',
                         zorder=5,
                         linewidth=2)
        stops = [(item, 1) for item in start_stop_dict[frame]['stops']]
        uag_stops = [(item, 1)
                     for item in start_stop_dict[frame]['stops']['TAG']]
        uaa_stops = [(item, 1)
                     for item in start_stop_dict[frame]['stops']['TAA']]
        uga_stops = [(item, 1)
                     for item in start_stop_dict[frame]['stops']['TGA']]
        axis.broken_barh(uag_stops, (0, 1),
                         color=comp_uag_col,
                         zorder=2,
                         linewidth=2)
        axis.broken_barh(uaa_stops, (0, 1),
                         color=comp_uaa_col,
                         zorder=2,
                         linewidth=2)
        axis.broken_barh(uga_stops, (0, 1),
                         color=comp_uga_col,
                         zorder=2,
                         linewidth=2)
        axis.set_ylabel('{}'.format(frame),
                        rotation='horizontal',
                        labelpad=10,
                        verticalalignment='center')
        axis.set_ylim(0, 1)
        axis.tick_params(top=False,
                         left=False,
                         right=False,
                         bottom=False,
                         labeltop=False,
                         labelleft=False,
                         labelright=False,
                         labelbottom=False)
    ax_f6.axes.get_yaxis().set_ticks([])
    ax_f2.axes.get_yaxis().set_ticks([])
    ax_f1.axes.get_yaxis().set_ticks([])
    title_str = '{} ({})'.format(gene, short_code)
    plt.title(title_str, fontsize=title_size, y=36)

    if not (hili_start == 0 and hili_stop == 0):
        hili_start = int(hili_start)
        hili_stop = int(hili_stop)
        hili = ax_main.fill_between([hili_start, hili_stop], [y_max, y_max],
                                    zorder=0,
                                    alpha=0.75,
                                    color="#fffbaf")
        labels.append("Highligter")
        start_visible.append(True)
        line_collections.append(hili)

    leg_offset = (legend_size - 17) * 5
    if leg_offset < 0:
        leg_offset = 0
    leg_offset += 230
    ilp = plugins.InteractiveLegendPlugin(line_collections,
                                          labels,
                                          alpha_unsel=0.05,
                                          xoffset=leg_offset,
                                          yoffset=20,
                                          start_visible=start_visible,
                                          fontsize=legend_size)
    plugins.connect(fig, ilp, plugins.TopToolbar(yoffset=100),
                    plugins.DownloadProfile(returnstr=returnstr),
                    plugins.DownloadPNG(returnstr=title_str))
    ax_main.set_axis_bgcolor(background_col)
    # This changes the size of the tick markers, works on both firefox and chrome.
    ax_main.tick_params('both', labelsize=marker_size)
    ax_main.xaxis.set_major_locator(plt.MaxNLocator(3))
    ax_main.yaxis.set_major_locator(plt.MaxNLocator(3))
    ax_main.grid(color="white", linewidth=20, linestyle="solid")
    graph = "<div style='padding-left: 55px;padding-top: 22px;'> <a href='https://trips.ucc.ie/short/{0}' target='_blank' ><button class='button centerbutton' type='submit'><b>Direct link to this plot</b></button></a> </div>".format(
        short_code)

    graph += mpld3.fig_to_html(fig)
    return graph
def states_problem(bailout_cost, deadweight_cost, discount_rate, nr_of_periods,
                   opt_pi_bank):
    # State's Problem

    # solve the equations from (20) on:
    v_state_opt = [0] * nr_of_periods
    pi_state_thresh = [0] * nr_of_periods

    x = math.pow(5, 2)

    # Period n=0
    f_state_0 = lambda x: math.pow(
        -deadweight_cost + bailout_cost + discount_rate * deadweight_cost *
        (1 - x) / (1 - discount_rate * x), 2)
    pi_state_thresh[0] = optimize.fsolve(f_state_0, 0.5)
    v_state_opt[0] = deadweight_cost * (1 - opt_pi_bank[0]) / (
        1 - discount_rate * opt_pi_bank[0])

    # previous periods:
    f_state = lambda x, y: math.pow(
        -deadweight_cost + bailout_cost + discount_rate *
        (bailout_cost + discount_rate * y) * (1 - x) /
        (1 - discount_rate * x), 2)

    # solve for the rest of the periods:
    for i in range(1, nr_of_periods):
        f_state_opt = lambda x: f_state(x, v_state_opt[i - 1])
        pi_state_thresh[i] = optimize.fsolve(f_state_opt, 0.5)

        #BANK's PROBABILITIE'sS ARE TAKEN FOR v_state
        v_state_opt[i] = (bailout_cost + discount_rate * v_state_opt[i - 1]) * (1 - opt_pi_bank[i]) / \
                         (1 - discount_rate * opt_pi_bank[i])

    nr_per = [0] * nr_of_periods
    for i in range(0, nr_of_periods):
        nr_per[i] = i

    # Create plots and pdf file. Save pdf file into graphics folder
    fig2, ax = pyplot.subplots()
    ax.plot(nr_per,
            flipud(opt_pi_bank),
            label='Bank',
            color='red',
            linestyle=':')
    ax.plot(nr_per,
            flipud(pi_state_thresh),
            label='State',
            color='blue',
            linestyle='--')
    ax.set_xlabel('Stage of the Game')
    ax.set_ylabel('Probability')
    ax.set_title('Optimal Probabilities')
    ax.legend(loc='upper left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig2 = mpld3.fig_to_html(fig2)
    pyplot.savefig(
        'tbtf/modelfinitebounded/graphics/FB_Optimal_Probabilities.pdf',
        format='pdf')
    pyplot.close(fig2)

    fig3, bx = pyplot.subplots()
    bx.plot(nr_per, flipud(v_state_opt), label='Bank', color='red')
    bx.set_title('State Value')
    bx.legend(loc='lower left',
              frameon=0,
              fontsize='medium',
              title='',
              fancybox=True)
    html_text_fig3 = mpld3.fig_to_html(fig3)
    pyplot.savefig('tbtf/modelfinitebounded/graphics/FB_State_Value.pdf',
                   format='pdf')
    pyplot.close(fig3)

    return html_text_fig2, html_text_fig3, v_state_opt
Esempio n. 51
0
def main():
    import argparse
    import argcomplete
    basic_desc = "experimental tool for opening a serialized PlotCollection (pickle format)"
    lic = "(c) [email protected]"
    main_parser = argparse.ArgumentParser(description="%s %s" %
                                          (basic_desc, lic))
    main_parser.add_argument("in_file",
                             help="path to a serialized plot_collection")
    main_parser.add_argument("-t",
                             "--title",
                             help="custom title (default: file name)")
    main_parser.add_argument("--save_plot",
                             help="path to save plot",
                             default=None)
    main_parser.add_argument("--serialize_plot",
                             help="path to re-serialize PlotCollection",
                             default=None)
    main_parser.add_argument("--to_html",
                             help="convert to html (requires mpld3 library)",
                             action="store_true")
    main_parser.add_argument("--no_warnings",
                             help="no warnings requiring user confirmation",
                             action="store_true")
    argcomplete.autocomplete(main_parser)
    args = main_parser.parse_args()

    from evo.tools import log, plot, user
    log.configure_logging(verbose=True)

    if not args.title:
        title = os.path.basename(args.in_file)
    else:
        title = args.title
    if not args.no_warnings:
        logger.warning(
            "Please note that this tool is experimental and not guranteed to work.\n"
            "Only works if the same plot settings are used as for serialization.\n"
            "If not, try: evo_config show/set \n" + SEP)

    plot_collection = plot.PlotCollection(title, deserialize=args.in_file)
    logger.debug("deserialized PlotCollection: " + str(plot_collection))
    plot_collection.show()

    if args.serialize_plot:
        logger.debug(SEP)
        plot_collection.serialize(args.serialize_plot,
                                  confirm_overwrite=not args.no_warnings)
    if args.save_plot:
        logger.debug(SEP)
        plot_collection.export(args.save_plot,
                               confirm_overwrite=not args.no_warnings)
    if args.to_html:
        import mpld3
        logger.debug(SEP + "\nhtml export\n")
        for name, fig in plot_collection.figures.items():
            html = mpld3.fig_to_html(fig)
            out = name + ".html"
            with open(out, 'w') as f:
                logger.debug(out)
                f.write(html)
    if not args.no_warnings:
        logger.debug(SEP)
        if user.confirm("save changes & overwrite original file " +
                        args.in_file + "? (y/n)"):
            plot_collection.serialize(args.in_file, confirm_overwrite=False)
Esempio n. 52
0
def create_interactive_figure():
    css = """
    h5
    {
      font-size: 1.5em;
      margin-top: 0.83em;
      margin-bottom: 0.83em;
      margin-left: 0.83em;
      margin-right: 0.83em;
      color: #0099ff;
      background-color: rgba(255,255,255,0.8);
    }
    h6
    {
      font-size: 1.5em;
      margin-top: 0.83em;
      margin-bottom: 0.83em;
      margin-left: 0.83em;
      margin-right: 0.83em;
      color: #ff3300;
      background-color: rgba(255,255,255,0.8);
    }
    """
    c5 = '#0099ff'
    c6 = '#ff3300'

    fig = plt.figure(figsize=(5, 7))
    ax = fig.gca()
    try:  # plot comparison if requested
        points_ref = ax.plot(comparison_T,
                             comparison_z,
                             marker='o',
                             ms=5,
                             label=comparison_label,
                             c=c6)

        labels_ref = []
        for i in range(comparison_z.size):
            label_ref = '<h6>Height: {:.1f} km<br>{}: {:.1f} {}</h6>'.format(
                comparison_z[i], xlabel, comparison_T[i], xunits)
            labels_ref.append(label_ref)

        tooltip_ref = mpld3.plugins.PointHTMLTooltip(points_ref[0],
                                                     labels_ref,
                                                     voffset=10,
                                                     hoffset=10,
                                                     css=css)
        comparison = True
    except ValueError:
        comparison = False
        pass

    points = ax.plot(T, z, marker='o', ms=5, label='your run', c=c5)
    plt.xlabel(f'{xlabel} [{xunits}]')
    plt.ylabel('Height [km]')
    plt.ylim(0, np.max(z))
    leg = plt.legend()
    leg.get_frame().set_edgecolor('white')

    labels = []
    for i in range(z.size):
        label = '<h5>Height: {:.1f} km<br>{}: {:.1f} {}</h5>'.format(
            z[i], xlabel, T[i], xunits)
        labels.append(label)

    tooltip_user = mpld3.plugins.PointHTMLTooltip(points[0],
                                                  labels,
                                                  voffset=10,
                                                  hoffset=10,
                                                  css=css)

    if comparison:
        mpld3.plugins.connect(fig, tooltip_user, tooltip_ref)
    else:
        mpld3.plugins.connect(fig, tooltip_user)

    return mpld3.fig_to_html(fig)
Esempio n. 53
0
import os
import matplotlib.pyplot as plt
import mpld3
from util import update_html_string

fig = plt.figure()
plt.plot([1, 2, 3])
html_fig = mpld3.fig_to_html(fig)

# look for a div with the following id
html_target_id = os.path.basename(__file__)
print(html_target_id)

with open(
        r'C:\Users\Ben Smus\Learning_Programming\Web\bensmus.github.io\index.html'
) as f:
    html_existing = f.read()

with open(
        r'C:\Users\Ben Smus\Learning_Programming\Web\bensmus.github.io\index.html',
        'w') as f:
    html_new = update_html_string(html_existing, html_fig, html_target_id)
    f.write(html_new)
    titleVar = 'NVIDIA Corporation'
    stk = "NVDA"
else:
    print('Error! Error! Errrooorrrr!')

labels = []
stock.to_csv('stock.csv')

fig1 = plt.figure(1)
plt.plot(stock["Adj Close"])  # Plot the adjusted closing price of stock
plt.grid()
fnt2 = plt.title("Last 1 year prices of " + titleVar)  # Title for plot
fnt2.set_size(18)
plt.xlabel('Months')
plt.ylabel('Stock price ' + titleVar)
mpld3.fig_to_html(fig1)
mpld3.save_html(fig1, "plot.html")

fig2 = plt.figure(2)
start = datetime.datetime.now().date() + datetime.timedelta(-31)
stock_val = pd.read_csv('stock.csv')
new_stock_val = stock_val[['Date', 'Close']]
new_stock_val = stock_val[pd.to_datetime(stock_val['Date']) > pd.to_datetime(
    start)]  ## Select values only upto previous month. This was hard :(
new_stock_val.to_csv('stock2.csv')
x = new_stock_val['Date']
y = new_stock_val['Close']
plt.grid()
plt.plot_date(x, y, fmt="r-", marker='o', color='b')  ## show blue dots
fnt1 = plt.title("Last 1 month stock value of " + titleVar)
fnt1.set_size(18)