예제 #1
0
    def testActiveLegend(self):
        import mpld3
        from mpld3 import plugins
        from mpld3.utils import get_id
        import numpy as np
        import collections
        import matplotlib.pyplot as plt

        N_paths = 5
        N_steps = 100

        x = np.linspace(0, 10, 100)
        y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)
        y = y.cumsum(1)
        fig = plt.figure(figsize=(16, 7))  # 8 inches wide by 6 inches tall
        ax = fig.add_subplot(2, 2, 1)

        # fig, ax = plt.subplots()
        labels = ["a", "b", "c", "d", "e"]
        line_collections = ax.plot(x, y.T, lw=4, alpha=0.2)
        interactive_legend = plugins.InteractiveLegendPlugin(
            line_collections, labels)
        plugins.connect(fig, interactive_legend)

        mpld3.show()
예제 #2
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()
예제 #3
0
    def create_plot(self, x, y1, y2):
        fig, ax = plt.subplots()

        y1, y1_b, y1_e = trim_list(y1)
        y2, y2_b, y2_e = trim_list(y2)

        x_dates = map(string2date, x)

        ax.set_xlim(left=min((x_dates[y1_b:-y1_e][0], x_dates[y2_b:-y2_e][0])),
                    right=max(
                        (x_dates[y1_b:-y1_e][-1], x_dates[y2_b:-y2_e][-1])))

        co = list()
        co.append(ax.plot(x_dates[y1_b:-y1_e], y1, 'b', label='One'))
        co.append(ax.plot(x_dates[y2_b:-y2_e], y2, 'r', label='Two'))

        handles, labels = ax.get_legend_handles_labels(
        )  # return lines and labels
        interactive_legend = plugins.InteractiveLegendPlugin(
            zip(handles, co),
            labels,
            alpha_unsel=0.5,
            alpha_over=1.5,
            start_visible=True)
        plugins.connect(fig, interactive_legend)

        ax.fill_between(x_dates[y1_b:-y1_e], y1, facecolors='blue', alpha=0.5)
        ax.fill_between(x_dates[y2_b:-y2_e], y2, facecolors='red', alpha=0.5)
        ax.set_title(self.title)
        plt.subplots_adjust(right=.8)
        fig.set_size_inches(10, 8, forward=True)

        return mpld3.fig_to_html(fig)
def get_plot(date=None, output='html'):

    date = date or dt.datetime.now()

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

    plot_db_data(ax, date)

    plot_forecast(ax, date)

    plot_grid(fig, ax)

    handles, labels = ax.get_legend_handles_labels()

    if output == 'html':
        interactive_legend = plugins.InteractiveLegendPlugin(handles, labels)
        plugins.connect(fig, interactive_legend)
        fig.subplots_adjust(right=0.7)
        plot_html = mpld3.fig_to_html(fig, template_type='general')
        return plot_html

    else:
        ax.legend(handles, labels)
        plt.savefig('./static/graph/graph.png')
        plt.close()
        f = open('./static/graph/graph.png', 'rb')
        return f
예제 #5
0
    def __call__(self, plot, view):
        if not self._applies(plot, view): return
        fig = plot.handles['fig']
        if 'legend' in plot.handles:
            plot.handles['legend'].set_visible(False)
        line_segments, labels = [], []
        keys = view.keys()
        for idx, subplot in enumerate(plot.subplots.values()):
            if isinstance(subplot, PointPlot):
                line_segments.append(subplot.handles['paths'])
                if isinstance(view, NdOverlay):
                    labels.append(str(keys[idx]))
                else:
                    labels.append(subplot.hmap.last.label)
            elif isinstance(subplot, CurvePlot):
                line_segments.append(subplot.handles['line_segment'])
                if isinstance(view, NdOverlay):
                    labels.append(str(keys[idx]))
                else:
                    labels.append(subplot.hmap.last.label)

        tooltip = plugins.InteractiveLegendPlugin(line_segments, labels,
                                                  alpha_sel=self.alpha_sel,
                                                  alpha_unsel=self.alpha_unsel)
        plugins.connect(fig, tooltip)
예제 #6
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')
def plot_norm_cart_interacive_el(el_co, fname, save_dir):

    plt.ioff()  #turns plot off

    el_co = el_co.convert_objects(convert_numeric=True)

    #normalize
    normalised_el_co, _ = Sector.normalise2("", el_co, el_co)

    #Get Freq list of column headers
    headers_el_co = list(el_co.dtypes.index)

    #Create plot
    fig, ax = plt.subplots(figsize=(12, 7))
    fig.subplots_adjust(right=.8)
    labels = headers_el_co

    line_collections = ax.plot(normalised_el_co, lw=1.5, alpha=0.9)
    interactive_legend = plugins.InteractiveLegendPlugin(line_collections,
                                                         labels,
                                                         alpha_unsel=0.1,
                                                         alpha_over=1.5,
                                                         start_visible=False)
    plugins.connect(fig, interactive_legend)

    ###########################################################################
    # Figure Settings

    #Set axis parameters
    ax.grid(alpha=0.25)
    ax.set_ylim([-40, 0.5])
    ax.set_xlim([0, 360])
    x_tick_spacing = 20
    y_tick_spacing = 3
    ax.xaxis.set_major_locator(ticker.MultipleLocator(x_tick_spacing))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(y_tick_spacing))

    #Set Plot title & axis titles

    ax.set_ylabel('dBi')
    ax.set_xlabel('Angle')

    ax.set_ylim([-40, 0.5])
    ax.set_xlim([0, 360])

    ax.set_title(fname)

    #Save the figure as a html
    mpld3.save_html(fig, save_dir + fname + ".html")

    plt.close('all')
    plt.ion()
예제 #8
0
def make_mpld3_position_plot(output_path, input_paths):
    fig,ax = make_agent_positions_figure(input_paths)
    handles, labels = ax.get_legend_handles_labels() # return lines and labels
    interactive_legend = plugins.InteractiveLegendPlugin(zip(handles,
                                                             ax.lines),
                                                         labels,
                                                         alpha_unsel=.1,
                                                         alpha_over=1.5,
                                                         start_visible=True)
    plugins.connect(fig, interactive_legend)

    html = mpld3.fig_to_html(fig)
    with open(output_path, "w") as html_file:
        html_file.write(html)
예제 #9
0
def create_plot():
    fig, ax = plt.subplots()

    x = np.linspace(0, 10, 100)
    l1 = ax.plot(x, np.sin(x), label='sin', lw=3, alpha=0.2)
    l2 = ax.plot(x, np.cos(x), label='cos', lw=3, alpha=0.2)
    l3 = ax.plot(x[::5], 0.5 * np.sin(x[::5] + 2), 'ob', label='dots',
                 alpha=0.2)

    labels = ['sin', 'cos', 'dots']
    interactive_legend = plugins.InteractiveLegendPlugin([l1, l2, l3], labels)
    plugins.connect(fig, interactive_legend)

    ax.set_title("Interactive legend test", size=20)

    return fig
예제 #10
0
def testInterLegend():
    import matplotlib.pyplot as plt
    import numpy as np
    import pandas as pd
    import mpld3
    from mpld3 import plugins
    np.random.seed(9615)

    # generate df
    N = 100
    df = pd.DataFrame(
        (.1 * (np.random.random((N, 5)) - .5)).cumsum(0),
        columns=['a', 'b', 'c', 'd', 'e'],
    )

    # plot line + confidence interval
    fig, ax = plt.subplots()
    ax.grid(True, alpha=0.3)

    for key, val in df.iteritems():
        l, = ax.plot(val.index, val.values, label=key)
        ax.fill_between(val.index,
                        val.values * .5,
                        val.values * 1.5,
                        color=l.get_color(),
                        alpha=.4)

    # define interactive legend

    handles, labels = ax.get_legend_handles_labels()  # return lines and labels
    interactive_legend = plugins.InteractiveLegendPlugin(zip(
        handles, ax.collections),
                                                         labels,
                                                         alpha_unsel=0.5,
                                                         alpha_over=1.5,
                                                         start_visible=True)
    plugins.connect(fig, interactive_legend)

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

    mpld3.show()
예제 #11
0
    def genRollGraph(self):

        fig, ax = plt.subplots()
        labels = ["Fast-Roll", "Ramp-Down", "Ramp-Up", "Slow-Roll"]

        line_collections = ax.plot(self.X_roll, self.y_roll, lw=4, alpha=0.2)
        interactive_legend = plugins.InteractiveLegendPlugin(
            line_collections, labels)
        plugins.connect(fig, interactive_legend)
        fig.set_size_inches(14, 6)
        plt.subplots_adjust(right=0.8)
        ax.set_xlabel("Time Segment")
        ax.set_ylabel("Confidence Level")
        ax.set_title("Turbine Roll", size=24)
        output = mpld3.fig_to_html(fig,
                                   d3_url='assets/js/d3.v3.min.js',
                                   mpld3_url='assets/js/mpld3.js')
        with open('rollGraph.html', 'w') as f:
            f.write(output)
        plt.savefig('rollGraph.png')
        plt.close()
    def plot(self):
        """Generate the plot and connects the plugins for interaction but do not show it"""

        self._setup_axes()

        self._plot_grid()

        self._plot_road()

        self._plot_sectors()

        # We do not care about indivual segments. But plotting them might help during debugging
        # self._plot_road_segments()

        self._plot_car()

        # Initialize the plot by visualizing nothign more than the grid. The road geometry will be there, as I cannot
        #   hide it for the moment....
        visible_elements = [False] * len(list(self.labels_and_handlers.keys()))
        visible_elements[0] = True  # Visualize the grid only by default

        # Configure the plugin that shows the interactive legend
        interactive_legend = plugins.InteractiveLegendPlugin(
            self.labels_and_handlers.values(),
            list(self.labels_and_handlers.keys()),
            # Setting alpha to 0.0 makes lines disappear but not patches
            alpha_unsel=0.0,
            alpha_over=1.5,
            start_visible=visible_elements,
            font_size=14,
            legend_offset=(-10, 0))

        # Configure the plugin that export road metadata as global variable
        export_road_metadata = ExportRoadMetadataPlugin(
            self._collect_road_metadata())

        plugins.connect(self.fig, interactive_legend, export_road_metadata)
예제 #13
0
 def genErrorGraph(self):
     fig, ax = plt.subplots()
     labels = [
         "Bearing Rub",
         "Preload",
         "Safe",
         "Weight Unbalance",
     ]
     line_collections = ax.plot(self.X_error, self.y_error, lw=4, alpha=0.2)
     interactive_legend = plugins.InteractiveLegendPlugin(
         line_collections, labels)
     plugins.connect(fig, interactive_legend)
     fig.set_size_inches(14, 6)
     plt.subplots_adjust(right=0.8)
     ax.set_xlabel("Time Segment")
     ax.set_ylabel("Confidence Level")
     ax.set_title("Turbine Error Modes", size=24)
     output = mpld3.fig_to_html(fig,
                                d3_url='assets/js/d3.v3.min.js',
                                mpld3_url='assets/js/mpld3.js')
     with open('errorGraph.html', 'w') as f:
         f.write(output)
     plt.savefig('errorGraph.png')
     plt.close()
예제 #14
0
    def __generatehtml(self, fig):
        i = 0
        for ax in fig.get_axes():
            plugins.connect(
                fig,
                plugins.InteractiveLegendPlugin(
                    plot_elements=ax.get_lines(),
                    labels=[str(x) for x in ax.get_legend().get_texts()],
                    ax=ax,
                    alpha_unsel=0.0,
                    alpha_over=1.5))
            i += 1
            for line in ax.get_lines():
                line.set_ydata(
                    [x if x is not None else 0 for x in line.get_ydata()])
                plugins.connect(
                    fig,
                    plugins.PointLabelTooltip(
                        points=line,
                        labels=[str(y) for y in line.get_ydata()],
                        hoffset=10,
                        voffset=10))

        return mpld3.fig_to_html(fig)
예제 #15
0
def four_group_plot(csv_file,
                    x_col,
                    y_col,
                    group1_col,
                    group2_col,
                    group3_col,
                    show_only_col,
                    show_only_col_flag=True):
    data = pd.read_csv(csv_file)

    data_group1 = data.groupby(group1_col)
    group2_labels = data[group2_col]

    fig, ax = plt.subplots()
    ax.margins(0.05)

    group1_marker_type = ['o', 's']
    group1_marker_size = [80, 100]
    group2_colors = ['purple', 'orange']
    group3_colors = ['purple', 'orange']
    group3_marker_size = [20, 30]

    # PLOT GROUP 1 and Group 2
    # TO be replaced by backgorund mesh
    # g1_cnt = 0
    # for name1,group1 in data_group1:
    #     marker=group1_marker_type[g1_cnt]
    #     marker_size = group1_marker_size[g1_cnt]
    #     data_group2 = group1.groupby(group2_col)
    #     g2_cnt=0
    #     for name2,group2 in data_group2:
    #         ax.scatter(group2[x_col],group2[y_col],marker=marker,s= marker_size,color=group2_colors[g2_cnt],alpha=.4)
    #         g2_cnt+=1
    #     g1_cnt+=1

    # Plot with just group 1
    # Plot with just group 1 and group 3
    g1_cnt = 0
    for name1, group1 in data_group1:
        marker = group1_marker_type[g1_cnt]
        marker_size = group3_marker_size[g1_cnt]
        data_group3 = group1.groupby(group3_col)
        g3_cnt = 0
        for name3, group3 in data_group3:
            lbl_str = group3_col + ': ' + str(name3)
            if marker == 'o':
                ax.scatter(group3[x_col],
                           group3[y_col],
                           marker=marker,
                           s=marker_size,
                           color=group3_colors[g3_cnt],
                           alpha=.6,
                           label=lbl_str)
            g3_cnt += 1
        g1_cnt += 1

    # g1_cnt=0
    # for name1,group1 in data_group1:
    #     marker=group1_marker_type[g1_cnt]
    #     marker_size=group1_marker_size[g1_cnt]
    #     ax.scatter(group1[x_col], group1[y_col], marker=marker, s= marker_size,facecolors='cyan',edgecolors='k',alpha=.1,label=group1_col+'_'+str(name1))
    #     g1_cnt+=1
    #     break

    ### show_only_col
    show_only_data = data[data[show_only_col] == show_only_col_flag]
    ax.scatter(show_only_data[x_col],
               show_only_data[y_col],
               marker='*',
               color='red',
               label='Please Label Manually (' + show_only_col + ')')
    #
    # ax.legend()
    # plt.show()

    handles, labels = ax.get_legend_handles_labels()  # return lines and labels
    interactive_legend = plugins.InteractiveLegendPlugin(
        zip(handles, ax.collections), labels)
    # alpha_unsel=0.5,
    # alpha_over=1.5,
    # start_visible=True)

    plugins.connect(fig, interactive_legend)
    ax.set_xlabel(x_col)
    ax.set_ylabel(y_col)
    ax.set_title('LOW DIM VISUALIZATION', size=20)
    mpld3.show()
예제 #16
0
import mpld3
from mpld3 import plugins
from mpld3.utils import get_id
import numpy as np
import collections
import matplotlib.pyplot as plt

N_paths = 1
N_steps = 100

x = np.linspace(0, 10, 100)
y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)
y = y.cumsum(1)

fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)

labels = ["a"]
l1 = ax1.plot(x, y.T, lw=4, alpha=0.1)
s1 = []
for i in range(N_paths):
    s = ax2.scatter(x, y[i, :], c="blue", alpha=0.1)
    s1.append(s)

plugins.connect(fig, plugins.InteractiveLegendPlugin(l1, labels, ax=ax1))
plugins.connect(fig, plugins.InteractiveLegendPlugin(s1, labels, ax=ax2))

mpld3.show()
pass
예제 #17
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('{}/trips.sqlite'.format(config.SCRIPT_LOC))
    # 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:
        if os.path.isfile("{0}{1}/{1}.{2}.sqlite".format(
                config.ANNOTATION_DIR, organism, transcriptome)):
            transhelve = sqlite3.connect("{0}{1}/{1}.{2}.sqlite".format(
                config.ANNOTATION_DIR, organism, transcriptome))
        else:
            return "Cannot find annotation file {}.{}.sqlite".format(
                organism, transcriptome)
    else:
        transhelve = sqlite3.connect(
            "{0}transcriptomes/{1}/{2}/{3}/{2}_{3}.v2.sqlite".format(
                config.UPLOADS_DIR, 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 = {}
            print "Normalization is true, normalizing by factor", item[8]
            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
예제 #18
0
파일: P2N-Cluster.py 프로젝트: ip-tools/P2N
    #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)
    #next shoul be done at the en of loop for I guess
    #TitFic = ['file:///D:/Doc-David/Developpement/SpyderWorkspace/Git-P2N/DATA/Banana/PatentContents/Metrics/' +Tit2FicName [lab] for lab in labels]
    TitFic = ['file:///' + Here + Tit2FicName[lab] for lab in labels]
    tempoLab.append(labels)
    tempoFic.append(TitFic)

interactive_legend = plugins.InteractiveLegendPlugin(memoFig,
                                                     memoLab,
                                                     legend_offset=(0, 300),
                                                     alpha_unsel=0.1,
                                                     alpha_over=0.9,
                                                     start_visible=False)
interactive_legend2 = plugins.InteractiveLegendPlugin(memoFig2,
                                                      memoLab2,
                                                      legend_offset=(0, 0),
                                                      alpha_unsel=0.1,
                                                      alpha_over=0.8,
                                                      start_visible=False)
#TitFics = []
#tempoFic=[]

mpld3.plugins.connect(fig, interactive_legend)
mpld3.plugins.connect(fig, interactive_legend2)

tooltip = dict()
def plot_norm_cart_interacive_az(az_co, az_cr, fname, save_dir):

    plt.ioff()

    #Defining a list of colors
    colours = list([
        "blue", "green", "red", "cyan", "magenta", "yellow", "black", "tan",
        "firebrick", "plum", "aqua", "darkblue", "crimson", "pink",
        "chocolate", "darkgrey", "blue", "green", "red", "cyan", "magenta",
        "yellow", "black", "tan", "firebrick", "plum", "aqua", "darkblue",
        "crimson", "pink", "chocolate", "darkgrey"
    ])

    #Convert data so that we can use it
    az_co = az_co.convert_objects(convert_numeric=True)
    az_cr = az_cr.convert_objects(convert_numeric=True)

    az_co, az_cr = Sector.normalise2("", az_co, az_cr)

    #X axis
    x1 = np.arange(0, 360, 1)

    #List of all frequencies
    key_list = list(az_co.keys())

    fig, ax = plt.subplots(figsize=(12, 7))
    fig.subplots_adjust(right=.8)

    #List of all lines
    ln = list()

    #Loop plotting both the co and cross for each frequency
    for i in range(len(key_list)):

        co = az_co[key_list[i]]
        cr = az_cr[key_list[i]]

        y1 = np.array([co.as_matrix(), cr.as_matrix()])

        ln.append(ax.plot(x1, y1.T, lw=1.6, alpha=0.9, label=key_list[i]))
        #,c=colours[i]))

    #Link up interactive legend
    plugins.connect(
        fig,
        plugins.InteractiveLegendPlugin(ln,
                                        key_list,
                                        alpha_unsel=0.05,
                                        alpha_over=1.5,
                                        start_visible=False))

    ###########################################################################
    # Figure Settings

    #Set axis parameters
    ax.grid(alpha=0.25)
    ax.set_ylim([-40, 0.5])
    ax.set_xlim([0, 360])
    x_tick_spacing = 20
    y_tick_spacing = 3
    ax.xaxis.set_major_locator(ticker.MultipleLocator(x_tick_spacing))
    ax.yaxis.set_major_locator(ticker.MultipleLocator(y_tick_spacing))

    #Set Plot title & axis titles
    ax.set_ylabel('dBi')
    ax.set_xlabel('Angle')

    ax.set_ylim([-40, 0.5])
    ax.set_xlim([0, 360])

    #ax.set_title(fname)
    ax.set_title(fname)

    #Save the figure as a html
    mpld3.save_html(fig, save_dir + fname + ".html")

    plt.close('all')
    plt.ion()
예제 #20
0
        'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
    ]

    #lines,places = runcovid()

    for i in range(len(places)):
        fv = gaussian_filter1d(lines[i], sigma=3)
        #fvfin = lines[i][:cleanuppt] + fv
        x, = ax.plot(fv, label=places[i])
        #x2, = ax.plot(np.arange(cleanuppt,len(lines[i])),fv[cleanuppt:],color=x.get_color(),linestyle=(0, (1, 1)))

    plt.ylim([0, 700000])
    plt.xlim([0, len(lines[0])])
    handles, labels = ax.get_legend_handles_labels()  # return lines and labels
    interactive_legend = plugins.InteractiveLegendPlugin(handles,
                                                         labels,
                                                         font_size=13,
                                                         start_visible=False)

    fig.subplots_adjust(right=0.7)

    #ax.axvline(x=cleanuppt,linewidth=0.4,color='r')
    ax.vlines(
        x=cleanuppt,
        ymin=0,
        ymax=700000,
        colors='r',
        linewidth=1,
        linestyles='dotted',
        label="Today",
    )
예제 #21
0
def generate_plot(tran, ambig, min_read, max_read, lite, ribocoverage,
                  organism, readscore, noisered, primetype, minfiles, nucseq,
                  user_hili_starts, user_hili_stops, uga_diff, file_paths_dict,
                  short_code, color_readlen_dist, background_col, uga_col,
                  uag_col, uaa_col, advanced, trips_annotation_location,
                  seqhili, seq_rules, title_size, subheading_size,
                  axis_label_size, marker_size, transcriptome,
                  trips_uploads_location, cds_marker_size, cds_marker_colour,
                  legend_size, ribo_linewidth, secondary_readscore, pcr,
                  mismatches, hili_start, hili_stop):
    if lite == "n" and ribocoverage == True:
        return "Error: Cannot display Ribo-Seq Coverage when 'Line Graph' is turned off"
    labels = [
        "Frame 1 profiles", "Frame 2 profiles", "Frame 3 profiles", "RNA",
        "Exon Junctions"
    ]
    start_visible = [True, True, True, True, False]
    if mismatches == True:
        labels.append("Mismatches A")
        labels.append("Mismatches T")
        labels.append("Mismatches G")
        labels.append("Mismatches C")
        start_visible.append(False)
        start_visible.append(False)
        start_visible.append(False)
        start_visible.append(False)
    start_visible.append(True)
    labels.append("CDS markers")
    #This is a list of booleans that decide if the interactive legends boxes are filled in or not.Needs to be same length as labels
    stop_codons = ["TAG", "TAA", "TGA"]
    frame_orfs = {1: [], 2: [], 3: []}
    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]
    }
    try:
        traninfo["stop_list"] = [int(x) for x in traninfo["stop_list"]]
    except:
        traninfo["stop_list"] = []

    try:
        traninfo["start_list"] = [int(x) for x in traninfo["start_list"]]
    except:
        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"]
    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": []}
    exon_junctions = traninfo["exon_junctions"]
    seq = traninfo["seq"].upper()
    for i in range(0, len(seq)):
        if seq[i:i + 3] in stop_codons:
            all_stops[seq[i:i + 3]].append(i + 1)
    # Error occurs if one of the frames is empty for any given start/stop, so we initialise with -5 as this won't be seen by user and will prevent the error
    start_stop_dict = {
        1: {
            "starts": [-5],
            "stops": {
                "TGA": [-5],
                "TAG": [-5],
                "TAA": [-5]
            }
        },
        2: {
            "starts": [-5],
            "stops": {
                "TGA": [-5],
                "TAG": [-5],
                "TAA": [-5]
            }
        },
        3: {
            "starts": [-5],
            "stops": {
                "TGA": [-5],
                "TAG": [-5],
                "TAA": [-5]
            }
        }
    }
    for start in all_starts:
        rem = ((start - 1) % 3) + 1
        start_stop_dict[rem]["starts"].append(start)
    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)
    #find all open reading frames
    for frame in [1, 2, 3]:
        for start in start_stop_dict[frame]["starts"]:
            best_stop_pos = 10000000
            for stop in start_stop_dict[frame]["stops"]:
                for stop_pos in start_stop_dict[frame]["stops"][stop]:
                    if stop_pos > start and stop_pos < best_stop_pos:
                        best_stop_pos = stop_pos
            if best_stop_pos != 10000000:
                frame_orfs[frame].append((start, best_stop_pos))
    all_rna_reads, rna_seqvar_dict = get_reads(ambig,
                                               min_read,
                                               max_read,
                                               tran,
                                               file_paths_dict,
                                               tranlen,
                                               True,
                                               organism,
                                               False,
                                               noisered,
                                               primetype,
                                               "rnaseq",
                                               readscore,
                                               pcr,
                                               get_mismatches=mismatches)
    all_subcodon_reads, ribo_seqvar_dict = get_reads(ambig,
                                                     min_read,
                                                     max_read,
                                                     tran,
                                                     file_paths_dict,
                                                     tranlen,
                                                     ribocoverage,
                                                     organism,
                                                     True,
                                                     noisered,
                                                     primetype,
                                                     "riboseq",
                                                     readscore,
                                                     secondary_readscore,
                                                     pcr,
                                                     get_mismatches=mismatches)
    seq_var_dict = merge_dicts(ribo_seqvar_dict, rna_seqvar_dict)
    try:
        rnamax = max(all_rna_reads.values())
    except:
        rnamax = 0
    try:
        subcodonmax = max(all_subcodon_reads.values())
    except:
        subcodonmax = 0

    y_max = max(1, rnamax, subcodonmax) * 1.1
    fig = plt.figure(figsize=(23, 12))
    ax_main = plt.subplot2grid((30, 1), (0, 0), rowspan=22)
    ax_main.spines['bottom'].set_visible(False)
    alt_seq_type_vars = []
    # Plot any alternative sequence types if there are any
    for seq_type in file_paths_dict:
        if seq_type != "riboseq" and seq_type != "rnaseq":
            if seq_rules[seq_type]["frame_breakdown"] == 1:
                frame_breakdown = True
            else:
                frame_breakdown = False
            alt_sequence_reads, empty_seqvar_dict = get_reads(
                ambig, min_read, max_read, tran, file_paths_dict, tranlen,
                True, organism, frame_breakdown, noisered, primetype, seq_type,
                readscore)

            if frame_breakdown == False:
                alt_seq_plot = ax_main.plot(alt_sequence_reads.keys(),
                                            alt_sequence_reads.values(),
                                            alpha=1,
                                            label=seq_type,
                                            zorder=2,
                                            color='lightblue',
                                            linewidth=2)
                labels.append(seq_type)
                start_visible.append(True)
                alt_seq_type_vars.append(alt_seq_plot)
            else:
                alt_frame_counts = {
                    0: collections.OrderedDict(),
                    1: collections.OrderedDict(),
                    2: collections.OrderedDict()
                }
                for key in alt_sequence_reads:
                    start = key
                    rem = start % 3
                    if rem == 1:  # frame 1
                        frame = 2
                    elif rem == 2:  # frame 2
                        frame = 0
                    elif rem == 0:  # frame 3
                        frame = 1
                    alt_frame_counts[frame][key] = alt_sequence_reads[key]
                frame0_altseqplot = ax_main.plot(alt_frame_counts[0].keys(),
                                                 alt_frame_counts[0].values(),
                                                 alpha=0.75,
                                                 label=seq_type + "frame0",
                                                 zorder=2,
                                                 color="#FF4A45",
                                                 linewidth=2)
                frame1_altseqplot = ax_main.plot(alt_frame_counts[1].keys(),
                                                 alt_frame_counts[1].values(),
                                                 alpha=0.75,
                                                 label=seq_type + "frame1",
                                                 zorder=2,
                                                 color="#64FC44",
                                                 linewidth=2)
                frame2_altseqplot = ax_main.plot(alt_frame_counts[2].keys(),
                                                 alt_frame_counts[2].values(),
                                                 alpha=0.75,
                                                 label=seq_type + "frame2*",
                                                 zorder=2,
                                                 color="#5687F9",
                                                 linewidth=2)
                labels.append(seq_type + "frame 1")
                labels.append(seq_type + "frame 2")
                labels.append(seq_type + "frame 3")
                start_visible.append(True)
                start_visible.append(True)
                start_visible.append(True)
                alt_seq_type_vars.append(frame0_altseqplot)
                alt_seq_type_vars.append(frame1_altseqplot)
                alt_seq_type_vars.append(frame2_altseqplot)
            if max(alt_sequence_reads.values()) > y_max:
                y_max = max(alt_sequence_reads.values())

    label = '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)
    ax_main.set_ylim(0, y_max)

    if lite == "n":
        rna_bars = ax_main.bar(all_rna_reads.keys(),
                               all_rna_reads.values(),
                               alpha=1,
                               label=labels,
                               zorder=1,
                               color='lightgray',
                               linewidth=0,
                               width=1)
    else:
        rna_bars = ax_main.plot(all_rna_reads.keys(),
                                all_rna_reads.values(),
                                alpha=1,
                                label=labels,
                                zorder=1,
                                color='#a7adb7',
                                linewidth=4)
    #if lite == "n":
    #	all_profiles = ax_main.bar(all_ribo_reads.keys(), all_ribo_reads.values(), alpha=0.01, label = labels, zorder=2, color='crimson', linewidth=0,width=1)
    #else:
    #	all_profiles = ax_main.plot(all_ribo_reads.keys(), all_ribo_reads.values(), alpha=0.01, label = labels, zorder=2, color='crimson', linewidth=1)
    cds_markers = ax_main.plot((cds_start + 1, cds_start + 1), (0, y_max),
                               color=cds_marker_colour,
                               linestyle='solid',
                               linewidth=cds_marker_size)
    cds_markers += ax_main.plot((cds_stop + 1, cds_stop + 1), (0, y_max),
                                color=cds_marker_colour,
                                linestyle='solid',
                                linewidth=cds_marker_size)
    ax_f1 = plt.subplot2grid((30, 1), (26, 0), rowspan=1, sharex=ax_main)
    ax_f1.set_axis_bgcolor(color_dict['frames'][0])
    ax_f2 = plt.subplot2grid((30, 1), (27, 0), rowspan=1, sharex=ax_main)
    ax_f2.set_axis_bgcolor(color_dict['frames'][1])
    ax_f3 = plt.subplot2grid((30, 1), (28, 0), rowspan=1, sharex=ax_main)
    ax_f3.set_axis_bgcolor(color_dict['frames'][2])
    ax_nucseq = plt.subplot2grid((30, 1), (29, 0), rowspan=1, sharex=ax_main)
    ax_nucseq.set_xlabel('Transcript: {} Length: {} nt'.format(tran, tranlen),
                         fontsize=subheading_size,
                         labelpad=10)

    #plot a dummy exon junction at postion -1, needed in cases there are no exon junctions, this wont be seen
    allexons = ax_main.plot((-1, -1), (0, 1),
                            alpha=0.01,
                            color='black',
                            linestyle='-.',
                            linewidth=2)
    for exon in exon_junctions:
        allexons += ax_main.plot((exon, exon), (0, y_max),
                                 alpha=0.01,
                                 color='black',
                                 linestyle='-.',
                                 linewidth=3)

    #dictionary for each frame in which the keys are the posistions and the values are the counts
    frame_counts = {
        0: collections.OrderedDict(),
        1: collections.OrderedDict(),
        2: collections.OrderedDict()
    }
    for key in all_subcodon_reads:
        rem = key % 3
        if rem == 1:  # frame 1
            frame = 2
        elif rem == 2:  # frame 2
            frame = 0
        elif rem == 0:  # frame 3
            frame = 1
        frame_counts[frame][key] = all_subcodon_reads[key]
        if lite == "n":
            frame_counts[frame][key + 1] = 0
            frame_counts[frame][key + 2] = 0

    if lite == "n":
        frame0subpro = ax_main.bar(frame_counts[0].keys(),
                                   frame_counts[0].values(),
                                   alpha=0.75,
                                   label=labels,
                                   zorder=2,
                                   color="#FF4A45",
                                   width=1,
                                   linewidth=0)
        frame1subpro = ax_main.bar(frame_counts[1].keys(),
                                   frame_counts[1].values(),
                                   alpha=0.75,
                                   label=labels,
                                   zorder=2,
                                   color="#64FC44",
                                   width=1,
                                   linewidth=0)
        frame2subpro = ax_main.bar(frame_counts[2].keys(),
                                   frame_counts[2].values(),
                                   alpha=0.75,
                                   label=labels,
                                   zorder=2,
                                   color="#5687F9",
                                   width=1,
                                   linewidth=0)
    else:
        frame0subpro = ax_main.plot(frame_counts[0].keys(),
                                    frame_counts[0].values(),
                                    alpha=0.75,
                                    label=labels,
                                    zorder=2,
                                    color="#FF4A45",
                                    linewidth=ribo_linewidth)
        frame1subpro = ax_main.plot(frame_counts[1].keys(),
                                    frame_counts[1].values(),
                                    alpha=0.75,
                                    label=labels,
                                    zorder=2,
                                    color="#64FC44",
                                    linewidth=ribo_linewidth)
        frame2subpro = ax_main.plot(frame_counts[2].keys(),
                                    frame_counts[2].values(),
                                    alpha=0.75,
                                    label=labels,
                                    zorder=2,
                                    color="#5687F9",
                                    linewidth=ribo_linewidth)
    if mismatches == True:
        a_mismatches = ax_main.plot(seq_var_dict["A"].keys(),
                                    seq_var_dict["A"].values(),
                                    alpha=0.01,
                                    label=labels,
                                    zorder=2,
                                    color="purple",
                                    linewidth=2)
        t_mismatches = ax_main.plot(seq_var_dict["T"].keys(),
                                    seq_var_dict["T"].values(),
                                    alpha=0.01,
                                    label=labels,
                                    zorder=2,
                                    color="yellow",
                                    linewidth=2)
        g_mismatches = ax_main.plot(seq_var_dict["G"].keys(),
                                    seq_var_dict["G"].values(),
                                    alpha=0.01,
                                    label=labels,
                                    zorder=2,
                                    color="orange",
                                    linewidth=2)
        c_mismatches = ax_main.plot(seq_var_dict["C"].keys(),
                                    seq_var_dict["C"].values(),
                                    alpha=0.01,
                                    label=labels,
                                    zorder=2,
                                    color="pink",
                                    linewidth=2)

    xy = 0
    if nucseq == True:
        ax_nucseq.set_axis_bgcolor(background_col)
        mrnaseq = seq.replace("T", "U")
        for char in mrnaseq:
            ax_nucseq.text((xy + 1) - 0.1,
                           0.2,
                           mrnaseq[xy],
                           fontsize=20,
                           color="grey")
            xy += 1

    # If the user passed a list of sequences to highlight, find and plot them here.
    if seqhili != ['']:
        near_cog_starts, signalhtml = get_user_defined_seqs(seq, seqhili)
        for slip in near_cog_starts[0]:
            try:
                hili_sequences += ax_f1.plot((slip, slip), (0, 0.5),
                                             alpha=1,
                                             label=labels,
                                             zorder=4,
                                             color='black',
                                             linewidth=5)
            except Exception as e:
                hili_sequences = ax_f1.plot((slip, slip), (0, 0.5),
                                            alpha=1,
                                            label=labels,
                                            zorder=4,
                                            color='black',
                                            linewidth=5)
        for slip in near_cog_starts[1]:
            try:
                hili_sequences += ax_f2.plot((slip, slip), (0, 0.5),
                                             alpha=1,
                                             label=labels,
                                             zorder=4,
                                             color='black',
                                             linewidth=5)
            except:
                hili_sequences = ax_f2.plot((slip, slip), (0, 0.5),
                                            alpha=1,
                                            label=labels,
                                            zorder=4,
                                            color='black',
                                            linewidth=5)
        for slip in near_cog_starts[2]:
            try:
                hili_sequences += ax_f3.plot((slip, slip), (0, 0.5),
                                             alpha=1,
                                             label=labels,
                                             zorder=4,
                                             color='black',
                                             linewidth=5)
            except:
                hili_sequences = ax_f3.plot((slip, slip), (0, 0.5),
                                            alpha=1,
                                            label=labels,
                                            zorder=4,
                                            color='black',
                                            linewidth=5)

        #Plot sequence identifiers which will create a popup telling user what the subsequence is (useful if they have passed multiple subsequences)
        frame1_subsequences = ax_f1.plot(near_cog_starts[0],
                                         [0.25] * len(near_cog_starts[0]),
                                         'o',
                                         color='b',
                                         mec='k',
                                         ms=12,
                                         mew=1,
                                         alpha=0,
                                         zorder=4)
        frame2_subsequences = ax_f2.plot(near_cog_starts[1],
                                         [0.25] * len(near_cog_starts[1]),
                                         'o',
                                         color='b',
                                         mec='k',
                                         ms=12,
                                         mew=1,
                                         alpha=0,
                                         zorder=4)
        frame3_subsequences = ax_f3.plot(near_cog_starts[2],
                                         [0.25] * len(near_cog_starts[2]),
                                         'o',
                                         color='b',
                                         mec='k',
                                         ms=12,
                                         mew=1,
                                         alpha=0,
                                         zorder=4)

        #Attach the labels to the subsequences plotted above
        signaltooltip1 = plugins.PointHTMLTooltip(frame1_subsequences[0],
                                                  signalhtml[0],
                                                  voffset=10,
                                                  hoffset=10,
                                                  css=point_tooltip_css)
        signaltooltip2 = plugins.PointHTMLTooltip(frame2_subsequences[0],
                                                  signalhtml[1],
                                                  voffset=10,
                                                  hoffset=10,
                                                  css=point_tooltip_css)
        signaltooltip3 = plugins.PointHTMLTooltip(frame3_subsequences[0],
                                                  signalhtml[2],
                                                  voffset=10,
                                                  hoffset=10,
                                                  css=point_tooltip_css)
    for axisname in (ax_f1, ax_f2, ax_f3, ax_nucseq):
        axisname.tick_params(top=False,
                             bottom=False,
                             labelleft=False,
                             labelright=False,
                             labelbottom=False)
    for label in ax_main.xaxis.get_majorticklabels():
        label.set_fontsize(36)
    for axis, frame in ((ax_f1, 1), (ax_f2, 2), (ax_f3, 3)):
        axis.set_xlim(1, tranlen)
        starts = [(item, 1) for item in start_stop_dict[frame]['starts']]
        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(starts, (0.5, 1), color="white", zorder=2)
        axis.broken_barh(uag_stops, (0, 1),
                         color=uag_col,
                         zorder=2,
                         linewidth=2)
        axis.broken_barh(uaa_stops, (0, 1),
                         color=uaa_col,
                         zorder=2,
                         linewidth=2)
        axis.broken_barh(uga_stops, (0, 1),
                         color=uga_col,
                         zorder=2,
                         linewidth=2)
        axis.set_ylim(0, 1)
        axis.set_ylabel('{}'.format(frame),
                        labelpad=10,
                        verticalalignment='center',
                        rotation="horizontal",
                        color="black")
    title_str = '{} ({})'.format(gene, short_code)
    plt.title(title_str, fontsize=title_size, y=36)
    line_collections = [
        frame0subpro, frame1subpro, frame2subpro, rna_bars, allexons
    ]

    if mismatches == True:
        line_collections.append(a_mismatches)
        line_collections.append(t_mismatches)
        line_collections.append(g_mismatches)
        line_collections.append(c_mismatches)
    line_collections.append(cds_markers)

    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("Highligted region")
        start_visible.append(True)
        line_collections.append(hili)

    for alt_plot in alt_seq_type_vars:
        line_collections.append(alt_plot)
    if 'hili_sequences' in locals():
        labels.append("Highligted sequences")
        start_visible.append(True)
        line_collections.append(hili_sequences)
    if user_hili_starts != [] and user_hili_stops != []:
        for i in range(0, len(user_hili_starts)):
            user_hili_start = int(user_hili_starts[i])
            user_hili_stop = int(user_hili_stops[i])
            try:
                hili += ax_main.fill_between([user_hili_start, user_hili_stop],
                                             [y_max, y_max],
                                             alpha=0.75,
                                             color="#fffbaf")
            except:
                hili = ax_main.fill_between([user_hili_start, user_hili_stop],
                                            [y_max, y_max],
                                            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

    ilp = plugins.InteractiveLegendPlugin(line_collections,
                                          labels,
                                          alpha_unsel=0,
                                          alpha_sel=0.85,
                                          start_visible=start_visible,
                                          fontsize=legend_size,
                                          xoffset=leg_offset)
    htmllabels = {1: [], 2: [], 3: []}
    all_start_points = {1: [], 2: [], 3: []}
    try:
        con_scores = SqliteDict("{0}homo_sapiens/score_dict.sqlite".format(
            trips_annotation_location))
    except Exception as e:
        print "Couldn't open conservation scores " + e
        con_scores = []
    for frame in [1, 2, 3]:
        orf_list = frame_orfs[frame]
        for tup in orf_list:
            orf_ribo = 0.0
            outframe_ribo = 0.0
            orf_rna = 0.0001
            start = tup[0]
            try:
                context = (seq[start - 7:start + 4].upper()).replace("T", "U")
            except Exception as e:
                con_score = "?"
            if len(context) != 11 or context[6:9] != "AUG":
                con_score = "?"
            else:
                try:
                    con_score = con_scores[context.upper()]
                except Exception as e:
                    con_score = "?"
            all_start_points[frame].append(start - 1)
            stop = tup[1]
            other_ribo = 0.0
            otherother_ribo = 0.0
            for i in range(start + 2, stop, 3):
                for subframe in [0, 1, 2]:
                    if i in frame_counts[subframe]:
                        orf_ribo += frame_counts[subframe][i]

            for i in range(start, stop, 3):
                for subframe in [0, 1, 2]:
                    if i in frame_counts[subframe]:
                        outframe_ribo += frame_counts[subframe][i]

            for i in range(start + 1, stop, 3):
                for subframe in [0, 1, 2]:
                    if i in frame_counts[subframe]:
                        outframe_ribo += frame_counts[subframe][i]

            for i in range(start, stop + 1):
                if i in all_rna_reads:
                    orf_rna += all_rna_reads[i]

            orf_te = float(orf_ribo) / float(orf_rna)
            orf_len = int(stop - start)

            try:
                in_out_ratio = orf_ribo / outframe_ribo
            except:
                in_out_ratio = "Null"

            datadict = {
                'inframe ribo': [orf_ribo],
                'outframe ribo': [outframe_ribo],
                'in/out ratio': [in_out_ratio],
                'rna': [orf_rna],
                'te': [orf_te],
                'len': [orf_len],
                'context_score': [str(con_score) + "/150"]
            }
            df = pd.DataFrame(datadict,
                              columns=([
                                  "inframe ribo", "outframe ribo",
                                  "in/out ratio", "rna", "te", "len",
                                  "context_score"
                              ]))
            label = df.ix[[0], :].T
            label.columns = ["Start pos: {}".format(start - 1)]
            htmllabels[frame].append(str(label.to_html()))

    points1 = ax_f1.plot(all_start_points[1],
                         [0.75] * len(all_start_points[1]),
                         'o',
                         color='b',
                         mec='k',
                         ms=13,
                         mew=1,
                         alpha=0,
                         zorder=3)
    points2 = ax_f2.plot(all_start_points[2],
                         [0.75] * len(all_start_points[2]),
                         'o',
                         color='b',
                         mec='k',
                         ms=13,
                         mew=1,
                         alpha=0,
                         zorder=3)
    points3 = ax_f3.plot(all_start_points[3],
                         [0.75] * len(all_start_points[3]),
                         'o',
                         color='b',
                         mec='k',
                         ms=13,
                         mew=1,
                         alpha=0,
                         zorder=3)

    tooltip1 = plugins.PointHTMLTooltip(points1[0],
                                        htmllabels[1],
                                        voffset=10,
                                        hoffset=10,
                                        css=point_tooltip_css)
    tooltip2 = plugins.PointHTMLTooltip(points2[0],
                                        htmllabels[2],
                                        voffset=10,
                                        hoffset=10,
                                        css=point_tooltip_css)
    tooltip3 = plugins.PointHTMLTooltip(points3[0],
                                        htmllabels[3],
                                        voffset=10,
                                        hoffset=10,
                                        css=point_tooltip_css)

    ax_f3.axes.get_yaxis().set_ticks([])
    ax_f2.axes.get_yaxis().set_ticks([])
    ax_f1.axes.get_yaxis().set_ticks([])

    returnstr = "Position,Sequence,Frame 1,Frame 2, Frame 3,RNA-Seq\n"
    for i in range(0, len(seq)):
        f1_count = 0
        f2_count = 0
        f3_count = 0
        rna_count = 0
        if i + 1 in frame_counts[0]:
            f1_count = frame_counts[0][i + 1]
        elif i + 1 in frame_counts[1]:
            f2_count = frame_counts[1][i + 1]
        elif i + 1 in frame_counts[2]:
            f3_count = frame_counts[2][i + 1]
        if i + 1 in all_rna_reads:
            rna_count = all_rna_reads[i + 1]
        returnstr += "{},{},{},{},{},{}\n".format(i + 1, seq[i], f1_count,
                                                  f2_count, f3_count,
                                                  rna_count)

    if seqhili == ['']:
        plugins.connect(fig, ilp, tooltip1, tooltip2, tooltip3,
                        plugins.TopToolbar(yoffset=100),
                        plugins.DownloadProfile(returnstr=returnstr),
                        plugins.DownloadPNG(returnstr=title_str))
    else:
        plugins.connect(fig, ilp, tooltip1, tooltip2, tooltip3, signaltooltip1,
                        signaltooltip2, signaltooltip3,
                        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(True, color="white", linewidth=30, linestyle="solid")
    #Without this style tag the markers sizes will appear correct on browser but be original size when downloaded via png
    graph = "<style>.mpld3-xaxis {{font-size: {0}px;}} .mpld3-yaxis {{font-size: {0}px;}}</style>".format(
        marker_size)
    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
예제 #22
0
    def nearest(self, points1=None, points2=None, file1=None, file2=None):
        try:
            if file1 is None:
                raise Exception("File now found")
            if points1 is not None and points2 is None:
                fig, ax = plt.subplots(figsize=(15, 10))

                p = ax.scatter(points1['x'],
                               points1['y'],
                               s=15,
                               color='b',
                               marker='o',
                               alpha=.3)
                labels = []

                for i in range(points1.shape[0]):
                    label = points1.ix[[i], :].T
                    label.columns = ['Row {0}'.format(0)]
                    labels.append(str(label.to_html()))

                # Setting axis label
                ax.set_xlabel('X')
                ax.set_ylabel('Y')
                ax.set_title('{} Nearest Neighbor'.format(file1), size=60)

                # for i, point in points1.iterrows():
                #     x = float(point.x + 0.050)
                #     y = float(point.y + 0.050)
                #     w = str(point.word)
                #
                #     ax.text(x, y, w, fontsize=11)
                # Plugins for tooltip
                tooltip = plugins.PointHTMLTooltip(p,
                                                   labels,
                                                   voffset=10,
                                                   hoffset=10,
                                                   css=css)

                plugins.connect(fig, tooltip)
                mpld3.show()

            if points1 is not None and points2 is not None:
                fig, ax = plt.subplots(figsize=(15, 10))

                p1 = ax.scatter(points1['x'],
                                points1['y'],
                                s=15,
                                color='b',
                                marker='o',
                                alpha=.3,
                                label=file1)
                p2 = ax.scatter(points2['x'],
                                points2['y'],
                                s=15,
                                color='r',
                                marker='o',
                                alpha=.3,
                                label=file2)

                labels = []
                for i in range(points1.shape[0]):
                    label = points1.ix[[i], :].T
                    label.columns = ['Row {0}'.format(0)]
                    labels.append(str(label.to_html()))

                labels2 = []
                for i in range(points2.shape[0]):
                    label = points2.ix[[i], :].T
                    label.columns = ['Row {0}'.format(0)]
                    labels2.append(str(label.to_html()))

                ax.set_xlabel('X')
                ax.set_ylabel('Y')
                ax.set_title('{} vs {} - Nearest Neighbor'.format(
                    file1, file2),
                             size=15)

                legend_labels = [file1, file2]
                plot_collection = [p1, p2]
                legend = plugins.InteractiveLegendPlugin(
                    plot_collection, legend_labels)
                tooltip = plugins.PointHTMLTooltip(p1,
                                                   labels,
                                                   voffset=10,
                                                   hoffset=10,
                                                   css=css)

                tooltip2 = plugins.PointHTMLTooltip(p2,
                                                    labels2,
                                                    voffset=10,
                                                    hoffset=10,
                                                    css=css)

                plugins.connect(fig, legend)
                plugins.connect(fig, tooltip)
                plugins.connect(fig, tooltip2)
                mpld3.show()

        except Exception as e:
            logger.getLogger().error(e)
            return

        # print(doc2vec.most_similar("security"))
        pass
예제 #23
0
# x = np.linspace(0, 10, 100)
# y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)
# y = y.cumsum(1)
#
#
# fig = plt.figure()
# ax1 = fig.add_subplot(2,1,1)
# ax2 = fig.add_subplot(2,1,2)
#
# labels = ["a", "b", "c", "d", "e"]
# l1 = ax1.plot(x, y.T, marker='x',lw=2, alpha=0.1)
# s1 = ax2.plot(x, y.T, 'o', ms=8, alpha=0.1)

ipdb.set_trace()

ilp = plugins.InteractiveLegendPlugin(zip(l1, s1), labels)

ilp.JAVASCRIPT = """
    mpld3.register_plugin("interactive_legend", InteractiveLegend);
    InteractiveLegend.prototype = Object.create(mpld3.Plugin.prototype);
    InteractiveLegend.prototype.constructor = InteractiveLegend;
    InteractiveLegend.prototype.requiredProps = ["element_ids", "labels"];
    InteractiveLegend.prototype.defaultProps = {"ax":null,
                                                "alpha_unsel":0.2,
                                                "alpha_over":1.0,
                                                "start_visible":true}
    function InteractiveLegend(fig, props){
        mpld3.Plugin.call(this, fig, props);
    };
    InteractiveLegend.prototype.draw = function(){
        var alpha_unsel = this.props.alpha_unsel;
예제 #24
0
    tmp_dict = {}
    for vals in v:
        if vals not in tmp_dict:
            tmp_dict[vals] = 0.0
        else:
            tmp_dict[vals] += 1.0
    if sys.argv[2] == 'p':  #for percentage graph
        for j, v in tmp_dict.iteritems():
            tmp_dict[j] /= count_dict[j]
    x, y = zip(*sorted(tmp_dict.items()))
    line_collections.append(ax.plot_date(x, y, '-'))
    #order titles to correspond with graph
    ordered_titles.append(label_titles[int(k)])

#make interactive legend
interactive_legend = plugins.InteractiveLegendPlugin(line_collections,
                                                     ordered_titles)

#set up graph
ax.set_ylabel("Number of Tweets Per Day")
ax.set_xlabel("Time")
if sys.argv[2] == 'p':
    name += "_percentage"
elif sys.argv[2] == 'n':
    name += "_counts"
ax.set_title("Topics Distribution During Hurricane Irma (Random Forest) (" +
             name + ")")
#connect matplotlib to d3 so it can be hosted in browser
mpld3.plugins.connect(fig, interactive_legend)
fig.set_size_inches(26.5, 12.5)
html_string = mpld3.fig_to_html(fig)
#mpld3.show() #uncomment this to show graph locally
def _create_interactive_network_graph(json_data):
    """
    Creates a network graph with draggable nodes Interface
    colors represent utilization. Tooltips show node name
    and interface info.

    This graph cannot show multiple parallel circuits
    between the same nodes.

    lightgray = circuit/node is down

    Interface utilization coloring (colors from named_colors.py):
    0-25%   = blue
    25-50%  = green
    50-75%  = yellow
    75-90%  = orangered
    90-100% = red
    > 100%  = darkviolet
    """
    # Prepare the plot
    fig, ax = plt.subplots(figsize=(15, 15))

    ax.set_facecolor('tan')

    # Plot the lines on ax
    links_iterator = (link for link in json_data['links'])
    for link in links_iterator:

        # Edge x,y coordinates
        x_coordinates = []
        y_coordinates = []

        # # Get the utilization
        # util = None
        # if 'failed' in link['utilization']:
        #     util_split = link['utilization'].split('%')
        #     util = util_split[0].split()[-1]
        # else:
        #     util = link['utilization'].split('%')[0]

        # Node plot stuff

        node_tool_tip_labels = []

        # Get the source node
        src = link['source']

        # Get the target node
        target = link['target']

        # Get source node position and label
        for node in json_data['nodes']:
            if node['name'] == src:
                source_y, source_x = node['pos']
                node_tool_tip_labels.append(node['name'])
                break

        # Get target node position and label
        for node in json_data['nodes']:
            if node['name'] == target:
                target_y, target_x = node['pos']
                if node['failed'] is False:
                    node_tool_tip_labels.append(node['name'])
                else:
                    node_tool_tip_labels.append((node['name'] + ' (failed)'))
                break

        # Get the coordinates of the nodes for the line
        src_x = None
        src_y = None
        dest_x = None
        dest_y = None

        node_iterator = (node for node in json_data['nodes'])
        for node in node_iterator:
            if node['name'] == link['source']:
                src_y, src_x = node['pos']
            if node['name'] == link['target']:
                dest_y, dest_x = node['pos']

            if None not in (src_x, src_y, dest_x,
                            dest_y != [None, None, None, None]):
                break

        x_coordinates = [src_x, dest_x]
        y_coordinates = [src_y, dest_y]

        # Plot only the relevant nodes to the line
        nodes_on_plot = ax.plot(x_coordinates,
                                y_coordinates,
                                'o',
                                color='r',
                                alpha=0.7,
                                markersize=15,
                                markeredgewidth=1)

        # Plot the line
        line = ax.plot(x_coordinates,
                       y_coordinates,
                       color=link['color'],
                       linewidth=5.0,
                       label=link['color'])

        # Need to normalize some stuff for the line labels for the line tooltips
        link_source = _find_link_source_node(link)

        line_labels = [
            'from-' + link['target'] + '-to-' + link_source + '; \
                            utilization = ' + link['utilization']
        ]

        # Add the tooltip labels to lines.
        # Notice we use the *LineLabelTooltip
        mpld3.plugins.connect(
            fig, mpld3.plugins.LineLabelTooltip(line[0], label=line_labels))

        # Drag plugin for nodes
        mpld3.plugins.connect(fig, LinkedDragPlugin(nodes_on_plot[0], line[0]))

        # Add the tooltip node labels
        # Notice we use the *PointLabelTooltip
        mpld3.plugins.connect(
            fig,
            mpld3.plugins.PointLabelTooltip(nodes_on_plot[0],
                                            labels=node_tool_tip_labels))

    # Plot the midpoint node labels
    for node in (node for node in json_data['nodes']
                 if 'midpoint' in node['id']):
        x, y = node['pos'][1], node['pos'][0]
        text = node['name']
        ax.text(x, y, text, fontsize=10, color='k', fontname='monospace')

    # Plot the router node labels
    for node in (node for node in json_data['nodes']
                 if 'midpoint' not in node['id']):

        x, y = node['pos'][1], node['pos'][0]
        if node['failed'] is False:
            text = node['name']
        else:
            text = node['name'] + ' (failed)'
        ax.text(x, y, text, fontsize=15, color='k', fontname='monospace')

    # Create interactive legend
    line_collections, line_group_labels = _create_legend_line_collections(ax)

    interactive_legend = plugins.InteractiveLegendPlugin(line_collections,
                                                         line_group_labels,
                                                         alpha_unsel=0.2,
                                                         alpha_over=1.8,
                                                         start_visible=True)
    fig.subplots_adjust(right=0.85)
    plugins.connect(fig, interactive_legend)

    time_now = str(time.asctime())
    ax.set_title("Network Graph:" + time_now, size=15)

    plot_thread = threading.Thread(target=_run_plot)
    plot_thread.start()
예제 #26
0
# import matplotlib.pyplot as plt, mpld3
# from mpld3 import plugins
# fig, ax = plt.subplots(1,1)
# xx = yy = range(10)
# scat = ax.scatter(xx, range(10))
# targets = map(lambda (x, y): "<marquee>It works!<br><h1>{}, {}</h1></marquee>".format(x, y),
#               zip(xx, yy))
# labels = map(lambda (x, y): "{}, {}".format(x,y), zip(xx, yy))
# from mpld3.plugins import PointClickableHTMLTooltip
# plugins.connect(fig, PointClickableHTMLTooltip(scat, labels=labels, targets=targets))

# mpld3.save_html(fig, "mpld3-newtab.html")

import matplotlib.pyplot as plt, mpld3
import numpy as np
from mpld3 import fig_to_html, plugins
N_paths = 5
N_steps = 100
x = np.linspace(0, 10, 100)
y = 0.1 * (np.random.random((N_paths, N_steps)) - 0.5)
y = y.cumsum(1)
fig, ax = plt.subplots()
labels = ["a", "b", "c", "d", "e"]
line_collections = ax.plot(x, y.T, lw=4, alpha=0.6)
interactive_legend = plugins.InteractiveLegendPlugin(line_collections, labels, alpha_unsel=0.2, alpha_over=1.5, start_visible=True)
plugins.connect(fig, interactive_legend)

mpld3.save_html(fig, "mpld3-filter.html")
예제 #27
0
N = 100
df = pd.DataFrame((.1 * (np.random.random((N, 5)) - .5)).cumsum(0),
                  columns=['a', 'b', 'c', 'd', 'e'],)

# plot line + confidence interval
fig, ax = plt.subplots()
ax.grid(True, alpha=0.3)

for key, val in df.iteritems():
    l, = ax.plot(val.index, val.values, label=key)
    ax.fill_between(val.index,
                    val.values * .5, val.values * 1.5,
                    color=l.get_color(), alpha=.4)

# define interactive legend

handles, labels = ax.get_legend_handles_labels() # return lines and labels
interactive_legend = plugins.InteractiveLegendPlugin(zip(handles,
                                                         ax.collections),
                                                     labels,
                                                     alpha_unsel=0.5,
                                                     alpha_over=1.5,
                                                     start_visible=True)
plugins.connect(fig, interactive_legend)

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

# mpld3.show()
예제 #28
0
def line_per_cmd(args):
    import matplotlib
    if args.output is not None:
        matplotlib.use('Agg') # We need this to make matplotlib work on systems with no DISPLAY
    import matplotlib.pyplot as plt
    import pylab

    matplotlib.rcParams.update({'figure.subplot.right': args.plot_size})
    matplotlib.rcParams.update({'figure.subplot.bottom': args.plot_size})
    plt.style.use(args.pyplot_style)

    # First we create `means` which map a command label and date to a pair of mean and standard deviation
    # e.g. means['Bean']['2017-05-02 13:29:34.87'] = (0.1289, 0.0006)
    (means, cmd_labels, meta_keys) = util.means_series_map(args)

    # Let's write a plot line for each command label
    fig, ax = plt.subplots()
    lines = []
    for cmd_label in cmd_labels:
        x, y, err = ([], [], [])
        for i in range(len(meta_keys)):
            (mean, std) = means[cmd_label].get(meta_keys[i], (0, 0))
            x.append(i)
            y.append(mean)
            err.append(std)
        lines.append(ax.errorbar(x, y, fmt='-o', yerr=err))

    if args.ymin is not None:
        plt.ylim(ymin=float(args.ymin))
    if args.ymax is not None:
        plt.ylim(ymax=float(args.ymax))
    if args.title is not None:
        plt.title(util.texsafe(args.title))

    # Add some text for labels, title and axes ticks
    ax.set_xticks(range(len(meta_keys)))
    ax.set_xticklabels(meta_keys, rotation=args.xticklabel_rotation)

    if not args.no_legend and not args.mpld3: # mpld3 comes with its own legends
        ax.legend(lines, cmd_labels, loc='best', fancybox=True, shadow=True)

    if args.mpld3:
        import mpld3
        from mpld3 import plugins
        interactive_legend = plugins.InteractiveLegendPlugin(lines, cmd_labels)
        plugins.connect(fig, interactive_legend)

    if args.output is not None:
        fname = args.output.name
        fformat = fname.split('.')[-1]
        print ("Writing file '%s' using format '%s'." % (fname, fformat))
        if args.mpld3:
            if fformat == "html":
                mpld3.save_html(fig, args.output)
            elif fformat == "json":
                mpld3.save_json(fig, args.output)
            else:
                raise ValueError("--mpld3: The output must be either `html` or `json`")
        else:
            pylab.savefig(args.output, format=fformat)
    else:
        if args.mpld3:
            mpld3.show()
        else:
            pylab.show()