def add_tooltip_to_plt(tooltip_values=None, tooltip_names=None, tooltip_title=None, tooltip_style=None, show=True): """Add tooltip to plt. :param tooltip_values: list of lists, where every sub-list represents the values that we want to show as tooltip :param tooltip_names: list, where every ith element if the label name of the ith value in every sub-list in tooltip_values :param tooltip_title: string to put on top of every tooltip as the title :param tooltip_style: style for html, including but not limited to table css: table, th, td Requires style with markers to show tooltips (for example, style "-" will not work, but ".-" will). @use add_tooltip_to_plt([[0,0], [1,1], [0,1], [1,0]], tooltip_names=["y", "x"], tooltip_title="Legend") """ if tooltip_style is None: tooltip_style = """ table { border-collapse: collapse; border: 1px solid #bbb;} th { border-bottom: 1px solid #bbb; } td { border-bottom: 1px solid #bbb; } table, th, td { font-family: sans-serif; padding: 4; opacity: 0.9; background-color: #fff; } """ tooltip_labels = None if tooltip_values is not None: tooltip_labels = [] for values in tooltip_values: tooltip_labels.append( html_table_from_lists(list_left=tooltip_names, list_right=values, title=tooltip_title)) if tooltip_labels is not None: points = plt.gca().lines fig = plt.gcf() tooltip = plugins.PointHTMLTooltip(points[0], tooltip_labels, voffset=10, hoffset=10, css=tooltip_style) plugins.connect(fig, tooltip) if show: mpld3.show()
def plot_d3js(): # Define some CSS to control our custom labels css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ fig, ax = plt.subplots() ax.grid(True, alpha=0.3) labels = suburb_list x = coords[:, 0] y = coords[:, 1] points = ax.plot(x, y, 'o', color='b', mec='k', ms=15, mew=1, alpha=.6) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('Ethnicity', size=20) tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) mpld3.show()
def agencyplot(agency): dataframe = d[agency] awards = list(dataframe["AwardedValue"]) id_list = [] for i in range(len(dataframe)): _id = dataframe.iloc[i]["_id"] id_list.append(_id) df = pd.DataFrame(index=range(len(id_list))) df['ID'] = [id_list[i] for i in range(len(id_list))] df['Awarded Value'] = [awards[i] for i in range(len(id_list))] labels = [] for i in range(len(id_list)): label = df.iloc[[i], :].T label = label label.columns = ['Project {0}'.format(i)] labels.append(str(label.to_html())) fig, ax = plt.subplots() fig.set_size_inches(9, 5) ax.set_title(agency.upper(), fontsize=18) ax.set_xlabel('Individual Projects', fontsize=15) points = ax.plot(awards, 'or', alpha=1, markersize=10, markeredgewidth=1, color='orange') tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10) plugins.connect(fig, DragPlugin(points[0])) plugins.connect(fig, tooltip) json01 = json.dumps(mpld3.fig_to_dict(fig)) return (json01)
def __call__(self, plot, view): if not self._applies(plot, view): return fig = plot.handles['fig'] ax = plot.handles['axis'] valid_opts = ['cmap'] opts = {k:v for k,v, in Store.lookup_options(view, 'style').options.items() if k in valid_opts} data = view.data if isinstance(view, HeatMap): data = np.ma.array(data, mask=np.isnan(data)) cmap = copy.copy(plt.cm.get_cmap(opts.get('cmap', 'gray'))) cmap.set_bad('w', 1.) for ann in plot.handles['annotations'].values(): ann.set_visible(False) df = view.dframe(True).fillna(0) df = df.sort([d.name for d in view.dimensions()[1:2]])[::-1] else: df = view.dframe().sort([d.name for d in view.dimensions()[0:2]][::-1])[::-1] # Generate color mesh to label each point l, b, r, t = view.extents rows, cols = view.data.shape cmin, cmax = view.range(2) x, y = np.meshgrid(np.linspace(l, r, cols+1), np.linspace(b, t, rows+1)) plot.handles['im'].set_visible(False) mesh = ax.pcolormesh(x, y, data[::-1,:], vmin=cmin, vmax=cmax, **opts) ax.invert_yaxis() # Doesn't work uninverted df.index = range(len(df)) labels = [] for i in range(len(df)): label = df.ix[[i], :].T label.columns = [view.label] labels.append(str(label.to_html(header=len(view.label)>0))) tooltip = plugins.PointHTMLTooltip(mesh, labels[::-1], hoffset=self.hoffset, voffset=self.voffset, css=self.css) plugins.connect(fig, tooltip)
def line_plots(agency): fig, ax = plt.subplots() fig.set_size_inches(7, 8.5) placeholder = list(value_retrieval_all(agency).keys()) years = [] for i in placeholder: years.append(str(i)) y = list(value_retrieval_all(agency).values()) lines = ax.plot(years, y, marker='o', markersize=15) # mpld3.plugins.connect(fig, mpld3.plugins.PointHTMLTooltip(lines[0],labels=y, css = css)) ax.grid(True, alpha=0.3) df = pd.DataFrame(index=range(len(y))) df['Amount Spent'] = y labels = [] for i in range(len(y)): label = df.iloc[[i], :].T label.columns = [years[i].format(i)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) ax.set_title('Total Expenditure by Year', size=20) tooltip = plugins.PointHTMLTooltip(lines[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) json01 = json.dumps(mpld3.fig_to_dict(fig)) return (json01)
def generateChart(companyName, data): ''' Use the data to calculate moving average and plot chart using moving average as label :param companyName: Name of the company :param data: stock prices :return: return chart in html ''' fig, ax = plt.subplots(figsize=(8, 5)) stockPrices = data.values movingAverageValues = Model.calcMovingAvg(stockPrices, 10) movingAverage = Model.crtLabelMovingAverage(movingAverageValues, stockPrices) stockPricePoints = plt.plot( data.index, stockPrices, marker='o', ls='-', ms=5, markerfacecolor='None', markeredgecolor='None', ) ax.set_xlabel('year') ax.set_ylabel('Open prices') ax.set_title('Stock chart of ' + companyName, size=20) tooltip = plugins.PointHTMLTooltip(stockPricePoints[0], movingAverage, voffset=10, hoffset=10) plugins.connect(fig, tooltip) return mpld3.fig_to_html(fig)
def main(): fig, ax = plt.subplots() N = 50 df = pd.DataFrame(index=range(N)) df['x'] = np.random.randn(N) df['y'] = np.random.randn(N) df['z'] = np.random.randn(N) labels = [] for i in range(N): label = df.ix[[i], :].T label.columns = ['Row {0}'.format(i)] labels.append(str(label.to_html( ))) # .to_html() is unicode, so make leading 'u' go away with str() points = ax.plot(df.x, df.y, 'o', color='k', mec='w', ms=15, mew=1, alpha=.9) ax.set_xlabel('x') ax.set_ylabel('y') tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) return fig
def plot_tree_mpld3(nodes: List[str], edgelist: List[Tuple[str]], svgs: List[Any], extra_string: Optional[List] = None, figsize: Optional[Tuple[int, int]] = None, layout: str = 'rt', figure_savefile: Optional[str] = None): G = Graph() G.add_vertices(nodes) G.add_edges(edgelist) if extra_string: lookup_str = "encoding='iso-8859-1'?>\n" # in the svg style = 'style="background-color:#ffffff"' for i, (exs, svg) in enumerate(zip(extra_string, svgs)): str_pos = svg.find(lookup_str) + len(lookup_str) _insert = ''.join( [f'<div {style}>{x}</div>' for x in exs.split('\n')]) svgs[i] = svg[:str_pos] + _insert + svg[str_pos:] x_edges, y_edges, x_nodes, y_nodes = compute_layout(G, layout) fig, ax = plt.subplots(figsize=figsize) for x, y in zip(x_edges, y_edges): ax.plot(x, y, c='gray', linestyle='--') points = ax.scatter(x_nodes, y_nodes, s=150, c='gray') tooltip = plugins.PointHTMLTooltip(points, svgs) plugins.connect(fig, tooltip) if figure_savefile is not None: with open(figure_savefile, 'w') as f: save_html(fig, f) display(fig)
def indexplot(company): x = list(index[company].keys()) y = list(index[company].values()) label = [''*len(index[company].keys())] df = pd.DataFrame(index=range(len(x))) df['Project Title'] = [x[i] for i in range(len(x))] df['Bid Index Difference'] = [y[i] for i in range(len(y))] labels = [] for i in range(len(x)): label = df.iloc[[i], :].T label.columns = ['Project {0}'.format(i)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) fig2, ax2 = plt.subplots() fig2.set_size_inches(5, 4) ax2.set_title(company, fontsize=18) ax2.set_ylabel('Bid Index Difference', fontsize = 15) ax2.set_xlabel('Individual Projects', fontsize = 15) points2 = ax2.plot(y, 'or', alpha=1,markersize=7.5, markeredgewidth=1, color = 'orange') tooltip = plugins.PointHTMLTooltip(points2[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig2, tooltip) plugins.connect(fig2, DragPlugin(points2[0])) json02 = json.dumps(mpld3.fig_to_dict(fig2)) return (json02)
'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()
def plots(x, y): bg = '#0B0C0E' tx = '#FFFFFF' # wtx = {'labelsize':20, 'colors':'blue'} dates, pred = x, y fig, ax = plt.subplots(figsize=(8, 8)) fig.patch.set_facecolor(bg) ax.set_facecolor(bg) # for side in ['top', 'bottom', 'left', 'right']: # ax.spines[side].set_color(tx) # ax.xaxis.label.set_color(tx) # ax.yaxis.label.set_color(tx) years = [int(day[-4:]) + int(day[:day.index("/")]) / 12 for day in dates] print(dates) # ax.set_xlim(min(years), max(years)) # Define some CSS to control our custom labels css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ # 1-2001 to 9-2020 months ax.grid(True, alpha=0.3) N = 236 colors = [ 'r', 'c', 'm', 'y', 'w', 'dodgerblue', '#5c6370', 'chocolate', 'purple' ] for k, v in pred.items(): points = ax.plot(years, v[0], '.-', color=colors[v[1] - 1], mec='k', ms=15, mew=1, alpha=.6) labels = [ get_html(date(dates[i]), '${:,.2f}'.format(v[0][i]), v[2]) for i in range(N) ] tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) ax.set_xlabel('Year', size=15) ax.set_ylabel('Predicted Change', size=15) ax.set_title('Predicted Change in GDP', size=20) return fig # def date(day): # bg = '#0B0C0E' # tx = '#FFFFFF' # month = {1:"Jan", 2:"Feb", 3:"Mar", 4:"Apr", 5:"May", 6:"Jun", # 7:"Jul", 8:"Aug", 9:"Sep", 10:"Oct", 11:"Nov", 12: "Dec"} # first = day.index("/") # return month[int(day[:first])] + " " + str(day[-4:]) # def plots(x, y): # bg = '#0B0C0E' # # bg = '#5c6370' # tx = '#FFFFFF' # dates, pred = x, y # fig, ax = plt.subplots(figsize=(8, 8)) # fig.patch.set_facecolor(bg) # ax.set_facecolor(bg) # for side in ['top', 'bottom', 'left', 'right']: # ax.spines[side].set_color(tx) # # ax.xaxis.label.set_color(tx) # # ax.yaxis.label.set_color(tx) # ax.tick_params(axis='x', color=tx) # ax.tick_params(axis='y', color=tx) # # Define some CSS to control our custom labels # css = """ # table # { # border-collapse: collapse; # } # th # { # color: #ffffff; # background-color: #000000; # } # td # { # background-color: #cccccc; # } # table, th, td # { # font-family:Arial, Helvetica, sans-serif; # border: 1px solid black; # text-align: right; # } # """ # # 1-2001 to 9-2020 months # ax.grid(True, alpha=.01) # N = 236 # # df = pd.DataFrame(index=range(N)) # # df['x'] = dates.array # # df['y'] = pred.array # # labels = [] # # for i in range(N): # # label = data.loc[[i], :].T # # # label = "waka waka" # # # label[i].DATE = date(label[i].DATE) # # label[i].Predict = '${:,.2f}'.format(label[i].Predict) # # label.columns = [str(date(label[i].DATE))] # # labels.append(str(label.to_html())) # # points = ax.plot(dates, pred, '.-', color='b', # # mec='k', ms=15, mew=1, alpha=.6) # colors = ['r','c','m','y','w', 'dodgerblue', '#5c6370', 'chocolate','purple'] # i = 0 # for key in y: # points = ax.plot(dates, y[key][0], '.-', color=colors[y[key][1] - 1], # mec='k', mew=1, alpha=.6) # i += 1 # ax.set_xlabel('Date', size=15, color='#8f96a3') # ax.set_ylabel('Predicted Change', size=15, color='#8f96a3') # ax.set_title('HTML tooltips', size=20, color='#8f96a3') # ax.spines['bottom'].set_color("red") # # ax.spines['top'].set_color("#8f96a30F") # # ax.spines['right'].set_color("#8f96a30F") # ax.spines['left'].set_color("red") # ax.tick_params(axis='x', color='#8f96a3') # ax.tick_params(axis='y', color='#8f96a3') # ax.yaxis.label.set_color('#8f96a3') # ax.xaxis.label.set_color('#8f96a3') # # ax.grid(False) # # tooltip = plugins.PointHTMLTooltip(points[0], labels, # # voffset=10, hoffset=10, css=css) # # plugins.connect(fig, tooltip) # # mpld3.show() # return fig
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
def create_neighbors_fig_mpld3(hostname, neighbors, ip_addr, port_dict): class ClickInfo(plugins.PluginBase): """Plugin for getting info on click""" JAVASCRIPT = """ mpld3.register_plugin("clickinfo", ClickInfo); ClickInfo.prototype = Object.create(mpld3.Plugin.prototype); ClickInfo.prototype.constructor = ClickInfo; ClickInfo.prototype.requiredProps = ["id", "urls"]; function ClickInfo(fig, props){ mpld3.Plugin.call(this, fig, props); }; ClickInfo.prototype.draw = function(){ var obj = mpld3.get_element(this.props.id); urls = this.props.urls; obj.elements().on("mousedown", function(d, i){window.location.href = urls[i];}); } """ def __init__(self, points, urls): self.dict_ = { "type": "clickinfo", "id": utils.get_id(points), "urls": urls } node_list1 = [hostname] node_list2 = neighbors[:] nodes = neighbors[:] nodes.append(hostname) children = neighbors g_children = [] for neighbor in neighbors: result = get_neighbors_list(neighbor, hostname) for n in result: nodes.append(n) g_children.append(n) port_list = get_port_num_list(n, neighbor) port_dict[n] = port_list port_dict['basehost_' + n] = neighbor G = nx.Graph() G.add_nodes_from(nodes) colors = [] added_nodes = [] for neighbor in neighbors: G.add_edge(hostname, neighbor) added_nodes.append(neighbor) for neighbor in neighbors: result = get_neighbors_list(neighbor, hostname) for sub_neighbor in result: G.add_edge(neighbor, sub_neighbor) pos = hierarchy_pos(G, hostname, list(set(children)), list(set(g_children))) host_addr = {} host_addr_list = [] for node in G.nodes(): host_addr_list.append(get_ip_addr2(node)) if node == hostname: colors.append('r') else: colors.append('#8fc5ff') fig, ax = plt.subplots(subplot_kw=dict(axisbg='#EEEEEE')) ax.axis('off') ax.xaxis.set_major_formatter(plt.NullFormatter()) ax.yaxis.set_major_formatter(plt.NullFormatter()) scatter = nx.draw_networkx_nodes(G, pos=pos, node_size=3000, node_shape='s', node_color=colors, ax=ax) line = nx.draw_networkx_edges(G, pos=pos, width=7, ax=ax) nx.draw_networkx_labels(G, pos=pos, font_size=16, font_color="#000000", ax=ax) plt.xticks([]) plt.yticks([]) plt.axis('off') labels = [] for key in G.edges(): table = '<table class="table table-bordered">' table += '<tr class="active"><th>' + key[0] + '</th><th>' + key[ 1] + '</th></tr>' result = get_port_pair([key[1], key[0]]) #result = get_port_pair([key[0], key[1]]) for port in result: table += '<tr class="active"><td>' + str( port[0]) + '</td><td>' + str(port[1]) + '</td></tr>' table += "</table>" labels.append(table) tooltip = plugins.PointHTMLTooltip(line, labels=labels) mpld3.plugins.connect(fig, tooltip) mpld3.plugins.connect(fig, ClickInfo(scatter, host_addr_list)) mpld3.save_html(fig, config.PYEZ_DEV_INFO_DIR + 'fig/' + ip_addr, d3_url='./static/d3.v3.min.js', mpld3_url='./static/mpld3.v0.2.js')
def mplimage1(request): # Define some CSS to control our custom labels css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Terminal, Arial, Helvetica, sans-serif; border: 2px solid black; text-align: right; } """ np.random.seed(0) P = np.random.random(size=9) P[0] = 6000 P[1] = 3313 P[2] = 6125 P[3] = 3300 P[4] = 5445 P[5] = 5500 P[6] = 6151 P[7] = 5700 P[8] = 7000 # FILL IN COLOR TABLE BASED ON TEMPERATURE N = len(P) CL = ["" for x in range(N)] i = 0 while i < N: if P[i] > 7000: CL[i] = 'violet' elif P[i] > 6500: CL[i] = 'magenta' elif P[i] > 6000: CL[i] = 'springgreen' elif P[i] > 5500: CL[i] = 'greenyellow' elif P[i] > 5000: CL[i] = 'yellow' elif P[i] > 4000: CL[i] = 'coral' elif P[i] > 3000: CL[i] = 'red' else: CL[i] = 'black' i = i + 1 A = np.random.random(size=9) A[0] = 2000 A[1] = 4168 A[2] = 50 A[3] = 3000 A[4] = 3200 A[5] = 2900 A[6] = 1186 A[7] = 565 A[8] = 2440 T = ["%.2f" % x for x in A] T[0] = 'Cepheus - delta Cephei' T[1] = 'Lyra - R Lyrae' T[2] = 'Lyra - RR Lyrae' T[3] = 'Lepus - RX Leponis' T[4] = 'Dorado - beta Doradus' T[5] = 'Gemini - zeta Geminorum' T[6] = 'Auriga - RT Aurigae' T[7] = 'Pavo - x Pavonis' T[8] = 'Polaris - alpha Ursa Minoris' # The period of variables C = np.random.random(size=9) C[0] = 5.3 C[1] = 46 C[2] = 0.57 C[3] = 60 C[4] = 9.84 C[5] = 10.15 C[6] = 3.73 C[7] = 9.09 C[8] = 3.97 # The magnitude variation D = np.random.random(size=9) D[0] = 1.1 D[1] = 1.1 D[2] = 1 D[3] = 2.4 D[4] = 0.6 D[5] = 0.6 D[6] = 0.8 D[7] = 0.9 D[8] = 0.3 # The magnitude mean value E = np.random.random(size=9) E[0] = 3.5 E[1] = 3.9 E[2] = 7.06 E[3] = 5 E[4] = 3.46 E[5] = 3.62 E[6] = 5 E[7] = 3.91 E[8] = 1.86 fig, ax = plt.subplots() ########### INSTABILITY AREA ########### # scatter periods and amplitudes ######## END OF INSTABILITY AREA ####### ax.grid(True, alpha=0.3) star_array = [ ['Sirius B', 25200, 0.056], ['Procyon B', 7740, 0.00049], ['LP 145-141', 8500, 0.0005], ['Van Maanen 2', 6220, 0.00017], ] #N = len(star_array) df = pd.DataFrame(index=range(N), columns=[ 'Name', 'Temperature', 'Luminocity', 'Period', 'Magnitude Variation', 'Color' ]) xy = zip(T, P, A, C, D, CL) i = 0 while (i < N): #df.loc[i] = [star_array[i][0],star_array[i][1],star_array[i][2]] df.loc[i] = [ xy[i][0], xy[i][1], xy[i][2], xy[i][3], xy[i][4], xy[i][5] ] i = i + 1 labels = [] for c in range(N): label = df.ix[[c], :].T label.columns = ['Star {0}'.format(c)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) #df.at[0,'A'] // points = ax.plot(df.Temperature, df.Luminocity, 'o', color=( 1.0, 1.0, 1.0 ), mec='k', ms=15, mew=1, alpha=.6) points = ax.plot(df.Temperature, df.Luminocity, '*', color='red', mec='k', ms=20, mew=1, alpha=.6) #for j in range(N): # points = ax.plot(5000 + 100*j, 100 + 10*j, '*', color=(j/15,j/25,j/100), mec='k', ms=20, mew=1, alpha=.6) #ax.set_xscale('log') ax.set_xlim(3000, 7100) ax.invert_xaxis() ax.set_xlabel('Temperature', size=15) #ax.set_yscale('log') ax.set_ylabel('Luminocity', size=15) ax.set_title('', size=0) ax.set_ylim(3 - 1000, 5000) #ax.text(9000, 0.01, 'Instability Strip', style='italic', bbox={'facecolor':'red', 'alpha':0.5, 'pad':10}) ax.set_title( "HR Diagram - Instability Strip (Hover over points to see characteristics)" ) tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) g = mpld3.fig_to_html(fig) mpld3.save_html(fig, "hr/templates/hr_full.html") return django.http.HttpResponse(g)
def ScatmPlot(c1, c2, c3, xx, yy, yz, Spec, Spec_inc, line, SysCon2_inc, interact, titl): #Spec is a set of specific points to focus on #Spec inc is whether or not to include those points #line variable indicates whether or not to have a line #SysCon2_inc variable indicates whether or not to include trials where SysCon2 = 0. If #SysCon2_inc = 1, then they will be included, if not, then they will be filtered out. #interact determines if plot will be an interactive plot using the mpld3 library plt.rcParams["font.weight"] = "bold" plt.rcParams["axes.labelweight"] = "bold" plt.rcParams['axes.titleweight'] = "bold" fig = plt.figure() #create individual dataframes for each category df1 = pd.DataFrame(index=c1.index) df1['x'] = c1[xx] df1['y'] = c1[yy] / (c1[yz]) df1['Category'] = 'SW-WW' df1['SysCon2'] = c1['SysCon2'] df1['Membrane_ID'] = c1.index df1.index = range(30) df2 = pd.DataFrame(index=c2.index) df2['x'] = c2[xx] df2['y'] = c2[yy] / (c2[yz]) df2['Category'] = 'ROC-WW' df2['SysCon2'] = c2['SysCon2'] df2['Membrane_ID'] = c2.index df2.index = range(30, 60) df3 = pd.DataFrame(index=c3.index) df3['x'] = c3[xx] df3['y'] = c3[yy] / (c3[yz]) df3['SysCon2'] = c3['SysCon2'] df3['Category'] = 'ROC-SW' df3['Membrane_ID'] = c3.index df3.index = range(60, 90) #merge all three into one dataframe df4 = pd.concat([df1, df2, df3], sort=True) if SysCon2_inc == 0: c1 = c1[c1.SysCon2 == 1] c2 = c2[c2.SysCon2 == 1] c3 = c3[c3.SysCon2 == 1] df4 = df4[df4.SysCon2 == 1] df4.drop('SysCon2', axis=1, inplace=True) df4.index = range(len(df4.index)) #SW vs WW x = c1[xx] y = c1[yy] / (c1[yz]) # Fit with polyfit b, m = polyfit(x, y, 1) plt.plot(x, y, '.', color='g') if line == 1: plt.plot(x, b + m * x, '-', color='g') #ROC vs WW x = c2[xx] y = c2[yy] / (c2[yz]) # Fit with polyfit b, m = polyfit(x, y, 1) plt.plot(x, y, '.', color='r') if line == 1: plt.plot(x, b + m * x, '-', color='r') #ROC vs SW x = c3[xx] y = c3[yy] / (c3[yz]) # Fit with polyfit b, m = polyfit(x, y, 1) plt.plot(x, y, '.', color='b') if line == 1: plt.plot(x, b + m * x, '-', color='b') if Spec_inc == 1: x1 = Spec[xx][0] x2 = Spec[xx][1] x3 = Spec[xx][2] y1 = Spec[yy][0] / Spec[yz][0] y2 = Spec[yy][1] / Spec[yz][1] y3 = Spec[yy][2] / Spec[yz][2] plt.scatter(x1, y1, marker=(5, 2), s=25, color='g') plt.scatter(x2, y2, marker=(5, 2), s=25, color='r') plt.scatter(x3, y3, marker=(5, 2), s=25, color='b') #Chart Details plt.xlabel("Net SE (kWh m^-3 of mixed solution)") plt.ylabel("Unit NPV ($ L^-1 hr of mixed solution)") if line == 1: plt.legend(['SW-WW', 'SW-WW', 'ROC-WW', 'ROC-WW', 'ROC-SW', 'ROC-SW']) else: plt.legend(['SW-WW', 'ROC-WW', 'ROC-SW']) plt.show() if interact == 1: # Define some CSS to control our custom labels css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #ffffff; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ plt.title( titl ) #Title block is here so that the normal figure doesn't have the title #fig, ax = plt.subplots() #ax.grid(True, alpha=0.3) points = plt.plot(df4.x, df4.y, 'o', color='b', mec='k', ms=10, mew=1, alpha=0) df4 = df4.rename(columns={'x': 'Net SE', 'y': 'Unit NPV'}) #print(df4) #return df4 N = len(df4.index) labels = [] memb = df4.Membrane_ID df4.drop('Membrane_ID', axis=1, inplace=True) for i in range(N): lab = df4.index[(i)] label = df4.loc[[lab], :].T label.columns = ['Membrane ID: {0}'.format(memb[i])] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) mpld3.show() mpld3.save_html(fig, "Fig_6_Interactive.html") #Note, if the JSON -serializable error comes up for mpld3, see this fix by ceprio: https://github.com/mpld3/mpld3/issues/441 return
def portfolioOptimization(stocks, st, ed, num_portfolios): #list of stocks in portfolio css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ stocks = ['AAPL', 'AMZN', 'MSFT', 'ACC'] yf.pdr_override() # <== that's all it takes :-) #indices= nift200Indices.objects(Ticker__in=["ACC.NS","AMZN.NS"], Date__lte=ed, Date__gte=st) #=nift200Indices.objects(Ticker ='AAPL.NS') yf.pdr_override() # <== that's all it takes :-) end = datetime.date(2018, 5, 27) begin = datetime.date(2017, 1, 1) #timestamp format and get apple stock. st = begin.strftime('%Y-%m-%d') ed = end.strftime('%Y-%m-%d') data = web.get_data_yahoo(stocks, st, ed)['Adj Close'] print("####################Data################") print(data) #download daily price data for each of the stocks in the portfolio #data = web.DataReader(stocks,data_source='yahoo',start='01/01/2010')['Adj Close'] data.sort_index(inplace=True) #convert daily stock prices into daily returns returns = data.pct_change() #calculate mean daily return and covariance of daily returns mean_daily_returns = returns.mean() cov_matrix = returns.cov() print("##########cov_matrix########") print(cov_matrix) num_portfolios = 100 #set up array to hold results #We have increased the size of the array to hold the weight values for each stock results = np.zeros((4 + len(stocks) - 1, num_portfolios)) labels = [] for i in range(num_portfolios): #select random weights for portfolio holdings weights = np.array(np.random.random(4)) #rebalance weights to sum to 1 weights /= np.sum(weights) print("#######WEIGHT##########") print(weights) #calculate portfolio return and volatility portfolio_return = np.sum(mean_daily_returns * weights) * 252 portfolio_std_dev = np.sqrt( np.dot(weights.T, np.dot(cov_matrix, weights))) * np.sqrt(252) # print("########### portfolio_return#############") #print(portfolio_return) #print("########### portfolio_std_dev#############") #print(portfolio_std_dev) #store results in results array results[0, i] = portfolio_return results[1, i] = portfolio_std_dev #store Sharpe Ratio (return / volatility) - risk free rate element excluded for simplicity results[2, i] = results[0, i] / results[1, i] #iterate through the weight vector and add data to results array for j in range(len(weights)): results[j + 3, i] = weights[j] print("#########################RESULT##############") print(results) fig, ax = plt.subplots() print("############ results.###### T###########") print(results.T) print("############ ARRAY###########") #convert results array to Pandas DataFrame results_frame = pd.DataFrame(results.T, columns=[ 'returns', 'standard_deviation', 'sharpe_ratio', stocks[0], stocks[1], stocks[2], stocks[3] ]) print("################## FRAME###############") for i in range(num_portfolios): label = results_frame.ix[[i], :].T label.columns = ['Statistics'] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) print(results_frame) #locate position of portfolio with highest Sharpe Ratio #print(results_frame['sharpe_ratio']) max_sharpe_port = results_frame.iloc[ results_frame['sharpe_ratio'].idxmax()] #locate positon of portfolio with minimum standard deviation min_vol_port = results_frame.iloc[ results_frame['standard_deviation'].idxmin()] #create scatter plot coloured by Sharpe Ratio plt.xlabel('Volatility') plt.ylabel('returns') points = plt.scatter(results_frame.standard_deviation, results_frame.returns, c=results_frame.sharpe_ratio, cmap='RdYlBu') #label = results_frame #label.columns = results_frame # .to_html() is unicode; so make leading 'u' go away with str() #labels.append(str(label.to_html())) plt.colorbar() #plot red star to highlight position of portfolio with highest sharpe_ratio Ratio plt.scatter(max_sharpe_port[1], max_sharpe_port[0], marker=(5, 1, 0), color='r', s=1000) #plot green star to highlight position of minimum variance portfolio plt.scatter(min_vol_port[1], min_vol_port[0], marker=(5, 1, 0), color='g', s=1000) print("#########################standard_deviation###########") #print(results_frame.standard_deviation) print("#########################return###########") # print(results_frame.return) print("#########################sharpe_ratio###########") #print(results_frame.sharpe) ax.grid(color='lightgray', alpha=0.7) #fig1 = plt.gcf() print("#############FIG###########") #print(fig1) byte_file = io.BytesIO() tooltip = plugins.PointHTMLTooltip(points, labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) # tooltip = plugins.PointHTMLTooltip(points[0], labels, # voffset=10, hoffset=10, css=css) #plugins.connect(fig, tooltip) #print(fig_to_html(fig)) # display_d3(fig1) # fig1.savefig(byte_file, format='png') #image_file= byte_file.getvalue() save_json(fig, "test.json") return fig_to_html(fig)
def create_mpld3_chart(): # Define some CSS to control our custom labels css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ fig, ax = plt.subplots() ax.grid(True, alpha=0.3) N = 50 df = pd.DataFrame(index=range(N)) df['x'] = np.random.randn(N) df['y'] = np.random.randn(N) df['z'] = np.random.randn(N) labels = [] for i in range(N): label = df.iloc[[i], :].T label.columns = ['Row {0}'.format(i)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) points = ax.plot(df.x, df.y, 'o', color='b', mec='k', ms=15, mew=1, alpha=.6) ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('HTML tooltips', size=20) tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) return mpld3.fig_to_html(fig)
def portfolioOptimization(portfolio, st,ed,num_portfolios): #list of stocks in portfolio css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ stocks = portfolio.Ticker_List #yf.pdr_override() # <== that's all it takes :-) #indices= nift200Indices.objects(Ticker__in=["ACC.NS","AMZN.NS"], Date__lte=ed, Date__gte=st) #=nift200Indices.objects(Ticker ='AAPL.NS') #yf.pdr_override() # <== that's all it takes :-) begin = datetime.date(2017,1,1) #timestamp format and get apple stock. start_date=begin.strftime('%Y-%m-%d') #end_date=end.strftime('%Y-%m-%d') end_date = dt.date.today() #data = web.get_data_yahoo(stocks,st,ed)['Adj Close'] i=0 for sysm in stocks: if portfolio.Company_Type =="nifty50": objnf50=nift50Indices.objects.filter(Date__gte=start_date, Date__lte=end_date,Ticker=sysm).values_list('Adj_Close','Date') elif portfolio.Company_Type =="nifty100": objnf50=nift100Indices.objects.filter(Date__gte=start_date, Date__lte=end_date,Ticker=sysm).values_list('Adj_Close','Date') elif portfolio.Company_Type =="nifty200": objnf50=nift200Indices.objects.filter(Date__gte=start_date, Date__lte=end_date,Ticker=sysm).values_list('Adj_Close','Date') else: objnf50=nift500Indices.objects.filter(Date__gte=start_date, Date__lte=end_date,Ticker=sysm).values_list('Adj_Close','Date') if i==0: data=pd.DataFrame(list(objnf50),columns=[sysm,'Date']) data=data.set_index('Date') else: data_frame =pd.DataFrame(list(objnf50),columns=[sysm,'Date']) data_frame=data_frame.set_index('Date') data = pd.merge( data, data_frame, right_index=True, left_index=True, how='outer') i+=1 #temp_data = data.iloc[:,0:len(data.columns)].apply(pd.to_numeric) #download daily price data for each of the stocks in the portfolio #data = web.DataReader(stocks,data_source='yahoo',start='01/01/2010')['Adj Close'] #print("########## data ##########") #print(portfolio.Portfolio_Name) #print(data) data.sort_index(inplace=True) #convert daily stock prices into daily returns returns = data.pct_change() #print("#########returns#########") returns = returns.dropna(how='any') #print(returns) #calculate mean daily return and covariance of daily returns mean_daily_returns = returns.mean() cov_matrix = returns.astype(float).cov() #print("#########DATA##########") #print(data) #print("##########cov_matrix##########") #print(cov_matrix) num_portfolios = 500 #set up array to hold results #We have increased the size of the array to hold the weight values for each stock results = np.zeros((4+len(stocks)-1,num_portfolios)) labels = [] for i in range(num_portfolios): #select random weights for portfolio holdings weights = np.array(np.random.random(len(stocks))) #rebalance weights to sum to 1 weights /= np.sum(weights) #calculate portfolio return and volatility portfolio_return = np.sum(mean_daily_returns * weights) * 252 portfolio_std_dev = np.sqrt(np.dot(weights.T,np.dot(cov_matrix, weights))) * np.sqrt(252) # print("########### portfolio_return#############") #print(portfolio_return) #print("########### portfolio_std_dev#############") #print(portfolio_std_dev) #store results in results array results[0,i] = portfolio_return results[1,i] = portfolio_std_dev #store Sharpe Ratio (return / volatility) - risk free rate element excluded for simplicity results[2,i] = results[0,i] / results[1,i] #iterate through the weight vector and add data to results array for j in range(len(weights)): results[j+3,i] = weights[j] fig, ax = plt.subplots() columnst=[] columnst.append("returns") columnst.append("standard_deviation") columnst.append("sharpe_ratio") for st in stocks: columnst.append(st) #convert results array to Pandas DataFrame #print("###########columst#########") #print(columnst) results_frame = pd.DataFrame(results.T,columns=columnst) for i in range(num_portfolios): label = results_frame.ix[[i], :].T label.columns = ['Statistics'] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) #locate position of portfolio with highest Sharpe Ratio #print(results_frame['sharpe_ratio']) max_sharpe_port = results_frame.iloc[results_frame['sharpe_ratio'].idxmax()] #locate positon of portfolio with minimum standard deviation min_vol_port = results_frame.iloc[results_frame['standard_deviation'].idxmin()] #create scatter plot coloured by Sharpe Ratio plt.xlabel('Volatility') plt.ylabel('returns') points = plt.scatter(results_frame.standard_deviation,results_frame.returns,c=results_frame.sharpe_ratio,cmap='RdYlBu') #label = results_frame #label.columns = results_frame # .to_html() is unicode; so make leading 'u' go away with str() #labels.append(str(label.to_html())) plt.colorbar() #plot red star to highlight position of portfolio with highest sharpe_ratio Ratio pointsst1=plt.scatter(max_sharpe_port[1],max_sharpe_port[0],marker=(5,1,0),color='r',s=1000) #plot green star to highlight position of minimum variance portfolio pointsst2=plt.scatter(min_vol_port[1],min_vol_port[0],marker=(5,1,0),color='g',s=1000) #print(results_frame.standard_deviation) # print(results_frame.return) #print(results_frame.sharpe) ax.grid(color='lightgray', alpha=0.7) #fig1 = plt.gcf() #print(fig1) byte_file = io.BytesIO() point1= [] pointsst1label = max_sharpe_port.ix[0:] pointsst1label=pointsst1label.to_frame() pointsst1label.columns = ['Statistics'] print("######## pointsst1label######") print(pointsst1label) point1.append(str(pointsst1label.to_html())) point2=[] pointsst2label = min_vol_port.ix[0:] pointsst2label=pointsst2label.to_frame() pointsst2label.columns = ['Statistics'] point2.append(str(pointsst2label.to_html())) tooltip = plugins.PointHTMLTooltip(points, labels,voffset=10, hoffset=10, css=css) tooltipstr1 = plugins.PointHTMLTooltip(pointsst1, point1,voffset=10, hoffset=10, css=css) tooltipstr2 = plugins.PointHTMLTooltip(pointsst2, point2,voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) plugins.connect(fig, tooltipstr1) plugins.connect(fig, tooltipstr2) fig.set_size_inches(11.7, 8.27) # tooltip = plugins.PointHTMLTooltip(points[0], labels, # voffset=10, hoffset=10, css=css) #plugins.connect(fig, tooltip) #print(fig_to_html(fig)) # display_d3(fig1) # fig1.savefig(byte_file, format='png') #image_file= byte_file.getvalue() save_json(fig,"heatmap.json") return fig
centers = ax.plot(cluster_center[0], cluster_center[1], 'o', markerfacecolor=col, markeredgecolor='k', markersize=6) labels = [] for movie in movies[k_means_labels == k]: labels.append( movie.get('Title', '') + " " + movie.get('imdbID', '') + " " + ", ".join(movie.get('Genre', '')) + " " + movie.get('', '') + " ") tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10) plugins.connect(fig, tooltip) ax.set_title('KMeans') ax.set_xticks(()) ax.set_yticks(()) ax.legend() blah = [] for k in range(N_CLUSTERS): innerDict = {} for movie in movies[k_means_labels == k]:
alpha=0.5, marker='.') y_vals = avg_temp_data.iloc[:num_years, j].to_numpy() x_vals = avg_temp_data.index[:num_years].to_numpy() trend_vals = p(avg_temp_data.index[:num_years]) tooltip_label = [None] * len(y_vals) trend_tooltip_label = [None] * len(trend_vals) for i in range(len(tooltip_label)): tooltip_label[i] = months[j][1] + " " + x_vals[i].astype( str) + ": " + ("{:.2f}".format(y_vals[i])) + " °C" trend_tooltip_label[i] = "Trend: " + months[j][1] + " " + x_vals[ i].astype(str) + ": " + ("{:.2f}".format(trend_vals[i])) + " °C" lines_tooltip[j] = plugins.PointHTMLTooltip(lines_int[j], labels=tooltip_label, voffset=10, hoffset=10, css=css) trend_lines_tooltip[j] = plugins.PointHTMLTooltip( trend_lines_int[j], labels=trend_tooltip_label, voffset=10, hoffset=10, css=css) # ---------------------------------------------------------------------------------- # Interactive Features - Add interactive features to the chart (e.g., filters) (15%) # ---------------------------------------------------------------------------------- # Create the interactive legend line_collections = [None] * 12 for i in range(12):
plot_rest = jitter( reduced[rest["Membership"]]["PC1"], reduced[rest["Membership"]]["PC2"], c="b", alpha=0.4, label="Non-WMC" ) plot_wmc = jitter( reduced[wmc["Membership"]]["PC1"], reduced[wmc["Membership"]]["PC2"], c="g", alpha=0.4, label="WMC" ) plt.xlabel("PC1") plt.ylabel("PC2") tooltip_rest = plugins.PointHTMLTooltip(plot_rest, reduced[rest["Membership"]].index.tolist()) tooltip_wmc = plugins.PointHTMLTooltip(plot_wmc, reduced[wmc["Membership"]].index.tolist()) zoom = plugins.Zoom(button=False, enabled=True) box_zoom = plugins.BoxZoom(button=False, enabled=True) plugins.connect(plt.gcf(), tooltip_rest) plugins.connect(plt.gcf(), tooltip_wmc) mpld3.save_html(plt.gcf(), "pca-2017.html")
def model_increase_fig(result,result1,title,temporal=0,_label=("订单趋势","出票趋势")): """ 绘图,将两个折线放在一个图里 :param result: :return: """ N = len(result) # TODO 这里要注意下,不能用df=df1=pd.DataFrame(index=range(N)),这样写他俩会指向同一对象 df = pd.DataFrame(index=range(N)) df1 = pd.DataFrame(index=range(N)) css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: center; } """ if temporal == 0: x = [datetime.strptime(one[0],"%Y-%m-%d").date() for one in result] x1 = [datetime.strptime(one[0],"%Y-%m-%d").date() for one in result1] else: x = [datetime.strptime(one[0], "%Y-%m").date() for one in result] x1 = [datetime.strptime(one[0], "%Y-%m") for one in result1] y = [one[1] for one in result] y1 = [one[1] for one in result1] df['数量'] = y df1['数量'] = y1 labels = []# TODO 设置标点样式 for i in range(N): label = df.ix[[i], :].T label.columns = ['{0}'.format(x[i])] labels.append(str(label.to_html())) labels1 = [] for i in range(N): label1 = df1.ix[[i], :].T label1.columns = ['{0}'.format(x1[i])] labels1.append(str(label1.to_html())) # 配置时间坐标 plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d')) plt.gca().xaxis.set_major_locator(mdates.DayLocator()) fig, ax = plt.subplots() points = ax.plot(x,y, '-or',label=_label[0])#TODO 画折线图,实线,红色,标记标点 points1 = ax.plot(x1,y1,'-ob',label=_label[1]) plt.gcf().autofmt_xdate() plt.legend(loc='upper right')#制定图例标注 interactive_legend = plugins.PointHTMLTooltip(points[0],labels,css=css) interactive_legend1 = plugins.PointHTMLTooltip(points1[0],labels1,css=css) ax.set_xlabel('日期') ax.set_ylabel('数量') title = "{0}数量趋势".format(title) ax.set_title(title, size=20) plugins.connect(fig, interactive_legend,interactive_legend1) #plugins.connect(fig,interactive_legend1) html_data = mpld3.fig_to_html(fig) return html_data
plt.yticks( np.arange((math.ceil(min(y) / dx) - 1) * dx, math.ceil(max(y) / dx) * dx + 1, dx)) interactive_legend = InteractiveLegendPlugin(line_collections, sims.O_NAMES, alpha_unsel=0.0, raw_data=output, limits=sims.O_LIMITS) plugins.connect(fig, interactive_legend) labels = [] for i in range(len(x)): # .to_html() is unicode; so make leading 'u' go away with str() labels.append('<table>' + ('<tr><td>Position</td><td>ft</td><td>%.3f, %.3f</td></tr>' % (x[i], y[i])) + ''.join([ '<tr><td>%s</td><td>%s</td><td>%.3f</td></tr>' % (sims.O_NAMES[o], sims.O_UNITS[o], output[i, o]) for o in range(len(output[i, :])) ]) + '</table>') tooltip = plugins.PointHTMLTooltip(line_collections[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip) mpld3.show()
def memory(): css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ session = db.session systemSelection = request.form.get("system") # Prevents error when first opening the page. Effectively makes 'ubuntu' the default selection. if systemSelection is None: # Query cpu_usage query = session.query( System.memory_percent).filter_by(system_id='ubuntu').all() # query Timestamps query2 = session.query( System.timestamp).filter_by(system_id='ubuntu').all() totalquery = session.query( System.memory_total).filter_by(system_id='ubuntu').first() systemSelection = 'ubuntu' else: query2 = session.query( System.timestamp).filter_by(system_id=systemSelection).all() query = session.query( System.memory_percent).filter_by(system_id=systemSelection).all() totalquery = session.query( System.memory_total).filter_by(system_id=systemSelection).first() cpu_list = [] timestamp_list = [] # add cpu usage to x for row in query: cpu_list.append(row) # add timestamp to y for row2 in query2: timestamp_list.append(row2) def flatten(listOfLists): "Flatten one level of nesting" return chain.from_iterable(listOfLists) # draw the graph figure = pyplot.figure(figsize=(10, 5)) figure.autofmt_xdate() ax = pyplot.axes() cpu_list = list(flatten(cpu_list)) time_list = list(flatten(timestamp_list)) hour = [] minute = [] for time, cpu in zip(time_list, cpu_list): timehour = [ "memory " + str(cpu) + " time " + " " + str(time.hour) + ":" + str(time.minute) + ":" + str(time.second) ] hour.append(timehour) labels = hour lines = ax.plot(time_list, cpu_list, marker='o', ls='-', ms=5) ax.fill_between(time_list, cpu_list) ax.set_title( str("Memory usage for " + systemSelection + " total memory: " + str(totalquery[0]))) ax.set_xlabel("Hours") ax.set_ylabel("Memory usage %") ax.grid() # query db for all system_id's systems = session.query(System.system_id).all() system_list = [] # for loop to add the va # appends values from the db to mylist for lists in systems: system_list.append(*lists._asdict().values()) tooltip = plugins.PointHTMLTooltip(lines[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(figure, tooltip) html_text = mpld3.fig_to_html(figure) # make the list contain only distinct items by converting it to a set and back uniqueList = list(set(system_list)) return render_template('memory.html', plot=html_text, systems=uniqueList)
def draw_fig(fig_type, startdate, k, Xo, L, N, I0, beta, gamma): #startdate='2019-03-01' #Logistic Growth Curve #k=10 # (steepness) #Xo=70 # (mid point) #L=9000 #(Max Infected Cumulative Cases) #SIR Model #N = 1200 # Total population, N #I0 =1 # Initial number of infected individuals R0 = 0 # Initial number of recovered individuals S0 = N - I0 - R0 # Everyone else, S0, is susceptible to infection initially. # Contact rate, beta, and mean recovery rate, gamma, (in 1/days). #beta=0.2 #gamma = 1./10 css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #cccccc; } table, th, td { font-family:Arial, Helvetica, sans-serif; border: 1px solid black; text-align: right; } """ """Returns html equivalent of matplotlib figure Parameters ---------- fig_type: string, type of figure one of following: * line * bar Returns -------- d3 representation of figure """ TotalCurves = plot_curves(startdate, k, Xo, L, N, I0, beta, gamma) LogisticCurve_Cumul_Max = TotalCurves[ 'Logistic_Growth_Curve_Cumulative'].max() LogisticCurve_Cumul_Max_Date = TotalCurves.index[ TotalCurves['Logistic_Growth_Curve_Cumulative'] == LogisticCurve_Cumul_Max].tolist()[0].strftime('%Y-%m-%d') LogisticCurve_Daily_Max = TotalCurves['Logistic_Growth_Curve_Daily'].max() LogisticCurve_Daily_Max_Date = TotalCurves.index[ TotalCurves['Logistic_Growth_Curve_Daily'] == LogisticCurve_Daily_Max].tolist()[0].strftime('%Y-%m-%d') SIR_Cumul_Max = TotalCurves['SIR_Model_Cumulative'].max() SIR_Cumul_Max_Date = TotalCurves.index[ TotalCurves['SIR_Model_Cumulative'] == SIR_Cumul_Max].tolist()[0].strftime('%Y-%m-%d') SIR_Daily_Max = TotalCurves['SIR_Model_Daily'].max() SIR_Daily_Max_Date = TotalCurves.index[TotalCurves['SIR_Model_Daily'] == SIR_Daily_Max].tolist()[0].strftime( '%Y-%m-%d') stringmessage1 = f"Estimated Max Cumulative Cases Of {round(LogisticCurve_Cumul_Max,0)} Occurs On {LogisticCurve_Cumul_Max_Date}" stringmessage2 = f"Estimated Max Daily Cases Of {round(LogisticCurve_Daily_Max,0)} Occurs On {LogisticCurve_Daily_Max_Date}" stringmessage3 = f"Estimated Max Cumulative Cases Of {round(SIR_Cumul_Max,0)} Occurs On {SIR_Cumul_Max_Date}" stringmessage4 = f"Estimated Max Daily Cases Of {round(SIR_Daily_Max,0)} Occurs On {SIR_Daily_Max_Date}" stringmessage = "Logistic Growth Model Estimates:" + "<br>" + stringmessage1 + "<br>" + stringmessage2 + "<br><br>" + "SIR Model Estimates:" + "<br>" + stringmessage3 + "<br>" + stringmessage4 + "<br" x = TotalCurves["Date"] fig, ax = plt.subplots() if fig_type == "daily": y = TotalCurves["SIR_Model_Daily"] z = TotalCurves["Logistic_Growth_Curve_Daily"] ax.plot(x, y) ax.plot(x, z) df = pd.DataFrame( zip(x, y, z), columns=["Date", "SIR_Model", "Logistic_Growth_Model"]) labels = [] for i in range(len(x)): label = df.iloc[[i], :].T label.columns = ['Row {0}'.format(i)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) points = ax.plot(df.index, df.SIR_Model, 'o', color='b', ls='-', ms=5, markerfacecolor='None', markeredgecolor='None') tooltip1 = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip1) elif fig_type == "cumulative": y = TotalCurves["SIR_Model_Cumulative"] z = TotalCurves["Logistic_Growth_Curve_Cumulative"] ax.plot(x, y) ax.plot(x, z) df = pd.DataFrame( zip(x, y, z), columns=["Date", "SIR_Model", "Logistic_Growth_Model"]) labels = [] for i in range(len(x)): label = df.iloc[[i], :].T label.columns = ['Row {0}'.format(i)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) points = ax.plot(df.index, df.SIR_Model, 'o', color='b', ls='-', ms=5, markerfacecolor='None', markeredgecolor='None') tooltip1 = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10, css=css) plugins.connect(fig, tooltip1) return mpld3.fig_to_html(fig) + stringmessage
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
def plotPower(sid, MCP='', pow=0, ss=0): powerdata = PowerTableModel.objects.filter(SID=sid)[::-1][0] parsdata = ParameterModel.objects.filter(SID=sid)[::-1][0] powtab = powerdata.data powtxt = powtab.round(2) cols = dict(zip(['BF', 'BH', 'RFT', 'UN'], Set1_9.mpl_colors)) sub = int(parsdata.Subj) newsubs = powtab.newsamplesize amax = int(50) css = """ table{border-collapse: collapse} td{background-color: rgba(217, 222, 230,50)} table, th, td{border: 1px solid;border-color: rgba(217, 222, 230,50);text-align: right;font-size: 12px} """ hover_BF = [ pd.DataFrame([ 'Bonferroni', 'Sample Size: ' + str(newsubs[i]), 'Power: ' + str(powtxt['BF'][i]) ]).to_html(header=False, index_names=False, index=False) for i in range(len(powtab)) ] hover_BH = [ pd.DataFrame([ 'Benjamini-Hochberg', 'Sample Size: ' + str(newsubs[i]), 'Power: ' + str(powtxt['BH'][i]) ]).to_html(header=False, index_names=False, index=False) for i in range(len(powtab)) ] hover_RFT = [ pd.DataFrame([ 'Random Field Theory', 'Sample Size: ' + str(newsubs[i]), 'Power: ' + str(powtxt['RFT'][i]) ]).to_html(header=False, index_names=False, index=False) for i in range(len(powtab)) ] hover_UN = [ pd.DataFrame([ 'Uncorrected', 'Sample Size: ' + str(newsubs[i]), 'Power: ' + str(powtxt['UN'][i]) ]).to_html(header=False, index_names=False, index=False) for i in range(len(powtab)) ] fig, axs = plt.subplots(1, 1, figsize=(8, 5)) fig.patch.set_facecolor('None') lty = ['--' if all(powtab.BF == powtab.RFT) else '-'] BF = axs.plot(newsubs, powtab.BF, 'o', markersize=15, alpha=0, label="") BH = axs.plot(newsubs, powtab.BH, 'o', markersize=15, alpha=0, label="") RFT = axs.plot(newsubs, powtab.RFT, 'o', markersize=15, alpha=0, label="") UN = axs.plot(newsubs, powtab.UN, 'o', markersize=15, alpha=0, label="") plugins.clear(fig) plugins.connect( fig, plugins.PointHTMLTooltip(BF[0], hover_BF, hoffset=0, voffset=10, css=css)) plugins.connect( fig, plugins.PointHTMLTooltip(BH[0], hover_BH, hoffset=0, voffset=10, css=css)) plugins.connect( fig, plugins.PointHTMLTooltip(RFT[0], hover_RFT, hoffset=0, voffset=10, css=css)) plugins.connect( fig, plugins.PointHTMLTooltip(UN[0], hover_UN, hoffset=0, voffset=10, css=css)) axs.plot(newsubs, powtab.BF, color=cols['BF'], lw=2, label="Bonferroni") axs.plot(newsubs, powtab.BH, color=cols['BH'], lw=2, label="Benjamini-Hochberg") axs.plot(newsubs, powtab.RFT, color=cols['RFT'], lw=2, linestyle=str(lty[0]), label="Random Field Theory") axs.plot(newsubs, powtab.UN, color=cols['UN'], lw=2, label="Uncorrected") text = "None" if pow != 0: if all(powtab[MCP] < pow): text = "To obtain a statistical power of " + str( pow ) + " this study would require a sample size larger than 300 subjects." else: min = int( np.min( [i for i, elem in enumerate(powtab[MCP] > pow, 1) if elem]) + sub - 1) axs.plot([min, min], [0, powtab[MCP][min - sub]], color=cols[MCP]) axs.plot([sub, min], [powtab[MCP][min - sub], powtab[MCP][min - sub]], color=cols[MCP]) text = "To obtain a statistical power of %s this study would require a sample size of %s subjects." % ( pow, min) amax = max(min, amax) if ss != 0: ss_pow = powtab[MCP][ss] axs.plot([ss, ss], [0, ss_pow], color=cols[MCP], linestyle="--") axs.plot([sub, ss], [ss_pow, ss_pow], color=cols[MCP], linestyle="--") xticks = [ x for x in list(np.arange((np.ceil(sub / 10.)) * 10, 100, 10)) if not x == np.round(ss / 10.) * 10 ] axs.set_xticks(xticks + [ss]) axs.set_yticks(list(np.arange(0, 1.1, 0.1))) text = "A sample size of %s subjects with %s control comes with a power of %s." % ( ss, MCP, str(np.round(ss_pow, decimals=2))) amax = max(ss, amax) axs.set_ylim([0, 1]) axs.set_xlim([sub, amax]) axs.set_title("Power curves") axs.set_xlabel("Subjects") axs.set_ylabel("Average power") axs.legend(loc="lower right", frameon=False, title="") code = mpld3.fig_to_html(fig) out = {"code": code, "text": text} return out
def get_html(df): N = df.shape[1] #width = 5 #pixels width = 400 - 20 #pixels height = 360 - 20 # pixels fig, ax = plt.subplots() #figsize=(width,height)) width *= 1. / fig.dpi height *= 1. / fig.dpi fig.set_figheight(height) fig.set_figwidth(width) ax.grid(True, alpha=0.3) #print("pixels", height, width) labels = [] for i in range(N): label = df.ix[[i], :].T label.columns = ['Row {0}'.format(i)] # .to_html() is unicode; so make leading 'u' go away with str() labels.append(str(label.to_html())) labelcolor = 'red' bgcolor = 'white' ax.spines['bottom'].set_color(labelcolor) ax.spines['top'].set_color(labelcolor) ax.xaxis.label.set_color(labelcolor) ax.yaxis.label.set_color(labelcolor) ax.tick_params(axis='x', color=labelcolor) ax.tick_params(axis='y', color=labelcolor) points = ax.plot(df.x, df.y, 'o', color='red', mec='k', ms=15, mew=1, alpha=.6) ax.grid(color=labelcolor) ax.set_facecolor(bgcolor) ax.set_xlabel('x') ax.set_ylabel('y') #ax.set_title('qxt test', size=10, color=labelcolor) tooltip = plugins.PointHTMLTooltip(points[0], labels, voffset=10, hoffset=10) #, css=css) plugins.connect(fig, tooltip) #print(tooltip.javascript()) #pp(dir(plugins)) html = mpld3.fig_to_html(fig) #mpld3.show() #return html htmlname = "mpl3test.html" fname = os.path.join(CURR_DIR, "src", "templates", htmlname) f = open(fname, 'w') f.writelines(html) f.close()
def draw(self, category, num_top_words_to_annotate=4, words_to_annotate=[], scores=None, transform=percentile_alphabetical): '''Outdated. MPLD3 drawing. Parameters ---------- category num_top_words_to_annotate words_to_annotate scores transform Returns ------- pd.DataFrame, html of fgure ''' try: import matplotlib.pyplot as plt except: raise Exception("matplotlib and mpld3 need to be installed to use this function.") try: from mpld3 import plugins, fig_to_html except: raise Exception("mpld3 need to be installed to use this function.") all_categories, other_categories = self._get_category_names(category) df = self._term_rank_score_and_frequency_df(all_categories, category, scores) if self.x_coords is None: df['x'], df['y'] = self._get_coordinates_from_transform_and_jitter_frequencies \ (category, df, other_categories, transform) df_to_annotate = df[(df['not category score rank'] <= num_top_words_to_annotate) | (df['category score rank'] <= num_top_words_to_annotate) | df['term'].isin(words_to_annotate)] words = list(df['term']) font = {'family': 'sans-serif', 'color': 'black', 'weight': 'normal', 'size': 'large'} fig, ax = plt.subplots() plt.figure(figsize=(10, 10)) plt.gcf().subplots_adjust(bottom=0.2) plt.gcf().subplots_adjust(right=0.2) points = ax.scatter(self.x_coords, self.y_coords, c=-df['color_scores'], cmap='seismic', s=10, edgecolors='none', alpha=0.9) tooltip = plugins.PointHTMLTooltip(points, ['<span id=a>%s</span>' % w for w in words], css='#a {background-color: white;}') plugins.connect(fig, tooltip) ax.set_ylim([-.2, 1.2]) ax.set_xlim([-.2, 1.2]) ax.xaxis.set_ticks([0., 0.5, 1.]) ax.yaxis.set_ticks([0., 0.5, 1.]) ax.set_ylabel(category.title() + ' Frequency Percentile', fontdict=font, labelpad=20) ax.set_xlabel('Not ' + category.title() + ' Frequency Percentile', fontdict=font, labelpad=20) for i, row in df_to_annotate.iterrows(): # alignment_criteria = row['category score rank'] < row['not category score rank'] alignment_criteria = i % 2 == 0 horizontalalignment = 'right' if alignment_criteria else 'left' verticalalignment = 'bottom' if alignment_criteria else 'top' term = row['term'] ax.annotate(term, (self.x_coords[i], y_data[i]), size=15, horizontalalignment=horizontalalignment, verticalalignment=verticalalignment, ) # texts.append( # ax.text(row['dem freq scaled'], row['rep freq scaled'], row['word']) # ) # adjust_text(texts, arrowprops=dict(arrowstyle="->", color='r', lw=0.5)) plt.show() return df, fig_to_html(fig)
def index(request): # Define some CSS to control our custom labels css = """ table { border-collapse: collapse; } th { color: #ffffff; background-color: #000000; } td { background-color: #FFA500; } table, th, td { font-family:Arial, Helvetica, sans-serif; font-size:20; border: 1px solid black; text-align: right; } """ # Set headers headers = requests.utils.default_headers() headers.update({ 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0' }) from bs4 import BeautifulSoup results = {} def scrape_wikipedia(): url = "https://he.wikipedia.org/wiki/%D7%94%D7%AA%D7%A4%D7%A8%D7%A6%D7%95%D7%AA_%D7%A0%D7%92%D7%99%D7%A3_%D7%94%D7%A7%D7%95%D7%A8%D7%95%D7%A0%D7%94_%D7%91%D7%99%D7%A9%D7%A8%D7%90%D7%9C" req = requests.get(url, headers) soup = BeautifulSoup(req.content, 'html.parser') #print(soup) div = soup.findAll("div", {"class": "barbox tleft"}) table = div[0].find("table") data = [] table_body = table.find('tbody') rows = table_body.find_all('tr') for row in rows: cols = row.find_all('td') cols = [ele.text.strip() for ele in cols] data.append([ele for ele in cols if ele]) # Get rid of empty values #print(data) results['wikipedia'] = data def scrape_worldometers(): url_alt = 'https://www.worldometers.info/coronavirus/country/israel/' req = requests.get(url_alt, headers) soup = BeautifulSoup(req.content, 'html.parser') #print(soup) mydivs = soup.find_all("div", class_="maincounter-number") #print(mydivs) divs = [ele.text.strip() for ele in mydivs] last_stat = divs[0] #print(last_stat) last_stat = re.sub(r'<div>.*>([\d,])<.*</div>', r'\1', last_stat, 0).replace(',', '') #print(last_stat) results['worldometers'] = last_stat def scrape_mako(): url_alt2 = 'https://corona.mako.co.il' req = requests.get(url_alt2, headers) soup = BeautifulSoup(req.content, 'html.parser') #print(soup) p = soup.find("p", class_="stat-total") last_stat2 = p.text.strip() #print(last_stat) last_stat2 = re.sub(r'<div>.*>([\d,])<.*</div>', r'\1', last_stat2, 0).replace(',', '') #print(last_stat2) results['mako'] = last_stat2 threads = [] process = Thread(target=scrape_wikipedia, args=[]) process.start() threads.append(process) process = Thread(target=scrape_worldometers, args=[]) process.start() threads.append(process) ##process = Thread(target=scrape_mako, args=[]) ##process.start() ##threads.append(process) for process in threads: process.join() #print('w'+results['worldometers']) #print(results['mako']) ##last_stat = str(max(int(results['worldometers']), int(results['mako']))) last_stat = results['worldometers'] #print(last_stat) data = results['wikipedia'] realDatesList = [] datesList = [] numSickList = [] for item in data: #print(item) #print('\n') if len(item) == 3: fullDate = item[0] date = fullDate date = re.sub(r'\d+-(\d+)-(\d+)', r'\2-\1', date, 0) #print(date) numSick = item[2] numSick = re.sub(r'([\d,]+).*', r'\1', numSick, 0).replace(',', '') #print(numSick) real_date = datetime.strptime(fullDate, "%Y-%m-%d") start_day = datetime.today() - timedelta(days=30) #start_day = datetime(2020,3,15) #print("start_day " + str(start_day)) if real_date >= start_day: #print(real_date) realDatesList.append(real_date) datesList.append(date) numSickList.append(numSick) if int(numSickList[len(numSickList) - 1]) < int(last_stat): if realDatesList[len(realDatesList) - 1].strftime("%j") == datetime.now().strftime("%j"): numSickList[len(numSickList) - 1] = str(last_stat) else: today = re.sub(r'\d+-(\d+)-(\d+).*', r'\2-\1', str(datetime.today()), 0) datesList.append(today) numSickList.append(last_stat) #print("{} {}".format(last_stat, numSickList[len(numSickList)-1])) print(datesList) print(numSickList) plt.rcParams.update({'font.size': 16}) DAY_RATE = 3 START_RANGE = 0 sickRate = [] labels = [] for i in range(START_RANGE, len(numSickList) - DAY_RATE): sickRate.append(int(numSickList[i + DAY_RATE]) / int(numSickList[i])) #print(datesList[i+DAY_RATE] + " : " + numSickList[i+DAY_RATE] + "/" + numSickList[i] + "= " + str(sickRate[i-4])) labels.append("<table><tr><td>" + numSickList[i + DAY_RATE] + "</td></tr><tr><td>" + numSickList[i] + "</td></tr><tr><td><b>" + str(round(sickRate[i], 3)) + "</b></td></tr></table>") i = i + 1 # x axis values x = datesList[START_RANGE + DAY_RATE:len(datesList)] # corresponding y axis values y = sickRate #print(x) #print(y) # plotting the points graph = plt.plot(x, y, color='green', linestyle='dashed', linewidth=3, marker='o', markerfacecolor='blue', markersize=20) # setting x and y axis range plt.ylim(0.95, 1.1) plt.xlim(0, len(datesList) - START_RANGE) #fig, ax = plt.subplots() ax = plt.gca() ax.set_xticklabels(x) ax.set_xticks(x) # naming the x axis plt.xlabel('x - Date') # naming the y axis plt.ylabel('y - 3 day rate') # giving a title to my graph current_sickRate = round(sickRate[len(sickRate) - 1], 3) current_sick = numSickList[len(numSickList) - 1] #future_sick = float(current_sick) * math.pow(current_sickRate, 10) ''' last = len(numSickList)-1 factor_two = int(current_sick)/2 past_sick = numSickList[last-1] i = 0 j = last-1 while int(past_sick) > int(factor_two) and j>=0: #print('past:'+past_sick) i+=1 j-=1 past_sick = numSickList[j] p = last-i #print( 'factor_two: ' + str(factor_two)) #print( 'past_sick: ' + str(past_sick)) #print("i: " + str(i)) days_to_multiple = i mod = int(factor_two) - int(past_sick) #print('mod: ' + str(mod)) avg = (float(current_sick)- float(numSickList[p]))/float(i) #print('numSickList[p]: ' + numSickList[p]) #print('avg : ' + str(avg)) factor_mod = mod/avg #print('factor_mod : ' + str(factor_mod)) total_mult_days = float(days_to_multiple) + round(factor_mod, 2) #ax.set_title('[ 3-day Contagion Rate: ' + str(current_sickRate) + '] [ In 30 days, total of: ' + f'{round(future_sick):,}' + ' sick ] [ Number of sick multiplies every ' + str(total_mult_days) + ' days ]') ''' today_sick = float(current_sick) yesterday_sick = float(numSickList[len(numSickList) - 2]) next_day_sick = float(current_sick) day_by_day_sickRate = today_sick / yesterday_sick #print(next_day_sick) i = 0 while next_day_sick < 2.0 * float(current_sick): i += 1 next_day_sick = next_day_sick * day_by_day_sickRate #print(next_day_sick) total_mult_days = i future_sick = float(current_sick) * math.pow(day_by_day_sickRate, 30) ax.set_title('[ Contagion Rate - 3-day: ' + str(current_sickRate) + ' 1-day: ' + str(round(day_by_day_sickRate, 3)) + ' ] [ In 30 days, total of: ' + f'{round(future_sick):,}' + ' (+ ' + str(round(float(future_sick) - float(current_sick))) + ') sick ] [ Number of sick multiplies every ' + str(total_mult_days) + ' days ]') fig = plt.gcf() fig.set_size_inches(18.5, 7.5) # function to show the plot #print(mpld3.fig_to_html(fig)) tooltip = plugins.PointHTMLTooltip(graph[0], labels, voffset=-80, hoffset=30, css=css) plugins.connect(fig, tooltip) #mpld3.show() htmlText = ''' <html>\n<head> <meta equiv="refresh" content="300"> <meta name="MobileOptimized" content="width"> <meta name="HandheldFriendly" content="true"> <meta name="viewport" content="width=device-width"> <meta name="viewport" content="width=device-width, initial-scale=1">''' htmlText += mpld3.fig_to_html(fig) htmlText += '</head>' #print(htmlText) #return HttpResponse(numSickList) return HttpResponse(htmlText) #index("aaa")