def draw_title(self, **props): """Add a title to the current subplot in layout dictionary. If there exists more than a single plot in the figure, titles revert to 'page'-referenced annotations. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Attempting to draw a title\n" if len(self.mpl_fig.axes) > 1: self.msg += " More than one subplot, adding title as " \ "annotation\n" x_px, y_px = props['mplobj'].get_transform().transform(props[ 'position']) x, y = mpltools.display_to_paper(x_px, y_px, self.plotly_fig['layout']) annotation = go.Annotation( text=props['text'], font=go.Font( color=props['style']['color'], size=props['style']['fontsize'] ), xref='paper', yref='paper', x=x, y=y, xanchor='center', yanchor='bottom', showarrow=False # no arrow for a title! ) self.plotly_fig['layout']['annotations'] += annotation, else: self.msg += " Only one subplot found, adding as a " \ "plotly title\n" self.plotly_fig['layout']['title'] = props['text'] titlefont = go.Font( size=props['style']['fontsize'], color=props['style']['color'] ) self.plotly_fig['layout']['titlefont'] = titlefont
def draw_ylabel(self, **props): """Add a yaxis label to the current subplot in layout dictionary. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Adding ylabel\n" axis_key = 'yaxis{0}'.format(self.axis_ct) self.plotly_fig['layout'][axis_key]['title'] = props['text'] titlefont = go.Font(size=props['style']['fontsize'], color=props['style']['color']) self.plotly_fig['layout'][axis_key]['titlefont'] = titlefont
def make_annotation(x, y): return go.Annotation( text=str(x), # text is the y-coord showarrow=False, # annotation w/o arrows, default is True x=x, # set x position xref='x', # position text horizontally with x-coords xanchor='left', # x position corresp. to center of text yref='y', # set y position yanchor='auto', # position text vertically with y-coords y=y, # y position corresp. to top of text font=go.Font( color='#262626', # set font color size=13 # and size ) )
def make_comparison_plots(name,set,loci_list,loci_dict,counts=None,subplots=True): set_size = len(set) annotations = [] height = set_size*300 width = set_size*300 fig = tools.make_subplots( print_grid=subplots, rows=set_size, cols=set_size, ) # horizontal_spacing (kwarg, float in [0,1], default=0.2 / columns) horizontal_spacing = 0.2/set_size # vertical_spacing (kwarg, float in [0,1], default=0.3 / rows) vertical_spacing = 0.3/set_size for plot_column in range(1,set_size+1): colory = colors2[plot_column] for plot_row in range(1,set_size+1): colorx = colors2[plot_row] plot_nr = plot_column + (plot_row - 1) * set_size if plot_row == plot_column: """ if counts: plot_nr = plot_x+(plot_y-1)*set_size test_domain = dict( x = fig['layout']['xaxis{}'.format(plot_nr)]['domain'], y = fig['layout']['yaxis{}'.format(plot_nr)]['domain'] ) test_dict = go.Table( columnwidth = [10,30], domain = test_domain, header = dict( values = ['', set[plot_x-1]], font = dict(size = 8), ), cells = dict( values = ['rc', counts[set[plot_x-1]]], ) ) fig['data'].append(test_dict) else: pass """ if counts: info_text = "x-axis of this row: <br>{} <br> <br>pos: {},{} <br>read count: {} ".format( set[plot_column-1], loci_dict[set[plot_column-1]]['info']['plate'], loci_dict[set[plot_column-1]]['info']['position'], counts[set[plot_column-1]] ) domain_x = fig['layout']['xaxis{}'.format(plot_nr)]['domain'] domain_y = fig['layout']['yaxis{}'.format(plot_nr)]['domain'] offset = (0.05 / (set_size-1)) annotation = go.Annotation( x=domain_x[0], y=domain_y[1], width=((domain_x[1]-domain_x[0])-offset)*width, height=(domain_y[1]-domain_y[0])*height, xref='paper', yref='paper', yanchor='top', xanchor='left', text=info_text, align='right', showarrow=False, bgcolor = 'lightgray'#colorx ) annotations.append(annotation) #elif plot_x > plot_y: # half of the grid to safe the server some work. # pass else: trace, layout_text, fit_trace = make_compare_trace(set[plot_row-1],set[plot_column-1],loci_list,loci_dict) fig.append_trace(fit_trace,plot_row,plot_column) fig.append_trace(trace,plot_row,plot_column) fig['layout']['xaxis{}'.format(plot_nr)].update( tickvals = [0, 25, 50, 75, 100], ticktext = ['0%', '', '50%', '', '100%'] ) #tickfont= dict(color=colorx) fig['layout']['yaxis{}'.format(plot_nr)].update( tickvals = [0, 25, 50, 75, 100], ticktext = ['0%','','50%','','100%'], ) #tickfont = dict(color=colory) offset = (0.05/set_size) # x = 20, # y = 90, # xref = 'x' + str(plot_nr), # yref = 'y' + str(plot_nr), annotation = go.Annotation( x = fig['layout']['xaxis{}'.format(plot_nr)]['domain'][0]+offset, y = fig['layout']['yaxis{}'.format(plot_nr)]['domain'][1], xref = 'paper', yref = 'paper', yanchor = 'middle', xanchor = 'left', text=layout_text, align='left', font=go.Font(size=8), showarrow=False ) annotations.append(annotation) # fix the layout # default figure margins: L=80,R=80,T=100,B=80 fig['layout'].update( title='proportion comparison {}'.format(name), width=width+160, height=height+180, showlegend=False, hovermode='closest', legend=dict( orientation='h' ), annotations = annotations ) # write the file #py.image.save_as(fig, filename=filename) div = pyoff.plot(fig, include_plotlyjs=False, output_type='div') return div
def start_end_stations(path, graph='all'): cum_station_hour = folder_reader(path) cum_station_hour.columns = [ 'rain', 'start_end', 'time', 'station', 'count' ] merged_commute_morning_h = cum_station_hour[ (cum_station_hour.time == 'morning') & (cum_station_hour.start_end == 'start') & (cum_station_hour.rain == 'high')].merge( cum_station_hour[(cum_station_hour.time == 'morning') & (cum_station_hour.start_end == 'end') & (cum_station_hour.rain == 'high')], how='outer', left_on=['station'], right_on=['station']) merged_commute_afternoon_h = cum_station_hour[ (cum_station_hour.time == 'afternoon') & (cum_station_hour.start_end == 'start') & (cum_station_hour.rain == 'high')].merge( cum_station_hour[(cum_station_hour.time == 'afternoon') & (cum_station_hour.start_end == 'end') & (cum_station_hour.rain == 'high')], how='outer', left_on=['station'], right_on=['station']) merged_commute_morning_n = cum_station_hour[ (cum_station_hour.time == 'morning') & (cum_station_hour.start_end == 'start') & (cum_station_hour.rain == 'no')].merge( cum_station_hour[(cum_station_hour.time == 'morning') & (cum_station_hour.start_end == 'end') & (cum_station_hour.rain == 'no')], how='outer', left_on=['station'], right_on=['station']) merged_commute_afternoon_n = cum_station_hour[ (cum_station_hour.time == 'afternoon') & (cum_station_hour.start_end == 'start') & (cum_station_hour.rain == 'no')].merge( cum_station_hour[(cum_station_hour.time == 'afternoon') & (cum_station_hour.start_end == 'end') & (cum_station_hour.rain == 'no')], how='outer', left_on=['station'], right_on=['station']) merged_commute_morning_l = cum_station_hour[ (cum_station_hour.time == 'morning') & (cum_station_hour.start_end == 'start') & (cum_station_hour.rain == 'low')].merge( cum_station_hour[(cum_station_hour.time == 'morning') & (cum_station_hour.start_end == 'end') & (cum_station_hour.rain == 'low')], how='outer', left_on=['station'], right_on=['station']) merged_commute_afternoon_l = cum_station_hour[ (cum_station_hour.time == 'afternoon') & (cum_station_hour.start_end == 'start') & (cum_station_hour.rain == 'low')].merge( cum_station_hour[(cum_station_hour.time == 'afternoon') & (cum_station_hour.start_end == 'end') & (cum_station_hour.rain == 'low')], how='outer', left_on=['station'], right_on=['station']) merged_commute_morning_h['start/end'] = merged_commute_morning_h.count_x / ( merged_commute_morning_h.count_y + merged_commute_morning_h.count_x) merged_commute_morning_h[ 'end/start'] = 1 - merged_commute_morning_h['start/end'] merged_commute_afternoon_h[ 'start/end'] = merged_commute_afternoon_h.count_x / ( merged_commute_afternoon_h.count_y + merged_commute_afternoon_h.count_x) merged_commute_afternoon_h[ 'end/start'] = 1 - merged_commute_afternoon_h['start/end'] merged_commute_morning_n['start/end'] = merged_commute_morning_n.count_x / ( merged_commute_morning_n.count_y + merged_commute_morning_n.count_x) merged_commute_morning_n[ 'end/start'] = 1 - merged_commute_morning_n['start/end'] merged_commute_afternoon_n[ 'start/end'] = merged_commute_afternoon_n.count_x / ( merged_commute_afternoon_n.count_y + merged_commute_afternoon_n.count_x) merged_commute_afternoon_n[ 'end/start'] = 1 - merged_commute_afternoon_n['start/end'] merged_commute_morning_l['start/end'] = merged_commute_morning_l.count_x / ( merged_commute_morning_l.count_y + merged_commute_morning_l.count_x) merged_commute_morning_l[ 'end/start'] = 1 - merged_commute_morning_l['start/end'] merged_commute_afternoon_l[ 'start/end'] = merged_commute_afternoon_l.count_x / ( merged_commute_afternoon_l.count_y + merged_commute_afternoon_l.count_x) merged_commute_afternoon_l[ 'end/start'] = 1 - merged_commute_afternoon_l['start/end'] merged_commute_morning_h = merged_commute_morning_h[[ 'station', 'count_x', 'count_y', 'start/end', 'end/start' ]] merged_commute_morning_h.columns = [ 'station', 'start_count', 'end_count', 'start/end', 'end/start' ] merged_commute_morning_h['start_count'] = merged_commute_morning_h[ 'start_count'] / (minutes_high_mor / 60) merged_commute_morning_h['end_count'] = merged_commute_morning_h[ 'end_count'] / (minutes_high_mor / 60) merged_commute_afternoon_h = merged_commute_afternoon_h[[ 'station', 'count_x', 'count_y', 'start/end', 'end/start' ]] merged_commute_afternoon_h.columns = [ 'station', 'start_count', 'end_count', 'start/end', 'end/start' ] merged_commute_afternoon_h['start_count'] = merged_commute_afternoon_h[ 'start_count'] / (minutes_high_aft / 60) merged_commute_afternoon_h['end_count'] = merged_commute_afternoon_h[ 'end_count'] / (minutes_high_aft / 60) merged_commute_morning_l = merged_commute_morning_l[[ 'station', 'count_x', 'count_y', 'start/end', 'end/start' ]] merged_commute_morning_l.columns = [ 'station', 'start_count', 'end_count', 'start/end', 'end/start' ] merged_commute_morning_l['start_count'] = merged_commute_morning_l[ 'start_count'] / (minutes_low_mor / 60) merged_commute_morning_l['end_count'] = merged_commute_morning_l[ 'end_count'] / (minutes_low_mor / 60) merged_commute_afternoon_l = merged_commute_afternoon_l[[ 'station', 'count_x', 'count_y', 'start/end', 'end/start' ]] merged_commute_afternoon_l.columns = [ 'station', 'start_count', 'end_count', 'start/end', 'end/start' ] merged_commute_afternoon_l['start_count'] = merged_commute_afternoon_l[ 'start_count'] / (minutes_low_aft / 60) merged_commute_afternoon_l['end_count'] = merged_commute_afternoon_l[ 'end_count'] / (minutes_low_aft / 60) merged_commute_morning_n = merged_commute_morning_n[[ 'station', 'count_x', 'count_y', 'start/end', 'end/start' ]] merged_commute_morning_n.columns = [ 'station', 'start_count', 'end_count', 'start/end', 'end/start' ] merged_commute_morning_n['start_count'] = merged_commute_morning_n[ 'start_count'] / (minutes_no_mor / 60) merged_commute_morning_n['end_count'] = merged_commute_morning_n[ 'end_count'] / (minutes_no_mor / 60) merged_commute_afternoon_n = merged_commute_afternoon_n[[ 'station', 'count_x', 'count_y', 'start/end', 'end/start' ]] merged_commute_afternoon_n.columns = [ 'station', 'start_count', 'end_count', 'start/end', 'end/start' ] merged_commute_afternoon_n['start_count'] = merged_commute_afternoon_n[ 'start_count'] / (minutes_no_aft / 60) merged_commute_afternoon_n['end_count'] = merged_commute_afternoon_n[ 'end_count'] / (minutes_no_aft / 60) merged_commute_morning = merged_commute_morning_h.merge( merged_commute_morning_n, how='inner', left_on=['station'], right_on=['station'], suffixes=('_h', '_n')) merged_commute_afternoon = merged_commute_afternoon_h.merge( merged_commute_afternoon_n, how='inner', left_on=['station'], right_on=['station'], suffixes=('_h', '_n')) mcm = merged_commute_morning mcm['diff'] = mcm['start/end_h'] - mcm['start/end_n'] mcm2 = mcm.sort('start/end_n', ascending=False) mca = merged_commute_afternoon mca['diff'] = mca['start/end_h'] - mca['start/end_n'] mcm3 = mca.sort('end/start_n', ascending=False) morning_starters = list(mcm[mcm['start/end_n'] > 0.8].station) day_sleeping_stations = list( mca[(mca['end/start_n'] > 0.8) & (mca.station.isin(morning_starters))].station) mcm_days = mcm[mcm.station.isin(day_sleeping_stations)].sort(['station']) mca_days = mca[mca.station.isin(day_sleeping_stations)].sort(['station']) if graph == 'all': data = [ go.Bar( x=mcm2['station'], # assign x as the dataframe column 'x' y=mcm2['start/end_n'], name='% of Trips Started', marker=dict(color='rgb(400, 83,40)')), go.Bar(x=mcm2['station'], y=mcm2['end/start_n'], name='% of Trips Ended', marker=dict(color='rgb(0, 150, 200)')) ] layout = go.Layout( barmode='group', title='% of Started and Ended Trips in Morning Commute') fig = go.Figure(data=data, layout=layout) url = py.offline.iplot(fig) data = [ go.Bar( x=mcm3['station'], # assign x as the dataframe column 'x' y=mcm3['end/start_n'], name='% of Trips Ended'), go.Bar(x=mcm3['station'], y=mcm3['start/end_n'], name='% of Trips Started') ] layout = go.Layout( barmode='group', title='% of Started and Ended Trips in Afternoon Commute') fig = go.Figure(data=data, layout=layout) # IPython notebook # py.iplot(fig, filename='pandas-bar-chart-layout') url = py.offline.iplot(fig) if graph == "top_day_starters": data = [ go.Bar( x=mcm_days['station'], # assign x as the dataframe column 'x' y=mcm_days['end/start_n'], name='% of Trips Ended', marker=dict(color='rgb(0, 150, 200)')), go.Bar(x=mcm_days['station'], y=mcm_days['start/end_n'], name='% of Trips Started', marker=dict(color='rgb(400, 83,40)')) ] layout = go.Layout( barmode='group', title= '% of Started and Ended Trips in Morning Commute for Top "Day Starters"' ) fig = go.Figure(data=data, layout=layout) url = py.offline.iplot(fig) data = [ go.Bar( x=mca_days['station'], # assign x as the dataframe column 'x' y=mca_days['end/start_n'], name='% of Trips Ended'), go.Bar(x=mca_days['station'], y=mca_days['start/end_n'], name='% of Trips Started') ] layout = go.Layout( barmode='group', title= '% of Started and Ended Trips in Afternoon Commute for Top "Day Starters"' ) fig = go.Figure(data=data, layout=layout) # IPython notebook # py.iplot(fig, filename='pandas-bar-chart-layout') url = py.offline.iplot(fig) if graph == "correlation": mca_merge = mca[[ 'station', 'start/end_n', 'start_count_n', 'end_count_n' ]] mcm_merge = mcm[[ 'station', 'start/end_n', 'start_count_n', 'end_count_n' ]] mca_merge.columns = [ 'station', 'start_end_aft', 'start_count_aft', 'end_count_aft' ] mcm_merge.columns = [ 'station', 'start_end_mor', 'start_count_mor', 'end_count_mor' ] merged_corr = mcm_merge.merge(mca_merge, how='inner', left_on=['station'], right_on=['station'], suffixes=('_m', '_a')) xi = np.array(merged_corr.start_end_mor) # (Almost) linear sequence y = np.array(merged_corr.start_end_aft) # Generated linear fit slope, intercept, r_value, p_value, std_err = stats.linregress(xi, y) line = slope * xi + intercept # Creating the dataset, and generating the plot trace1 = go.Scatter(x=xi, y=y, mode='markers', marker=go.Marker(color='rgb(255, 127, 14)'), name='Data') trace2 = go.Scatter(x=xi, y=line, mode='lines', marker=go.Marker(color='rgb(31, 119, 180)'), name='Fit') annotation = go.Annotation(x=3.5, y=23.5, showarrow=False, font=go.Font(size=16)) layout = go.Layout( title= 'Correlation between % of started trips in the morning and % of started trips in the afternoon, No rain', plot_bgcolor='rgb(229, 229, 229)', # xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), # yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), xaxis=dict(range=[0, 1]), yaxis=dict(range=[0, 1])) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) py.offline.iplot(fig) print("r-squared:", r_value**2) if graph == "correlation_high": mca_merge = mca[[ 'station', 'start/end_h', 'start_count_h', 'end_count_h' ]] mcm_merge = mcm[[ 'station', 'start/end_h', 'start_count_h', 'end_count_h' ]] mca_merge.columns = [ 'station', 'start_end_aft', 'start_count_aft', 'end_count_aft' ] mcm_merge.columns = [ 'station', 'start_end_mor', 'start_count_mor', 'end_count_mor' ] merged_corr = mcm_merge.merge(mca_merge, how='inner', left_on=['station'], right_on=['station'], suffixes=('_m', '_a')) xi = np.array(merged_corr.start_end_mor) # (Almost) linear sequence y = np.array(merged_corr.start_end_aft) # Generated linear fit slope, intercept, r_value, p_value, std_err = stats.linregress(xi, y) line = slope * xi + intercept # Creating the dataset, and generating the plot trace1 = go.Scatter(x=xi, y=y, mode='markers', marker=go.Marker(color='rgb(255, 127, 14)'), name='Data') trace2 = go.Scatter(x=xi, y=line, mode='lines', marker=go.Marker(color='rgb(31, 119, 180)'), name='Fit') annotation = go.Annotation(x=3.5, y=23.5, showarrow=False, font=go.Font(size=16)) layout = go.Layout( title= 'Correlation between % of started trips in the morning and % of started trips in the afternoon, High rain', plot_bgcolor='rgb(229, 229, 229)', # xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), # yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), xaxis=dict(range=[0, 1]), yaxis=dict(range=[0, 1])) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) py.offline.iplot(fig) print("r-squared:", r_value**2) if graph == "top_day_starters_quant": data = [ go.Bar( x=mcm_days['station'], # assign x as the dataframe column 'x' y=mcm_days['end_count_n'], name='AVG No. of Trips Ended per minute', marker=dict(color='rgb(0, 150, 200)')), go.Bar(x=mcm_days['station'], y=mcm_days['start_count_n'], name='AVG No. of Trips Started per minute', marker=dict(color='rgb(400, 83,40)')) ] layout = go.Layout( barmode='group', title= ' Number of Started and Ended Trips in Morning Commute for Top "Day Starters with No Rain"' ) fig = go.Figure(data=data, layout=layout) url = py.offline.iplot(fig) data = [ go.Bar( x=mca_days['station'], # assign x as the dataframe column 'x' y=mca_days['end_count_n'], name='AVG No. of Trips Ended per minute'), go.Bar(x=mca_days['station'], y=mca_days['start_count_n'], name='AVG No. of Trips Started per minute') ] layout = go.Layout( barmode='group', title= 'Number of Started and Ended Trips in Afternoon Commute for Top "Day Starters" with No Rain' ) fig = go.Figure(data=data, layout=layout) # IPython notebook # py.iplot(fig, filename='pandas-bar-chart-layout') url = py.offline.iplot(fig) if graph == "end_quant_weather": data = [ go.Bar( x=mca_days['station'], # assign x as the dataframe column 'x' y=mca_days['end_count_n'], name='AVG No. of Trips Ended per minute with no Rain', marker=dict(color='rgb(400, 83,400)')), go.Bar(x=mca_days['station'], y=mca_days['end_count_h'], name='AVG No. of Trips Ended per minute with High Rain', marker=dict(color='rgb(0, 150, 0)')) ] layout = go.Layout( barmode='group', title=' AVG Number of Ended Trips in the Afternoon by weather') fig = go.Figure(data=data, layout=layout) url = py.offline.iplot(fig)
def update_graph(occupation_data, skill_data): """ Creates the 3D movable graph to visualize the common skills between respective roles """ input_data = occupation_data + skill_data input_data = [a.lower() for a in input_data] if input_data == []: fix_position_list = [ 'web developer', 'ict project manager', 'ict business analyst', 'software developer', 'ict network engineer', 'database administrator', 'ict consultant', 'data analyst', 'ict application developer', 'mobile app developer' ] for key, value in graph_visualization_data.items(): if key == 'nodes': nodes_new = [ k for k in value if k['id'].lower() in fix_position_list ] if key == 'links': links_new = [ k for k in value if k['source'].lower() in fix_position_list or k['target'].lower() in fix_position_list ] new_graph_data = {'nodes': nodes_new, 'links': links_new} G = nx.json_graph.node_link_graph(new_graph_data) pos = nx.spring_layout(G, dim=3) Xn_role = [pos[k][0] for k in pos if k in occupations] Yn_role = [pos[k][1] for k in pos if k in occupations] Zn_role = [pos[k][2] for k in pos if k in occupations] role_labels = [k for k in pos if k in occupations] Xn_skill = [pos[k][0] for k in pos if k in skills_graph] Yn_skill = [pos[k][1] for k in pos if k in skills_graph] Zn_skill = [pos[k][2] for k in pos if k in skills_graph] skill_labels = [k for k in pos if k in skills_graph] Xe = [] Ye = [] Ze = [] for e in G.edges(): Xe.extend([pos[e[0]][0], pos[e[1]][0], None]) Ye.extend([pos[e[0]][1], pos[e[1]][1], None]) Ze.extend([pos[e[0]][2], pos[e[1]][2], None]) trace_nodes_role = go.Scatter3d(x=Xn_role, y=Yn_role, z=Zn_role, mode='markers', marker=dict(size='18', symbol='dot', color='rgb(255,140,0)'), text=role_labels, hoverinfo='text') trace_nodes_skill = go.Scatter3d(x=Xn_skill, y=Yn_skill, z=Zn_skill, mode='markers', marker=dict(symbol='dot', color='rgb(33,188,235)'), text=skill_labels, hoverinfo='text') trace_edges = go.Scatter3d(mode='lines', x=Xe, y=Ye, z=Ze, line=dict(width=0.6, color='rgb(119,136,153)'), hoverinfo='none') axis = dict( showline=False, # hide axis line, grid, ticklabels and title zeroline=False, showgrid=False, showticklabels=False, showbackground=False, title='') layout_skill_graph = go.Layout( title="Skill tree for Jobs", titlefont=dict(color='white'), showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=dict(l=0, r=0, t=40, b=0), hovermode='closest', plot_bgcolor='#131827', paper_bgcolor='#131827', #set background color annotations=go.Annotations([ go.Annotation(showarrow=False, text="", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=go.Font(size=14)) ])) data = go.Data([trace_edges, trace_nodes_role, trace_nodes_skill]) fig = go.Figure(data=data, layout=layout_skill_graph) return fig else: for key, value in graph_visualization_data.items(): if key == 'nodes': nodes_new = [k for k in value if k['id'].lower() in input_data] if key == 'links': links_new = [ k for k in value if k['source'].lower() in input_data or k['target'].lower() in input_data ] new_graph_data = {'nodes': nodes_new, 'links': links_new} G = nx.json_graph.node_link_graph(new_graph_data) pos = nx.spring_layout(G, dim=3) Xn_role = [pos[k][0] for k in pos if k in occupations] Yn_role = [pos[k][1] for k in pos if k in occupations] Zn_role = [pos[k][2] for k in pos if k in occupations] role_labels = [k for k in pos if k in occupations] Xn_skill = [pos[k][0] for k in pos if k in skills_graph] Yn_skill = [pos[k][1] for k in pos if k in skills_graph] Zn_skill = [pos[k][2] for k in pos if k in skills_graph] skill_labels = [k for k in pos if k in skills_graph] Xe = [] Ye = [] Ze = [] for e in G.edges(): Xe.extend([pos[e[0]][0], pos[e[1]][0], None]) Ye.extend([pos[e[0]][1], pos[e[1]][1], None]) Ze.extend([pos[e[0]][2], pos[e[1]][2], None]) trace_nodes_role = go.Scatter3d(x=Xn_role, y=Yn_role, z=Zn_role, mode='markers', marker=dict(size='18', symbol='dot', color='rgb(255,140,0)'), text=role_labels, hoverinfo='text') trace_nodes_skill = go.Scatter3d(x=Xn_skill, y=Yn_skill, z=Zn_skill, mode='markers', marker=dict(symbol='dot', color='rgb(33,188,235)'), text=skill_labels, hoverinfo='text') trace_edges = go.Scatter3d(mode='lines', x=Xe, y=Ye, z=Ze, line=dict(width=0.6, color='rgb(119,136,153)'), hoverinfo='none') axis = dict( showline=False, # hide axis line, grid, ticklabels and title zeroline=False, showgrid=False, showticklabels=False, showbackground=False, title='') layout_skill_graph = go.Layout( title="Skill tree for Jobs", titlefont=dict(color='white'), showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=dict(l=0, r=0, t=40, b=0), hovermode='closest', plot_bgcolor='#131827', paper_bgcolor='#131827', #set background color annotations=go.Annotations([ go.Annotation(showarrow=False, text="", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=go.Font(size=14)) ])) data = go.Data([trace_edges, trace_nodes_role, trace_nodes_skill]) fig = go.Figure(data=data, layout=layout_skill_graph) return fig
# Creating the dataset, and generating the plot trace1 = go.Scatter(x=xi, y=y, mode='markers', marker=go.Marker(color='rgb(255, 127, 14)'), name='Data') trace2 = go.Scatter(x=xi, y=line, mode='lines', marker=go.Marker(color='rgb(31, 119, 180)'), name='Fit') annotation = go.Annotation(x=3.5, y=23.5, text='$R^2 = 0.9551,\\Y = 0.716X + 19.18$', showarrow=False, font=go.Font(size=16)) layout = go.Layout(title='Linear Fit in Python', plot_bgcolor='rgb(229, 229, 229)', xaxis=go.XAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), yaxis=go.YAxis(zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), annotations=[annotation]) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) py.plot(fig, filename='Linear-Fit-in-python')
def make_snp_plots(samples_dict, snp_name="", jbrouwse_url=None): height = 1 * 350 width = 2 * 350 # if there are multiple plots the space in between has to be acounted for. width += width * 0.2 #height += height*0.3 # and the borders height += 180 width += 160 fig = tools.make_subplots(rows=1, cols=2, subplot_titles=('allele depth', 'allele 2 percent')) fig['layout'].update( title="sequence variant {}.".format(snp_name), width=width, height=height, hovermode='closest', showlegend=False, annotations=[ dict( x=0.48, y=1, xref='paper', yref='paper', yanchor='middle', xanchor='right', text='lines do not reflect SNP calling rules', font=go.Font(size=8), showarrow=False, ), dict( x=1, y=1, xref='paper', yref='paper', yanchor='middle', xanchor='right', text='lines do not reflect SNP calling rules', font=go.Font(size=8), showarrow=False, ) ], shapes=[ { 'type': 'line', 'x0': 0, 'x1': 10, 'xref': "x", 'y0': 10, 'y1': 0, 'yref': "y", 'line': { 'color': 'yellow', 'width': 2, }, }, { 'type': 'line', 'x0': 9, 'x1': 10000, 'xref': "x", 'y0': 1, 'y1': 1000, 'yref': "y", 'line': { 'color': 'red', 'width': 2, }, }, { 'type': 'line', 'x0': 1000, 'x1': 1, 'xref': "x", 'y0': 10000, 'y1': 9, 'yref': "y", 'line': { 'color': 'blue', 'width': 2, } }, { 'type': 'line', 'x0': 5, 'x1': 10000, 'xref': "x", 'y0': 5, 'y1': 10000, 'yref': "y", 'line': { 'color': 'purple', 'width': 2, } }, { 'type': 'line', 'x0': 6.6, 'x1': 10000, 'xref': "x", 'y0': 3.3, 'y1': 5000, 'yref': "y", 'line': { 'color': 'black', 'width': 2, } }, { 'type': 'line', 'x0': 3.3, 'x1': 5000, 'xref': "x", 'y0': 6.6, 'y1': 10000, 'yref': "y", 'line': { 'color': 'black', 'width': 2, } }, # and now the percentage plot { 'type': 'line', 'x0': 10, 'x1': 10, 'xref': "x2", 'y0': 0, 'y1': 100, 'yref': "y2", 'line': { 'color': 'yellow', 'width': 2, } }, { 'type': 'line', 'x0': 0, 'x1': 10000, 'xref': "x2", 'y0': 10, 'y1': 10, 'yref': "y2", 'line': { 'color': 'red', 'width': 2, } }, { 'type': 'line', 'x0': 0, 'x1': 10000, 'xref': "x2", 'y0': 90, 'y1': 90, 'yref': "y2", 'line': { 'color': 'blue', 'width': 2, } }, { 'type': 'line', 'x0': 0, 'x1': 10000, 'xref': "x2", 'y0': 66.6, 'y1': 66.6, 'yref': "y2", 'line': { 'color': 'black', 'width': 2, } }, { 'type': 'line', 'x0': 0, 'x1': 10000, 'xref': "x2", 'y0': 33.3, 'y1': 33.3, 'yref': "y2", 'line': { 'color': 'black', 'width': 2, } } ], ) #make both axes the same scale max_depth = max([sum([v['x'], v['y']]) for v in samples_dict.values()]) fig['layout']['xaxis1'].update(range=[max_depth * -0.05, max_depth * 1.05], title='Reference depth (vcf)') fig['layout']['yaxis1'].update(range=[max_depth * -0.05, max_depth * 1.05], title='Alternate depth (vcf)') fig['layout']['xaxis2'].update(range=[max_depth * -0.05, max_depth * 1.05], title='depth (vcf)') fig['layout']['yaxis2'].update(tickvals=[0, 25, 50, 75, 100], ticktext=['0%', '', '50%', '', '100%'], title='percent alternate allele') # making the traces themselves fig.append_trace( make_allele_depth_trace(samples_dict, jbrouwse_url=jbrouwse_url), 1, 1) fig.append_trace( make_allele_percent_trace(samples_dict, jbrouwse_url=jbrouwse_url), 1, 2) locus_div = pyoff.plot(fig, include_plotlyjs=False, output_type='div') return locus_div
def handle(self, *args, **options): """Lazy no error checking, relies on good collection of data. """ qs = Diary.objects.filter(staff__user__username='******').filter(day__year='2018') # group for tasktypes to stack in proportion to number of tasks per type # all adding up to the hours for the day traces = {} labels = {} for d in [{val: list()} for val in TaskType.objects.values_list('name', flat=True)]: traces.update(d) #labels.update(d) x = [] for ob in qs: # compile x axis array x.append(ob.day) # get the tasktypes of tasks on this day tasktypes = list(ob.tasks.values_list('tasktype__name', flat=True)) tasktypes.sort() # group the tasktypes and calculate as a fraction of the total hours for day # e.g. {'Duties': 4.0, 'Fields': 2.0, 'Gardens': 1.0} = 7 hours groups = {} for d in [{key: len(list(group))/len(tasktypes) * ob.hours} for key, group in groupby(tasktypes)]: groups.update(d) # update the dict of traces with each day for name, array in traces.items(): if name in groups.keys(): array.append(groups[name]) #labels[key].append(groups[key]/ob.hours * 100) else: array.append(0.0) #labels[key].append(0.0) pp = PrettyPrinter() data = [] colors = sequential.Thermal_10.hex_colors step = 0 for name, array in traces.items(): data.append(go.Bar( x=x, y=array, name=name, marker=dict(color=colors[step]), )) step = step + 1 title = 'Year to Date' layout = go.Layout( title=title, font=go.Font( family='Raleway, sans-serif' ), barmode='stack', showlegend=True, xaxis=go.XAxis( title='Day', tickangle=-45 ), yaxis=go.YAxis( title='Hours' ), ) layout.update(dict( shapes = [{ 'type': 'line', 'x0': x[0], 'x1': x[len(x) - 1], 'y0': 8, 'y1': 8, 'opacity': 0.7, 'line': { 'color': 'red', 'width': 1 } }] )) fig = go.Figure(data=data, layout=layout) py.plot(fig, filename=title) return # read from list comprehension to mark 'normal' working hours work_hours = pd.read_json( json.dumps( [{ 'day': ob.day, 'hours': 0 if ob.day.weekday() in [5, 6] or (ob.day.day < 15 and ob.day.month == 1) else 8 } for ob in qs ], default=str ) ) #img_path = 'my_first_figure.png' #py.image.save_as(fig, img_path) #img = Image.open(img_path) #img.show() return
def draw_text(self, **props): """Create an annotation dict for a text obj. Currently, plotly uses either 'page' or 'data' to reference annotation locations. These refer to 'display' and 'data', respectively for the 'coordinates' key used in the Exporter. Appropriate measures are taken to transform text locations to reference one of these two options. props.keys() -- [ 'text', (actual content string, not the text obj) 'position', (an x, y pair, not an mpl Bbox) 'coordinates', ('data', 'axes', 'figure', 'display') 'text_type', ('title', 'xlabel', or 'ylabel') 'style', (style dict, see below) 'mplobj' (actual mpl text object) ] props['style'].keys() -- [ 'alpha', (opacity of text) 'fontsize', (size in points of text) 'color', (hex color) 'halign', (horizontal alignment, 'left', 'center', or 'right') 'valign', (vertical alignment, 'baseline', 'center', or 'top') 'rotation', 'zorder', (precedence of text when stacked with other objs) ] """ self.msg += " Attempting to draw an mpl text object\n" if not mpltools.check_corners(props['mplobj'], self.mpl_fig): warnings.warn( "Looks like the annotation(s) you are trying \n" "to draw lies/lay outside the given figure size.\n\n" "Therefore, the resulting Plotly figure may not be \n" "large enough to view the full text. To adjust \n" "the size of the figure, use the 'width' and \n" "'height' keys in the Layout object. Alternatively,\n" "use the Margin object to adjust the figure's margins.") align = props['mplobj']._multialignment if not align: align = props['style']['halign'] # mpl default if 'annotations' not in self.plotly_fig['layout']: self.plotly_fig['layout']['annotations'] = go.Annotations() if props['text_type'] == 'xlabel': self.msg += " Text object is an xlabel\n" self.draw_xlabel(**props) elif props['text_type'] == 'ylabel': self.msg += " Text object is a ylabel\n" self.draw_ylabel(**props) elif props['text_type'] == 'title': self.msg += " Text object is a title\n" self.draw_title(**props) else: # just a regular text annotation... self.msg += " Text object is a normal annotation\n" if props['coordinates'] is not 'data': self.msg += " Text object isn't linked to 'data' " \ "coordinates\n" x_px, y_px = props['mplobj'].get_transform().transform( props['position']) x, y = mpltools.display_to_paper(x_px, y_px, self.plotly_fig['layout']) xref = 'paper' yref = 'paper' xanchor = props['style']['halign'] # no difference here! yanchor = mpltools.convert_va(props['style']['valign']) else: self.msg += " Text object is linked to 'data' " \ "coordinates\n" x, y = props['position'] axis_ct = self.axis_ct xaxis = self.plotly_fig['layout']['xaxis{0}'.format(axis_ct)] yaxis = self.plotly_fig['layout']['yaxis{0}'.format(axis_ct)] if (xaxis['range'][0] < x < xaxis['range'][1] and yaxis['range'][0] < y < yaxis['range'][1]): xref = 'x{0}'.format(self.axis_ct) yref = 'y{0}'.format(self.axis_ct) else: self.msg += " Text object is outside " \ "plotting area, making 'paper' reference.\n" x_px, y_px = props['mplobj'].get_transform().transform( props['position']) x, y = mpltools.display_to_paper(x_px, y_px, self.plotly_fig['layout']) xref = 'paper' yref = 'paper' xanchor = props['style']['halign'] # no difference here! yanchor = mpltools.convert_va(props['style']['valign']) annotation = go.Annotation( text=props['text'], opacity=props['style']['alpha'], x=x, y=y, xref=xref, yref=yref, align=align, xanchor=xanchor, yanchor=yanchor, showarrow=False, # change this later? font=go.Font(color=props['style']['color'], size=props['style']['fontsize'])) self.plotly_fig['layout']['annotations'] += annotation, self.msg += " Heck, yeah I drew that annotation\n"
data = go.Data([ go.Contour( z=Z2-Z1, x=x1, y=y1, colorscale='Hot', reversescale=True, opacity=0.9, contours=go.Contours( showlines=False) ) ]) layout = go.Layout( title= title, font= go.Font(family='Georgia, serif', color='#635F5D'), showlegend=False, autosize=False, width=650, height=650, xaxis=go.XAxis( range=[a,b], showgrid=False, nticks=7, title=x_label ), yaxis=go.YAxis( range=[c,d], showgrid=False, nticks=7, title=y_label
def plot_and_tables(excelfile,indexsheet): import xlrd import pandas as pd import plotly.plotly as py import plotly.graph_objs as pl xls = pd.ExcelFile(excelfile) book = xlrd.open_workbook(excelfile) this_sheet = book.sheet_by_index(indexsheet) nrows = int(this_sheet.cell(3, 2).value) ncols = int(this_sheet.cell(4, 2).value) indexopt = int(this_sheet.cell(7, 0).value) incluir_total_footer = int(this_sheet.cell(3, 9).value) type_graph_ind = list() trace_colors = list() for ii in range(1, ncols+1): type_graph_ind.append(int(this_sheet.cell(7, ii).value)) trace_colors.append(this_sheet.cell(8, ii).value) titulo = this_sheet.cell(2, 2).value descripcion = this_sheet.cell(5,1).value typeofgraph = int(this_sheet.cell(6, 2).value) if this_sheet.cell(3, 6).value =="a": maxvalue ="a" minvalue ="a" else: maxvalue = int(this_sheet.cell(3, 6).value) minvalue = int(this_sheet.cell(4, 6).value) if indexopt==0: data = xls.parse(indexsheet, skiprows=9,parse_cols=ncols, na_values=['NA']) else: # index_col=["none"] data = xls.parse(indexsheet, skiprows=9, parse_cols=ncols, na_values=['NA']) print(nrows) df = data[:nrows] print(df.head()) print('Llamando bibliotecas') #Plot with plotly py.sign_in("glezma", "0q6w6pozu7") print('Hecho!!') print('Procesando graficos') listdata = list() for count in range(1,ncols+1): print(type_graph_ind[count-1]) print(type_graph_ind) if type_graph_ind[count-1]==0: df.head() plot = pl.Scatter(x=df['Fecha'], y=df.ix[:,count], mode='lines+markers', marker=pl.Marker(size=8), name=df.columns[int(count)]) listdata.append(plot) print(count) layout = pl.Layout() elif type_graph_ind[count-1]==1: df.head() print(df['Fecha']) if df.ix[:,count].iloc[-1]>=0: plot = pl.Bar(x=df['Fecha'], y=df.ix[:,count], name=df.columns[int(count)],yaxis='y1', marker=pl.Marker( color=trace_colors[int(count-1)] ) ) else: plot = pl.Bar(x=df['Fecha'], y=df.ix[:,count], name=df.columns[int(count)],yaxis='y2',marker=pl.Marker( color=trace_colors[int(count-1)] )) listdata.append(plot) print(count) else: plot = pl.Scatter(x=df['Fecha'], y=df.ix[:,count], mode='lines+markers', marker=pl.Marker(size=8, color='rgba(0, 0, 0, 0.95)'), name=df.columns[int(count)],yaxis='y2') listdata.append(plot) print(count) layout = pl.Layout() if minvalue!="a": layout = pl.Layout(barmode='stack',bargap=0.6,yaxis=pl.YAxis(title='yaxis title',range=[minvalue, maxvalue]), yaxis2=pl.YAxis(title='yaxis title',side='right',overlaying='y', tickfont=pl.Font(color='rgb(1, 1, 1)'),range=[minvalue, maxvalue])) else: layout = pl.Layout() #layout = pl.Layout(barmode='stack',yaxis=pl.YAxis(title='yaxis title'),yaxis2=pl.YAxis(title='yaxis title',side='right',overlaying='y')) print('Hecho!!') pdata = pl.Data(listdata) fig = pl.Figure(data=pdata, layout=layout) print('Intentando conexion remota...') plot_url = py.plot(fig, filename='Repjs_'+ str(indexsheet), auto_open=False) plot_url = plot_url + '.embed' # plot_url = 'https://plot.ly/~glezma/271.embed' df1=df.set_index('Fecha').T summary_table = df1 .to_html() .replace('<table border="1" class="dataframe">', '<table class="display", align = "center", style="width:100%;">') # use bootstrap styling summary_table = summary_table .replace('<tr style="text-align: right;">', '<tr>') # use bootstrap styling if incluir_total_footer != 0: lastindex = df1.index.values[-1] toreplace = '''<tr>\n <th>''' + lastindex toplace = '<tfoot>\n <tr>\n <th>' + lastindex summary_table = summary_table .replace(toreplace, toplace) # use bootstrap styling toreplace ='</tbody>' toplace ='</tfoot></tbody>' summary_table = summary_table .replace(toreplace, toplace) return (summary_table, plot_url, titulo ,descripcion)
2: 'Download', 3: 'Upload', 4: 'Date' }, inplace=True) df = df.sort_values(by=['Date'], ascending=[1]) trace1 = go.Scatter( x=df['Date'], y=df['Download'], name='Download', ) trace2 = go.Scatter(x=df['Date'], y=df['Upload'], name='Upload', yaxis='y2') layout = go.Layout(title="Plateau Fiber Speed", xaxis=go.XAxis(title='Date'), yaxis=go.YAxis(title='Download speed (mb/s)', range=[0, 110]), yaxis2=go.YAxis( title='Upload speed (mb/s)', range=[0, 110], titlefont=go.Font(color='rgb(148, 103, 189)'), tickfont=go.Font(color='rgb(148, 103, 189)'), overlaying='y', side='right')) py.sign_in(user, api_key) data = go.Data([trace1, trace2]) fig = go.Figure(data=data, layout=layout) py.plot(fig, filename='Internet Speeds', world_readable=False, auto_open=False) py.image.save_as(fig, filename=os.path.join(img_path, 'internet_speeds.png'))
def main(): # Centrality for graph representation. centrality = Centrality.Centrality(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") centrality.arg.LoadEdgesData() betweenness = centrality.BetweenCentrality() closeness = centrality.ClosenessCentrality() eigenVec = centrality.EigenVector_Centrality() #PredictionModels, Split positive and negative data pos_train = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") neg_train = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") pos_val = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") neg_val = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") #Load Edges for each graph pos_train.LoadEdgesData() neg_train.LoadEdgesData() pos_val.LoadEdgesData() neg_val.LoadEdgesData() #Split into negative and positive files. pos_train.GenerateTrain() #we will get pos and neg files. pos_val.GenerateValidation() #we will get pos and neg files. #LabelData into dictionnary for files pos_train.LoadGeneratedFiles(pos_train.fluxTr) neg_train.LoadGeneratedFiles(neg_train.fluxTr_neg) pos_val.LoadGeneratedFiles(pos_val.fluxV) neg_val.LoadGeneratedFiles(neg_val.fluxVal_neg) X_train_pos = pos_train.LabelData() X_train_neg = neg_train.LabelData() X_val_pos = pos_val.LabelData() X_val_neg = neg_val.LabelData() print('----------------- Spliting and labeling data X & Y------------------------- \n') Y_train_pos = np.full(shape=(X_train_pos.shape[0],1), fill_value=1) Y_train_neg = np.full(shape=(X_train_neg.shape[0],1), fill_value=0) Y_val_pos = np.full(shape=(X_val_pos.shape[0],1), fill_value=1) Y_val_neg = np.full(shape=(X_val_neg.shape[0],1), fill_value=0) X_train = np.append(X_train_pos,X_train_neg,axis=0) y_train = np.append(Y_train_pos,Y_train_neg,axis=0) X_val = np.append(X_val_pos, X_val_neg, axis=0) y_val = np.append(Y_val_pos, Y_val_neg, axis=0) np.random.shuffle(X_train) np.random.shuffle(y_train) np.random.shuffle(X_val) np.random.shuffle(y_val) print('----------------- Done ------------------------- \n ') print('\n----------------- Linear Model Predictions ------------------------- \n') reg = linear_model.Ridge (alpha = .5) reg.fit(X=X_train[:-1],y=y_train[:-1]) reg.predict(X_train[-1:]) len(reg.predict(X_val)) np.mean((reg.predict(X_val) - y_val)**2) print('Log loss ',log_loss(y_val,reg.predict(X_val))) print('\n ----------------- Linear LinearRegression ------------------------- \n') regressor = LinearRegression() regressor.fit(X_train, y_train) print('Slope',regressor.intercept_) y_pred = regressor.predict(X_val) df = pd.DataFrame({'Actual': y_val.flatten(), 'Predicted': y_pred.flatten()}) #print(df) print('\n ----------------- SVM ------------------------- \n') clf_svm = svm.SVC() clf_svm.fit(X=X_train[:-1],y=y_train[:-1]) print(log_loss(y_val,clf_svm.predict(X_val))) print('\n ------------------------ Implementing Kernel SVM | Polynomial ------------------------ \n') svclassifier2 = svm.SVC(kernel='poly', degree=8,C=150) # this is the degre of the polynomial. svclassifier2.fit(X_train, y_train) #making prediction y_predp = svclassifier2.predict(X_val) #evaluating the poly svm print(confusion_matrix(y_val, y_predp)) print(classification_report(y_val, y_predp)) print('\n --------------------------- Implementing Kernel SVM | Linear -------------------------- \n') svclassifier1 = svm.SVC(kernel='linear') svclassifier1.fit(X_train, y_train) #Make predict y_pred = svclassifier1.predict(X_val) #Evaluating the Algorithm print(svclassifier1.score(X_val, y_val)) print(confusion_matrix(y_val,y_pred)) print(classification_report(y_val,y_pred)) print('\n ------------------------ Implementing Kernel SVM | Sigmoid ------------------------ \n') svclassifier4 = svm.SVC(kernel='sigmoid') svclassifier4.fit(X_train, y_train) #making predict y_preds = svclassifier4.predict(X_val) #Evaluating Algorithm print(confusion_matrix(y_val, y_preds)) print(classification_report(y_val, y_preds)) print('\n------------------------ Implementing Kernel SVM | Gaussian ------------------------\n') svclassifier3 = svm.SVC(kernel='rbf') svclassifier3.fit(X_train, y_train) #making predit y_predg = svclassifier3.predict(X_val) #Evaluating Algorithm print(confusion_matrix(y_val, y_predg)) print(classification_report(y_val, y_predg)) print('\n ------------------------ KNN ------------------------ \n') sc = StandardScaler() sc.fit(X_train) X_train = sc.transform(X_train) X_val = sc.transform(X_val) print('Value for K Math.sqrt(len of X_train) -------> ',math.sqrt(len(X_train))) print("Please wait for graph representation ....") accuracy = [] #We agregate the Accuracy averages for 18 neighbors. f1_scores = [] #Metrics ... index = range(3,81) for i in index: classifier = KNeighborsClassifier(n_neighbors = i, metric= 'euclidean', weights='uniform', leaf_size= 30) #27 classifiers classifier.fit(X_train, y_train) y_pred = classifier.predict(X_val) # Predict the class labels for the provided data conf_matrix = confusion_matrix(y_val, y_pred) # What we predit <VS> what actually is on test data. res = (conf_matrix[0, 0] + conf_matrix[1, 1]) / sum(sum(conf_matrix)) # Calculate Accuracy of our predit. accuracy.append(res) f1_scores.append(list(zip(y_val, y_pred))) print('In the range of 3 to 39 we have this values of accuracy') print(accuracy) # Evaluate the Model. print('We evaluate the Matrix of Confusion') mc = confusion_matrix(y_val, y_pred) print(classification_report(y_val, y_pred)) print(mc) # Graph representation plt.figure(figsize=(10, 6), num='Knn Algorithm Facebook Network Prediction') plt.plot(index, accuracy, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=10) plt.title('Accuracy ratio according to K values') plt.xlabel('K Values') plt.ylabel('Accuracy average') plt.show() #LoadLabels. nodesLabels = [] edges = [] #nodesLabels = [nodesLabels.append(eachEgo['name']) for eachEgo in centrality.arg.graph.vs] for eachEgo in centrality.arg.graph.vs: nodesLabels.append(eachEgo['name']) #edges = [edges.append(edge.tuple) for edge in centrality.arg.graph.es] #ça marche pas je ne sais pas pourquoi. for e in centrality.arg.graph.es: edges.append(e.tuple) layout = centrality.arg.graph.layout('kk', dim=3) #Prepare coordinates for Nodes and Edges. Xn=[layout[n][0] for n in range(len(centrality.arg.graph.vs))]# x-coordinates of nodes Yn=[layout[n][1] for n in range(len(centrality.arg.graph.vs))]# y-coordinates Zn=[layout[n][2] for n in range(len(centrality.arg.graph.vs))]# z-coordinates #Lists of edges. Xe=[] Ye=[] Ze=[] for e in edges: Xe+=[layout[e[0]][0],layout[e[1]][0], None]# x-coordinates of edge ends Ye+=[layout[e[0]][1],layout[e[1]][1], None] Ze+=[layout[e[0]][2],layout[e[1]][2], None] trace1=go.Scatter3d(x=Xe, y=Ye, z=Ze, mode='lines', line=go.Line(color='rgb(125,125,125)', width=1), hoverinfo='none' ) trace2=go.Scatter3d(x=Xn, y=Yn, z=Zn, mode='markers', name='Alters', marker=go.Marker(symbol='circle', color=eigenVec, size=10,colorbar=go.ColorBar( title='Node Degree' ), colorscale='Viridis', line=go.Line(color='rgb(158,18,130)', width=0.5) ), text=nodesLabels, hoverinfo='text' ) axis=dict(showbackground=True, showline=True, zeroline=False, showgrid=True, showticklabels=True, title='' ) plan = go.Layout( title="Facebook Ego-Network", width=1000, height=1000, showlegend=True, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest', annotations=go.Annotations([ go.Annotation( showarrow=True, xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='top', font=go.Font(size=14) ) ]),) data=go.Data([trace1, trace2]) fig=go.Figure(data=data, layout=plan) fig.show()
def make_readdepth_regression_plot(loci_dict,average=True): # this requires the R^2 added to loci_dict in the 'make_comparison_plots' function layout = go.Layout( title='distribution of the regression fits', width=1500, height=640, hovermode='closest', legend=dict( orientation='h' ), yaxis=dict( title='R^2 value' ), xaxis=dict( title='total reads of ' ) ) r_list = [] c_list = [] t_list = [] for sample,value in loci_dict.items(): info = value['info'] if 'r_squared' in info: if average: r_list.append(sum(info['r_squared'])/len(info['r_squared'])) else: r_list.append(max(info['r_squared'])) c_list.append(int(info['count'])) t_list.append(sample) #scatter trace scatter = go.Scatter( x = c_list, y = r_list, mode='markers', text=t_list, marker=dict( size=5, line=dict( width=0.5, ) ) ) # fitting logaritmic function """ #creating subset c_sub_list = [] r_sub_list = [] c_treshold = 300000 for i,count in enumerate(c_list): if count < c_treshold: c_sub_list.append(count) r_sub_list.append(r_list[i].round(2)) """ # the math function as python function def fivepl(x, b, c): a = 0 d = 1 g = 0.25 # https: // en.wikipedia.org / wiki / Generalised_logistic_function # https://stats.stackexchange.com/questions/190107/curve-fit-with-logarithmic-regression-in-python/338472#338472 return (((a - d) / numpy.power(1 + (numpy.power((x / c),b)), g)) + d) #popt,pcov = scipy.optimize.curve_fit(lambda t,a,b: a+b*numpy.log(t), c_list, r_list) popt,pcov = scipy.optimize.curve_fit(fivepl, c_list, r_list, bounds=([0.1,1000],[5,10000])) fit_max = max(c_list) xi = numpy.arange(1, fit_max, (fit_max/100)) line = fivepl(xi,*popt) fit_trace = go.Scatter( x=xi, y=line, mode='lines', marker=go.Marker(color='rgb(31, 119, 180)'), name='Fit' ) layout_text = "growth rate = {:.2f}<br>inflection point = {:0f}".format(*popt) annotation1 = go.Annotation( x=0, y=1, xref='paper', yref='paper', yanchor='middle', xanchor='left', text=layout_text, align='left', font=go.Font(size=8), showarrow=False ) #get the count cutoff cutoff = optimize.fsolve(lambda x: fivepl(x, *popt) - 0.965,1000) annotation2 = go.Annotation( x=cutoff[0], y=0.5, yanchor="middle", xanchor='left', text=str(int(cutoff[0])), align='left', showarrow=False ) layout.update( shapes=[{ 'type': 'line', 'x0': cutoff[0], 'x1': cutoff[0]+1, 'y0':0, 'y1':1, 'line': { 'color': 'rgb(55, 128, 191)', 'width': 3, 'dash': 'dashdot', }, }], annotations = [annotation1,annotation2] ) # return the graph fig = dict(data=[scatter,fit_trace], layout=layout) div = pyoff.plot(fig, include_plotlyjs=False, output_type='div') return div
def plot_plotly(self, plotly_filename=None, mpl_type=False, xlim=None, ylim=None, title=None, figsize=None, xlabel=None, ylabel=None, fontsize=None, show_legend=True, grid=False): """ Plot data using plotly library in IPython :param plotly_filename: name for resulting plot file on server (use unique name, else the same plot will be showen) :type plotly_filename: None or str :param bool mpl_type: use or not plotly converter from matplotlib (experimental parameter) :param xlim: x-axis range :param ylim: y-axis range :type xlim: None or tuple(x_min, x_max) :type ylim: None or tuple(y_min, y_max) :param title: title :type title: None or str :param figsize: figure size :type figsize: None or tuple(weight, height) :param xlabel: x-axis name :type xlabel: None or str :param ylabel: y-axis name :type ylabel: None or str :param fontsize: font size :type fontsize: None or int :param bool show_legend: show or not labels for plots :param bool grid: show grid or not """ import plotly.plotly as py from plotly import graph_objs from ipykernel import connect plotly_filename = self.plotly_filename if plotly_filename is None else plotly_filename try: connection_file_path = connect.find_connection_file() connection_file = os.path.basename(connection_file_path) if '-' in connection_file: kernel_id = connection_file.split('-', 1)[1].split('.')[0] else: kernel_id = connection_file.split('.')[0] except Exception as e: kernel_id = "no_kernel" PLOTLY_API_USER, PLOTLY_API_KEY, PLOTLY_USER = self._plotly_config() save_name = '{user}_{id}:{name}'.format(user=PLOTLY_USER, id=kernel_id, name=plotly_filename) py.sign_in(PLOTLY_API_USER, PLOTLY_API_KEY) if mpl_type: self.plot(new_plot=True, xlim=xlim, ylim=ylim, title=title, figsize=figsize, xlabel=xlabel, ylabel=ylabel, fontsize=fontsize, grid=grid) mpl_fig = plt.gcf() update = dict( layout=dict( showlegend=show_legend ), data=[dict(name=leg) for leg in mpl_fig.legends] ) return py.iplot_mpl(mpl_fig, width=self.figsize[0] * 60, update=update, height=self.figsize[1] * 60, filename=save_name, fileopt='overwrite') xlabel = self.xlabel if xlabel is None else xlabel ylabel = self.ylabel if ylabel is None else ylabel title = self.title if title is None else title figsize = self.figsize if figsize is None else figsize fontsize = self.fontsize if fontsize is None else fontsize layout = graph_objs.Layout(yaxis={'title': ylabel, 'ticks': ''}, xaxis={'title': xlabel, 'ticks': ''}, showlegend=show_legend, title=title, font=graph_objs.Font(family='Courier New, monospace', size=fontsize), width=figsize[0] * self.PLOTLY_RESIZE, height=figsize[1] * self.PLOTLY_RESIZE ) fig = self._plot_plotly(layout) return py.iplot(fig, width=figsize[0] * self.PLOTLY_RESIZE, height=figsize[1] * self.PLOTLY_RESIZE, filename=save_name)
def nx_graph_to_plotly_fig(g, pos): #print(pos) x_nodes = [pos[k][0] for k in g.nodes()] y_nodes = [pos[k][1] for k in g.nodes()] labels = [g.node[k]['label'] for k in g.nodes()] edge_traces = [] colors = ['red','green','blue','orange'] ptr_labels = ['left', 'right', 'next'] for num, lbl in enumerate(ptr_labels): x_src_edges = [] y_src_edges = [] x_trg_edges = [] y_trg_edges = [] if lbl == 'right': offset_factor = -1 else: offset_factor = 1 def add_edge(from_, to, is_src_edge): if is_src_edge: x_ls, y_ls = x_src_edges, y_src_edges else: x_ls, y_ls = x_trg_edges, y_trg_edges x_ls.extend([from_[0], to[0], None]) y_ls.extend([from_[1], to[1], None]) #print('Added edge from {} to {}'.format(from_, to)) # Compute edge positions for edge in g.edges(): src, trg = edge[0], edge[1] if g[src][trg]['label'] != lbl: continue src_pos = [pos[src][i] for i in (0,1)] trg_pos = [pos[trg][i] for i in (0,1)] diff_x = trg_pos[0] - src_pos[0] diff_y = trg_pos[1] - src_pos[1] normal_vec = [diff_y, - diff_x] #print('src={}, trg={}, src_pos={}, trg_pos={}, diff={}, {}, normal_vec = {}'.format(src, trg, src_pos, trg_pos, diff_x, diff_y, normal_vec)) offset_step = 0.05 num_frags = 64 for i in range(num_frags): src_num_steps = min(num_frags - i, i) trg_num_steps = min(num_frags - (i + 1), i + 1) src_offset = offset_factor * offset_step * math.sin(src_num_steps / num_frags * math.pi ) trg_offset = offset_factor * offset_step * math.sin(trg_num_steps / num_frags * math.pi ) src_pos = [pos[src][j] + ((pos[trg][j] - pos[src][j]) * i / num_frags) + src_offset * normal_vec[j] for j in (0,1)] trg_pos = [pos[src][j] + ((pos[trg][j] - pos[src][j]) * (i+1) / num_frags) + trg_offset * normal_vec[j] for j in (0,1)] is_src_edge = i < num_frags * 3 / 4 add_edge(src_pos, trg_pos, is_src_edge) # Create edge traces col = colors[num] edge_traces.extend([ go.Scatter(x=x_src_edges, y=y_src_edges, mode='lines', name=lbl, textposition='top', line=go.Line(color=col, width=2), hoverinfo='none', showlegend=True ), go.Scatter(x=x_trg_edges, y=y_trg_edges, mode='lines', line=go.Line(color=col, width=7), showlegend=False, hoverinfo='none' ) ]) vertex_trace=go.Scatter(x=x_nodes, y=y_nodes, mode='markers+text', marker=go.Marker(symbol='dot', size=18, color='rgb(127,127,255)', line=go.Line(color='rgb(50,50,50)', width=2) ), text=labels, showlegend=False, textposition='top', hoverinfo='text' ) data=go.Data([*edge_traces, vertex_trace]) layout=go.Layout(title=g.graph.get('title', 'Model'), font=go.Font(size=12), xaxis=go.XAxis(showgrid=False, zeroline=False, showticklabels=False), yaxis=go.YAxis(showgrid=False, zeroline=False, showticklabels=False), hovermode='closest' ) fig=go.Figure(data=data, layout=layout) #print(fig) return fig
def get_figure( self, emin=None, emax=None, width=800, height=400, margin_left=100, margin_bottom=60, margin_top=30, margin_right=0, colorbar_size=0.4, xaxis_label=None, yaxis_label=None, xaxis_nticks=None, yaxis_nticks=None, xtick_angle=30, font='"Droid Serif", "Open Serif", serif', font_size=12, title_font_size=None, show_sample_labels=True, **kwargs): """Generate a plotly figure of the heatmap. Parameters ---------- emin : int, float, or None, optional The expression value corresponding to the lower end of the colorscale. If None, determine, automatically. [None] emax : int, float, or None, optional The expression value corresponding to the upper end of the colorscale. If None, determine automatically. [None] margin_left : int, optional The size of the left margin (in px). [100] margin_right : int, optional The size of the right margin (in px). [0] margin_top : int, optional The size of the top margin (in px). [30] margin_bottom : int, optional The size of the bottom margin (in px). [60] colorbar_size : int or float, optional The sze of the colorbar, relative to the figure size. [0.4] xaxis_label : str or None, optional X-axis label. If None, use `ExpMatrix` default. [None] yaxis_label : str or None, optional y-axis label. If None, use `ExpMatrix` default. [None] xtick_angle : int or float, optional X-axis tick angle (in degrees). [30] font : str, optional Name of font to use. Can be multiple, separated by comma, to specify a prioritized list. [' "Droid Serif", "Open Serif", "serif"'] font_size : int or float, optional Font size to use throughout the figure, in points. [12] title_font_size : int or float or None, optional Font size to use for labels on axes and the colorbar. If None, use `font_size` value. [None] show_sample_labels : bool, optional Whether to show the sample labels. [True] Returns ------- `plotly.graph_objs.Figure` The plotly figure. """ # emin and/or emax are unspecified, set to data min/max values if emax is None: emax = self.matrix.X.max() if emin is None: emin = self.matrix.X.min() title = self.title if title_font_size is None: title_font_size = font_size colorbar_label = self.colorbar_label or 'Expression' colorbar = go.ColorBar( lenmode='fraction', len=colorbar_size, title=colorbar_label, titlefont=dict( size=title_font_size, ), titleside='right', xpad=0, ypad=0, outlinewidth=0, # no border thickness=20, # in pixels # outlinecolor = '#000000', ) def fix_plotly_label_bug(labels): """ This fixes a bug whereby plotly treats labels that look like numbers (integers or floats) as numeric instead of categorical, even when they are passed as strings. The fix consists of appending an underscore to any label that looks like a number. """ assert isinstance(labels, Iterable) fixed_labels = [] for l in labels: try: float(l) except (ValueError, TypeError): fixed_labels.append(str(l)) else: fixed_labels.append(str(l) + '_') return fixed_labels x = fix_plotly_label_bug(self.matrix.samples) gene_labels = self.matrix.genes.tolist() if self.gene_aliases: for i, gene in enumerate(gene_labels): try: alias = self.gene_aliases[gene] except KeyError: pass else: gene_labels[i] = '%s/%s' % (gene, alias) gene_labels = fix_plotly_label_bug(gene_labels) data = [ go.Heatmap( z=self.matrix.X, x=x, y=gene_labels, zmin=emin, zmax=emax, colorscale=self.colorscale, colorbar=colorbar, hoverinfo='x+y+z', **kwargs ), ] xticks = 'outside' if not show_sample_labels: xticks = '' if xaxis_label is None: if self.matrix.samples.name is not None: xaxis_label = self.matrix.samples.name else: xaxis_label = 'Samples' xaxis_label = xaxis_label + ' (n = %d)' % self.matrix.n if yaxis_label is None: if self.matrix.genes.name is not None: yaxis_label = self.matrix.genes.name else: yaxis_label = 'Genes' yaxis_label = yaxis_label + ' (p = %d)' % self.matrix.p layout = go.Layout( width=width, height=height, title=title, titlefont=go.Font( size=title_font_size ), font=go.Font( size=font_size, family=font ), xaxis=go.XAxis( title=xaxis_label, titlefont=dict(size=title_font_size), showticklabels=show_sample_labels, ticks=xticks, nticks=xaxis_nticks, tickangle=xtick_angle, showline=True ), yaxis=go.YAxis( title=yaxis_label, titlefont=dict(size=title_font_size), nticks=yaxis_nticks, autorange='reversed', showline=True ), margin=go.Margin( l=margin_left, t=margin_top, b=margin_bottom, r=margin_right, pad=0 ), ) # add annotations # we need separate, but overlaying, axes to place the annotations layout['xaxis2'] = go.XAxis( overlaying = 'x', showline = False, tickfont = dict(size=0), autorange=False, range=[-0.5, self.matrix.n-0.5], ticks='', showticklabels=False ) layout['yaxis2'] = go.YAxis( overlaying='y', showline=False, tickfont=dict(size=0), autorange=False, range=[self.matrix.p-0.5, -0.5], ticks='', showticklabels=False ) # gene (row) annotations for ann in self.gene_annotations: i = self.matrix.genes.get_loc(ann.gene) xmn = -0.5 xmx = self.matrix.n-0.5 ymn = i-0.5 ymx = i+0.5 #logger.debug('Transparency is %.1f', ann.transparency) data.append( go.Scatter( x=[xmn, xmx, xmx, xmn, xmn], y=[ymn, ymn, ymx, ymx, ymn], mode='lines', hoverinfo='none', showlegend=False, line=dict(color=ann.color), xaxis='x2', yaxis='y2', #opacity=0.5, opacity=1-ann.transparency, ) ) if ann.label is not None: layout.annotations.append( go.Annotation( text=ann.label, x=0.01, y=i-0.5, #y=i+0.5, xref='paper', yref='y2', xanchor='left', yanchor='bottom', showarrow=False, bgcolor='white', #opacity=1-ann.transparency, opacity=0.8, borderpad=0, #textangle=30, font=dict(color=ann.color) ) ) # sample (column) annotations for ann in self.sample_annotations: j = self.matrix.samples.get_loc(ann.sample) xmn = j-0.5 xmx = j+0.5 ymn = -0.5 ymx = self.matrix.p-0.5 data.append( go.Scatter( x=[xmn, xmx, xmx, xmn, xmn], y=[ymn, ymn, ymx, ymx, ymn], mode='lines', hoverinfo='none', showlegend=False, line=dict(color=ann.color), xaxis='x2', yaxis='y2', opacity=1.0) ) if ann.label is not None: layout.annotations.append( go.Annotation( text=ann.label, y=0.99, x=j+0.5, #y=i+0.5, xref='x2', yref='paper', xanchor='left', yanchor='top', showarrow=False, bgcolor='white', opacity=1-ann.transparency, borderpad=0, textangle=90, font=dict(color=ann.color) ) ) fig = go.Figure( data=data, layout=layout ) return fig
def get_plotly_networkx_3d(self, varset="all", include_plotlyjs=False): import networkx as nx g, edge_labels = nx.Graph(), {} for i, (name, var) in enumerate(self.items()): #if i == 5: break if varset != "all" and var.varset != varset: continue g.add_node(var, name=name) for parent in var.get_parents(): #print(parent, "is parent of ", name) parent = self[parent] g.add_edge(parent, var) # TODO: Add getters! What about locked nodes! #i = [dep.node for dep in child.deps].index(task) #edge_labels[(task, child)] = " ".join(child.deps[i].exts) # Get positions for all nodes using layout_type. # e.g. pos = nx.spring_layout(g) #pos = getattr(nx, layout_type + "_layout")(g) #, scale=100000, iterations=30) pos = nx.spring_layout(g, dim=3) import plotly.graph_objs as go trace1 = go.Scatter3d(x=[], y=[], z=[], mode='lines', line=go.Line(width=2.0, color='#888'), #, dash="dot"), hoverinfo='none', ) for edge in g.edges(): x0, y0, z0 = pos[edge[0]] x1, y1, z1 = pos[edge[1]] trace1['x'] += [x0, x1, None] trace1['y'] += [y0, y1, None] trace1['z'] += [z0, z1, None] trace2 = go.Scatter3d( x=[], y=[], z=[], name='variable', marker=go.Marker(symbol='dot', size=10, #color=group, colorscale='Viridis', line=go.Line(width=2) ), text=[v.name for v in g], textposition='center', mode='markers+text', hoverinfo='text', hovertext=[v.mnemonics for v in g.nodes()], ) for node in g.nodes(): x, y, z = pos[node] trace2['x'].append(x) trace2['y'].append(y) trace2['z'].append(z) axis=dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='' ) layout = go.Layout( title="Abinit variables (3D visualization)", width=1000, height=1000, showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest', annotations=go.Annotations([go.Annotation( showarrow=False, #text="Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1]</a>", text="Hello", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=go.Font(size=14), )]), ) data = go.Data([trace1, trace2]) fig = go.Figure(data=data, layout=layout) import plotly s = plotly.offline.plot(fig, include_plotlyjs=include_plotlyjs, output_type='div') return s
def get_figure(self, **kwargs): """Get a plotly figure of the heatmap.""" emin = kwargs.pop('emin', -1.0) emax = kwargs.pop('emax', 1.0) width = kwargs.pop('width', 800) height = kwargs.pop('height', 600) margin_left = kwargs.pop('margin_left', 100) margin_bottom = kwargs.pop('margin_bottom', 60) margin_top = kwargs.pop('margin_top', 30) margin_right = kwargs.pop('margin_right', 0) colorbar_size = kwargs.pop('colorbar_size', 0.4) xaxis_label = kwargs.pop('xaxis_label', None) yaxis_label = kwargs.pop('yaxis_label', None) xticks_angle = kwargs.pop('xaxis_angle', 30) font = kwargs.pop('font', '"Droid Serif", "Open Serif", serif') font_size = kwargs.pop('font_size', 12) title = kwargs.pop('title', None) title_font_size = kwargs.pop('title_font_size', None) annotation_font_size = kwargs.pop('annotation_font_size', None) show_sample_labels = kwargs.pop('show_sample_labels', 'x') if show_sample_labels not in ['none', 'x', 'y', 'both']: raise ValueError('"show_sample_labels" must be "none", "x", "y", ' 'or "both".') padding_top = kwargs.pop('padding_top', 0.1) padding_right = kwargs.pop('padding_top', 0.1) xaxis_nticks = kwargs.pop('xaxis_nticks', None) #yaxis_nticks = kwargs.pop('yaxis_nticks', None) if title_font_size is None: title_font_size = font_size if annotation_font_size is None: annotation_font_size = font_size colorbar_label = self.colorbar_label or 'Pearson Correlation' ### set up heatmap colorbar = go.ColorBar( lenmode='fraction', len=colorbar_size, title=colorbar_label, titlefont=dict(size=title_font_size, ), titleside='right', xpad=0, ypad=0, outlinewidth=0, # no border thickness=20, # in pixels # outlinecolor = '#000000', ) def fix_plotly_label_bug(labels): """ This fixes a bug whereby plotly treats labels that look like numbers (integers or floats) as numeric instead of categorical, even when they are passed as strings. The fix consists of appending an underscore to any label that looks like a number. """ assert isinstance(labels, Iterable) fixed_labels = [] for l in labels: try: float(l) except ValueError: fixed_labels.append(str(l)) else: fixed_labels.append(str(l) + '_') return fixed_labels x = fix_plotly_label_bug(self.corr_matrix.samples) y = x data = [ go.Heatmap(z=self.corr_matrix.X, x=x, y=y, zmin=emin, zmax=emax, colorscale=self.colorscale, colorbar=colorbar, hoverinfo='x+y+z', **kwargs), ] xshowticklabels = False yshowticklabels = False ### set up layout if show_sample_labels == 'x': xshowticklabels = True elif show_sample_labels == 'y': yshowticklabels = True elif show_sample_labels == 'both': xshowticklabels = True yshowticklabels = True xticks = 'outside' yticks = 'outside' if xaxis_label is None: if self.corr_matrix.samples.name is not None: xaxis_label = self.corr_matrix.samples.name else: xaxis_label = 'Samples' xaxis_label = xaxis_label + ' (n = %d)' % self.corr_matrix.n if yaxis_label is None: yaxis_label = xaxis_label layout = go.Layout( width=width, height=height, title=title, titlefont=go.Font(size=title_font_size), font=go.Font(size=font_size, family=font), xaxis=go.XAxis( title=xaxis_label, titlefont=dict(size=title_font_size), showticklabels=xshowticklabels, ticks=xticks, nticks=xaxis_nticks, tickangle=xticks_angle, #range=[-0.5, self.corr_matrix.n-0.5], showline=True, zeroline=False, showgrid=False, ), yaxis=go.YAxis( title=yaxis_label, titlefont=dict(size=title_font_size), showticklabels=yshowticklabels, ticks=xticks, nticks=xaxis_nticks, autorange='reversed', showline=True, zeroline=False, showgrid=False, ), margin=go.Margin(l=margin_left, t=margin_top, b=margin_bottom, r=margin_right, pad=0), ) ### add annotations # we need separate, but overlaying, axes to place the annotations layout['xaxis2'] = go.XAxis( overlaying='x', showline=False, tickfont=dict(size=0), autorange=False, #range=[-0.5, self.corr_matrix.n-0.5], range=[-0.5, self.corr_matrix.n - 0.5], ticks='', showticklabels=False, zeroline=False, showgrid=False, ) layout['yaxis2'] = go.YAxis( overlaying='y', showline=False, tickfont=dict(size=0), autorange=False, range=[self.corr_matrix.n - 0.5, -0.5], ticks='', showticklabels=False, zeroline=False, showgrid=False, ) # generate coordinates and labels for the block annotations k = len(self.block_annotations) block_coords = np.zeros((k, 2), dtype=np.float64) block_labels = [] for i, ann in enumerate(self.block_annotations): block_coords[i, :] = [ann.start_index - 0.5, ann.end_index + 0.5] block_labels.append(ann.label) # this produces the squares for the block annotations for i in range(k): mn = block_coords[i, 0] mx = block_coords[i, 1] data.append( go.Scatter( x=[mn, mx, mx, mn, mn], y=[mn, mn, mx, mx, mn], mode='lines', hoverinfo='none', showlegend=False, line=dict(color='black'), xaxis='x2', yaxis='y2', )) # - this produces the square labels for the block annotations # - we use plotly annotations, so that the labels are not limited # by the plotting area for i in range(k): mn = block_coords[i, 0] mx = block_coords[i, 1] layout.annotations.append( dict( x=mx, y=(mn + mx) / 2.0, text=block_labels[i], xref='x2', yref='y2', showarrow=False, #ax=20, #ay=-20, xanchor='left', yanchor='middle', font=dict( color='black', size=annotation_font_size, family='serif bold', ), bgcolor='white', opacity=0.7, #textanchor='top right' )) fig = go.Figure(data=data, layout=layout) return fig
sorted_by_rank = sorted(list(simranks.items()), key=lambda x: -x[1]) sorted_by_route = sorted(list(simranks.items()), key=lambda x: x[0]) # sorted_by_rank = sorted_by_rank[1:] rank_only = [one_to_zero(sr) for rt, sr in sorted_by_route] ranks_2d.append(rank_only) routes.append(route) # sr_mean = statistics.mean(rank_only) # sr_std = statistics.stdev(rank_only) # out = str([abrv(rt) for rt, sr in sorted_by_rank]).replace("'", "") # print(abrv(route), min(rank_only), max(rank_only), sr_mean, sr_std) print(ranks_2d) print(routes) layout = go.Layout( title="MBTA Route SimRank", # set plot's title font=go.Font(family="Droid Sans, sans-serif", ), xaxis=go.XAxis( title='Routes', # x-axis title showgrid=False # remove grid ), yaxis=go.YAxis( title='Routes', # y-axis title # autorange='reversed', # (!) reverse tick ordering showgrid=False, # remove grid autotick=False, # custom ticks dtick=1 # show 1 tick per day ) # autosize=False, # custom size # height=height, # plot's height in pixels # width=width # plot's width in pixels )