def make_annotations(pos, text, font_size=10, font_color='rgb(250,250,250)'): L = len(pos) if len(text) != L: raise ValueError('The lists pos and text must have the same len') annotations = go.Annotations() for k in range(L): annotations.append( go.Annotation( text=G.vs['label'] [k], # or replace labels with a different list for the text within the circle x=pos[k][0], y=2 * M - position[k][1], xref='x1', yref='y1', font=dict(color=font_color, size=font_size), showarrow=False)) # now make line annotations for edge in G.es: edge_text = edge['label'] node0, node1 = edge.tuple x0 = Xn[node0] x1 = Xn[node1] y0 = Yn[node0] y1 = Yn[node1] x_pos = (x0 - (x0 - x1) / 2.0) y_pos = (y0 - (y0 - y1) / 2.0) annotations.append( go.Annotation(text=edge_text, x=x_pos, y=y_pos, showarrow=False)) return annotations
def check_ann(annotation): local_list=[] try: _annotation=dict_replace_keyword({},'font',annotation,False) _annotation=dict_replace_keyword(_annotation,'font',kwargs,False) local_list.append(go.Annotation(_annotation)) except: if 'title' in annotation: local_list.append( go.Annotation( text=annotation['title'], showarrow=False, x=0, y=1, xref='paper', yref='paper', font={'size':24 if not 'fontsize' in kwargs else kwargs['fontsize']} ) ) del annotation['title'] for k,v in list(annotation.items()): if kind in ('candlestick','ohlc','candle'): d=ta._ohlc_dict(df) maxv=df[d['high']].ix[k] yref='y2' else: maxv=df.ix[k].sum() if k in df.index else 0 yref='y1' ann=go.Annotation( x=k, y=maxv, xref='x', yref=yref, text=v, showarrow=True, arrowhead=7, ax=0, ay=-100, textangle=-90 ) local_list.append(ann) _l=[] for i in local_list: _l.append(dict_replace_keyword(i,'font',kwargs,True)) local_list=_l return local_list
def make_annotations(self, labels=[], font_size=10, font_color='rgb(250,250,250)'): """Interactive tree graph. Args: labels: New labels. font_size: Size of the font. font_color: Color of the font. Returns: annotations: Graph annotation. """ if not labels: labels = self.labels elif len(labels) != self.num_nodes: raise ValueError( 'The lists positions and labels must have the same len') annotations = go.Annotations() for k in range(self.num_nodes): annotations.append( go.Annotation(text=labels[k], x=self.positions[k][0], y=2 * self.tree_height - self.positions[k][1], xref='x1', yref='y1', font=dict(color=font_color, size=font_size), showarrow=False)) return annotations
def interploation_1d(life_ladder_data, no_of_years, no_of_days, power): num = range(0, no_of_years) x = np.array(tuple(num)) y = np.array(life_ladder_data) z = np.polyfit(x, y, power) f = np.poly1d(z) num_1 = no_of_years - 1 x_new = np.linspace(0, num_1, no_of_days) y_new = f(x_new) print("The interpolated y-values are:\n", y_new) print("Total:", len(y_new), "datapoints generated for this specific diagram.") trace1 = go.Scatter( x=x, y=y, mode='markers', name='Data', marker=dict( size=12)) trace2 = go.Scatter( x=x_new, y=y_new, mode='lines', name='Fit') annotation = go.Annotation( x=1, y=1, showarrow=False) layout = go.Layout(title='Polynomial Fit in Python', annotations=[annotation]) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) iplot(fig) return y_new
def predattweek(tid=None): mydb = mysql.connector.connect(host="localhost", user="******", passwd="battlefield4", database="footballmanagement") mycursor = mydb.cursor() if tid == None: mycursor.execute( "select matches.mid, matches.Attendance from matches,teams where matches.season='2017/18' and matches.HT=teams.TID ;" ) else: mycursor.execute( "select matches.mid, matches.Attendance from matches,teams where matches.season='2017/18' and matches.HT=teams.TID and teams.tid=" + str(tid) + ";") myresult = mycursor.fetchall() # print(myresult) x1 = list() ctr = 0 for x in myresult: x1.append((ctr, x[1])) ctr = ctr + 1 # print(x1) x = x1 points = np.array(x) x = points[:, 0] y = points[:, 1] z = np.polyfit(x, y, 3) f = np.poly1d(z) x_new = np.linspace(0, 20, 50) y_new = f(x_new) trace1 = go.Scatter(x=x, y=y, mode='markers', name='Data', marker=dict(size=12)) trace2 = go.Scatter(x=x_new, y=y_new, mode='lines', name='Fit') annotation = go.Annotation(x=6, y=-4.5, text='Expected attendance at the Bridge', showarrow=False) layout = go.Layout(title='Polynomial Fit', annotations=[annotation]) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) # py.iplot(fig, filename='interpolation-and-extrapolation') graphJSON = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder) return graphJSON
def errorfig( txt='There was an error! We\'ve logged it and will try to fix it. Try something else!' ): data = [go.Heatmap(z=[0], x=[0], y=[['']], showscale=False)] layout = go.Layout(annotations=go.Annotations( [go.Annotation(x=0, y=2, showarrow=False, text=txt)])) fig = go.Figure(data=data, layout=layout) return fig
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.layout.annotation.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 = dict( size=props['style']['fontsize'], color=props['style']['color'] ) self.plotly_fig['layout']['titlefont'] = titlefont
def generate_individual_bar(x, y, annotations, tpe, mn, mx): dt_format = '%Y-%m-%d %H:%M:%S' if len(x) == 0: div = no_data(f"{tpe}-meal blood sugar bar chart") else: trace = go.Bar(x=x, y=y, text=annotations, marker=dict(color=determine_bar_colors(y, tpe))) layout = go.Layout(title=f"{tpe}-meal blood sugar".capitalize()) x00_pt = str(datetime.datetime.strptime(x[0], dt_format) + datetime.timedelta(days=1)) x01_pt = str(datetime.datetime.strptime(x[len(x) - 1], dt_format) - datetime.timedelta(days=1)) if len(x) > 1: layout.update(dict(shapes = [ {'type': 'rect', 'xref': 'x', 'yref': 'y', 'x0': x00_pt, 'y0': mn, 'x1': x01_pt, 'y1': mn, 'fillcolor': RED, 'line': {'color': RED}, 'opacity': 0.9}, {'type': 'rect', 'xref': 'x', 'yref': 'y', 'x0': x00_pt, 'y0': mx, 'x1': x01_pt, 'y1': mx, 'fillcolor': RED, 'line': {'color': RED}, 'opacity': 0.9} ]), annotations=[go.Annotation(text=f"Lowest normal<br>{tpe}-meal blood sugar<br>({mn})", x=x00_pt, y=mn, font=dict(color="#000000"), bgcolor='#ff7f0e'), go.Annotation(text=f"Highest normal<br>{tpe}-meal blood sugar<br>({mx})", x=x01_pt, y=mx, font=dict(color="#000000"), bgcolor='#ff7f0e')] ) fig = go.Figure(data=[trace], layout=layout) div = plotly.offline.plot(fig, include_plotlyjs=False, output_type="div") return div
def plot_scatter(apc, ec, text, plot_file): scatter_data = go.Scatter(x=apc, y=ec, mode='markers', marker=dict(color="black"), text=text, showlegend=False) diagonal = go.Scatter(x=[0, np.max(list(apc) + list(ec))], y=[0, np.max(list(apc) + list(ec))], mode="lines", line=dict(color="darkgrey", width=4, dash="dot"), showlegend=False) pearson_r = pearsonr(apc, ec) data = [] data.append(diagonal) data.append(scatter_data) plot = { "data": data, "layout": go.Layout(font=dict(size=24), yaxis=dict(title="Entropy Correction", exponentformat="e", showexponent='All', scaleratio=1, scaleanchor='x'), xaxis=dict(title="Average Product Correction", exponentformat="e", showexponent='All', scaleratio=1, scaleanchor='y'), annotations=go.Annotations([ go.Annotation(x=0.05, y=0.95, showarrow=False, text='Pearson r = {0}'.format( np.round(pearson_r[0], decimals=3)), font=dict(color="black", size=24), xref='paper', yref='paper') ]), margin=dict(t=10), width="550", height="500") } plotly_plot(plot, filename=plot_file, auto_open=False, show_link=False)
def correlationMatrix(self, df, title): """Create and display a correlation matrix for every column in df.""" #setup ldf = df.copy() ldf.replace(0, np.nan, inplace=True) labs = ldf.columns.values mat = ldf.corr( min_periods=10 ).values # get correlation mat with minimum samples for calculation #add annotations annotations = go.Annotations() for i, r in enumerate(mat): for ii, rr in enumerate(r): annotations.append( go.Annotation(text=str('%.2f' % (rr)), x=labs[i], y=labs[ii], xref='x1', yref='y1', showarrow=False, font=dict(size=8, color='black'))) #develop layout layout = go.Layout(title={"text": title}, xaxis={ "tickfont": { "size": 8 }, "showgrid": False }, yaxis={ "tickfont": { "size": 8 }, "showgrid": False }, plot_bgcolor='grey', autosize=False, annotations=annotations) #add data from ldf data = go.Heatmap(type='heatmap', x=ldf.columns.values, y=ldf.columns.values, z=mat, colorscale='blues') #create and show figure fig = go.Figure(data=data, layout=layout) fig.show()
def add_data_source_note(fig): fig['layout'].update(annotations=go.Annotations([ go.Annotation( text='Data source: Yahoo Finance', showarrow=False, xref='paper', yref='paper', x=0.98, y=0.02, bgcolor='#FFFFFF' # white ) ])) return fig
def generate_heatmap(value): df = xyq_tools.getEntity() if value is None: return {'data': []} else: dff = df.loc[value, :] scaled_size = 200 + 150 + 50 * len(value) # for row in dff.iterrows(): z = dff.values.T.tolist() y = dff.columns.tolist() x = dff.index.T.tolist() print(x) print(y) print(z) annotations = [] for n, row in enumerate(z): for m, val in enumerate(row): annotations.append( go.Annotation(text=str(z[n][m]), x=x[m], y=y[n], xref='x1', yref='y1', showarrow=False)) return { 'data': [{ 'z': dff.values.T.tolist(), 'y': dff.columns.tolist(), 'x': dff.index.tolist(), 'ygap': 2, 'reversescale': 'true', 'colorscale': [[0, "#caf3ff"], [1, "#2c82ff"]], 'type': 'heatmap', }], 'layout': { 'height': 750, 'width': scaled_size, 'xaxis': { 'side': 'top' }, 'annotations': annotations, 'margin': { 'l': 200, 'r': 100, 'b': 150, 't': 100, } } }
def make_annotations(pos, text, font_size=10, font_color='rgb(250,250,250)'): L=len(pos) if len(text)!=L: raise ValueError('The lists pos and text must have the same len') annotations = go.Annotations() for k in range(L): annotations.append( go.Annotation( text=labels[k], # or replace labels with a different list for the text within the circle x=pos[k][0], y=2*M-position[k][1], xref='x1', yref='y1', font=dict(color=font_color, size=font_size), showarrow=False) ) return annotations
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_annotations(self, font_size=20, font_color='rgb(250,250,250)'): annotations = dict() #go.Annotations() nodes = self.snapshots[self.current_snapshot_index]['nodes'] for node, value in nodes.items(): annotations[node] = go.Annotation( text=node.key, # or replace labels with a different list for the text within the circle x=value[0][0], y=value[0][1], xref='x1', yref='y1', font=dict(color=font_color, size=font_size), showarrow=False ) #endfor return annotations
def create_annotated_heatmap(x=None, y=None, z=None): data = go.Heatmap(z=z, x=x, y=y, colorscale='Viridis') annotations = go.Annotations() for n, row in enumerate(z): for m, val in enumerate(row): annotations.append(go.Annotation(text=str(z[n][m]), x=x[m], y=y[n], xref='x1', yref='y1', showarrow=False)) fig = go.Figure(data=go.Data([data])) fig['layout'].update( annotations=annotations, xaxis=go.XAxis(ticks='', title='y_pred', side='bottom'), yaxis=go.YAxis(ticks='', title='y_true', ticksuffix=' '), # ticksuffix is a workaround to add a bit of padding autosize=False ) return json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
def update_graph4(product_selected1, product_selected2, product_selected3): z = confusion_matrix(y_test, y_pred) trace15 = go.Heatmap(z=z, x=['0', '1'], y=['1', '0'], colorscale=["#ab6d41", "#ffb785"]) xx = [0, 1] yy = [1, 0] annotations = go.Annotations() for n, row in enumerate(z): for m, val in enumerate(row): annotations.append( go.Annotation(text=str(z[n][m]), x=xx[m], y=yy[n], xref='x1', yref='y1', showarrow=False)) return { 'data': [trace15], 'layout': go.Layout( font=dict(family="Arial Narrow, sans-serif", color="black", size=16), annotations=annotations, width=400, height=400, autosize=False, showlegend=False, yaxis=dict(tickmode='array', tickvals=[-0.5, 0, 0.5, 1, 1.5], ticktext=['', 'Self-harm', '', 'No<br>self-harm', '']), xaxis=dict(tickmode='array', tickvals=[-0.5, 0, 0.5, 1, 1.5], ticktext=['', 'No self-harm', '', 'Self-harm', '']), title='<b>Confusion Matrix</b>', yaxis_title_text='<b>actual</b>', xaxis_title_text='<b>predicted</b>', paper_bgcolor='#F5F5F7') }
def make_annotations(pos, info, font_size=8, font_color='rgb(0,0,0)'): L = len(pos) if len(info) != L: raise ValueError( 'make_annotations()::The lists pos and info must have the same len' ) annotations = go.Annotations() for k in range(L): item = go.Annotation( text=info[k], # text to be shown within the node x=pos[k][0], y=pos[k][1], xref='x1', yref='y1', font=dict(color=font_color, size=font_size), showarrow=False) annotations.append(item) return annotations
def make_annotations(pos, text, font_size=14, font_color='#fff'): if len(text) != L: raise ValueError('The lists pos and text must have the same len') labels = go.Annotations() for k in pos: labels.append( go.Annotation( text=annotations[vertex_to_i[k]], x=pos[k][0], y=2 * M - position[k][1], xref='x1', yref='y1', font=dict(color=font_color, size=font_size), showarrow=False, bgcolor='#6175c1', bordercolor='#c7c7c7', borderwidth=1, borderpad=2, ), ) return labels
def MakeAnnotations(positions, texts, font_size=10, font_color='rgb(250,250,250)'): posLength = len(positions) textLength = len(texts) if (posLength != textLength): raise ValueError('Position and texts mismatch.') annotations = objs.Annotations() for k in range(posLength): currentColor = font_color currentText = texts[k] if (texts[k] == "None"): currentText = "X" currentColor = "red" annotations.append( objs.Annotation(text=currentText, x=positions[k][0], y=positions[k][1], xref='x1', yref='y1', font=dict(color=currentColor, size=font_size), showarrow=False)) return annotations
xx = np.linspace(1, 50, 1000) yy = exponenial_func(xx, *popt) trace1 = go.Scatter(x=x_to_fit, y=cum_sum, mode='markers', marker=go.Marker(color='rgb(255, 127, 14)'), name='Data') trace2 = go.Scatter(x=xx, y=yy, mode='lines', marker=go.Marker(color='rgb(31, 119, 180)'), name='Fit') annotation = go.Annotation(x=20, y=10, text='', showarrow=False) layout = go.Layout(title='Fit datos COVID19, Colombia', 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) st.write(fig) # Gráficas por ciudad st.title('Acumulado por Ciudad y R0 Estimado')
def update_graph(filtered_df_json, value, settings, graph_mode, clusters, threshold, graphName, xLabel, yLabel, zLabel, extraMin, extraMax, seriesInput, dbTableInput, figure, vessels, *filter_settings): if figure is not None: figure['data'] = [] minSet = [] gformula = "" gsols = 0.0 gr_squared = 0.0 valuesOrigin = "" # Populate with 2D Data when X and Y set TODO: Remove hardcode + Account for 3D if value[1] is None or value[2] is None: figure['data'] = [] else: # Create the dataset for the vessels selected # Get specifications specifications = [] for i in range(1, len(filter_settings), 3): specifications.append( get_condition(filter_settings[i], filter_settings[i + 1], filter_settings[i + 2])) # Cleanup and prepare conditions conditions = [] for specification in specifications: for condition in specification: conditions.append(condition) # Obtain filtered df df = [] singleLineAll = False secondAll = False for vessel in filter_settings[0]: if vessel == "All" and 'multiline' not in settings: df = SQL().get_df_from_series(dbTableInput, seriesInput) singleLineAll = True break elif vessel == "All": secondAll = True continue df.append(dfs[vessel].get_filtered(conditions=conditions)) if len(df) == 0 and secondAll: singleLineAll = True df = SQL().get_df_from_series(dbTableInput, seriesInput) if singleLineAll: dfsDF = df else: dfsDF = pd.concat(df) # Remove any NaN values print("THIS IS VALUE: {}".format(value)) if value[0] == "2D": dfsDF = dfsDF.dropna(subset=[value[1], value[2]]) else: dfsDF = dfsDF.dropna(subset=[value[1], value[2], value[3]]) if threshold != "None": # Remove outliers NOTE: Adjust the threshold to modify how strictly filtered the data will be. So far tested 1, 1.5, 3. Strict ~ Lax mean = np.mean(dfsDF[value[1]]) stdio = np.std(dfsDF[value[1]]) print "Mean: " + str(mean) + " Std: " + str(stdio) dfsDF = dfsDF[np.abs(dfsDF[value[1]] - mean) <= (threshold * stdio)] mean = np.mean(dfsDF[value[2]]) stdio = np.std(dfsDF[value[2]]) print "Mean: " + str(mean) + " Std: " + str(stdio) dfsDF = dfsDF[np.abs(dfsDF[value[2]] - mean) <= (threshold * stdio)] if value[0] == "3D": mean = np.mean(dfsDF[value[3]]) stdio = np.std(dfsDF[value[3]]) print "Mean: " + str(mean) + " Std: " + str(stdio) dfsDF = dfsDF[np.abs(dfsDF[value[3]] - mean) <= (threshold * stdio)] # unqVessels = dfsDF['Vessel'].unique().tolist() unqVessels = filter_settings[0] print unqVessels # Set axis labels if any if xLabel == "": xName = value[1] else: xName = xLabel if yLabel == "": yName = value[2] else: yName = yLabel if graphName == "": if value[0] == "2D": gName = value[1] + " vs " + value[2] else: gName = value[1] + " vs " + value[2] + " vs " + value[3] else: gName = graphName # Multiline Logic if 'multiline' in settings: counter = 0 benchmark = 0 annotation = [] sfList = SQL().get_vessel_codes() for vessel in unqVessels: global valuesOrigin valuesOrigin = vessel print list(dfsDF) if vessel == "All": vesselRow = SQL().get_df_from_series( dbTableInput, seriesInput) if value[0] == "2D": vesselRow = vesselRow.dropna( subset=[value[1], value[2]]) else: vesselRow = vesselRow.dropna( subset=[value[1], value[2], value[3]]) else: vesselRow = dfsDF.loc[dfsDF['Vessel'] == vessel] # Clustering & Hover Data Generation hoverData = [] if 'clustering' in settings: vesselRow = k_means(value, vesselRow, clusters) vesselRow['Vessel'] = pd.Series(vessel, index=vesselRow.index) hoverData = np.c_[vesselRow[value[1]], vesselRow[value[2]]] else: # Generate the Hover Data # Iterate each row from db for key, value1 in vesselRow.iterrows(): placeholderText = "" # Iterate each column in the row for index, row in value1.items(): # Compare the value in important_attributes for col in full_attributes: if col == index: if isinstance(row, float): placeholderText += "<b>" + index + "</b>: " + str( round(row, 3)) + "<br>" else: placeholderText += "<b>" + index + "</b>: " + str( row) + "<br>" break hoverData.append(placeholderText) if 'datapoints' in settings: if value[0] == "2D": scatterPoints = go.Scatter( x=vesselRow[value[1].encode('utf8')], y=vesselRow[value[2].encode('utf8')], name=vessel + " Marker", mode='markers', marker=go.Marker(color=colorList[counter]), text=hoverData, ) else: scatterPoints = go.Scatter3d( x=vesselRow[value[1].encode('utf8')], y=vesselRow[value[2].encode('utf8')], z=vesselRow[value[3].encode('utf8')], name=vessel + " Marker", mode='markers', marker=go.Marker(color=colorList[counter]), text=hoverData, ) figure['data'].append(scatterPoints) if 'regression' in settings: if value[0] == "2D": line_data, r_squared, sols, formula = regression( vesselRow[value[1].encode('utf8')], vesselRow[value[2].encode('utf8')], graph_mode, extraMin, extraMax) # tmpLst = [] # tmpLst.append(r_squared) # tmpLst.append(sols) # tmpLst.append(formula) # gformula[vessel] = tmpLst # gformula[vessel] = [r_squared, sols, formula] global gr_squared, gsols, gformula gr_squared = r_squared gsols = sols gformula = formula eqString, supScript = generateEquationString( formula) bestFit = go.Scatter( x=line_data['x'], y=line_data['y'], name=vessel + ' Line', mode='lines', marker=go.Marker(color=colorList[counter]), ) figure['data'].append(bestFit) if benchmark == 0: benchmark = max(line_data['y']) if vessel == "All": vslCde = "All" else: vslCde = sfList[vessel] annotation.append( go.Annotation(x=min(line_data['x']) + 10, y=benchmark - counter * benchmark * 0.1, text=vslCde + ": " + eqString.format(*supScript), showarrow=False)) layout2d = go.Layout( title=gName, plot_bgcolor='rgb(229, 229, 229)', xaxis=go.XAxis( title=xName, zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), yaxis=dict(title=yName, zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), annotations=annotation, # yaxis2=dict(title='Percentage', gridcolor='blue', overlaying='y', side='right', range=[100,0]), ) figure['layout'] = layout2d else: surfacePlot, surfaceLayout = plot_3d( vesselRow[value[1].encode('utf8')], vesselRow[value[2].encode('utf8')], vesselRow[value[3].encode('utf8')], value[1], value[2], value[3]) figure['data'].append(surfacePlot) figure['layout'] = surfaceLayout counter += 1 else: # If no multiline # Clustering & Hover Data Generation hoverData = [] if 'clustering' in settings: dfsDF = k_means(value, dfsDF, clusters) hoverData = np.c_[dfsDF[value[1]], dfsDF[value[2]]] else: # Generate the Hover Data # Iterate each row from db for key, value1 in dfsDF.iterrows(): placeholderText = "" # Iterate each column in the row for index, row in value1.items(): # Compare the value in important_attributes for col in full_attributes: if col == index: if isinstance(row, float): placeholderText += "<b>" + index + "</b>: " + str( round(row, 3)) + "<br>" else: placeholderText += "<b>" + index + "</b>: " + str( row) + "<br>" break hoverData.append(placeholderText) if value[0] == "2D": # Add scatter from data set if 'datapoints' toggled if len(figure['data']) < 1: figure['data'].append({}) if 'datapoints' in settings: figure['data'][0] = go.Scatter( x=dfsDF[value[1].encode('utf8')], y=dfsDF[value[2].encode('utf8')], name='Data Marker', mode='markers', text=hoverData, ) else: figure['data'][0] = None # Add Line/Curve if 'regression' toggled if len(figure['data']) < 2: figure['data'].append({}) if 'regression' in settings: line_data, r_squared, sols, formula = regression( dfsDF[value[1].encode('utf8')], dfsDF[value[2].encode('utf8')], graph_mode, extraMin, extraMax) print "R-Squared: " + str(r_squared) print "Sum of Least Squares: " + str(sols) print "A Formula: " print formula # global gr_squared, gsols, gformula gr_squared = r_squared gsols = sols gformula = formula eqString, supScript = generateEquationString(formula) figure['data'][1] = go.Scatter( x=line_data['x'], y=line_data['y'], name='Line', mode='lines', ) annotation = go.Annotation(x=min(line_data['x']) + 10, y=max(line_data['y']), text="y=" + eqString.format(*supScript), showarrow=False) layout2d = go.Layout( title=gName, plot_bgcolor='rgb(229, 229, 229)', xaxis=go.XAxis(title=xName, zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), yaxis=dict(title=yName, zerolinecolor='rgb(255,255,255)', gridcolor='rgb(255,255,255)'), annotations=[annotation], # yaxis2=dict(title='Percentage', gridcolor='blue', overlaying='y', side='right', range=[100,0]), ) figure['layout'] = layout2d else: figure['data'][1] = None else: # 3D # Add scatter from data set if 'datapoints' toggled if len(figure['data']) < 1: figure['data'].append({}) if 'datapoints' in settings: figure['data'][0] = go.Scatter3d( x=dfsDF[value[1].encode('utf8')], y=dfsDF[value[2].encode('utf8')], z=dfsDF[value[3].encode('utf8')], name='Data Marker', mode='markers', text=hoverData, ) else: figure['data'][0] = None # Add Line/Curve if 'regression' toggled if len(figure['data']) < 2: figure['data'].append({}) if 'regression' in settings: surfacePlot, surfaceLayout, minimumSet = plot_3d( dfsDF[value[1].encode('utf8')], dfsDF[value[2].encode('utf8')], dfsDF[value[3].encode('utf8')], value[1], value[2], value[3]) figure['data'][1] = surfacePlot figure['layout'] = surfaceLayout minSet = minimumSet print "MINSET" print minSet else: figure['data'][1] = None figure['layout']['scene']['xaxis']['title'] = xName figure['layout']['scene']['yaxis']['title'] = yName figure['layout']['scene']['zaxis']['title'] = zName figure['layout']['title'] = gName # Clean figure data figure['data'] = [i for i in figure['data'] if i is not None] return figure return default_figure
def predyellowcardweek(tid=None): mydb = mysql.connector.connect( host="localhost", user="******", passwd="battlefield4", database="footballmanagement" ) mycursor = mydb.cursor() if tid==None: mycursor.execute("select count(eid),matches.mid from event,matches where event.`Type`='Y' and event.mid=matches.mid and matches.season='2017/18' group by event.mid order by mid asc;") else: mycursor.execute("select count(eid),matches.mid from event,matches where event.`Type`='Y' and event.tid="+str(tid)+" and event.mid=matches.mid and matches.season='2017/18' group by event.mid order by mid asc;") myresult = mycursor.fetchall() # print(myresult) x1 = list() ctr=0 for x in myresult: x1.append((ctr,x[0])) ctr=ctr+1 # print(x1) x=x1 points = np.array(x) x = points[:,0] y = points[:,1] z = np.polyfit(x, y, 3) f = np.poly1d(z) x_new = np.linspace(0, 40, 50) y_new = f(x_new) trace1 = go.Scatter( x=x, y=y, mode='markers', name='Data', marker=dict( size=12 ) ) trace2 = go.Scatter( x=x_new, y=y_new, mode='lines', name='Fit' ) annotation = go.Annotation( x=6, y=-4.5, text='Chelsea expected goals', showarrow=False ) layout = go.Layout( title='Polynomial Fit', annotations=[annotation] ) data = [trace1,trace2] fig = go.Figure(data=data, layout=layout) # py.plot(data, filename='interpolation-and-extrapolation') graphJSON = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder) print(data) return graphJSON
# Extracts the labels for the legend genres = list(Titles.index.values) platforms = list(Titles.columns.values) # Label of heatmap cells annotations = go.Annotations() for i, row in enumerate(Sales.values): for j, val in enumerate(row): annotations.append( go.Annotation(text=str(Popularity[i][j]), x=platforms[j], y=genres[i], xref='x1', yref='y1', showarrow=False, font={ 'size': 11, 'color': 'white' } if Popularity[i][j] < 4.5 else { 'size': 11, 'color': 'black' })) # Hover text info. Numbers of titles produced titles_info = Titles.values.astype(str) sales_info = Sales.values.astype(str) for i, row in enumerate(Titles.values): for j, val in enumerate(row): titles_info[i][j] = "Number of titles: " + str(titles_info[i][j]) # Layout of the heatmap graph
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 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=(str(props['text']) if isinstance(props['text'], six.string_types) else 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.layout.annotation.Font( color=props['style']['color'], size=props['style']['fontsize'] ) ) self.plotly_fig['layout']['annotations'] += annotation, self.msg += " Heck, yeah I drew that annotation\n"
def goalteam(tid=None): # print("booo") mydb = mysql.connector.connect(host="localhost", user="******", passwd="battlefield4", database="footballmanagement") mycursor = mydb.cursor() if tid == None: mycursor.execute( "select matches.Season,count(goal.GID) from goal,matches,teams where goal.mid=matches.mid and goal.tid=teams.tid group by matches.season;" ) else: mycursor.execute( "select matches.Season,count(goal.GID) from goal,matches,teams where goal.mid=matches.mid and goal.tid=teams.tid and teams.tid=" + str(tid) + " group by matches.season;") myresult = mycursor.fetchall() # print(myresult) x1 = list() ctr = 0 for x in myresult: x1.append((ctr, x[1])) ctr = ctr + 1 # print(x1) x = x1 points = np.array(x) x = points[:, 0] y = points[:, 1] z = np.polyfit(x, y, 3) f = np.poly1d(z) print("hello") x_new = np.linspace(0, 30, 50) y_new = f(x_new) print(y_new) trace1 = go.Scatter(x=x, y=y, mode='markers', name='Data', marker=dict(size=12)) trace2 = go.Scatter(x=x_new, y=y_new, mode='lines', name='Fit') annotation = go.Annotation(x=6, y=-4.5, text='Chelsea expected goals', showarrow=False) layout = go.Layout(title='Polynomial Fit', annotations=[annotation]) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) # # py.plot(fig, filename='interpolation-and-extrapolation') # graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) # # print(graphJSON) # points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)]) # x = points[:,0] # y = points[:,1] # z = np.polyfit(x, y, 3) # f = np.poly1d(z) # x_new = np.linspace(0, 10, 50) # y_new = f(x_new) # trace1 = go.Scatter( # x=x, # y=y, # mode='markers', # name='Data', # marker=dict( # size=12 # ) # ) # trace2 = go.Scatter( # x=x_new, # y=y_new, # mode='lines', # name='Fit' # ) # annotation = go.Annotation( # x=6, # y=-4.5, # text='$0.43X^3 - 0.56X^2 + 16.78X + 10.61$', # showarrow=False # ) # layout = go.Layout( # title='Polynomial Fit in Python', # annotations=[annotation] # ) # data = [trace1, trace2] # fig = go.Figure(data=data, layout=layout) # py.plot(data, filename='interpolation-and-extrapolation') # graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) # print(graphJSON) # count = 500 # xScale = np.linspace(0, 100, count) # yScale = np.random.randn(count) # # Create a trace # trace = go.Scatter( # x = xScale, # y = yScale # ) # data = [trace] graphJSON = json.dumps(data, cls=plotly.utils.PlotlyJSONEncoder) return graphJSON
def plot_stacked_barchart_cath(stats_df, title, plot_out=None): plot_dict = {} for cath in np.unique(stats_df['cath_topology']): plot_dict[cath] = {} for dataset in np.unique(stats_df['dataset']): plot_dict[cath]['Set ' + str(dataset)] = len(stats_df.query('cath_topology == @cath and dataset == @dataset')) plot_df = pd.DataFrame(plot_dict) plot_df_relative = plot_df.apply(lambda x: x/np.sum(x), axis=1) plot_df.loc['Total'] = plot_df.sum(axis=0).tolist() plot_df_relative.loc['Total'] = plot_df.loc['Total'] / plot_df.loc['Total'].sum(axis=0) #add bar for every group == cath data = [] for cath in plot_df.columns: data.append( go.Bar( x=plot_df_relative.index.tolist(), y=plot_df_relative[cath], showlegend=True, name=cath ) ) #add annotation for every bar y=0 annotations_list = [] for cath in plot_df.columns: y += plot_df_relative[cath]['Total'] for dataset in plot_df.index.tolist(): annotations_list.append( go.Annotation( x=dataset, y=y-0.1, text=str(plot_df[cath][dataset]), showarrow=False, font=dict(color='#ffffff') ) ) plot = { "data": data, "layout": go.Layout( barmode="stack", title=title, yaxis=dict( title="Proportion of CATH classes", exponentformat="e", showexponent='All' ), annotations=go.Annotations(annotations_list), legend=dict(orientation="h"), font=dict(size=16) ) } if title=="": plot['layout']['margin']['t'] = 10 plotly_plot(plot, filename=plot_out, auto_open=False)
points = np.array([(1, 1), (2, 4), (3, 1), (9, 3)]) x = points[:, 0] y = points[:, 1] z = np.polyfit(x, y, 3) f = np.poly1d(z) x_new = np.linspace(0, 10, 50) y_new = f(x_new) trace1 = go.Scatter(x=x, y=y, mode='markers', name='Data', marker=dict(size=12)) trace2 = go.Scatter(x=x_new, y=y_new, mode='lines', name='Fit') annotation = go.Annotation(x=6, y=-4.5, text='$0.43X^3 - 0.56X^2 + 16.78X + 10.61$', showarrow=False) layout = go.Layout(title='Polynomial Fit in Python', annotations=[annotation]) data = [trace1, trace2] fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='interpolation-and-extrapolation')
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