def plot_xy(self,
             df_to_plot,
             x_index,
             y_index,
             x_label,
             y_label,
             title,
             dest_path,
             median_filter=0):
     time = df_to_plot[x_index]
     temperature = df_to_plot[y_index]
     if median_filter > 1:
         #temperature = pd.rolling_median(temperature, median_filter)
         temperature = temperature.rolling(window=median_filter,
                                           center=False).median()
         time = time.rolling(window=median_filter, center=False).median()
         #filtered_data = self.median_filter(temperature, median_filter, time)
         #temperature = filtered_data[0]
         #time = filtered_data[1]
     plt.xlabel(x_label)
     plt.ylabel(y_label)
     fig = plt.figure(figsize=(10, 7))
     #fig.suptitle('Titel', fontsize=25)
     ax = fig.gca()
     ax.plot(self.timeListToFloatList(time), temperature)
     ax.set_title(title, fontsize=25)
     ax.set_xlabel(x_label, fontsize=18)
     ax.set_ylabel(y_label, fontsize=18)
     #mpld3.show(fig)
     mpld3.save_html(fig, dest_path)
     print('DONE')
Example #2
0
    def graphme(self, pngfilename="my_sample_png.png"):

        import numpy as np
        import matplotlib.pyplot as plt
        import matplotlib.dates as mdates
        import mpld3
        import datetime

        """ creating background info"""
        # create a plot with as may subplots as you choose
        fig, ax = plt.subplots()
        # add a grid to the background
        ax.grid(True, alpha = 0.2)
        # the x axis contains date
        fig.autofmt_xdate()
        # the dates are year, month
        ax.fmt_xdata = mdates.DateFormatter('%Y-%m')

        if self.table not in ['MS04314', 'MS00114', 'MS04334','MS04315','MS00115']:
            final_glitch = self.decide()

            dates = sorted(final_glitch.keys())
            dates2 = [x for x in dates if final_glitch[x]['mean'] != None and final_glitch[x]['mean'] != "None"]
            vals = [final_glitch[x]['mean'] for x in dates2]
            glitched_values = ax.plot(dates2, vals, 'b-')
            ax.legend(loc=4)
            ax.set_xlabel("dates")
            ax.set_ylabel("values")
            mpld3.show()
            mpld3.save_html(fig, 'my_output_html.html')
            import pylab
            pylab.savefig(pngfilename)
Example #3
0
def graph():
    data1 = pandas.read_csv(lambdaOutModeName, header=None)
    data2 = pandas.read_csv(dockerOutModeName, header=None)
    # print(data1)
    x = data1[data1.columns[0]].replace(',', '.', regex=True).astype(float)
    y = data1[data1.columns[1]].replace(',', '.', regex=True).astype(float)

    x2 = data2[data2.columns[0]].replace(',', '.', regex=True).astype(float)
    y2 = data2[data2.columns[1]].replace(',', '.', regex=True).astype(float)

    fig, axes = plt.subplots(1, 2, figsize=(20, 8))

    axes[0].set_xlabel('memory')
    axes[0].set_ylabel('pcpu')
    axes[0].set_title('Lambda')
    axes[0].plot(list(x), list(y), '-bo')
    axes[0].axis([0, 3050, 0, 210])

    axes[1].set_xlabel('cpu')
    axes[1].set_ylabel('pcpu')
    axes[1].set_title('Docker')
    axes[1].plot(list(x2), list(y2), '-ro')
    axes[1].axis([0, 2.5, 0, 210])

    mpld3.save_html(fig, 'figure1.html')
    print('figure1.html created!')
Example #4
0
def plot_multi_axis(time, axes, labels=None, fname=None):
    """Plots a 3-axis set of data, with the three axes and an envelope.

    Args:
        time (array): time series to plot data against in seconds
        axes (list): the three axes to plot, pre-scaled to desired units
        labels (list): title, xlabel, ylabel, legend labels
        fname (str): name of the file to store the mpld3 html in
    """
    fig = plt.figure(figsize=(10, 6))
    if labels:
        for i in range(len(axes)):
            plt.plot(time, axes[i], label=labels[3][i])
        plt.plot(time, vecnorm(*axes), 'k', label=labels[3][len(axes)])
        plt.title(labels[0])
        plt.xlabel(labels[1])
        plt.ylabel(labels[2])
        plt.legend(loc='upper left')
    else:
        for i in range(len(axes)):
            plt.plot(time, axes[i])
        plt.plot(time, vecnorm(axes), 'k')
    if fname:
        with open(fname, 'w') as f:
            mpld3.save_html(fig, f)
Example #5
0
def drawMultiplePlot(data, dataSumNE, xlab='Jahr', filename='figboth', recolor=False):
    objects = data.loc['Elektrisch',]
    y_pos = np.arange(len(objects))
    fig = plt.figure(figsize=[16, 9], dpi=250)
    p1 = plt.bar(y_pos, height=objects)
    p2 = plt.bar(y_pos, height=dataSumNE, bottom=objects)
    if recolor:
        p1[(len(p1) - 1)].set_color('C3')
        p2[(len(p2) - 1)].set_color('#FFC080')
    plt.xticks(y_pos, data)
    plt.ylabel('Zulassungen')
    plt.xlabel(xlab)
    plt.legend(['Elektroautos', 'Andere Antriebe'])
    plt.savefig('outputs/png/' + genDate() + '_' + filename + '_legend.png')
    plt.close(fig)
    fig2 = plt.figure()
    p1 = plt.bar(y_pos, height=objects)
    p2 = plt.bar(y_pos, height=dataSumNE, bottom=objects)
    if recolor:
        p1[(len(p1) - 1)].set_color('C3')
        p2[(len(p2) - 1)].set_color('#FFC080')
    plt.xticks(y_pos, data)
    plt.ylabel('Zulassungen')
    plt.xlabel(xlab)
    plt.legend(['Elektroautos', 'Andere Antriebe'])
    mpld3.save_html(fig, 'outputs/mpld3/' + genDate() + '_' + filename + '_code.html')
Example #6
0
def graph():
    total_tomatoes = []
    total_tomatoes_count = 0

    db = SQL("sqlite:///pomodoro.db")
    user_daily_history = db.execute(
        "SELECT * FROM daily_history WHERE table_id=:table_id",
        table_id=int(decrypt_message(request.cookies.get("table_id"))),
    )

    for row in user_daily_history:
        total_tomatoes_count += row["tomato_count"]
        total_tomatoes.append(total_tomatoes_count)

    fig = figure()
    ax = fig.gca()
    ax.plot(total_tomatoes)

    # if os.path.exists("./templates/myfig.html"):
    #     os.remove("./templates/myfig.html")

    mpld3.save_html(
        fig,
        "./templates/myfig{}.html".format(
            decrypt_message(request.cookies.get("table_id"))
        ),
    )
    return render_template(
        "myfig{}.html".format(decrypt_message(request.cookies.get("table_id")))
    )
Example #7
0
def drawRelativePlot(data, dataSumNE, xlab='Jahr', filename='figboth', recolor=False):
    objects = data
    y_pos = np.arange(len(objects))
    fig = plt.figure(figsize=[16, 9], dpi=72)
    p1 = plt.bar(y_pos, height=objects)
    p2 = plt.bar(y_pos, height=dataSumNE, bottom=objects)
    if recolor:
        p1[(len(p1) - 1)].set_color('C3')
        p2[(len(p2) - 1)].set_color('#FFC080')
    plt.xticks(ticks=y_pos, labels=dataSumNE.index)
    plt.ylabel('Anteile Gesamte Inverkehrssetzungen')
    plt.xlabel(xlab)
    plt.legend(['Elektroautos', 'Andere Antriebe'])
    plt.savefig('outputs/png/' + genDate() + '_' + filename + '_legend.png')
    plt.close(fig)
    fig2 = plt.figure()
    p1 = plt.bar(y_pos, height=objects)
    p2 = plt.bar(y_pos, height=dataSumNE, bottom=objects)
    if recolor:
        p1[(len(p1) - 1)].set_color('C3')
        p2[(len(p2) - 1)].set_color('#FFC080')
    plt.xticks(ticks=y_pos, labels=dataSumNE.index)
    plt.ylabel('Anteile Gesamte Inverkehrssetzungen')
    plt.xlabel(xlab)
    plt.legend(['Elektroautos', 'Andere Antriebe'])
    mpld3.save_html(fig2, 'outputs/mpld3/' + genDate() + '_' + filename + '_code.html')
Example #8
0
def save_fig(fig, title, plot_dict, pic_path):
    output_file = plot_dict.get('output_file', '')
    if output_file is not '':
        # We do not want to have to remember whether the ending has to be supplied
        if output_file.endswith('.pdf'):
            out_file = output_file
        else:
            out_file = output_file + '.pdf'
    else:
        out_file = title + '.pdf'
    print 'Writing to output:', out_file.replace('.pdf', '')
    out_path = os.path.join(pic_path, out_file).replace(' ', '_')
    strip_chars = ['(', ')', ',', ';']
    for sc in strip_chars:
        out_path = out_path.replace(sc, '')
    fig.savefig(out_path, dpi=fig.dpi)
    out_path = out_path.replace('.pdf', '.svg')
    fig.savefig(out_path, dpi=fig.dpi)
    if mpld3 is not None:
        out_path = out_path.replace('.svg', '.html')
        with open(out_path, 'w') as fileobj:
            mpld3.save_html(fig,
                            fileobj,
                            template_type='simple',
                            mpld3_url='../mpld3.v0.2.js',
                            d3_url='../d3.v3.min.js')
    plt.close(fig)
def main():
    G = nx.Graph()
    N = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    E = [(1, 2), (1, 8), (2, 3), (2, 4), (4, 5), (6, 7), (6, 8), (8, 9),
         (8, 10), (4, 9), (2, 5), (3, 7)]
    G.add_nodes_from(N)
    G.add_edges_from(E)

    plt.title("Title")
    plt.axis('equal')  #両軸を同じスケールに
    plt.gcf().set_facecolor('w')

    pos = nx.spring_layout(G)  #描画位置はここで確定,全ノードの重みを1にするので重みがかかるのは引力計算のみ
    nx.draw_networkx(G, pos=pos, node_color='w')

    #ax=plt.gca()
    #zoom_factory(ax,base_scale=2.)
    #plt.show()

    fig = plt.gcf()
    print mpld3.plugins.get_plugins(fig)
    mpld3.plugins.clear(fig)
    zoom_plugin = mpld3.plugins.Zoom(button=False, enabled=True)
    print mpld3.plugins.get_plugins(fig)
    mpld3.plugins.connect(fig, zoom_plugin)
    mpld3.save_html(fig, "test.html")
    mpld3.show(template_type="general", no_extras=True)  # showする!
Example #10
0
def plot_html(coordinates, labels=None, ids=None, title="plot",
              discrete=False, output="output", pointsize=1.0, alpha=0.3,
              cname="Spectral_r"):
    if labels is None:
        colvec = "black"
    elif discrete:
        uniqClasses, label_numeric = np.unique(labels, return_inverse=True)
        colvec = np.squeeze(label_numeric)
    elif not discrete:
        label_numeric = labels
        colvec = np.squeeze(label_numeric)
    fig, ax = plt.subplots(subplot_kw=dict(facecolor='#EEEEEE'))
    ax.grid(color='white', linestyle='solid')
    ax.set_title(title, size=30)
    pointsidx = ['point {0}'.format(i + 1)
                 for i in range(coordinates.shape[0])]
    scatter = ax.scatter(coordinates[:, 0].tolist(),
                         coordinates[:, 1].tolist(),
                         c=colvec, s=20*pointsize, alpha=alpha,
                         cmap=plt.get_cmap(cname),
                         edgecolor='black')
    if ids is not None and labels is not None:
        tooltipstr = ["%s: label=%s" % t for t in zip(ids, labels)]
    elif ids is not None:
        tooltipstr = ["%s: id=%s" % t for t in zip(pointsidx, ids)]
    elif labels is not None:
        tooltipstr = ["%s: label=%s" % t for t in zip(pointsidx, labels)]
    else:
        tooltipstr = list(pointsidx)
    tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=tooltipstr)
    mpld3.plugins.connect(fig, tooltip)
    mpld3.save_html(fig, output+".html")
    plt.close(fig)
    print("\nWrote html plot to disk: %s\n" % (output+".html"))
Example #11
0
    def visualize(X, contexts, file):
        import matplotlib.pyplot
        import mpld3

        fig, ax = matplotlib.pyplot.subplots()

        ax.grid(True, alpha=0.3)

        points = ax.plot(X[:, 0], X[:, 1], 'o', color='b',
                         mec='k', ms=5, mew=1, alpha=.6)

        ax.set_xlabel('x')
        ax.set_ylabel('y')
        ax.set_title('Hover mouse to reveal context', size=20)

        tooltip = mpld3.plugins.PointHTMLTooltip(
            points[0],
            contexts,
            voffset=10,
            hoffset=10
        )

        mpld3.plugins.connect(fig, tooltip)

        mpld3.save_html(fig, file)
Example #12
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)
Example #13
0
def main(args):
    # Import data
    logger.info(u'Importing data with following parameters: \n\tWide: {0}\n\tDesign: {1}\n\tUnique ID: {2}\n\tGroup Column: {3}'.format(args.fname, args.dname, args.uniqID, args.group))
    dat = wideToDesign(args.fname, args.dname, args.uniqID, args.group)
    dat.wide.convert_objects(convert_numeric=True)

    fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(20, 20))
    plt.subplots_adjust(hspace=0.3)

    # If there is group information, color by group.
    if hasattr(dat, 'group'):
        logger.info('Plotting sample distributions by group')
        legend1 = pltByTrt(dat, ax1)
    else:
        logger.info('Plotting sample distributions')
        pltBySample(dat, ax1)

    # Create Legend
    handles, labels = ax1.get_legend_handles_labels()
    ax1.legend(handles, labels, ncol=5, loc='upper right', fontsize=10)

    # Create second legend if there is group information
    if hasattr(dat, 'group'):
        ax1.add_artist(legend1)

    # Plot boxplot of samples
    pltBoxplot(dat, ax2)

    plt.savefig(args.ofig, format='pdf')
    mpld3.save_html(fig, args.ofig2, template_type='simple')
Example #14
0
def save(fig, filename, kw):
    """
    Save the figure

    Args:
        fig (mpl.Figure): current figure
        filename (str): output filename
        kw (dict): kwargs dict

    """
    # is this deprecated??
    st()
    try:

        if kw['save_ext'] == 'html':
            import mpld3
            mpld3.save_html(fig, filename)

        else:
            fig.savefig(filename)

        if kw['show']:
            os.startfile(filename)

    except:
        if kwargs.get('show_filename', False):
            print(filename)
        raise NameError('%s is not a valid filename!' % filename)
Example #15
0
 def export_fmt(self, filename, size, sizeofsizes, format):
     if sizeofsizes == 1:
         size = 'none'
     if format is 'png':
         add = '.png'
     elif format is 'pgf':
         add = '.pgf'
     elif format is 'pdf':
         add = '.pdf'
     elif format is 'html':
         add = '.html'
     elif format is 'svg':
         # save as pdf, then pdf2svg
         self.fig.savefig(filename + self.sizestring[size] + '.pdf',
                     bbox_extra_artists=self.artists, bbox_inches='tight',
                     transparent=True)
         os.system('pdf2svg ' + filename + self.sizestring[size] + '.pdf ' +
                   filename + self.sizestring[size] + '.svg')
         os.remove(filename + self.sizestring[size] + '.pdf')
     elif format is 'websvg':
         add = 'web.svg'
     if (format is not 'svg') and (format is not 'html'):
         self.fig.savefig(filename + self.sizestring[size] + add,
                     bbox_extra_artists=self.artists, bbox_inches='tight',
                     transparent=True)
     if format is 'html':
         add = '.html'
         mpld3.save_html(self.fig, filename + add)
         self.add_math_jax(filename + add)
     if format is 'pgf':
         self.remove_font_sizes(filename + self.sizestring[size] + add)
Example #16
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)
Example #17
0
def save_abstractness_x_instability_scatter(x_values, x_label, y_values, y_label, ball_values, ball_label, color_values, color_label, annotations, filename_prefix, scope_name, show_diagonal=True):
    #plt.figure()  # new one, or they will be mixed
    fig, ax = plt.subplots()
    ax.set_xticks([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]) # http://stackoverflow.com/questions/8209568/how-do-i-draw-a-grid-onto-a-plot-in-python
    ax.set_yticks([0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
    plt.grid()
    if show_diagonal:
        plt.plot([0.0, 1.0], [1.0, 0.0], 'k-', ls="--", lw=2, alpha=0.5, color='green') # http://matplotlib.org/api/lines_api.html

        plt.plot([0.0, 0.7], [0.7, 0.0], 'k-', ls="--", lw=1, alpha=0.7, color='orange')  # http://matplotlib.org/api/lines_api.html
        plt.plot([0.3, 1.0], [1.0, 0.3], 'k-', ls="--", lw=1, alpha=0.7, color='orange')  # http://matplotlib.org/api/lines_api.html

        plt.plot([0.0, 0.4], [0.4, 0.0], 'k-', ls="--", lw=1, alpha=0.9, color='red')  # http://matplotlib.org/api/lines_api.html
        plt.plot([0.6, 1.0], [1.0, 0.6], 'k-', ls="--", lw=1, alpha=0.9, color='red')  # http://matplotlib.org/api/lines_api.html

    scatter = ax.scatter(x_values, y_values, ball_values, alpha=0.5, c=color_values)
    plt.xlabel(x_label)
    plt.ylabel(y_label)
    plt.title("%i %s items. Circles: %s & %s" % (len(x_values), scope_name, ball_label, color_label))
    tooltip = mpld3.plugins.PointHTMLTooltip(scatter, labels=annotations, hoffset=10, voffset=-25)
    mpld3.plugins.connect(fig, tooltip)
    mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fmt=".2f"))
    mpld3.plugins.connect(fig, ClickSendToBack(scatter))
    filename = "%s-scatter-%s-%s_%s_%s.html" % (filename_prefix, scope_name, x_label, y_label, ball_label)
    mpld3.save_html(fig, filename)
    return filename
Example #18
0
def italy_graph():

    #get total cases
    affected = [dic.get('totale_casi') for dic in italy_list]

    #creating x and y coordinates
    x = [i for i in range(0, len(italy_list))]
    y = [int(num) for num in affected]

    #creating the figure with matplotlib
    fig = Figure(figsize=(10, 4))

    #divinding into subplots
    ax = fig.subplots()

    #setting x label
    ax.set_xticks(x)

    ax.set_xticklabels(ticks, rotation=45)

    #plotting diagram
    ax.plot(x, y, color='magenta')

    #setting labels
    ax.set_ylabel('affected people', size=20)
    ax.set_title('People discovered to have virus', size=40)

    #save html with library function
    mpld3.save_html(fig, 'dynamic_html_files/graph.html')
Example #19
0
def turn_scatter_into_interactive(fig,
                                  scatter_plot,
                                  df,
                                  file_name,
                                  figsize=False):
    from jinja2 import Template, Environment, FileSystemLoader
    from mpld3 import plugins
    # file_path = './assets/interactive_plots/'+file_name
    file_path = './' + file_name
    env = Environment(loader=FileSystemLoader('./views'))
    movie_template = env.get_template('movie.jinjia')
    movies = df.to_dict(orient='records')
    movie_cards = [
        movie_template.render(movie=movie, show_ratings_num=True)
        for movie in movies
    ]
    if figsize:
        fig.set_size_inches(figsize)
    else:
        fig.set_size_inches(8.5, 8.5)
    plugins.connect(
        fig,
        plugins.PointHTMLTooltip(scatter_plot,
                                 movie_cards,
                                 css=load_css(raw=True)))
    mpld3.save_html(fig, file_path)
    button = '''<a class='btn btn-default' style="text-decoration: none;;"
    href="{0}" target='_blank'>
    Interactive Scatter Plot</a>
    '''.format(file_path)
    return HTML(button)
Example #20
0
def save_residuals_plot(obs, save_path, data_name):
    import matplotlib.pyplot as plt
    import mpld3  # pylint: disable=import-error
    fig = plt.figure(figsize=(10, 20))
    fig.suptitle('Residuals of ' + data_name, fontsize=24)
    n = len(list(obs.keys()))
    start_times = [obs[kind]['t'][0] for kind in obs]
    start_time = min(start_times)
    xlims = [start_time + 3, start_time + 60]

    for i, kind in enumerate(obs):
        ax = fig.add_subplot(n, 1, i + 1)
        ax.set_xlim(xlims)
        t = obs[kind]['t']
        res = obs[kind]['residual']
        start_idx = bisect(t, xlims[0])
        if len(res) == start_idx:
            continue
        ylim = max(np.linalg.norm(res[start_idx:], axis=1))
        ax.set_ylim([-ylim, ylim])
        if int(kind) in SAT_OBS:
            svIds = obs[kind]['svIds']
            for svId in set(svIds):
                svId_idx = (svIds == svId)
                t = obs[kind]['t'][svId_idx]
                res = obs[kind]['residual'][svId_idx]
                ax.plot(t, res, label='SV ' + str(int(svId)))
                ax.legend(loc='right')
        else:
            ax.plot(t, res)
        plt.title('Residual of kind ' + ObservationKind.to_string(int(kind)),
                  fontsize=20)
    plt.tight_layout()
    os.makedirs(save_path)
    mpld3.save_html(fig, save_path + 'residuals_plot.html')
Example #21
0
def as_html(pd_data: pd.DataFrame, output_file_dir: str) -> None:
    """ Create HTML page with graphics. """
    fig = plt.figure()
    ax = fig.subplots()
    ax.plot(pd_data)
    file_obj = os.path.join(output_file_dir, "plots.html")
    mpld3.save_html(fig, file_obj)
Example #22
0
def plot_by_mpld3(dataframe,figsize=(12,6),marker='o',grid=True,
                           alpha_ax=0.3,alpha_plot=0.4,alpha_unsel=0.3,alpha_over=1.5,
                           title=None,xlabel=None,ylabel=None,mode="display",file=None):
    ## DataFrame 데이터를 이용하여 웹용 D3 chart 스크립트 생성
    # plot line + confidence interval
    fig, ax = plt.subplots(figsize=figsize)
    ax.grid(grid, alpha=alpha_ax)

    for key, val in dataframe.iteritems():
        l, = ax.plot(val.index, val.values, label=key, marker=marker)
        ax.plot(val.index,val.values, color=l.get_color(), alpha=alpha_plot)
    # define interactive legend
    handles, labels = ax.get_legend_handles_labels() # return lines and labels
    interactive_legend = plugins.InteractiveLegendPlugin(handles,labels,
                                                         alpha_unsel=alpha_unsel,
                                                         alpha_over=alpha_over, 
                                                         start_visible=True)
    plugins.connect(fig, interactive_legend)

    if xlabel:
        ax.set_xlabel(xlabel)
    if ylabel:
        ax.set_ylabel(ylabel)
    if title:
        ax.set_title(title, size=len(title)+5)

    ## mode
    if mode == 'html': # return html script
        return mpld3.fig_to_html(fig)
    elif mode == 'save' and file: # save file
        mpld3.save_html(fig,file)
    else: # display chart
        #mpld3.enable_notebook()
        return mpld3.display()
Example #23
0
def test_interactive_linearPP(save_to_html=False):
    ## Define left and right state
    ql = np.array([-2.0, 2.0])
    qr = np.array([0.0, -3.0])
    # Define two eigenvectors and eigenvalues (acoustics)
    zz = 2.0
    rho0 = 1.0
    r1 = np.array([zz, 1.0])
    r2 = np.array([-zz, 1.0])
    lam1 = zz / rho0
    lam2 = -zz / rho0
    plotopts = {
        'q1min': -5,
        'q1max': 5,
        'q2min': -5,
        'q2max': 5,
        'domain': 5,
        'time': 1,
        'title1': "Pressure",
        'title2': "Velocity"
    }
    pt = linear_phase_plane(ql, qr, r1, r2, lam1, lam2, **plotopts)
    if save_to_html:
        mpld3.save_html(pt, "test_linearPP.html")
    mpld3.show()
Example #24
0
    def to_html(self, filename, node_formatter=_node_to_label_IC, **kwargs):
        """Draw an interactive lattice using :class:`clustering.Node`'s drawing function and mpld3.

        Arguments:
            filename (str): filename of the exported html page.
            node_formatter (func): custom function to format nodes
        """
        css = _load_external_text("table.css")
        fig, lines, ordered_nodes = self._draw_one(
            self.nodes,
            figsize=(20, 9),
            n=4,
            scale=False,
            point={"s": 50},  # TODO: Something wrong here
            interactive=True,
            **kwargs)

        paths = list(
            filter(
                lambda obj: type(obj) is matplotlib.collections.PathCollection,
                fig.axes[0].get_children(),
            ))
        lines = list(
            filter(
                lambda obj: type(obj) is matplotlib.lines.Line2D and len(
                    obj.get_xdata(orig=True)) > 1, fig.axes[0].get_children()))
        points_ids = []
        corrd_to_points = {}

        for p, v in zip(paths, ordered_nodes):
            x, y = v.attributes["_x"], v.attributes["_y"]
            p_id = mpld3.utils.get_id(p)  # ,"pts")
            corrd_to_points[(x, y)] = p_id
            label = node_formatter(v, comp=self.comp)
            points_ids.append(p_id)
            tooltip = mpld3.plugins.PointHTMLTooltip(p, [label], css=css)
            mpld3.plugins.connect(fig, tooltip)

        point_to_artists = defaultdict(set)
        lines = sorted(lines, key=lambda l: min(l.get_ydata()))
        for l in lines:
            parent, child = zip(*l.get_data(orig=True))
            childp = corrd_to_points[tuple(child)]
            parentp = corrd_to_points[tuple(parent)]

            if 0 in child:
                point_to_artists[childp] = set()
            point_to_artists[parentp].add(childp)
            point_to_artists[parentp].add(mpld3.utils.get_id(l))
            point_to_artists[parentp].update(point_to_artists[childp])

        point_to_artists = {
            p: list(point_to_artists[p])
            for p in point_to_artists
        }

        # root = corrd_to_points[(node.attributes["_x"],node.attributes["_y"])]
        mpld3.plugins.connect(
            fig, _HighlightSubTrees(points_ids, dict(point_to_artists)))
        mpld3.save_html(fig, filename, template_type="simple")
Example #25
0
 def term_sheet_to_html(self,
                        window=30,
                        windows=[30, 60, 90, 120],
                        quantiles=[0.25, 0.75],
                        bins=100,
                        normed=True,
                        bench='SPY',
                        open=False):
     ext = '.html'
     cones_fig, cones_plt = self.cones(windows=windows, quantiles=quantiles)
     rolling_quantiles_fig, rolling_quantiles_plt = self.rolling_quantiles(
         window=window, quantiles=quantiles)
     rolling_extremes_fig, rolling_extremes_plt = self.rolling_extremes(
         window=window)
     rolling_descriptives_fig, rolling_descriptives_plt = self.rolling_descriptives(
         window=window)
     histogram_fig, histogram_plt = self.histogram(window=window,
                                                   bins=bins,
                                                   normed=normed)
     mpld3.save_html(cones_fig, self.define_fig("cones", ext))
     mpld3.save_html(rolling_quantiles_fig,
                     self.define_fig("rolling_quantiles", ext))
     mpld3.save_html(rolling_extremes_fig,
                     self.define_fig("rolling_extremes", ext))
     mpld3.save_html(rolling_descriptives_fig,
                     self.define_fig("rolling_desc", ext))
     mpld3.save_html(histogram_fig, self.define_fig("histogram", ext))
     pyplt.close(cones_fig)
     pyplt.close(rolling_quantiles_fig)
     pyplt.close(rolling_extremes_fig)
     pyplt.close(rolling_descriptives_fig)
     pyplt.close(histogram_fig)
Example #26
0
def generate_embedding(param_file, model_file, mnist_file, output_file=None,
                       param_key=None):
    predictor = optimus.load(model_file)
    params = biggie.Stash(param_file)
    param_key = sorted(params.keys())[-1] if param_key is None else param_key
    predictor.param_values = params.get(param_key)

    train, valid, test = datatools.load_mnist_npz(mnist_file)
    idx = np.random.permutation(len(valid[0]))[:2000]
    x_in = valid[0][idx]
    y_true = valid[1][idx]
    z_out = predictor(x_in=x_in)['z_out']

    imgfiles = [datatools.generate_imagename(i, y)
                for i, y in enumerate(idx, y_true)]

    labels = ['<img src="{}{}" width=100 height=100>'.format(URL_BASE, img)
              for img in imgfiles]

    palette = seaborn.color_palette("Set3", 10)
    colors = np.asarray([palette[y] for y in y_true])
    fig = plt.figure(figsize=(10, 10))
    ax = fig.gca()
    handle = ax.scatter(z_out.T[0], z_out.T[1],
                        c=colors, s=75, alpha=0.66)

    tooltip = plugins.PointHTMLTooltip(
        handle, labels,
        voffset=10, hoffset=10)

    plugins.connect(fig, tooltip)
    plt.show()
    if output_file:
        with open(output_file, 'w') as fp:
            mpld3.save_html(fig, fp)
Example #27
0
 def test_american_cities(self):
     fLOG(
         __file__,
         self._testMethodName,
         OutputPrint=__name__ == "__main__")
     if sys.version_info[:2] <= (3, 4):
         warnings.warn(
             "Issue with Python 3.4, bug probably related to wrong pointers")
         return
     fix_tkinter_issues_virtualenv()
     import matplotlib.pyplot as plt
     import mpld3
     filter = {"NewYork": "NY", "Chicago": "CH",
               "SanFrancisco": "SF", "Seattle": "Sea"}
     temp = get_temp_folder(__file__, "temp_american_cities")
     data = os.path.join(temp, "..", "data", "american_cities.txt")
     df = pandas.read_csv(data)
     df["Longitude"] = -df["Longitude"]
     df["City"] = df["City"].apply(lambda v: filter.get(v, ""))
     fig, ax = plt.subplots(figsize=(32, 32))
     df = df[df.Latitude < 52]
     df = df[df.Longitude > -130].copy()
     ax = graph_cities(df, ax=ax, markersize=3, fontcolor=(0, 1.0, 0), fontsize='40',
                       fontname="Courrier", fontweight="bold")
     assert ax is not None
     img = os.path.join(temp, "img.png")
     fig.savefig(img)
     assert os.path.exists(img)
     name2 = os.path.join(temp, "picture.html")
     mpld3.save_html(fig, name2)
     assert os.path.exists(name2)
     if __name__ == "__main__":
         fig.show()
     plt.close('all')
     fLOG("end")
Example #28
0
def draw_scatter(input_file_name,
                 output_file_name,
                 axis1_col="axis1",
                 axis2_col="axis2",
                 group_col="group",
                 title_text="Scatter plot",
                 title_size=20,
                 point_size=100,
                 point_alpha=0.3,
                 grid_color="white",
                 grid_style="solid",
                 backgroud_color="#EEEEEE",
                 sep="\t"):
    """
    Draw scatter plot from mothur's axes file and save figure to file.

    Parameters
    -------
    input_file_name: str
        Input file name.
    output_file_name: str
        Output file name.
    axis1_col: str, default <axis1>
        Axis 1 column name in axis file.
    axis2_col: str, default <axis2>
        Axis 2 column name in axis file.
    group_col: str, default <group>
        Group column name in axis file.
    title_text: str, default <Scatter plot>
        Text displayed as plot title.
    title_size: int, default <20>
        Plot title size.
    point_size: int, default <100>
        Point size.
    point_alpha: float, default <0.3>
        Point transparency.
    grid_color: str, default <white>
        Color of the plot grid.
    grid_style: str, default <solid>
        Style of the plot grid.
    backgroud_color: str, default <#EEEEEE>
        Color of the plot backgroud.
    sep: str, default <\t>
        Delimiter to use for reading-in axes file.
    """
    df = read_csv(input_file_name,
                  sep=sep)
    fig, ax = plt.subplots()
    scatter = ax.scatter(np.array(df[axis1_col]),
                         np.array(df[axis2_col]),
                         c=np.random.random(size=len(df)),
                         s=100,
                         alpha=0.3,
                         cmap=plt.cm.jet)
    ax.grid(color=grid_color, linestyle=grid_style)
    ax.set_title(title_text, size=title_size)
    labels = list(df[group_col])
    tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
    mpld3.plugins.connect(fig, tooltip)
    mpld3.save_html(fig, output_file_name)
Example #29
0
def scatter_interactive(fig, scatter_plot, df, file_name, figsize=False):
    from jinja2 import Environment, FileSystemLoader
    from mpld3 import plugins
    from helpers.application_helper import PointClickableHTMLTooltip2
    # file_path = './assets/interactive_plots/'+file_name
    file_path = file_name
    env = Environment(loader=FileSystemLoader('./views'))
    restaurants = df.to_dict(orient='records')
    restaurant_template = env.get_template('restaurant.jinja')
    cards = [
        restaurant_template.render(restaurant=restaurant)
        for restaurant in restaurants
    ]
    if figsize:
        fig.set_size_inches(figsize)
    else:
        fig.set_size_inches(8.5, 8.5)
    plugins.connect(
        fig,
        PointClickableHTMLTooltip2(scatter_plot,
                                   labels=cards,
                                   targets=df['url'].tolist(),
                                   css=load_css(raw=True)))
    mpld3.save_html(fig, file_path)
    button = '''<a class='btn btn-default' style="text-decoration: none;;"
    href="{0}" target='_blank'>
    Interactive Scatter Plot</a>
    '''.format(file_path)
    return HTML(button)
Example #30
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)
Example #31
0
    def update_sentiment_plot(self):
        fig, ax = plt.subplots(figsize=(9, 4))
        #plt.xticks(range(len(self.df["Positivity"])),ticks,rotation=45)
        #plt.xticks(range(len(ticks)),ticks,rotation=45)
        plt.setp(ax.get_xticklabels(), visible=False)
        plt.plot(self.df['Negativity'], color="red", label='Negativity')
        plt.plot(self.df['Positivity'], color="green", label='Positivity')
        plt.legend(loc='upper center')
        plt.locator_params(nbins=10)
        plt.title('Sentiment analysis of the articles')
        plt.ylabel('Strength of the feeling')
        # fig.canvas.draw()
        # ax.set_xlim([0, len(self.df['Negativity'])])
        #
        # ax.set_xticks(range(len(self.df['Negativity'])))
        # ax.set_xticklabels(self.labels, rotation=45)
        # plt.plot(self.df['Negativity'], color="red")
        # plt.plot(self.df['Positivity'], color="green")
        #
        # plt.legend(loc='upper center')
        # plt.locator_params(nbins=10)
        # plt.title('Sentiment analysis of the articles')
        # plt.ylabel('Strength of the feeling')

        mpld3.save_html(fig, 'templates/' + self.prefix + 'sentiment.html')
Example #32
0
def html_scatter(fig, ax, h, x_col, y_col, title, labels='dflt', outpath='dflt'):
    outpath = gt.check_dfltarg(outpath, gt.dflt_outpath('foo'))
    labels = gt.check_dfltarg(labels, h.label)
    scatter = ax.scatter(h[x_col].tolist(), h[y_col].tolist(), alpha=0.001)
    tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
    mpld3.plugins.connect(fig, tooltip)
    myoutpath = os.path.join(outpath, title + '.html')
    mpld3.save_html(fig, myoutpath)
Example #33
0
    def render_mpl(self, fig, path, **kwargs):
        import mpld3
        kwargs.pop('dpi')

        mpld3.save_html(
            fig,
            fileobj=path,
            **kwargs)
Example #34
0
    def allTrend(self):
        self.loadAlldata()
        fig = plt.figure(figsize=(17, 13))  # 8 inches wide by 6 inches tall
        ax = fig.add_subplot(2, 2, 1)
        ax.set_ylabel('hours')
        ax.set_xlabel('days in a month')
        ax.set_ylim(0, 12)
        ax.set_title('time trend for different activity')
        labels = [
            "light activity", "fairly activity", "very activity",
            'very not productive', 'not productive', 'neutural', 'productive',
            'very productive', 'awake', 'REM', 'light sleep', 'deep sleep'
        ]

        colors = [
            'lime', 'green', 'darkgreen', 'gray', 'brown', 'blue', 'darkblue',
            'navy', 'coral', 'violet', 'plum', 'purple'
        ]
        line_collections = []
        activityvalue = np.array(list(self.activity.values()))
        for i in range(3):
            y1 = activityvalue[:, i] / 60
            x1 = np.array(range(
                len(y1)))  # light activity, fairly activity, very activity
            l1 = ax.plot(x1, y1, lw=3, alpha=0.4, c=colors[i], label=labels[i])
            line_collections.append(l1)

        provalues = np.array(list(self.desk.values()))
        for i in range(5):
            y1 = provalues[:, i]
            x1 = np.array(range(len(y1)))  #
            l1 = ax.plot(x1,
                         y1,
                         lw=3,
                         alpha=0.4,
                         c=colors[3 + i],
                         label=labels[3 + i])
            line_collections.append(l1)

        sleepvalues = np.array(list(self.sleep.values()))

        for i in range(4):
            y1 = sleepvalues[:, i] / 60
            x1 = np.array(range(len(y1)))  #
            l1 = ax.plot(x1,
                         y1,
                         lw=4,
                         alpha=0.4,
                         c=colors[8 + i],
                         label=labels[8 + i])
            line_collections.append(l1)

        plugins.connect(
            fig, plugins.InteractiveLegendPlugin(line_collections, labels))

        # mpld3.show()
        mpld3.save_html(fig, 'trend.html')
Example #35
0
def pcoa_plot_interactive(profile, output_dir, dist_type):
    """Generate interactive PCoA plot.
    
    Args:
        profile (metagenomic_profile): Profile instance containing data. 
        output_dir (str): path to directory to save output
        dist_type (str): distance metric to use in PCoA.
        
    Returns:
        Path to output file. 
    """
    try:
        import mpld3  # Provides interactive graphs
        import plugins  # Custom mpld3 plugins
    except ImportError:
        print(
            "Could not import mpld3. Please install mpld3 or set 'interactive_plots' option to 'false.'"
        )
        return

    __check_input(output_dir)

    df = __partition_abundance_data(profile)
    eig_pairs = __get_eig_pairs(df, dist_type)

    eig_pairs.sort()
    eig_pairs.reverse()

    PCo1 = eig_pairs[0][1]
    PCo2 = eig_pairs[1][1]

    # Begin plotting

    # Clear any current figures from the plot
    plt.clf()

    lgd_labels = __plot_markers_interactive(profile, PCo1, PCo2,
                                            a=0.7)  # Main plotting

    # Padding for x and y labels
    ax = plt.gca()
    ax.tick_params(axis='both', which='major', pad=15)

    plt.xlabel("PCo1", fontsize=16)
    plt.ylabel("PCo2", fontsize=16)

    fname = output_dir + "/" + "pcoa_" + dist_type + ".html"

    fig = plt.gcf()

    mpld3.plugins.connect(fig, plugins.TweakToolbar())
    mpld3.save_html(fig, fname)

    # Create legend
    lgd_fname = __create_legend(lgd_labels, output_dir)

    return fname, lgd_fname
Example #36
0
def plotdataPop(data, vals):
    """
    simple data popup, take in data and values, plots it as a mpld3 plot allowing for interaction, 
    add it to an Iframe what then can be used in a folium map popup    
    
    returns a popup
    
    
    Last edit: 25/05/2019
    -added exceptions with line number 
    """
    try:
        df = data[vals]
        # print(df)
        width = 300
        height = 320
        #If more than one varable add fadding
        alpha = 1
        if len(vals) > 1:
            alpha = 0.8
        #define figure
        fig, ax = plt.subplots(figsize=(4, 4))
        #plot data
        for val in vals:
            ax.plot(df[val], alpha=alpha, label=val)

        ax.legend()
        #Plot information
        ax.set_ylabel('Mass concentration (ug/m^3)')
        ax.set_xlabel('')  #solving issue with x labes being cut in 1/2
        #set tile based of time interval

        ax.set_title(df.index[0].strftime("%Y/%m/%d") + "-(" +
                     df.index[0].strftime("%H:%M") + " to " +
                     df.index[len(df) - 1].strftime("%H:%M"))
        ax.grid()  #grid

        #setppop up in mld3
        now = datetime.datetime.now()
        date = df.index[0].strftime("%Y%m%d")  #get the date
        #save=datetime.datetime.strptime(now,"%HH%MM%SS")
        figname = 'popup_plot' + date + str(now.microsecond) + '.html'
        mpld3.save_html(fig, 'Mapoutput//' + figname)
        time.sleep(0.1)
        a = mpld3.fig_to_html(fig)
        #put pop up in iframe format ready to be put into the popup
        iframe = IFrame(a, width=(width), height=(height))
        popup = folium.Popup(iframe, max_width=600)

        return popup
    except Exception as e:
        print("Error in GPS Data cirlce generation")
        print('Error on line {}'.format(sys.exc_info()[-1].tb_lineno))
        print(type(e))
        print(e.args)
        pass
Example #37
0
    def html_node(self, node, fileobj):
        '''  plots node and saves to html using mpld3 '''
        import mpld3

        n = node.getnumdata()
        self.subplts = [
            plot_single_data(node, i, n, self.log) for i in range(n)
        ]

        mpld3.save_html(pylab.gcf(), fileobj)
def plot_param(df, path, param, units):
    fig, ax = plt.subplots(figsize=(8,6))
    df[param].plot(marker='o', ax=ax, legend=None)
    try:
        plt.ylabel(units[df.columns.get_loc(param)], labelpad=10)
    except:
        pass
    plt.xlabel('')
    plt.title(param + ' (time in UTC)')
    mpld3.save_html(fig, '{o}{p}.html'.format(o=path, p=param))
    plt.close(fig)
Example #39
0
def pcoa_plot_interactive(profile, output_dir, dist_type):
    """Generate interactive PCoA plot.
    
    Args:
        profile (metagenomic_profile): Profile instance containing data. 
        output_dir (str): path to directory to save output
        dist_type (str): distance metric to use in PCoA.
        
    Returns:
        Path to output file. 
    """
    try:
        import mpld3 # Provides interactive graphs 
        import plugins # Custom mpld3 plugins    
    except ImportError:
        print("Could not import mpld3. Please install mpld3 or set 'interactive_plots' option to 'false.'")
        return
        
    __check_input(output_dir)
    
    df = __partition_abundance_data(profile)
    eig_pairs = __get_eig_pairs(df, dist_type)
    
    eig_pairs.sort()
    eig_pairs.reverse()
        
    PCo1 = eig_pairs[0][1]
    PCo2 = eig_pairs[1][1]
    
    # Begin plotting

    # Clear any current figures from the plot
    plt.clf()
    
    lgd_labels = __plot_markers_interactive(profile, PCo1, PCo2, a=0.7) # Main plotting 

    # Padding for x and y labels    
    ax = plt.gca()
    ax.tick_params(axis='both', which='major', pad=15)    
    
    plt.xlabel("PCo1", fontsize=16)
    plt.ylabel("PCo2", fontsize=16)
    
    fname = output_dir + "/" + "pcoa_" + dist_type + ".html"
    
    fig = plt.gcf()
    
    mpld3.plugins.connect(fig, plugins.TweakToolbar())
    mpld3.save_html(fig, fname)
    
    # Create legend 
    lgd_fname = __create_legend(lgd_labels, output_dir)
    
    return fname, lgd_fname
Example #40
0
def test_interactive_shallowPP(save_to_html=False):
    # Define left and right state (h,hu)
    ql = np.array([3.0, 5.0])
    qr = np.array([3.0, -5.0])
    # Define optional parameters (otherwise chooses default values)
    plotopts = {'g':1.0, 'time':2.0, 'tmax':5, 'hmax':10, 'humin':-15, 'humax':15}
    # Call interactive function (can be called without any argument)
    pt = shallow_water(ql,qr,**plotopts)
    if save_to_html:
        mpld3.save_html(pt, "test_shallow.html")
    mpld3.show()
def plot_observed_aimpoints(obs_aimpoints):
    """
    Make png and html (mpld3) plot of data in the ``obs_aimpoints`` table.
    """
    plt.close(1)
    fig = plt.figure(1, figsize=(8, 4))

    dates = DateTime(obs_aimpoints['mean_date'])
    years = dates.frac_year
    times = dates.secs
    ok = years > np.max(years) - float(opt.lookback) / 365.25
    obs_aimpoints = obs_aimpoints[ok]
    times = times[ok]

    lolims = {}
    uplims = {}
    for axis in ('dx', 'dy'):
        lolims[axis] = obs_aimpoints[axis] > 10
        uplims[axis] = obs_aimpoints[axis] < -10
        obs_aimpoints[axis] = obs_aimpoints[axis].clip(-10, 10)

    ok = ((np.abs(obs_aimpoints['target_offset_y']) < 100) &
          (np.abs(obs_aimpoints['target_offset_z']) < 100))
    plot_cxctime(times[ok], obs_aimpoints['dx'][ok], 'ob', label='CHIPX')
    plot_cxctime(times[ok], obs_aimpoints['dy'][ok], 'or', label='CHIPY')
    plot_cxctime(times[~ok], obs_aimpoints['dx'][~ok], '*b', label='CHIPX (offset > 100")')
    plot_cxctime(times[~ok], obs_aimpoints['dy'][~ok], '*r', label='CHIPY (offset > 100")')

    for axis in ('dx', 'dy'):
        if np.any(lolims[axis]):
            plt.errorbar(DateTime(times[lolims[axis]]).plotdate,
                         obs_aimpoints[axis][lolims[axis]], marker='.', yerr=1.5, lolims=True)
        if np.any(uplims[axis]):
            plt.errorbar(DateTime(times[uplims[axis]]).plotdate,
                         obs_aimpoints[axis][uplims[axis]], marker='.', yerr=1.5, uplims=True)

    plt.grid()
    ymax = max(12, np.max(np.abs(obs_aimpoints['dx'])), np.max(np.abs(obs_aimpoints['dy'])))
    plt.ylim(-ymax, ymax)
    plt.ylabel('Offset (arcsec)')
    plt.title('Observed aimpoint offsets')

    plt.legend(loc='upper left', fontsize='small', title='', framealpha=0.5)

    outroot = os.path.join(opt.data_root, 'observed_aimpoints')
    logger.info('Writing plot files {}.png,html'.format(outroot))
    mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fmt='.1f'))
    mpld3.save_html(fig, outroot + '.html')
    fig.patch.set_visible(False)
    plt.savefig(outroot + '.png', frameon=False)
Example #42
0
def plot_housing_temperature():
    dat = fetch.Msid('aach1t', '2000:001', stat='daily')
    plt.close(1)
    fig = plt.figure(figsize=(8, 4))
    year = Time(dat.times, format='cxcsec').decimalyear
    plt.plot(year, dat.vals)
    plt.grid()
    plt.xlabel('Year')
    plt.ylabel('Temperature (degF)')
    plt.title('Aspect Camera housing temperature trend')

    outroot = os.path.join(opt.data_root, 'aca_housing_temperature')
    logger.info('Writing plot files {}.png,html'.format(outroot))
    mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fmt='.1f'))
    mpld3.save_html(fig, outroot + '.html')
    fig.patch.set_visible(False)
    plt.savefig(outroot + '.png', frameon=False)
Example #43
0
def test_interactive_linearPP(save_to_html=False):
    ## Define left and right state 
    ql = np.array([-2.0, 2.0]) 
    qr = np.array([0.0, -3.0])
    # Define two eigenvectors and eigenvalues (acoustics)
    zz = 2.0
    rho0 = 1.0
    r1 = np.array([zz,1.0])
    r2 = np.array([-zz,1.0])
    lam1 = zz/rho0
    lam2 = -zz/rho0
    plotopts={'q1min':-5, 'q1max':5, 'q2min':-5, 'q2max':5, 'domain':5, 'time':1, 
            'title1':"Pressure", 'title2':"Velocity"}
    pt = linear_phase_plane(ql,qr,r1,r2,lam1,lam2,**plotopts)
    if save_to_html:
        mpld3.save_html(pt, "test_linearPP.html")
    mpld3.show()
Example #44
0
    def show_mpld3(self,fig,ax,points,xl_list,xl_labels):
        import mpld3
        from mpld3 import plugins
        import pandas as pd

        # Define some CSS to control our custom labels
        css = """
        table
        {
          border-collapse: collapse;
        }
        th
        {
          color: #000000;
          background-color: #ffffff;
        }
        td
        {
          background-color: #cccccc;
        }
        table, th, td
        {
          font-family:Arial, Helvetica, sans-serif;
          border: 1px solid black;
          text-align: right;
          font-size: 10px;
        }
        """
        df = pd.DataFrame(index=xl_labels)

        sorted_keys=sorted(xl_list[0].keys())

        for k in sorted_keys:
            df[k] = np.array([xl[k] for xl in xl_list])

        labels = []
        for i in range(len(xl_labels)):
            label = df.ix[[i], :].T
            # .to_html() is unicode; so make leading 'u' go away with str()
            labels.append(str(label.to_html()))

        tooltip = plugins.PointHTMLTooltip(points, labels,
                                   voffset=10, hoffset=10, css=css)
        plugins.connect(fig, tooltip)
        mpld3.save_html(fig,"output.html")
Example #45
0
def downloadVideoAndEntropy(f, frame):
    fhandle = open(f, 'ab')
    ftp.retrbinary('RETR ' + f, fhandle.write)
    cap = cv2.VideoCapture(f)
    if cap.isOpened():
        cap.set(1, frame)
        ret, newframe = cap.read()
        if ret:
            newname = f.split(".")[0]
            finalname = newname + '_frame_' + str(frame) + '.png'
            cv2.imwrite(finalname, newframe)
            colorIm = Image.open(finalname)
            greyIm = colorIm.convert('L')
            colorIm = np.array(colorIm)
            greyIm = np.array(greyIm)
            N = 5
            S = greyIm.shape
            E = np.array(greyIm)
            for row in range(S[0]):
                for col in range(S[1]):
                    Lx = np.max([0, col - N])
                    Ux = np.min([S[1], col + N])
                    Ly = np.max([0, row - N])
                    Uy = np.min([S[0], row + N])
                    region = greyIm[Ly:Uy, Lx:Ux].flatten()
                    E[row, col] = entropy(region)
            grayImage = cv2.applyColorMap(greyIm, cv2.COLOR_BGR2GRAY)
            entropyImage = cv2.applyColorMap(greyIm, cv2.COLORMAP_JET)
            cv2.imwrite('gray_' + finalname, greyIm)
            # cv2.imwrite('color_' + finalname, entropyImage)
            a = np.empty_like(E)
            a[:, :] = E
            a = np.flipud(a)
            fig = plt.figure()
            plt.imshow(a, cmap=plt.get_cmap("jet"), origin="lower")
            plt.xlabel('Entropy in 10x10 neighborhood')
            plt.colorbar()
            plt.savefig('color_' + finalname, bbox_inches='tight')
            plt.imshow(E, cmap=plt.get_cmap("jet"), origin="lower")
            plt.plot()
            htmlname = newname + '_frame_' + str(frame) + '.html'
            mpld3.save_html(fig, htmlname)
            # mpld3.fig_to_html(fig, template_type="simple")
            print 'finished!'
    cap.release()
Example #46
0
def colormagMPLD3(tabfile, bfilter, rfilter):
    import matplotlib.pyplot as plt
    import mpld3

    coldict = dict(B=0, V=1, R=2, J=3, K=4)
    photdata = getdata(tabfile)
    cp = [cleandata(i) for i in photdata]
    colorarray = colexces(cp[coldict[bfilter]], cp[coldict[rfilter]])
    fig, ax = plt.subplots()
    scatter = ax.scatter(colorarray[3], colorarray[2] - colorarray[3], c=colorarray[1], s=40)
    ax.axes.invert_xaxis()
    ax.axes.set_xlabel(rfilter)
    ax.axes.set_ylabel(bfilter + " - " + rfilter)
    labels = [str(colorarray[1][i]) for i in range(0, len(colorarray[1]))]
    tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
    mpld3.plugins.connect(fig, tooltip)
    mpld3.save_html(fig, "mpld3color.html")
    return
Example #47
0
    def construct_plot(self, save_template=False, template_name="model.html"):
        # build plot data
        fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
        scatter = ax.scatter(self.model_data[self.target].values,
                             self.model_data['prediction'].values.astype(int),
                             c= 'r',
                             cmap=plt.cm.jet)
        ax.grid(color='white', linestyle='solid')
        ax.set_title("Actual (target) vs. prediction", size=20)
        plt.ylabel('prediction')
        plt.xlabel('actual')

        # create interactive tooltip, save as HTML
        tooltip = mpld3.plugins.PointLabelTooltip(scatter)
        mpld3.plugins.connect(fig, tooltip)
        if save_template:
            mpld3.save_html(fig, template_name)
        return fig
Example #48
0
def generate_plots(image_filename=None, html_filename=None):
    """Generate static and/or dynamic plots for sources count metric"""
    if not (image_filename or html_filename):
        return
    data_set = get_data_set()
    dates = [item["date"] for item in data_set]

    fig, ax = pyplot.subplots()
    ax.set_title("Sources count")
    for i, vcs in enumerate(VCS_TYPES):
        col = get_rgb_color(i * 1.0 / len(VCS_TYPES))
        ax.plot(dates, [item[vcs] for item in data_set], color=col, label=vcs)
    ax.legend(loc="upper left")
    if image_filename:
        # Save static plot as image
        pyplot.savefig(image_filename)
    if html_filename:
        # Save dynamic plot as html document
        save_html(fig, html_filename)
Example #49
0
def lightcurveMPLD3(tabfile, filters):
    import matplotlib.pyplot as plt
    import mpld3

    fig = plt.figure()
    numplots = len(filters)
    coldict = dict(B=0, V=1, R=2, J=3, K=4)
    photdata = getdata(tabfile)
    photdatatrim = [photdata[coldict[j]] for j in filters]
    cp = [cleandata(k) for k in photdatatrim]
    for i in range(0, len(filters)):
        a = fig.add_subplot(numplots, 1, i + 1)
        a.scatter(cp[i][0], cp[i][1])
        a.axes.invert_yaxis()
        a.axes.set_xlabel("Julian Date")
        a.axes.set_ylabel(filters[i] + " magnitude")
    mpld3.plugins.connect(fig, mpld3.plugins.Zoom(button=False, enabled=True))
    mpld3.save_html(fig, "mpld3lc.html")
    return
def vis_data(post_data):
    # Use Mpld3 to visualize data
    # CSS for labels
    css = """

    body {
      background-color: #E0E4CC
    }
    iframe {
      width: 80%
      height: 900%
      text-align: left
    }
    table, th, td, iframe
    {
      font-family: Arial, Helvetica, sans-serif;
      text-align: right;
      color: #000000;
      background-color: #A7DBD8;
      border: none;
      border-spacing: 0;
      padding: 0;
    }
    """
    pd.set_option('display.max_colwidth', -1)
    size = []
    for likes in post_data['likes']:
        if likes > 1:
            size.append(likes * 10)
        else:
            size.append(9)

    data = pd.DataFrame({
        'Color': post_data['tones']
    })
    labels = []
    for i in range(len(post_data['ids'])):
        label = data.ix[[i], :].T
        label.columns = ['Post {0}'.format(i + 1)]
        labels.append((label.to_html()))

    fig, ax = plt.subplots()

    scatter = ax.scatter(matplotlib.dates.date2num(post_data['times']),
                         post_data['follower_counts'],
                         c=post_data['tones'],
                         s=size,
                         alpha=0.7,
                         cmap=plt.cm.jet)
    ax.grid(color='white', linestyle='solid')

    ax.set_title('#{0} Trends'.format(hash_tag), size=40)
    green = mpatches.Patch(color='green', label='Positive')
    gray = mpatches.Patch(color='gray', label='Neutral')
    red = mpatches.Patch(color='red', label='Negative')
    plt.legend(handles=[green, gray, red], ncol=3, framealpha=0.5)
    plt.xlabel('Time', size=25)
    plt.ylabel('Number of Followers the Poster has', size=25)
    frame = plt.gca()
    frame.axes.get_xaxis().set_ticks([])
    frame.axes.get_yaxis().set_ticks([])

    tooltip = mpld3.plugins.PointHTMLTooltip(scatter, labels, css=css)
    mpld3.plugins.connect(fig, tooltip)

    fig.set_size_inches(8.5, 6.5)

    mpld3.save_html(fig, '{0}-graph.html'.format(hash_tag))
from pylab import show, figure
import ShapelyChipDesigns as SD
import mpld3

n1      = 3    
n2      = 2    
wfinger = 3    
lfinger = 90    
wgap    = 3   
ltaper  = 100 
wc      = 10    
wGgap   = 4.5 

CAP = SD.MakeFingercapacitor(n1, n2,
                             wfinger, lfinger, wgap, ltaper, 
                             wc, wGgap)

fig = figure()
CAP.show_info()
SD.mouseshow() 

mpld3.save_html(fig, "_img/test_finger_capacitor.html")
Example #52
0
fig2 = pl.figure(2)
ax2 = fig2.add_axes([0.1,0.15,0.8,0.8])
percent_growth = (records['data2'] - records['data1'])*100./records['data1']

scatter = ax2.scatter(records['data1'], percent_growth, s = 100., alpha = 0.4, c = records['lat']*20, cmap = pl.cm.Reds)
ax2.set_xlim(0,20000)
pl.xticks(size = 16)
pl.yticks(size = 16)
pl.ylabel('% Increase in population (2000-10)', size = 18)
pl.xlabel('Population in year 2000 in 000s', size = 18)
labels = [i for i in records['keys']]
#fig2.plugins = [plugins.PointLabelTooltip(scatter, labels)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig2, tooltip)
mpld3.save_html(fig2, 'mpld3_test.html')                     
#pl.show()
#mpld3.show()

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
fig3 = pl.figure(3)
map = Basemap(projection='merc', lat_0 = 20, lon_0 = 80,
    resolution = 'h', area_thresh = 1.,
    llcrnrlon=68.0, llcrnrlat=8.0,
    urcrnrlon=95.0, urcrnrlat=37.0)

map.drawmapboundary(fill_color='#1E90FF')
map.fillcontinents(color='#ddaa66',lake_color='#1E90FF')
map.drawcoastlines()
map.drawcountries()
Example #53
0
		dx = xmax - xmin
		ddx=0.1*dx
		dy = ymax - ymin
		ddy=0.1*dy
	plt.hold(False)
	plt.xlim(xmin-ddx,xmax+ddx)
	plt.ylim(ymin-ddy,ymax+ddy)
	plt.title('Bibliography Graph %s' %year_iter)
	plt.axis('off')
	plt.savefig("../output/bib-graph%s.png" %year_iter)  # save as png
	
	fig = plt.figure(1, figsize=(10, 10))
	plt.axis('off')
	pos=nx.drawing.nx_agraph.graphviz_layout(bib_graph)
	node_color=[c*10 for c in nx.core_number(bib_graph).values()]
#	for node in bib_graph:
#		pos[node]=(pos[node][0]*10, pos[node][1]*10)
	nx.draw_networkx(bib_graph, pos, node_size=node_weight, node_color=node_color, edge_color=edge_weight, alph=0.5, cmap=plt.cm.OrRd, font_size=11, font_family='arial')
	plt.title('Bibliography Graph %s' %year_iter)
	mpld3.plugins.connect(fig)
	mpld3.save_html(fig, html_fig)				
	# Uncomment for more output formats
	#nx.write_graphml(bib_graph, './output/biblio-perso-globale%s.graphml' %year_iter)
	#nx.write_gml(bib_graph, './output/biblio-perso-globale.gml')
	#nx.write_graphml(bib_graph, './output/biblio-perso-globale.graphml')
	#plt.show() # display
	
input = "../output/bib-graph????.png"
output = "../output/bib-animation.gif"
os.system("convert -delay 100 -loop 0 %s %s" % (input, output))
html_fig.close()
Example #54
0
normalize(df, 'v-relative', 'v-relative-normalized')
normalize(df, 'minimum-AU-distance', 'minimum-AU-distance-normalized')
df['mining-index'] = .8 * df['v-relative-normalized'] + .2 * df['minimum-AU-distance-normalized'] # lower is better
df['close-approach-date'] = pd.to_datetime(df['close-approach-date'], coerce=True)
df = df[['close-approach-date', 'mining-index', 'object', 'minimum-AU-distance-normalized', 'v-relative']]

for year in range(2025, 2201):
    first_date = str(year)+'-01-01'
    last_date = str(year)+'-12-31'
    df_copy = df[(df['close-approach-date'] >= first_date) & (df['close-approach-date'] <= last_date)]
    df_copy.set_index(['close-approach-date'], inplace=True)
    fig, ax = plt.subplots(subplot_kw=dict(axisbg="#FFFFFF"), figsize=(15,7))
    ax.text

    scatter = ax.scatter(df_copy.index, df_copy['mining-index'], c=df_copy['mining-index'], s=30)
    ax.set_xlabel('Month', fontsize=18)
    ax.set_ylabel('Extraction Index', fontsize=18, family='sans-serif', weight='bold')
    ax.set_title('Element Extraction from Close Asteroids in ' + str(year), fontsize=24)
    ax.set_xlim(datetime.date(year, 1, 1), datetime.date(year, 12, 31))
    ax.set_ylim(0.0, 1.0)

    ax.tick_params(axis='both', which='major', pad=15, labelsize=10)
    df_copy['formatted'] = df_copy.index
    df_copy['formatted'] = df_copy['formatted'].apply(lambda x: x.strftime('%b %d, %Y'))
    labels = ['<table><tr><th>Object</th><td>{0}</td></tr><tr><th>Velocity</th><td>{1} km/s</td></tr><tr><th>Distance</th><td>{2} AU</td></tr><tr><th>Date</th><td>{3}</td></tr></table>'.format(i[0], i[1], i[2], i[3]) for i in df_copy[['object', 'v-relative', 'minimum-AU-distance-normalized', 'formatted']].as_matrix()]
    tooltip = mpld3.plugins.PointHTMLTooltip(scatter, labels=labels, css=css)
    mpld3.plugins.connect(fig, tooltip)
    mpld3.save_html(fig, str(year)+'_scatter')
    #mpld3.show()

Use the toolbar buttons at the bottom-right of the plot to enable zooming
and panning, and to reset the view.
"""
import matplotlib.pyplot as plt
import numpy as np
import mpld3

fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE'))
N = 100

scatter = ax.scatter(np.random.normal(size=N),
                     np.random.normal(size=N),
                     c=np.random.random(size=N),
                     s=1000 * np.random.random(size=N),
                     alpha=0.3,
                     cmap=plt.cm.jet)
ax.grid(color='white', linestyle='solid')

ax.set_title("Scatter Plot (with tooltips!)", size=20)

labels = ['point {0}'.format(i + 1) for i in range(N)]
tooltip = mpld3.plugins.PointLabelTooltip(scatter, labels=labels)
mpld3.plugins.connect(fig, tooltip)

plt.savefig("E:/IPython2/141007 Sphinx and ShapelyChipDesigns/try4/docs/pyplots/img/scatter_plot.png")
plt.savefig("scatter_plot.png")
mpld3.save_html(fig, "E:/IPython2/141007 Sphinx and ShapelyChipDesigns/try4/docs/pyplots/img/scatter_plot.html")
#mpld3.save_html(fig, "E:/IPython2/141007 Sphinx and ShapelyChipDesigns/try4/docs/_build/html/_images/scatter_plot.html")
mpld3.save_html(fig, "E:/IPython2/141007 Sphinx and ShapelyChipDesigns/try4/docs/pyplots/scatter_plot.html")
mpld3.save_html(fig, "scatter_plot.html")
mpld3.save_html(fig, "_img/scatter_plot.html")
Example #56
0
    def plot_intra_obs_dy_dz(self):
        # Make a table with the max delta chipx and chipy during each obsid.
        # First get the mins, maxes, means for each obsid by grouping.
        obsids = self.asol.group_by('obsid')['obsid', 'year', 'dy', 'dz']
        mins = obsids.groups.aggregate(np.minimum)
        maxes = obsids.groups.aggregate(np.maximum)
        means = obsids.groups.aggregate(np.mean)

        t = Table([means['year'],
                   (maxes['dy'] - mins['dy']) * 20,
                   (maxes['dz'] - mins['dz']) * 20],
                  names=('year', 'dy', 'dz'))
        t['ybin'] = np.trunc((t['year'] - t['year'][-1] - 0.0001) * 4.0)

        # Now group the dx and dy vals in 3-month intervals and find the 50th
        # and 90th percentile within each time bin.  This again using aggregation,
        # but this time in a trickier way by returning a Column object as the
        # aggregation object instead of a single value.
        t_bin = t.group_by('ybin')
        i_sorts = t_bin.groups.aggregate(np.argsort)
        outs = {}
        for col in ('dy', 'dz'):
            for perc in (50, 90, -5):
                rows = []
                for group, i_sort in izip(t_bin.groups, i_sorts[col]):
                    if perc < 0:
                        for row in group[i_sort[perc:]]:
                            rows.append(row)
                    else:
                        ii = (int(perc) * (len(group) - 1)) // 100
                        rows.append(group[i_sort[ii]])
                outs[str(perc) + col] = Table(rows=rows, names=t_bin.colnames)

        plt.close(1)
        fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True, num=1)
        for col, ax in izip(('dy', 'dz'), axes):
            for perc, color in izip((50, 90, -5), ('b', 'm', 'r')):
                t = outs[str(perc) + col]
                if perc < 0:
                    ax.plot(t['year'], t[col], '.', color=color, markersize=5,
                            label='Outliers')
                else:
                    ax.plot(t['year'], t[col], color=color, label='{}th percentile'.format(perc))
                    ax.fill_between(t['year'], t[col], color=color, alpha=0.3)
            ax.grid()
            ax.set_ylabel(col.upper() + ' (arcsec)')

        ylims = [axes[i].get_ylim()[1] for i in (0, 1)]
        for ii in (0, 1):
            axes[ii].set_ylim(0, max(ylims))
        axes[0].set_title('Intra-observation aimpoint drift (spacecraft coordinates)')
        axes[0].legend(loc='upper left', fontsize='small', title='')
        axes[1].set_xlabel('Year')

        mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fmt='.1f'))

        outroot = os.path.join(opt.data_root, 'intra_obs_dy_dz')
        logger.info('Writing plot files {}.png,html'.format(outroot))
        mpld3.save_html(fig, outroot + '.html')
        fig.patch.set_visible(False)
        plt.savefig(outroot + '.png', frameon=False)
Example #57
0
    def plot_chip_x_y(self, info_det):
        """
        Make 3-panel plot showing CHIPX vs. CHIPY, CHIPX vs. time, CHIPY vs. time.

        :param det: detector
        :param info_det: dict of relevant information for plot

        :returns: None
        """
        # Gather plot data from percentile tables
        years = []
        chipxs = []
        chipys = []
        for perc in (0, 10, 50, 90, 100):
            for xy in ('x', 'y'):
                dat = getattr(self, 'p{}_{}{}'.format(perc, self.chip_col, xy))
                years.append(dat['year'])
                chipxs.append(dat[self.chipx_col])
                chipys.append(dat[self.chipy_col])

        year = np.concatenate(years)
        chipx = np.concatenate(chipxs)
        chipy = np.concatenate(chipys)

        # Open three plot axes for CHIPX vs CHIPY, CHIPX vs time and CHIPY vs Time
        plt.close(1)
        fig = plt.figure(1, figsize=(10, 5))
        ax1 = plt.subplot2grid((2, 4), (0, 0), colspan=2, rowspan=2)
        ax2 = plt.subplot2grid((2, 4), (0, 2), colspan=2)
        ax3 = plt.subplot2grid((2, 4), (1, 2), colspan=2, sharex=ax2)

        cm = matplotlib.cm.get_cmap('YlOrRd')

        # Make the N-month bounding box
        asol = self.asol
        iok = np.searchsorted(asol['year'], asol['year'][-1] - float(opt.box_duration) / 12)
        asol = self.asol[iok:]
        x0, x1 = info_det['chipx']['min'], info_det['chipx']['max']
        y0, y1 = info_det['chipy']['min'], info_det['chipy']['max']
        dx = x1 - x0
        dy = y1 - y0
        year0 = asol['year'][0]
        dyear = asol['year'][-1] - year0
        ax1.add_patch(Rectangle((x0, y0), dx, dy,
                                facecolor='#fff8f8', edgecolor='k',
                                zorder=-100))
        ax2.add_patch(Rectangle((year0, x0), dyear, dx,
                                facecolor='#fff8f8', edgecolor='k',
                                zorder=-100))
        ax3.add_patch(Rectangle((year0, y0), dyear, dy,
                                facecolor='#fff8f8', edgecolor='k',
                                zorder=-100))
        pogx = POG[self.det][0]
        pogy = POG[self.det][1]

        ax1.plot([pogx], [pogy], '*r', ms=15, zorder=100)
        ax2.plot([POG['year']], [pogx], '*r', ms=15, zorder=100)
        ax3.plot([POG['year']], [pogy], '*r', ms=15, zorder=100)

        plot_opt = dict(c=year, cmap=cm, alpha=0.8, s=6.0, linewidths=0.5, zorder=10)
        points = ax1.scatter(chipx, chipy, **plot_opt)
        ax1.set_xlabel('CHIPX')
        ax1.set_ylabel('CHIPY')
        ax1.set_title('{} aimpoint position (CCD {})'.format(self.det, self.ccd))
        ax1.set_aspect('equal', 'datalim')
        ax1.grid()

        ax2.scatter(year, chipx, **plot_opt)  # points =
        ax2.set_ylabel('CHIPX')
        ax2.yaxis.tick_right()
        ax2.grid()

        ax3.scatter(year, chipy, **plot_opt)  # points =
        ax3.set_xlabel('Year')
        ax3.set_ylabel('CHIPY')
        ax3.yaxis.tick_right()
        ax3.grid()

        plt.show()
        mpld3.plugins.connect(fig, mpld3.plugins.LinkedBrush(points))
        mpld3.plugins.connect(fig, mpld3.plugins.MousePosition(fmt='.1f'))

        outroot = os.path.join(opt.data_root, 'chip_x_y_{}'.format(self.det_title))
        logger.info('Writing plot files {}.png,html'.format(outroot))
        mpld3.save_html(fig, outroot + '.html')
        fig.patch.set_visible(False)
        plt.savefig(outroot + '.png', frameon=False)
Example #58
0
def create_plot(players, defaults=PLOT_DEFAULTS, plot='all'):
    title = defaults[plot]['title']
    x_var = defaults[plot]['x_var']
    y_var = defaults[plot]['y_var']
    y_var2 = defaults[plot]['y_var2']
    y_err_regex = defaults[plot]['y_err_regex']
    group = defaults[plot]['group']
    drafted = defaults[plot]['drafted']
    save_file = defaults[plot]['save_file']
    if 'type' in defaults[plot]:
        type = defaults[plot]['type']
    else:
        type = None

    if 'undrafted_x_var' in defaults[plot]:
        undrafted_x_var = defaults[plot]['undrafted_x_var']
    else:
        undrafted_x_var = None


    print('creating plot {}'.format(plot))

    if drafted == False:
        show_draft = False
        drafted = 'drafted'
    else:
        show_draft = True

    player_list = []
    for player in players:
        player_list.append(player.flatten())
    df = DataFrame(player_list)

    df = df.sort(y_var, ascending=False)
    fig, ax = plt.subplots()
    elements = []
    labels=[]
    for name, big_group in df.groupby(group):
        positions = []
        color=next(ax._get_lines.color_cycle)
        for drafted_name, group in big_group.groupby(drafted):
            x = group[x_var]
            y = group[y_var]
            y2 = group[y_var2]
            yerr = [group[y_var] - group.filter(regex=y_err_regex).min(axis=1),
                            group.filter(regex=y_err_regex).max(axis=1) - group[y_var]]

            if drafted_name:
                if show_draft:
                    element = ax.scatter(x,y, color=color,
                                         s=60, alpha=0.6, marker='D')
                    ax.errorbar(x,y,yerr=yerr, linestyle="None", lw='1',color=color, )
                    positions.append(element)

                    if type != 'price':
                        element2 = ax.scatter(x,y2, color=color,
                            s=90, alpha=0.5, marker='+')
                        positions.append(element2)

                    players = list(group.apply(lambda x: PLOT_TOOLTIP.format(**x),axis=1))

                    mpld3.plugins.connect(fig, mpld3.plugins.PointHTMLTooltip(element, players,
                                                                               voffset=-75, hoffset=10))
            else:
                if undrafted_x_var: #special x var for undrafted price
                    x = group[undrafted_x_var]
                element = ax.scatter(x,y, color=color,
                                     s=90, alpha=0.2, marker='o')
                ax.errorbar(x,y,yerr=yerr, linestyle="None", lw='1',color=color, )
                positions.append(element)

                if type != 'price':
                    element2 = ax.scatter(x,y2, color=color,
                            s=90, alpha=0.5, marker='+')
                    positions.append(element2)

                players = list(group.apply(lambda x: PLOT_TOOLTIP.format(**x),axis=1))

                mpld3.plugins.connect(fig, mpld3.plugins.PointHTMLTooltip(element, players,
                                                                           voffset=-75, hoffset=10))




        elements.append(positions)
        labels.append(name)

    ax.set_title(title)
    if type == 'price':
        ax.set_xlim((df[x_var].max()+1), 0) # hard axis on price, reversed
    else:
        ax.set_xlim(0,(df[x_var].max() + 10))
    mpld3.plugins.connect(fig, mpld3.plugins.InteractiveLegendPlugin(elements, labels))
    mpld3.save_html(fig, PLOT_SAVE_DIR + '/' + save_file)
    return save_file
column_rename_map = {'season': 'Season',
                     'number': 'Episode',
                     'rating' : 'Rating',
                     'votes' : 'Votes',
                     'first_aired' : 'Air date', 
                     'overview' : 'Synopsis'}
for i in range(len(df)):
    label = df.iloc[[i]][columns_for_labels].T
    label.columns = df.iloc[[i]].title
    labels.append(label.rename(column_rename_map).to_html())

# Plot scatter points
c = (df.season-1).map(pd.Series(list(sns.color_palette(n_colors=len(df.season.value_counts())))))
points = ax.scatter(df.number_abs, df.rating, alpha=.99, c=c, zorder=2)


ax.set_ylim(5,10)
ax.set_xlim(0,main_episodes.number_abs.max()+1)
ax.set_ylabel('Trakt.tv Episode Rating')
ax.set_xlabel('Episode number')
fig.set_size_inches(12, 6)
ax.set_title(show_summary.title[0], size=20)

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

mpld3.save_html(fig, str(show_summary.title[0] + ".html"))
mpld3.display()

Example #60
0
    def getHTML(self, params):
        # print(params)
        global current_gene
        data_max = 0
        fig = plt.figure(figsize=(9, 7))
        gs = gridspec.GridSpec(4, 1, height_ratios=[6, 1, 1, 1])
        gene = params["freq"].upper()
        range_ext = int(params["range"])
        searched = "False"
        lines = []
        if gene.startswith("CHR"):
            temp = gene.split(":")
            current_gene = ["Region", temp[0][3:], int(temp[1]), int(temp[2]), "+"]
        else:
            for item in genes:
                if item[0] == gene:
                    current_gene = item
                    break

            else:
                temp_gene = self.search_SGD(gene)
                print(temp_gene)
                searched = "True"
                for item in genes:
                    if item[0] == temp_gene[0]:
                        current_gene = item
                        break
                else:
                    current_gene = temp_gene
                    print(gene)
        # Todo add possibility to search SGD defaulting to SGD coordinates
        print(
            "*" * 40
            + "\nQuery by user:"******";\nrange extension:"
            + str(range_ext)
            + " ;Checked Boxes:"
            + str(params["check_boxes"])
            + ";SGD used:"
            + searched
            + "\n"
            + "*" * 40
        )
        # print(params)
        splt1 = plt.subplot(gs[0, :])
        splt1.set_title(
            "{}-Chromosome {}:{}:{}, strand:{}".format(
                current_gene[0], current_gene[1], current_gene[2], current_gene[3], current_gene[4]
            ),
            size=20,
        )
        # splt1.set_xlabel('Distance from TSS (nt)')
        splt1.axes.xaxis.set_ticklabels([])
        splt1.set_ylabel("Normalized counts per million reads\n ", size=16, labelpad=15)
        n = 0
        fill_colors = [
            "#921B16",
            "#D6701C",
            "#251F47",
            "#68A691",
            "#68A691",
            "#447604",
            "#232621",
            "#0BBE30",
            "#BC3908",
            "#191923",
            "#391923",
            "#591923",
        ]
        for item in params["check_boxes"]:
            if current_gene[4] == "+":
                data = data_sets[data_sets_names[int(item)]][current_gene[1]][
                    current_gene[2] - range_ext : current_gene[3] + range_ext
                ]
                factor = 100.0 / len(data)

                data = ndimage.interpolation.zoom(data, factor)

                _ = splt1.plot(data, label=data_sets_legend[int(item)], color=fill_colors[n], alpha=0.3, lw=3)
                lines.extend(_)
                splt1.fill_between(range(0, len(data)), 0, data, alpha=0.2, color=fill_colors[n])
            if current_gene[4] == "-":
                data = data_sets[data_sets_names[int(item)]][current_gene[1]][
                    current_gene[2] - range_ext : current_gene[3] + range_ext
                ]
                factor = 100.0 / len(data)

                data = ndimage.interpolation.zoom(data[::-1], factor)
                print(data)
                _ = splt1.plot(data, label=data_sets_legend[int(item)], color=fill_colors[n], alpha=0.3, lw=3)
                lines.extend(_)
                splt1.fill_between(range(0, len(data)), 0, data, alpha=0.2, color=fill_colors[n])
            if np.max(data) > data_max:
                data_max = np.max(data)
            n += 1
        # plot2
        if current_gene[4] == "+":
            y_values1 = data_sets["genes"][current_gene[1]][
                0, current_gene[2] - range_ext : current_gene[3] + range_ext
            ]
            y_values2 = data_sets["genes"][current_gene[1]][
                1, current_gene[2] - range_ext : current_gene[3] + range_ext
            ]
        else:
            y_values1 = data_sets["genes"][current_gene[1]][
                0, current_gene[2] - range_ext : current_gene[3] + range_ext
            ][::-1]
            y_values2 = data_sets["genes"][current_gene[1]][
                1, current_gene[2] - range_ext : current_gene[3] + range_ext
            ][::-1]

        # splt2.plot(y_values, 'grey', linewidth=2)
        factor = 100.0 / len(y_values1)
        splt2 = plt.subplot(gs[1, :])
        y_values1 = ndimage.interpolation.zoom(y_values1, factor)
        y_values2 = ndimage.interpolation.zoom(y_values2, factor)
        x_values = range(0, len(y_values1))
        splt2.plot([0, len(y_values1)], [0, 0], "black", linewidth=1.5)
        splt2.fill_between(x_values, y_values1 / 4.0, y_values1, color="#FFB30F")
        splt2.fill_between(x_values, y_values2 / 4.0, y_values2, color="#FD151B")
        splt2.set_ylim(-1.5, 1.5)
        splt2.text(1.01, 0.5, "GENES", fontsize=12, transform=splt2.transAxes, verticalalignment="center")
        splt2.yaxis.set_tick_params(color="w")
        splt2.set_yticks([-0.5, 0.5])
        splt2.spines["left"].set_position(("outward", 10))
        splt2.set_yticklabels([])
        splt2.axes.xaxis.set_ticklabels([])
        # plot3

        if current_gene[4] == "+":
            y_values1 = data_sets["CUTS"][current_gene[1]][0, current_gene[2] - range_ext : current_gene[3] + range_ext]
            y_values2 = data_sets["CUTS"][current_gene[1]][1, current_gene[2] - range_ext : current_gene[3] + range_ext]
        else:
            y_values1 = data_sets["CUTS"][current_gene[1]][
                0, current_gene[2] - range_ext : current_gene[3] + range_ext
            ][::-1]
            y_values2 = data_sets["CUTS"][current_gene[1]][
                1, current_gene[2] - range_ext : current_gene[3] + range_ext
            ][::-1]

        # splt2.plot(y_values, 'grey', linewidth=2)
        factor = 100.0 / len(y_values1)
        splt3 = plt.subplot(gs[2, :])
        y_values1 = ndimage.interpolation.zoom(y_values1, factor)
        y_values2 = ndimage.interpolation.zoom(y_values2, factor)
        x_values = range(0, len(y_values1))
        splt3.plot([0, len(y_values1)], [0, 0], "black", linewidth=1.5)
        splt3.fill_between(x_values, y_values1 / 4.0, y_values1, color="#028090")
        splt3.fill_between(x_values, y_values2 / 4.0, y_values2, color="#6EEB83")
        splt3.set_ylim(-1.5, 1.5)
        splt3.text(1.01, 0.5, "CUTS", fontsize=12, transform=splt3.transAxes, verticalalignment="center")
        splt3.yaxis.set_tick_params(color="w")
        splt3.set_yticks([-0.5, 0.5])
        splt3.spines["left"].set_position(("outward", 10))
        splt3.set_yticklabels([])
        splt3.axes.xaxis.set_ticklabels([])
        # plot4

        if current_gene[4] == "+":
            y_values1 = data_sets["SUTS"][current_gene[1]][0, current_gene[2] - range_ext : current_gene[3] + range_ext]
            y_values2 = data_sets["SUTS"][current_gene[1]][1, current_gene[2] - range_ext : current_gene[3] + range_ext]
        else:
            y_values1 = data_sets["SUTS"][current_gene[1]][
                0, current_gene[2] - range_ext : current_gene[3] + range_ext
            ][::-1]
            y_values2 = data_sets["SUTS"][current_gene[1]][
                1, current_gene[2] - range_ext : current_gene[3] + range_ext
            ][::-1]

        # splt2.plot(y_values, 'grey', linewidth=2)
        factor = 100.0 / len(y_values1)
        splt4 = plt.subplot(gs[3, :])
        y_values1 = ndimage.interpolation.zoom(y_values1, factor)
        y_values2 = ndimage.interpolation.zoom(y_values2, factor)
        x_values = range(0, len(y_values1))
        splt4.plot([0, len(y_values1)], [0, 0], "black", linewidth=1.5)
        splt4.fill_between(x_values, y_values1 / 4.0, y_values1, color="#028090")
        splt4.fill_between(x_values, y_values2 / 4.0, y_values2, color="#6EEB83")
        splt4.set_ylim(-1.5, 1.5)
        splt4.text(1.01, 0.5, "SUTS", fontsize=12, transform=splt4.transAxes, verticalalignment="center")
        splt4.yaxis.set_tick_params(color="w")
        splt4.set_yticks([-0.5, 0.5])
        splt4.spines["left"].set_position(("outward", 10))
        splt4.set_yticklabels([])
        splt4.axes.xaxis.set_ticklabels([])
        leg = splt1.legend(ncol=3)
        leg.set_title("")
        for legobj in leg.legendHandles:
            legobj.set_linewidth(3.0)
        size = (current_gene[3] + range_ext) - (current_gene[2] - range_ext)
        splt1.set_ylim(0, np.max(data_max) * 1.3)
        # print(str(plt.ylim()[1] - 5))
        splt1.text(0.02, 0.02, "Luis Soares", fontsize=6, transform=splt1.transAxes, verticalalignment="top")

        leg_line = leg.get_lines()
        number_of_samples = len(leg_line)

        class HighlightLines(plugins.PluginBase):
            """A plugin to highlight lines on hover"""

            JAVASCRIPT = """
    mpld3.register_plugin("linehighlight", LineHighlightPlugin);
    LineHighlightPlugin.prototype = Object.create(mpld3.Plugin.prototype);
    LineHighlightPlugin.prototype.constructor = LineHighlightPlugin;
    LineHighlightPlugin.prototype.requiredProps = ["legend_ids","line_ids"];
    LineHighlightPlugin.prototype.defaultProps = {alpha_bg:0.3, alpha_fg:1.0}
    function LineHighlightPlugin(fig, props){
        mpld3.Plugin.call(this, fig, props);
    };

    LineHighlightPlugin.prototype.draw = function(){
      var obj=[]
      for(var i=0; i<this.props.legend_ids.length; i++){
         obj=obj.concat([mpld3.get_element(this.props.legend_ids[i], this.fig),
         mpld3.get_element(this.props.line_ids[i], this.fig)]);

        alpha_fg = this.props.alpha_fg;
        alpha_bg = this.props.alpha_bg;
}
%s

        }
    """ % (
                create_java(number_of_samples * 2)
            )

            def __init__(self, legend, lines, samples):
                self.lines = lines
                self.legend = legend
                self.dict_ = {
                    "type": "linehighlight",
                    "legend_ids": [utils.get_id(line) for line in legend],
                    "line_ids": [utils.get_id(line) for line in lines],
                    "alpha_bg": lines[0].get_alpha(),
                    "alpha_fg": 1.0,
                }

        class TopToolbar(plugins.PluginBase):
            """Plugin for moving toolbar to top of figure"""

            JAVASCRIPT = """
    mpld3.register_plugin("toptoolbar", TopToolbar);
    TopToolbar.prototype = Object.create(mpld3.Plugin.prototype);
    TopToolbar.prototype.constructor = TopToolbar;
    function TopToolbar(fig, props){
        mpld3.Plugin.call(this, fig, props);
    };

    TopToolbar.prototype.draw = function(){
      // the toolbar svg doesn't exist
      // yet, so first draw it
      this.fig.toolbar.draw();

      // then change the y position to be
      // at the top of the figure
      this.fig.toolbar.toolbar.attr("y", 2);

      // then remove the draw function,
      // so that it is not called again
      this.fig.toolbar.draw = function() {}
    }
    """

            def __init__(self):
                self.dict_ = {"type": "toptoolbar"}

        plugins.clear(fig)
        plugins.connect(
            fig, TopToolbar(), HighlightLines(leg_line, lines, number_of_samples), plugins.Reset(), plugins.BoxZoom()
        )
        file = cStringIO.StringIO()
        mpld3.save_html(fig, file)
        return file.getvalue()