import pandas as pd import csv import plotly.graph_objects as go df=pd.read_csv("data.csv") print(df.groupby("level")["attempt"].mean()) fig=go.Figure(go.Bar( x=df.groupby("level")["attempt"].mean(), y=['Level 1','Level 2','Level 3','Level 4'], orientation='h' )) fig.show()
conteo.columns = ['Facultad', 'Conteo', 'encuestados'] ###################### #3 Creando el grafico# ###################### #Facultades trace_0 = go.Pie(labels=conteo.loc[:, 'Facultad'], values=conteo.loc[:, 'encuestados']) layout_0 = go.Layout(title='% Muestra Facultades UNAs', xaxis=dict(title='xvalue', zeroline=False, showline=True), yaxis=dict(title='yvalue', zeroline=False, showline=True), hovermode='closest') fig_0 = go.Figure(data=[trace_0], layout=layout_0) ######################## #4 Creacion Dash Layout# ######################## def create_layout(app): return html.Div([dcc.Graph(id='plot_0', figure=fig_0)], className="six columns", style={ 'width': '50%', 'display': 'inline-block' })
def update_map(neighbor,room,price): if (neighbor is None) or (room is None) or (price is None): notice = do.Figure(do.Indicator( mode= 'number', value= 0, title = {"text": 'Please apply filters','font':{'color':'#ffffff','size':15}}, number={'font':{'color':'#ffffff'}})) notice.update_layout(margin= do.layout.Margin(t=0,b=0), plot_bgcolor='#303030', paper_bgcolor='#303030') return notice,notice,notice,notice,notice init_df = data[(data['neighbourhood']==neighbor)&(data['room_type']==room)&(data['price'] < int(price))] df_lat = list(init_df['latitude']) df_lon = list(init_df['longitude']) df_name = list(init_df['name']) df_host = list(init_df['host_name']) df_price = list(init_df['price']) df_nite = list(init_df['minimum_nights']) df_rev = list(init_df['number_of_reviews']) df_last = list(init_df['last_review']) df_ava = list(init_df['availability_365']) midpoint = (np.average(init_df['latitude']), np.average(init_df['longitude'])) df_reviewed = init_df['number_of_reviews'].max() df_available = init_df['availability_365'].max() colored = np.array(['#29EA8B']*init_df.shape[0]) colored[init_df['number_of_reviews'] == df_reviewed] = '#3498db' colored[init_df['availability_365'] == df_available] = '#f39c12' sized = np.array([7]*init_df.shape[0]) sized[init_df['number_of_reviews'] == df_reviewed] = 20 sized[init_df['availability_365'] == df_available] = 20 hover_text = ['Name: '+'{}'.format(t) + '<br>Host: '+'{}'.format(u) + '<br>Price: '+'{}'.format(v) + '<br>Minimum Nights: '+'{}'.format(w) + '<br>No. of Reviews: '+'{}'.format(x) + '<br>Last Review: '+'{}'.format(y) + '<br>Available days per year: '+'{}'.format(z) for t,u,v,w,x,y,z in zip(df_name,df_host,df_price,df_nite,df_rev,df_last,df_ava)] map1 = do.Figure() map1.add_trace( do.Scattermapbox( lat=df_lat, lon=df_lon, mode='markers', marker=do.scattermapbox.Marker( size=sized, color=colored, opacity=0.7 ), text= hover_text, hoverinfo='text', showlegend= False ) ) map1.update_layout( mapbox= do.layout.Mapbox( accesstoken= map_token, center= do.layout.mapbox.Center( lat= midpoint[0], lon= midpoint[1] ), zoom= 13, style= map_style ), margin= do.layout.Margin( l=0, r=0, t=0, b=0 ) ) card1 = do.Figure(do.Indicator( mode= 'number', value= len(init_df), title = {"text": 'Number of Listings','font':{'color':'#ffffff'}}, number={'font':{'color':'#ffffff', 'size':70}})) card1.update_layout(margin= do.layout.Margin(t=40,b=0), plot_bgcolor='#303030', paper_bgcolor='#303030') card2 = do.Figure(do.Indicator( mode= 'number', value= np.average(init_df['price']), title = {"text": 'Average Price','font':{'color':'#ffffff'}}, number={'valueformat':'.1f','font':{'color':'#ffffff', 'size':70}})) card2.update_layout(margin= do.layout.Margin(t=40,b=0), plot_bgcolor='#303030', paper_bgcolor='#303030') reviewed = do.Figure(do.Indicator( mode= 'number', value= df_reviewed, title = {"text": "<span style='font-size:0.95em;color:white'>Most Reviews</span><br><span style='font-size:0.7em;color:#3498db'>{}</span>".format(init_df.loc[init_df['number_of_reviews'] == df_reviewed].name.values[0])}, number={'font':{'color':'#ffffff'}})) reviewed.update_layout(margin= do.layout.Margin(t=58,b=0), plot_bgcolor='#303030', paper_bgcolor='#303030') availbl = do.Figure(do.Indicator( mode= 'number', value= df_available, title = {"text": "<span style='font-size:0.95em;color:white'>Highly Available (days/year)</span><br><span style='font-size:0.7em;color:#f39c12'>{}</span>".format(init_df.loc[init_df['availability_365'] == df_available].name.values[0])}, number={'font':{'color':'#ffffff'}})) availbl.update_layout(margin= do.layout.Margin(t=58,b=0), plot_bgcolor='#303030', paper_bgcolor='#303030') return map1,card1,card2,reviewed,availbl
def build_continent_map(map_date,val='Active', continent = 'World', pop = 1): df_continent = get_df_projections() if continent !='World': df_continent = df_continent.loc[df_continent.Continent == continent] #Filter by continent if map_date is None: return None if isinstance(map_date, str): map_date = datetime.datetime.strptime(map_date, '%Y-%m-%d').date() df_map = df_continent.loc[df_continent['Day'] == map_date] df_map = df_map.loc[df_map['Province'] == 'None'] #exclude province data df_map = df_map.loc[df_map['Country'] != 'None'] #exclude global world data population = np.array([]) PopInfo = pd.read_csv('data/predicted/WorldPopulationInformation.csv', sep=",") for i in df_map['Country']: ind1 = np.logical_and(PopInfo['Country']==i, PopInfo['Province']=='None') pop_val = PopInfo.loc[ind1,'pop'].values population = np.concatenate((population, pop_val),0) df_map['Population'] =population df_map['Active Per Million'] = (np.round(1000000*df_map['Active']/df_map['Population'], decimals = 2)) df_map['Total Detected Per Million'] = (np.round(1000000*df_map['Total Detected']/df_map['Population'], decimals = 2)) df_map['Active Hospitalized Per Million'] = (np.round(1000000*df_map['Active Hospitalized']/df_map['Population'], decimals = 2)) df_map['Cumulative Hospitalized Per Million'] = (np.round(1000000*df_map['Cumulative Hospitalized']/df_map['Population'], decimals = 2)) df_map['Total Detected Deaths Per Million'] = (np.round(1000000*df_map['Total Detected Deaths']/df_map['Population'], decimals = 2)) df_map = df_map.applymap(str) cols = get_cols() if (val is not None) and (val in cols) and pop == 1: df_map.loc[:,'text'] = df_map['Country'] + '<br>' + \ 'Total Detected ' + df_map['Total Detected'] + '<br>' + \ 'Active ' + df_map['Active'] + '<br>' + \ 'Active Hospitalized ' + df_map['Active Hospitalized'] + '<br>' + \ 'Cumulative Hospitalized ' + df_map['Cumulative Hospitalized'] + '<br>' + \ 'Total Detected Deaths ' + df_map['Total Detected Deaths'] zval = df_map[val].astype(float) if (val is not None) and (val in cols) and pop != 1: df_map.loc[:,'text'] = df_map['Country'] + '<br>' + \ 'Total Detected Per Million ' + df_map['Total Detected Per Million'] + '<br>' + \ 'Active Per Million ' + df_map['Active Per Million'] + '<br>' + \ 'Active Hospitalized Per Million ' + df_map['Active Hospitalized Per Million'] + '<br>' + \ 'Cumulative Hospitalized Per Million ' + df_map['Cumulative Hospitalized Per Million'] + '<br>' + \ 'Total Detected Deaths Per Million ' + df_map['Total Detected Deaths Per Million'] zval = df_map[val+ " Per Million"].astype(float) if (val is not None) and (val in cols): fig = go.Figure(data=go.Choropleth( locations=df_map['Country'], z= zval, locationmode="country names", autocolorscale=False, colorscale='inferno_r', text=df_map['text'], # hover text marker_line_color='black', # line markers between states colorbar_title='<br>'.join(wrap(''.join(['{}'.format(add_cases(val))]), width=10)) )) fig.update_layout( margin=dict(l=10, r=10, t=50, b=50), title_text=add_cases('{} Predicted {} {}'.format(map_date.strftime('%b %d,%Y'), continent, val)), geo = dict( scope= continent.lower() if continent is not None else None, projection=go.layout.geo.Projection(type = 'natural earth'), showlakes=True, # lakes lakecolor='rgb(255, 255, 255)', countrycolor='lightgray', landcolor='whitesmoke', showland=True, showframe = False, showcoastlines = True, showcountries=True, visible = False, ), modebar={ 'orientation': 'v', 'bgcolor': 'rgba(0,0,0,0)', 'color': 'lightgray', 'activecolor': 'gray' } ) graph = dcc.Graph( id='continent-projection-map', figure=fig, ) return graph return
def main(): # get data selected_seasons = GC.season(compare=True) compares = GC.seasonMetrics(compare=True, filter='Data contains "P"') # all pmc data pmc_dict = GC.seasonPmc(all=True, metric="BikeStress") pmc = pd.DataFrame(pmc_dict, index=pmc_dict['date']) fig = go.Figure() intensity_factor = [] stress_balance = [] for compare, season_name in zip(compares, selected_seasons['name']): if 'Workout_Title' in compare[0]: metrics = pd.DataFrame(compare[0], index=compare[0]['date']).filter( ['date', 'IF', 'color', 'Workout_Code', 'BikeStress', 'Workout_Title']) else: metrics = pd.DataFrame(compare[0], index=compare[0]['date']).filter( ['date', 'IF', 'color', 'Workout_Code', 'BikeStress']) # Filter out IF = 0 (probably rides without power) metrics = metrics.loc[metrics.IF > 0] # combine pmc and metric data merged_metrics = pd.merge(metrics, pmc) stress_balance.extend(merged_metrics.sb.tolist()) intensity_factor.extend(merged_metrics.IF.tolist()) # Determine the radius of the circles based on BikeStress (on a scale of 30-100) # merged_metrics['radius'] = np.sqrt((merged_metrics.BikeStress / 3.1415927)) a = 30 b = 100 minimal = merged_metrics.BikeStress.min() maximal = merged_metrics.BikeStress.max() div = maximal - minimal if div == 0: div = 1 # norm = [(number - a) / (b - a) for number in residual] merged_metrics['radius'] = a + ((merged_metrics.BikeStress - minimal) * (b - a) / div) # norm = [a + ((number - minimal) * (b - a) / (maximal - minimal)) for number in residual] # Determine hovertext titles = "Title: " + merged_metrics.Workout_Title.map(str) if 'Workout_Title' in merged_metrics else "" merged_metrics['date'] = pd.to_datetime(merged_metrics.date) merged_metrics['hovertext'] = "Date: " + merged_metrics.date.dt.strftime('%d-%m-%Y').map(str) + "<br>" + \ "TSS: " + merged_metrics.BikeStress.astype(int).map(str) + "<br>" + \ "TSB: " + round(merged_metrics.sb, 1).map(str) + "<br>" + \ titles # make transparent for overlapping # colors <- adjustcolor(merged_metrics.color, 0.6) if not len(compares) == 1: color = compare[1] merged_metrics['color'] = color merged_metrics['legend'] = season_name else: merged_metrics['legend'] = merged_metrics['Workout_Code'] for i in merged_metrics.color.unique(): cur_metrics = merged_metrics.loc[merged_metrics.color == i] trace_name = cur_metrics.iloc[0]['legend'] if not trace_name: trace_name = "None" # Add scatter traces fig.add_traces( go.Scatter( x=cur_metrics.sb, y=cur_metrics.IF, mode='markers+text', marker=dict( size=cur_metrics.radius, color=cur_metrics.color ), name=trace_name, showlegend=True, hoverinfo="text", hovertext=cur_metrics.hovertext, text=cur_metrics['date'].dt.strftime('%d-%m-<br>%Y'), textfont=dict( size=8, color='darkgray', ) ) ) # print(merged_metrics[['date', 'sb', 'BikeStress', 'radius', 'Workout_Code', 'color', 'legend']].head()) # Add Quadrant text min_intensity_factor = min(min(intensity_factor) * 0.9, 0.7) max_intensity_factor = max(max(intensity_factor) * 1.1, 0.9) min_stress_balance = min(min(stress_balance) * 1.2, -5) max_stress_balance = max(max(stress_balance) * 1.2, 5) annotation = [ get_annotation(min_stress_balance / 2, min_intensity_factor * 1.03, "Maintain"), get_annotation(max_stress_balance / 2, max_intensity_factor * 0.98, "Race"), get_annotation(min_stress_balance / 2, max_intensity_factor * 0.98, "Overload"), get_annotation(max_stress_balance / 2, min_intensity_factor * 1.03, "Junk") ] fig.update_layout( title="TSB vs IF (" + ",".join(selected_seasons['name']) + ")", paper_bgcolor=gc_bg_color, plot_bgcolor=gc_bg_color, font=dict( color=gc_text_color, size=12 ), annotations=annotation, ) # Add horizontal IF 0.85 line fig.add_trace( go.Scatter( x=[min_stress_balance, max_stress_balance], y=[0.85, 0.85], mode='lines', showlegend=False, line=dict( color="White", dash='dash' ) ) ) # Add vertical TSB 0 line fig.add_trace( go.Scatter( x=[0, 0], y=[min_intensity_factor, max_intensity_factor], mode='lines', showlegend=False, line=dict( color="White", dash='dash' ) ) ) # Set axes properties fig.update_xaxes(range=[min_stress_balance, max_stress_balance], zeroline=False, gridcolor='gray', mirror=True, ticks='outside', showline=True, ) fig.update_yaxes(range=[min_intensity_factor, max_intensity_factor], gridcolor='gray', mirror=True, ticks='outside', showline=True, ) # fig.show() plotly.offline.plot(fig, auto_open=False, filename=temp_file.name) GC.webpage(pathlib.Path(temp_file.name).as_uri())
#print(edge_width) #node_color=my_new_list = [i * 2000 for i in rules['lift'].tolist()] plt.figure(2, figsize=(50, 50)) G.add_edges_from(details) nx.draw_networkx(G, with_label=True, node_size=lift_size, width=edge_width, edge_color='.4', cmap=plt.cm.Blues, font_weight='bold', font_size=25) #plt.axis('off') plt.legend('Lift', 'Confidence') plt.show() fig = go.Figure(data=[ go.Scatter(x=rules['support'].tolist(), y=rules['confidence'].tolist(), mode='markers', text=details, marker=dict( color=rules['lift'].tolist(), size=np.arange(0, len(lift_size)), showscale=True, )) ]) fig.show()
def plot_trajectory_graph(data=[], target_name='hand_selected_004', phase='pre', save=False, out_dir='./plots', extension='', x_lower_bound=4, x_upper_bound=13, edge_width_scale_factor=0.8, node_size_scale_factor=0.8, colors=["#1c373e", "#EF553B"]): ''' makes F1 world state trajectory graph for specific target structure and phase ''' # check to make sure data was passed in if len(data) == 0: raise Exception('No data was passed in! Please pass in data.') # Create graph t = GenericBuildGraph(target_name=target_name) # make new tree a = data[(data.targetName == target_name) & (data.phase_extended == phase)] a = a.groupby('gameID') a.apply(lambda g: t.add_build_path(g)) ### EXTRACT DATA VALS node_xs, node_ys, edge_xs, edge_ys, node_sizes, edge_ws, node_colors, edge_colors = t.get_coords( ) ### HACKY POSTPROCESSING node_sizes = [i * node_size_scale_factor for i in node_sizes] ## pretty structure names pretty_names = [ 'structure {}'.format(i + 1) for i in np.arange(len(targets)) ] hs2pn = dict(zip(targets, pretty_names)) ## opacity dictionary O = {1: 0.1, 2: 0.3, 3: 0.5, 4: 0.7} def make_edge(x, y, width): return go.Scatter(x=x, y=y, line=dict(width=width), hoverinfo='none', mode='lines') def make_edges(edge_xs, edge_ys, edge_ws): edge_trace = [] for i in range(0, int(len(edge_xs) / 2)): edge_trace.append( go.Scatter( x=(edge_xs[i * 2], edge_xs[i * 2 + 1]), y=(edge_ys[i * 2], edge_ys[i * 2 + 1]), #colorscale = ['#636EFA', '#EF553B'], line=dict(width=edge_ws[i] * edge_width_scale_factor, color=(list(reversed(colors)))[edge_colors[i]]), opacity=O[edge_ws[i]] if edge_ws[i] in O.keys() else 0.95, hoverinfo='none', mode='lines')) return edge_trace ### MAKE EDGES edge_trace = make_edges(edge_xs, edge_ys, edge_ws) ### MAKE NODES node_trace = go.Scatter( x=node_xs, y=node_ys, mode='markers', hoverinfo='text', marker=dict( #colorscale='Greys', colorscale=colors, reversescale=True, size=node_sizes, #color = '#2e2e2e', color=node_colors, opacity=1., line_width=0, line_color='#FFF')) ### FIGURE DEFINITION fig = go.Figure( data=edge_trace + [node_trace], layout=go.Layout( #title= str(hs2pn[target_name]) + ' | ' + str(phase), titlefont_size=22, showlegend=False, hovermode='closest', margin=dict(b=20, l=5, r=5, t=40), xaxis={ 'range': [x_lower_bound, x_upper_bound], 'showgrid': False, 'zeroline': True, 'visible': False }, yaxis={ 'range': [-0.1, 1.05], 'showgrid': False })) ### FIG DIMENSIONS AND THEME fig.update_yaxes(tickfont=dict(family='Helvetica', color='black', size=24), title_text='F1 score', title_font=dict(size=24, family='Helvetica', color='black')) fig.update_layout(width=600, height=400, template='simple_white') ### HORIZONTAL REF LINES fig.add_shape(type='line', x0=x_lower_bound, y0=1, x1=x_upper_bound, y1=1, line={ 'color': 'black', 'width': 1, 'dash': 'dot' }) fig.add_shape(type='line', x0=x_lower_bound, y0=0, x1=x_upper_bound, y1=0, line={ 'color': 'black', 'width': 1, 'dash': 'dot' }) fig.show() if save: if not os.path.exists(out_dir): os.mkdir(out_dir) if not os.path.exists(os.path.join(out_dir, 'state_trajectory')): os.mkdir(os.path.join(out_dir, 'state_trajectory')) plot_path = os.path.join( out_dir, 'state_trajectory', (target_name + '_' + phase + '_' + extension + '.pdf')) fig.write_image(plot_path)
def createStockTimeline(dfHistoryMax): df = dfHistoryMax # Reseting the index df = df.reset_index() df.rename(columns={'index': 'Date'}, inplace=True) # Converting the datatype to float for i in ['Open', 'High', 'Close', 'Low', "Volume"]: df[i] = df[i].astype('float64') fig = go.Figure( go.Scatter(x=df['Date'], y=df['High'], mode='lines', line=dict(color='#2B3C4D', width=1))) fig.update_xaxes( rangeslider_visible=True, rangeselector=dict(buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all") ])), type="date") fig.update_layout( dragmode="zoom", title='Stock Price', title_font_size=22, title_x=.5, hovermode="x unified", legend=dict(traceorder="reversed"), template="plotly_white", height=400, margin=dict(t=50, b=50), ) figVol = go.Figure([ go.Scatter(x=df['Date'], y=df['Volume'], mode='lines', line=dict(color='#F39C12', width=1)) ]) figVol.update_xaxes( rangeslider_visible=True, rangeselector=dict(buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(count=6, label="6m", step="month", stepmode="backward"), dict(count=1, label="YTD", step="year", stepmode="todate"), dict(count=1, label="1y", step="year", stepmode="backward"), dict(step="all") ])), type="date") figVol.update_layout( dragmode="zoom", title='Stock Volume', title_font_size=22, title_x=.5, hovermode="x unified", legend=dict(traceorder="reversed"), template="plotly_white", height=250, margin=dict(t=50, b=50), ) return fig, figVol
from app import app import plotly as py from plotly.subplots import make_subplots import plotly.graph_objects as go import dash_player as player import numpy as np import time as t labels = ['steady', 'forward_tilt', 'side_tilt', 'forward_rotation'] values = [390, 161, 97, 52] # pull is given as a fraction of the pie radius fig = go.Figure( data=[go.Pie(labels=labels, values=values, pull=[0.2, 0, 0, 0])]) video = player.DashPlayer( id="main_video.mp4", url='https://youtu.be/k6cO7OQNGAA', controls=True, playing=False, volume=1, width="650px", height="500px", ) layout = html.Div([ html.H1('strYa', style={ "textAlign": "center", "color": "#8A2BE2"
def update_ranked_stops(lineIds, selected_param, axis_type): try: if type(lineIds) is str: lineIds = [lineIds] #Nodes for the graph if 'All (Slow charging)' in lineIds: G = stops_net stops_pr = stops.set_index('id')[['stop_name']] else: if 'Day-time (Slow charging)' in lineIds: G = stops_day_net lineIds = [] for line in line_stops_dict.keys(): if line not in night_lines: lineIds.append(line) elif 'Night-time' in lineIds: G = stops_night_net lineIds = [] for line in line_stops_dict.keys(): if line in night_lines: lineIds.append(line) else: if 'Selected' in lineIds: lineIds = [ '1', '44', '82', '91', '92', '99', '132', '133', '502', '506' ] G = build_net_graph(lineIds) stops_of_lines = [] for lineId in lineIds: try: if line_stops_dict[lineId] != None: if line_stops_dict[lineId]['1'] != None: stops_of_lines = stops_of_lines + line_stops_dict[ lineId]['1'] if line_stops_dict[lineId]['2'] != None: stops_of_lines = stops_of_lines + line_stops_dict[ lineId]['2'] except: continue stops_of_lines = list(set(stops_of_lines)) stops_selected = stops.loc[stops.id.isin(stops_of_lines)] stops_pr = stops_selected.set_index('id')[['stop_name']] #Rank nodes pagerank = pd.Series(nx.pagerank(G, alpha=0.9)) deg_centrality = pd.Series(nx.degree_centrality(G)) in_centrality = pd.Series(nx.in_degree_centrality(G)) out_centrality = pd.Series(nx.out_degree_centrality(G)) stops_pr['pagerank'] = pagerank stops_pr['deg_centrality'] = deg_centrality stops_pr['in_centrality'] = in_centrality stops_pr['out_centrality'] = out_centrality if stops_pr.shape[0] < 1: #If there is an error we ask for a valid line id return [ html.H1('Please select one or multiple line ids from the list', className='subtitle is-3'), dcc.Graph(id='stops-rank-graph', figure=go.Figure(), style={ 'display': 'none', 'height': box_height }, clear_on_unhover=True, config={'displayModeBar': False}) ] #Gen bar graph top_stops_graph = build_bar_graph(stops_pr, selected_param, axis_type) return [ dcc.Graph(id='stops-rank-graph', style=dict(height=box_height), figure=top_stops_graph, clear_on_unhover=True, config={'displayModeBar': False}) ] except: #If there is an error we ask for a valid line id return [ html.H1('Please select one or multiple line ids from the list', className='subtitle is-3'), dcc.Graph(id='stops-rank-graph', figure=go.Figure(), style={ 'display': 'none', 'height': box_height }, clear_on_unhover=True, config={'displayModeBar': False}) ]
def update_hovered_stop_info(lineIds, clickData1, clickData2, clickData3): #Hover data hovered = True try: if context.triggered[0]['prop_id'] == 'lines-map.clickData': stop_id = clickData1['points'][0]['text'].split('[')[1].split( ']')[0] elif context.triggered[0]['prop_id'] == 'net-graph.clickData': stop_id = clickData2['points'][0]['text'].split('[')[1].split( ']')[0] elif context.triggered[0]['prop_id'] == 'stops-rank-graph.clickData': stop_id = clickData3['points'][0]['label'].split('[')[1].split( ']')[0] else: hovered = False except: hovered = False if hovered: #LineIds if type(lineIds) is str: lineIds = [lineIds] if 'All (Slow charging)' in lineIds: stops_of_lines = stops.id.tolist() stops_selected = stops lines_selected = lines_shapes else: if 'Day-time (Slow charging)' in lineIds: lineIds = [] for line in line_stops_dict.keys(): if line not in night_lines: lineIds.append(line) elif 'Night-time' in lineIds: lineIds = [] for line in line_stops_dict.keys(): if line in night_lines: lineIds.append(line) elif 'Selected' in lineIds: lineIds = [ '1', '44', '82', '91', '92', '99', '132', '133', '502', '506' ] #Create map stop_map = go.Figure() stop = stops[stops.id == int(stop_id)].iloc[0] #And set the figure layout stop_map.update_layout( title='', margin=dict(r=0, l=0, t=0, b=0), showlegend=False, mapbox=dict( accesstoken= 'pk.eyJ1IjoiYWxlanAxOTk4IiwiYSI6ImNrYTFhMDN5aTB3a2kzbG52dmV4bXF4b3EifQ.jtFT5GtqWddniEwssyPYKQ', bearing=0, center=dict(lat=stop.lat, lon=stop.lon), pitch=0, zoom=14, style=style)) #Add lines to the figure G = stops_net stop_lines = intersect([int(k) for k in lineIds], G.nodes[stop.id]['lines']) lines_selected = lines_shapes.loc[lines_shapes.line_id.isin( stop_lines)] for line_id in lines_selected.line_id.unique(): for direction in [1, 2]: color = '#1E90FF' if direction == 1 else '#B22222' line_dir_df = lines_selected.loc[ (lines_selected.line_id == line_id) & (lines_selected.direction == direction)] stop_map.add_trace( go.Scattermapbox(lat=line_dir_df.lat, lon=line_dir_df.lon, mode='lines', line=dict(width=2, color=color), text='Línea : {}-{}'.format( line_id, direction), hoverinfo='text', opacity=1)) #Add the stops to the figure stop_map.add_trace( go.Scattermapbox( lat=[stop.lat], lon=[stop.lon], mode='markers', marker=go.scattermapbox.Marker(size=20, color='#2F4F4F', opacity=0.9), text=['<b>[' + str(stop.id) + '] ' + stop.stop_name + '</b>'], hoverinfo='text')) return [ html.Div(style={'width': '30vh'}, children=[ html.H1('[{}] {}'.format(stop.id, stop.stop_name), className='subtitle is-5'), dcc.Graph(id='hovered-stop-map', figure=stop_map, style={'height': '25vh'}, config={'displayModeBar': False}) ]) ] else: return 'Click a stop'
def update_lines_graph(lineIds): try: if type(lineIds) is str: lineIds = [lineIds] if 'All (Slow charging)' in lineIds: stops_of_lines = stops.id.tolist() stops_selected = stops lines_selected = lines_shapes else: if 'Day-time (Slow charging)' in lineIds: lineIds = [] for line in line_stops_dict.keys(): if line not in night_lines: lineIds.append(line) elif 'Night-time' in lineIds: lineIds = [] for line in line_stops_dict.keys(): if line in night_lines: lineIds.append(line) elif 'Selected' in lineIds: lineIds = [ '1', '44', '82', '91', '92', '99', '132', '133', '502', '506' ] stops_of_lines = [] for lineId in lineIds: try: if line_stops_dict[lineId] != None: if line_stops_dict[lineId]['1'] != None: stops_of_lines = stops_of_lines + line_stops_dict[ lineId]['1'] if line_stops_dict[lineId]['2'] != None: stops_of_lines = stops_of_lines + line_stops_dict[ lineId]['2'] except: continue stops_of_lines = list(set(stops_of_lines)) stops_selected = stops.loc[stops.id.isin(stops_of_lines)] lineIds = [int(i) for i in lineIds] lines_selected = lines_shapes.loc[lines_shapes.line_id.isin( lineIds)] lines_map = build_lines_map(stops_of_lines, stops_selected, lines_selected) #And finally we return the graph element if len(stops_of_lines) == 0: return [ html.H1('Please select one or multiple line ids from the list', className='subtitle is-3'), dcc.Graph(id='lines-map', figure=go.Figure(), style={ 'display': 'none', 'height': box_height }, clear_on_unhover=True, config={'displayModeBar': False}) ] else: return [ dcc.Graph(id='lines-map', style=dict(height=box_height), figure=lines_map, clear_on_unhover=True, config={'displayModeBar': False}) ] except: #If there is an error we ask for a valid line id return [ html.H1('Please select one or multiple line ids from the list', className='subtitle is-3'), dcc.Graph(id='lines-map', figure=go.Figure(), style={ 'display': 'none', 'height': box_height }, clear_on_unhover=True, config={'displayModeBar': False}) ]
def gen_graph(G): N = G.number_of_nodes() V = G.number_of_edges() #pos=nx.spring_layout(G) Xv = [G.nodes[k]['coords'][0] for k in G.nodes()] Yv = [G.nodes[k]['coords'][1] for k in G.nodes()] center_x = mean(Xv) center_y = mean(Yv) edge_nodes, Xed, Yed, Wed, Led = [], [], [], [], [] for edge in G.edges: edge_nodes.append((edge[0], edge[1])) Xed.append( [G.nodes[edge[0]]['coords'][0], G.nodes[edge[1]]['coords'][0]]) Yed.append( [G.nodes[edge[0]]['coords'][1], G.nodes[edge[1]]['coords'][1]]) Wed += [G.edges[edge]['weight']] Led += [G.edges[edge]['lines']] #Linear color map with steps based on quantiles max_weight = max(Wed) line_traces = [] for i in range(len(Wed)): Xed_orig, Yed_orig = Xed[i], Yed[i] Xed_new, Yed_new = [], [] N = 10 for k in range(N + 1): Xed_new.append(Xed_orig[0] + (Xed_orig[1] - Xed_orig[0]) * k / N) Yed_new.append(Yed_orig[0] + (Yed_orig[1] - Yed_orig[0]) * k / N) line_trace=go.Scatter( x=Xed_new, y=Yed_new, mode='lines', line=dict( width=0.5+(Wed[i]/max_weight)*5, color='blue' ), opacity=0.9, hoverinfo='text', text='Orig Stop: '+str(edge_nodes[i][0])+'<br>'+ \ 'Dest Stop: '+str(edge_nodes[i][1])+'<br>'+ \ 'Lines: '+str(Led[i])+'<br>'+ \ 'Weight: '+str(Wed[i])+'<br>' ) line_traces.append(line_trace) trace4=go.Scatter( x=Xv, y=Yv, mode='markers', name='net', marker=dict( symbol='circle-dot', size=[G.out_degree(k,weight='weight')*2+5 for k in G.nodes()], color=colors[2], line=dict( color='black', width=1 ), opacity=0.9 ), text=['<b>[' + str(node) + '] ' + str(G.nodes[node]['name']) + '</b>'\ '<br>Out Degree: ' + str(G.out_degree(node,weight='weight')) + \ '<br>In Degree: ' + str(G.in_degree(node,weight='weight')) + \ '<br>Lines: ' + str(G.nodes[node]['lines']) \ for node in G.nodes()], hoverinfo='text' ) layout = go.Layout(title="<b>STOPS NETWORK GRAPH", showlegend=False, margin=dict(r=0, l=0, t=30, b=0), xaxis={ 'showgrid': False, 'visible': False }, yaxis={ 'showgrid': False, 'showline': False, 'zeroline': False, 'visible': False }, annotations=[ dict(ax=(Xed[i][0] + Xed[i][1]) / 2, ay=(Yed[i][0] + Yed[i][1]) / 2, axref='x', ayref='y', x=(Xed[i][1] * 3 + Xed[i][0]) / 4, y=(Yed[i][1] * 3 + Yed[i][0]) / 4, xref='x', yref='y', showarrow=True, arrowhead=4, arrowsize=1 + (Wed[i] / max_weight) * 2, arrowwidth=1, opacity=1) for i in range(len(Xed)) ]) data = line_traces + [trace4] graph = go.Figure(data=data, layout=layout) return graph
]) ]), html.Div( className='columns', children=[ html.Div( className='column', style={'position': 'relative'}, children=[ html.Div( id='lines-graph', className='box', children=[ dcc.Graph( id='lines-map', figure=go.Figure(), style={'display': 'none'}, clear_on_unhover=True, config={'displayModeBar': False}) ]), html.Div( id='hovered-stop-info', className='box', style={ 'position': 'absolute', 'top': '0', 'right': '0', 'z-index': '0' }, children=[ dcc.Graph(
'https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv' ) for col in df.columns: df[col] = df[col].astype(str) df['text'] = df['state'] + '<br>' + \ 'Beef ' + df['beef'] + ' Dairy ' + df['dairy'] + '<br>' + \ 'Fruits ' + df['total fruits'] + ' Veggies ' + df['total veggies'] + '<br>' + \ 'Wheat ' + df['wheat'] + ' Corn ' + df['corn'] fig = go.Figure(data=go.Choropleth( locations=df['code'], z=df['total exports'].astype(float), locationmode='USA-states', colorscale='Reds', autocolorscale=False, text=df['text'], # hover text marker_line_color='white', # line markers between states colorbar_title="Millions USD")) fig.update_layout( title_text='2011 US Agriculture Exports by State<br>(Hover for breakdown)', geo=dict( scope='usa', projection=go.layout.geo.Projection(type='albers usa'), showlakes=True, # lakes lakecolor='rgb(255, 255, 255)'), ) st.slider('test') fig.show()
fig = go.Figure(data=[go.Surface(contours = {"z": {"show": True, "size": 0.01, "color":"white"}},\ z=z, colorscale = 'haline'), \ go.Scatter3d( x=env_CUAD01_scene01[0].path[:,0], y=env_CUAD01_scene01[0].path[:,1], \ z=env_CUAD01_scene01[0].pathElevation, marker=dict( size=2, color='red' )), \ go.Scatter3d( x=env_CUAD01_scene01[1].path[:,0], y=env_CUAD01_scene01[1].path[:,1], \ z=env_CUAD01_scene01[1].pathElevation, marker=dict( size=2, color='red' )), \ go.Scatter3d( x=env_CUAD02_scene01[0].path[:,0], y=env_CUAD02_scene01[0].path[:,1], \ z=env_CUAD02_scene01[0].pathElevation, marker=dict( size=2, color='magenta' )), \ go.Scatter3d( x=env_CUAD02_scene01[1].path[:,0], y=env_CUAD02_scene01[1].path[:,1], \ z=env_CUAD02_scene01[1].pathElevation, marker=dict( size=2, color='magenta' )), \ go.Scatter3d( x=env_CUAD03_scene01[0].path[:,0], y=env_CUAD03_scene01[0].path[:,1], \ z=env_CUAD03_scene01[0].pathElevation, marker=dict( size=2, color='orange' )), \ go.Scatter3d( x=env_CUAD03_scene01[1].path[:,0], y=env_CUAD03_scene01[1].path[:,1], \ z=env_CUAD03_scene01[1].pathElevation, marker=dict( size=2, color='orange' )), \ go.Scatter3d( x=env_CUAD04_scene01[0].path[:,0], y=env_CUAD04_scene01[0].path[:,1], \ z=env_CUAD04_scene01[0].pathElevation, marker=dict( size=2, color='blue' )), \ go.Scatter3d( x=env_CUAD04_scene01[1].path[:,0], y=env_CUAD04_scene01[1].path[:,1], \ z=env_CUAD04_scene01[1].pathElevation, marker=dict( size=2, color='blue' )), \ go.Scatter3d( x=env_CUAD05_scene01[0].path[:,0], y=env_CUAD05_scene01[0].path[:,1], \ z=env_CUAD05_scene01[0].pathElevation, marker=dict( size=2, color='cyan' )), \ go.Scatter3d( x=env_CUAD05_scene01[1].path[:,0], y=env_CUAD05_scene01[1].path[:,1], \ z=env_CUAD05_scene01[1].pathElevation, marker=dict( size=2, color='cyan' )), \ go.Scatter3d( x=env_CUAD06_scene01[0].path[:,0], y=env_CUAD06_scene01[0].path[:,1], \ z=env_CUAD06_scene01[0].pathElevation, marker=dict( size=2, color='green' )), \ go.Scatter3d( x=env_CUAD06_scene01[1].path[:,0], y=env_CUAD06_scene01[1].path[:,1], \ z=env_CUAD06_scene01[1].pathElevation, marker=dict( size=2, color='green' )) ])
import plotly.graph_objects as go import numpy as np pts = np.loadtxt(np.DataSource().open('https://raw.githubusercontent.com/plotly/datasets/master/mesh_dataset.txt')) x, y, z = pts.T fig = go.Figure(data=[go.Mesh3d(x=x, y=y, z=z, alphahull=5, opacity=0.4, color='cyan')]) fig.show()
import plotly import pandas as pd import plotly.graph_objects as go import plotly.express as px excel_file = 'animate.xlsx' df = pd.read_excel(excel_file) print(df) data = [go.Scatter(x=df['Date'], y=df['fulldeaths'])] fig = go.Figure(data) plotly.offline.plot(fig, filename="fulldeaths.html")
import pandas as pd quakes = pd.read_csv( 'https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv' ) import plotly.graph_objects as go fig = go.Figure( go.Densitymapbox(lat=quakes.Latitude, lon=quakes.Longitude, z=quakes.Magnitude, radius=10)) fig.update_layout(mapbox_style="stamen-terrain", mapbox_center_lon=180) fig.update_layout(margin={"r": 0, "t": 0, "l": 0, "b": 0}) fig.show()
def produce_tour_progression_sankey(fn): df = pd.read_excel(fn, sheet_name="Raw") tours = pd.read_excel(fn, sheet_name="Tours") players = pd.read_excel(fn, sheet_name="PlayerNames") # data label = tours['TourName'].to_list() all_label = label.copy() all_label.extend(label) all_label.extend(label) all_label.extend(label) all_label.extend(label) all_label.extend(label) all_source = np.array((0,)) all_target = np.array((0,)) all_value = np.array((0,)) for y in range(0, 5): source = (y * len(tours)) + np.arange(1, len(tours)+1) target = (y * len(tours)) + 1 + len(tours) + np.zeros(np.shape(source)) source_base = np.arange(1, len(tours)+1) target_base = np.zeros(np.shape(source_base)) + 1 for i in range(2,len(tours)+1): source = np.append(source, (y * len(tours)) + np.arange(1, len(tours) + 1)) target = np.append(target, (y * len(tours)) + len(tours) + i*np.ones(np.shape(np.arange(1, len(tours)+ 1)))) source_base = np.append(source_base, np.arange(1, len(tours) + 1)) target_base = np.append(target_base, i*np.ones(np.shape(np.arange(1, len(tours)+ 1)))) all_source = np.append(all_source, source) all_target = np.append(all_target, target) yr_str1 = str(2015 + y) yr_str2 = str(2015 + 1 + y) value = np.zeros(np.shape(source)) for ii in range(0, len(source_base)): booll = (df[yr_str1] == source_base[ii]) & (df[yr_str2] == target_base[ii]) value[ii] = np.sum(booll) all_value = np.append(all_value, value) all_source = all_source[1:] - 1 all_target = all_target[1:] - 1 all_value = all_value[1:] # data to dict, dict to sankey link = dict(source = all_source, target = all_target, value = all_value) node = dict(label = all_label) data = go.Sankey(link = link, node=node) # plot fig = go.Figure(data) return fig
def build_us_map(map_date,val='Active', pop = 1): if map_date is None: return None if isinstance(map_date, str): map_date = datetime.datetime.strptime(map_date, '%Y-%m-%d').date() df_us = get_df_us() df_map = df_us.loc[df_us['Day']==map_date] df_map = df_map.loc[df_us['Province']!='US'] states = get_states() df_map.loc[:,'code'] = df_map.Province.apply(lambda x: states[x]) population = np.array([]) PopInfo = pd.read_csv('data/predicted/WorldPopulationInformation.csv', sep=",") for i in df_map['Province']: pop_val = PopInfo.loc[PopInfo['Province']==i,'pop'].values population = np.concatenate((population, pop_val),0) df_map['Population'] =population df_map['Active Per Million'] = (np.round(1000000*df_map['Active']/df_map['Population'], decimals = 2)) df_map['Total Detected Per Million'] = (np.round(1000000*df_map['Total Detected']/df_map['Population'], decimals = 2)) df_map['Active Hospitalized Per Million'] = (np.round(1000000*df_map['Active Hospitalized']/df_map['Population'], decimals = 2)) df_map['Cumulative Hospitalized Per Million'] = (np.round(1000000*df_map['Cumulative Hospitalized']/df_map['Population'], decimals = 2)) df_map['Total Detected Deaths Per Million'] = (np.round(1000000*df_map['Total Detected Deaths']/df_map['Population'], decimals = 2)) df_map = df_map.applymap(str) cols = get_cols() if (val is not None) and (val in cols) and pop == 1: df_map.loc[:,'text'] = df_map['Province'] + '<br>' + \ 'Total Detected ' + df_map['Total Detected'] + '<br>' + \ 'Active ' + df_map['Active'] + '<br>' + \ 'Active Hospitalized ' + df_map['Active Hospitalized'] + '<br>' + \ 'Cumulative Hospitalized ' + df_map['Cumulative Hospitalized'] + '<br>' + \ 'Total Detected Deaths ' + df_map['Total Detected Deaths'] z_val = df_map[val].astype(float) if (val is not None) and (val in cols) and pop != 1: df_map.loc[:,'text'] = df_map['Province'] + '<br>' + \ 'Total Detected Per Million ' + df_map['Total Detected Per Million'] + '<br>' + \ 'Active Per Million ' + df_map['Active Per Million'] + '<br>' + \ 'Active Hospitalized Per Million ' + df_map['Active Hospitalized Per Million'] + '<br>' + \ 'Cumulative Hospitalized Per Million ' + df_map['Cumulative Hospitalized Per Million'] + '<br>' + \ 'Total Detected Deaths Per Million ' + df_map['Total Detected Deaths Per Million'] z_val =df_map[val+ " Per Million"].astype(float) if (val is not None) and (val in cols): fig = go.Figure(data=go.Choropleth( locations=df_map['code'], z=z_val, locationmode='USA-states', colorscale='inferno_r', autocolorscale=False, text=df_map['text'], # hover text marker_line_color='white' , # line markers between states colorbar_title='<br>'.join(wrap(''.join(['{}'.format(add_cases(val))]), width=10)) )) fig.update_layout( margin=dict(l=10, r=10, t=50, b=50), title_text=add_cases('{} Predicted US {}'.format(map_date.strftime('%b %d,%Y'), val)), geo = dict( scope='usa', projection=go.layout.geo.Projection(type = 'albers usa'), showlakes=True, # lakes lakecolor='rgb(255, 255, 255)' ), modebar={ 'orientation': 'v', 'bgcolor': 'rgba(0,0,0,0)', 'color': 'lightgray', 'activecolor': 'gray' } ) graph = dcc.Graph( id='us-projection-map', figure=fig ) return graph return
q2_plot.update_layout(showlegend=False) st.plotly_chart(q2_plot, use_container_width=True) st.markdown( "### **Q3:** What is the reason for your hesitancy? (May choose multiple answers)" ) df_q3 = df['Q3'].value_counts() df_q3 = df_q3.rename_axis('Q3 Response').reset_index(name='Frequency') df_q3.index += 1 q3_plot = go.Figure(data=[ go.Table(header=dict( values=list(df_q3.columns), fill_color='paleturquoise', align='left'), cells=dict(values=[df_q3['Q3 Response'], df_q3['Frequency']], fill_color='lavender', align='left')) ]) q3_plot.update_layout(showlegend=False) st.plotly_chart(q3_plot, use_container_width=True) st.markdown( "### **Q4:** What would definitely make you agree to getting vaccinated?") df['Q4'] = df['Q4'].fillna('None') q4_common = df['Q4'].apply(lambda words: ' '.join(word.lower( ) for word in words.split() if word not in stop)).value_counts()[:100] st.write(q4_common) st.markdown(
def build_state_projection(state, country, continent, vals): location = find_smallest_scope(state, country, continent) df_projections = get_df_projections() df_projections_sub = df_projections.loc[ (df_projections.Province == state) & (df_projections.Country == country)] if continent not in ['US', 'World']: df_projections_sub = df_projections_sub.loc[(df_projections_sub.Continent == continent)] if continent == 'US': df_projections_sub = df_projections.loc[(df_projections.Country == 'US') & (df_projections.Province == state)] if continent == 'World': if country =='None': df_projections_sub = df_projections.loc[(df_projections.Continent == 'None')] #include only global world data fig = go.Figure() cols = get_cols() if (vals is not None) and (set(vals).issubset(set(cols))): colors = get_colors() for val in vals: i = cols[val] fig.add_trace(go.Scatter( name=val, showlegend=True, x=df_projections_sub['Day'], y=df_projections_sub[val].values, mode="lines+markers", marker=dict(color=colors[i]), line=dict(color=colors[i]) )) title = '<br>'.join(wrap('<b> Projections for {} </b>'.format(location), width=26)) fig.update_layout( height=550, title={ 'text': title, 'y':0.95, 'x':0.5, 'xanchor': 'center', 'yanchor': 'top'}, title_font_size=25, xaxis={'title': "Date",'linecolor': 'lightgrey'}, yaxis={'title': "Count",'linecolor': 'lightgrey'}, legend_title='<b> Values Predicted </b>', margin={'l': 40, 'b': 40, 't': 40, 'r': 10}, hovermode='closest', paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', legend={ "orientation": "h", "xanchor": "center", "y": -0.2, "x": 0.5 }, modebar={ 'orientation': 'v', 'bgcolor': 'rgba(0,0,0,0)', 'color': 'lightgray', 'activecolor': 'gray' } ) graph = dcc.Graph( id='projection-graph', figure=fig ) return graph
#%% adjs(pride) #%% [markdown] # # Creating Figures # There are many ways to create figures. Below is one example of a table. You can save the figure to a file. # # ``` # conda install -c plotly # ``` #%% import plotly.graph_objects as go fig = go.Figure(data=[ go.Table(header=dict(values=['A Scores', 'B Scores']), cells=dict(values=[[100, 90, 80, 90], [95, 85, 75, 95]])) ]) fig.show() #%% [markdown] # # Lab 2 #%% # [2-1] ON YOUR OWN: # Choose a text that was not previously analyzed above from Project Gutenberg. # 1. Write code that retrieves and writes the text to a file in the current project. You may save it to any file, but I recommend to save it to the lab2 subdirectory. from gutenberg.acquire import load_etext from gutenberg.cleanup import strip_headers from textblob import TextBlob
# -*- coding: utf-8 -*- import fileinput import json import os import sys import plotly import plotly.graph_objects as go plotly.io.orca.config.server_url = "orca:9091" first = True framework = None fig = go.Figure() x, y, point_labels = [], [], [] batch_sizes = set() for line in fileinput.input(): data = json.loads(line) times = [ time for epoch_stats in data["epochs"] for time in data["epochs"][epoch_stats]["time"] ] batch_size = data["batch_size"] batch_sizes.add(batch_size) epochs = len(data["epochs"]) total_time = sum(times) points_per_second = (epochs * 60000) / total_time latency = total_time / len(times) accuracy = data["epochs"][max(data["epochs"])]["accuracy"]
def _master_plot(x, y, title='', xerr=None, yerr=None, method_name='', target_name='', plot_type='', guidelines=True, origins=True, statistics=['RMSE', 'MUE'], filename=None): nsamples = len(x) ax_min = min(min(x), min(y)) - 0.5 ax_max = max(max(x), max(y)) + 0.5 fig = go.Figure() # x = 0 and y = 0 axes through origin if origins: # x=0 fig.add_trace(go.Scatter(x=[0, 0], y=[ax_min, ax_max], line_color='black', mode='lines', showlegend=False )) # y =0 fig.add_trace(go.Scatter(x=[ax_min, ax_max], y=[0, 0], line_color='black', mode='lines', showlegend=False )) if guidelines: small_dist = 0.5 fig.add_trace(go.Scatter(x=[ax_min, ax_max, ax_max, ax_min], y=[ax_min + 2. * small_dist, ax_max + 2. * small_dist, ax_max - 2. * small_dist, ax_min - 2. * small_dist], name='1 kcal/mol margin', hoveron='points+fills', hoverinfo='name', fill='toself', mode='lines', line_width=0, fillcolor='rgba(0, 0, 0, 0.2)', showlegend=False)) fig.add_trace(go.Scatter(x=[ax_min, ax_max, ax_max, ax_min], y=[ax_min + small_dist, ax_max + small_dist, ax_max - small_dist, ax_min - small_dist], name='.5 kcal/mol margin', hoveron='points+fills', hoverinfo='name', fill='toself', mode='lines', line_width=0, fillcolor='rgba(0, 0, 0, 0.2)', showlegend=False)) # diagonal fig.add_trace(go.Scatter(x=[ax_min, ax_max], y=[ax_min, ax_max], line_color='black', mode='lines', showlegend=False )) # 2.372 kcal / mol = 4 RT clr = np.abs(x - y) / 2.372 fig.add_trace(go.Scatter(x=x, y=y, mode='markers', name=f'{target_name},{method_name}', marker=dict( symbol='circle', color=clr, colorscale='BlueRed' ), error_x=dict( type='data', # value of error bar given in data coordinates array=xerr, visible=True), error_y=dict( type='data', # value of error bar given in data coordinates array=yerr, visible=True), showlegend=False )) # stats and title string = [] for statistic in statistics: s = stats.bootstrap_statistic(x, y, statistic=statistic) string.append( f"{statistic + ':':5s}{s['mle']:5.2f} [95%: {s['low']:5.2f}, {s['high']:5.2f}]") statistics_string = '<br>'.join(string) long_title = f'{title}<br>{target_name} (N = {nsamples})<br>{statistics_string}' # figure layout fig.update_layout( title=dict( text=long_title, font_family='monospace', x=0.0, y=0.95, font_size=14, ), xaxis=dict( title=f'Experimental {plot_type} [kcal mol<sup>-1</sup>]', titlefont_size=14, tickfont_size=12, range=(ax_min, ax_max) ), yaxis=dict( title=f'Calculated {plot_type} {method_name} [kcal mol<sup>-1</sup>]', titlefont_size=14, tickfont_size=12, range=(ax_min, ax_max) ), width=400, height=400 # legend=dict( # x=1.0, # y=1.0, # bgcolor='rgba(255, 255, 255, 0)', # bordercolor='rgba(255, 255, 255, 0)', # font_size=12 # ) ) if filename is None: fig.show() elif filename.find('.html') > 0: fig.write_html(filename) else: fig.write_image(filename)
def series_fig( df_arr, # Array of dataframes (will be thinned if required) min_max=False, # If passed non-thinned dataframe x='step', # What the x axis is called in dataframes (might be 'ts', for instance) - now retrieved from thinned df metadata y='value', # What the y axis is called in the dataframes xrange=None, yrange=None, # User defined axis range ([low, high]) ylog=False, # Do log plot point_format='(%{x:s},%{y:s})', # Can include python formatting information fig=None, # Can pass in a fig to add on to it ): if not init_plotly_done: init_plotly() import plotly_express as pltx import plotly.graph_objects as pltgo if fig is None: fig = pltgo.Figure() #matplotlib.colors.to_rgb('yellow') -> (1.0, 1.0, 0.0) # Choosing a good sequence of different colours isn't going to be so easy... # http://colorbrewer2.org/#type=qualitative&scheme=Dark2&n=5 # https://www.chronicle.com/blogs/profhacker/color-blind-accessible-figures/59189 :: Okabe and Ito : cmap = [(0, 0, 0), (230, 159, 0), (86, 180, 224), (0, 158, 115), (240, 228, 66), (0, 114, 178), (213, 94, 0), (204, 121, 167)] def rbga(c, alpha=1.0): ret = f'rgba({c[0]},{c[1]},{c[2]},{alpha})' #print(ret) return ret for i, df in enumerate(df_arr): if getattr(df, 'x', None) is None: df = ranges( df, y=y, min_max=min_max, ) x = getattr(df, 'x', 'step') c = cmap[i % len(cmap)] hovertemplate = ( point_format + "<br>" #+"2019-12-13_01-slim-decoder-from0<br>" + getattr(df, 'experiment', '') + "<br>" #+"ferts-fert-loss" + getattr(df, 'series', '') + "<br>" + "<extra></extra>") # https://plot.ly/python-api-reference/generated/plotly.express.line.html#plotly.express.line # https://plot.ly/python/hover-text-and-formatting/ #fig = pltx.line(df, x='step', y='mean', range_y=[0.8,1.6]) fig.add_trace( pltgo.Scatter( x=df[x], y=df['mid'], # +0.2 fill=None, mode='lines', #line_color='blue', line_color=rbga(c), #name="2019-12-13_01-slim-decoder-from0.ferts-fert-loss", name=getattr(df, 'experiment', '') + " " + getattr(df, 'series', ''), hovertemplate=hovertemplate, )) #fig.add_scatter(x=df['step'], y=df['mean_plus'], mode='lines') # https://plot.ly/python/line-charts/#filled-lines # https://plot.ly/python/filled-area-plots/ fig.add_trace( pltgo.Scatter( x=df[x], y=df['upper'], mode='lines', fill=None, fillcolor=rbga(c, alpha=0.2), #line_color='rgba(255,255,255,0)', # transparent line_color=rbga(c, alpha=0.2), #hovertemplate=point_format+"<br><extra></extra>", hovertemplate=hovertemplate, showlegend=False, )) fig.add_trace( pltgo.Scatter( x=df[x], y=df['lower'], mode='lines', fill='tonexty', # fill area between trace0 and trace1 #opacity=0.50, fillcolor=rbga(c, alpha=0.2), #line_color='rgba(255,255,255,0)', # transparent line_color=rbga(c, alpha=0.2), #hovertemplate=point_format+"<br><extra></extra>", hovertemplate=hovertemplate, showlegend=False, )) #fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='LightPink') if xrange is not None: fig.update_xaxes(range=xrange) if yrange is not None: fig.update_yaxes(range=yrange) if ylog: # https://plot.ly/python/log-plot/ fig.update_layout(yaxis_type="log") #https://plot.ly/python/reference/#layout-legend fig.update_layout(legend=dict( x=-0.1, y=1.02, yanchor='bottom', )) return fig
def plot_bar(df, ddg_cols, error_cols, exp_col='exp', exp_error_col='dexp', name_col='edge', title='', filename=None): ''' Creates a plotly barplot. It takes a pandas.Dataframe df as input and plots horizontal bars grouping the values in the rows together. The columns which will be used are specified by ddg_cols (DDG values), error_cols (corresponding errors), exp_col (column with exp. values), exp_error_col (column with exp. errors) and name_col (column which will be used as y axis tick labels). ''' # create color palette colors = sns.color_palette(palette='bright') numEdges = df.shape[0] numBarsPerEdge = len(ddg_cols) height = 20 * (numBarsPerEdge + 0.3) * numEdges exp_size = height / numEdges / 2.0 alim = np.max(np.fabs(df.loc[:, ddg_cols + [exp_col]].values) + np.fabs( df.loc[:, error_cols + [exp_error_col]].values)) * 1.05 fig = go.Figure() # add data for i, (col, ecol) in enumerate(zip(ddg_cols, error_cols)): fig.add_trace(go.Bar(x=df.loc[:, col].values, y=df[name_col].values, error_x=dict( type='data', # value of error bar given in data coordinates array=df.loc[:, ecol].values, visible=True), name=col, marker=dict( color=f'rgba{colors[i]}', line=None ), orientation='h' )) if exp_col != None: fig.add_trace(go.Scatter(x=df.loc[:, exp_col].values, y=df[name_col].values, name="experiment", mode='markers', marker=dict( symbol='line-ns', color='black', size=exp_size, line_width=4, ) )) fig.add_trace(go.Scatter(x=df.loc[:, exp_col].values - df.loc[:, exp_error_col].values, y=df[name_col].values, name='ExpErrors1', mode='markers', marker=dict( symbol='line-ns', color='black', size=exp_size, line_width=2, ), showlegend=False )) fig.add_trace(go.Scatter(x=df.loc[:, exp_col].values + df.loc[:, exp_error_col].values, y=df[name_col].values, name='ExpErrors2', mode='markers', marker=dict( symbol='line-ns', color='black', size=exp_size, line_width=2, ), showlegend=False )) fig.update_layout( title=title, xaxis=dict( title=r'$\Delta\Delta G\, \mathrm{[kcal\,mol^{-1}]}$', titlefont_size=16, tickfont_size=14, range=(-alim, alim) ), yaxis=dict( title='Edge', titlefont_size=16, tickfont_size=14, range=(-.5, numEdges - .5) ), width=800, height=height, legend=dict( x=1.0, y=1.0, bgcolor='rgba(255, 255, 255, 0)', bordercolor='rgba(255, 255, 255, 0)', font_size=16 ), barmode='group', bargap=0.3, # gap between bars of adjacent location coordinates. bargroupgap=0.0 # gap between bars of the same location coordinate. ) if filename is None: fig.show() elif filename.find('.html'): fig.write_html(filename) else: fig.write_image(filename)
dt.DataTable( id='table_describe', columns=[{"name": i, "id": i} for i in df_describe.columns], data=df_describe.to_dict('records'), ), html.H4('Matriz de Correlacao'), dcc.Graph( id='crr-matrix', figure = go.Figure(data=go.Heatmap( z=[df_corr.Survived, df_corr.Pclass, df_corr.Age, df_corr.SibSp, df_corr.Parch, df_corr.Fare], x =['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare'], y =['Survived', 'Pclass', 'Age', 'SibSp', 'Parch', 'Fare'] )) ), html.H2('Analise Dos Atributos'), html.Div(id='selected-indexes'), dcc.Dropdown( id='atributes-list', options=[ {'label': i, 'value': i} for i in train.columns
def get_visualization_figure(mesh, label): x, y, z = mesh.numpy().T fig = go.Figure(data=[go.Scatter3d(x=x, y=y, z=z, mode='markers')]) fig.update_layout(title=label) return fig