def plot3Dgraph(self, fig, x, y, z): import plotly.graph_objs as go surface = go.Surface(x=x, y=y, z=z) data = go.Data([surface]) layout = go.Layout( title='Parametric Plot', scene=go.Scene(xaxis=go.XAxis(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)'), yaxis=go.YAxis(gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)'), zaxis=go.ZAxis( gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)', showbackground=True, backgroundcolor='rgb(230, 230,230)'))) fig = go.Figure(data=data, layout=go.Layout(title='Offline Plotly Testing', width=800, height=500, xaxis=dict(title='X-axis'), yaxis=dict(title='Y-axis'))) plot(fig, show_link=False)
def plotScatter3D(df, mytitle, allDict, notebook=True): import plotly import plotly.graph_objs as go statTooltips = [] doctopics = [] docdates = [] for key in df.index: try: statTooltips.append(tooltipText(allDict[key])) except: print(key, "not found") colorDropdown = widgets.Dropdown(description='Topics:', value=df["specGroup"][0], options=df["specGroup"]) trace = go.Scatter3d( x=df['pca-one'], y=df['pca-two'], z=df['pca-three'], mode='markers', marker=dict( size=df["size"], color=df["specGroup"], colorscale='Viridis', colorbar=dict(title="Topic<br>Relevance"), # symbol=df["specGroup"], # TODO actually want Feedname showscale=True, opacity=0.6), # symbol=df["specGroup"], text=statTooltips, textfont=dict(family="sans serif", size=8, color='crimson'), hoverinfo='text') # Configure the layout. # layout = go.Layout(margin={'l': 0, 'r': 0, 'b': 0, 't': 0}) layout = go.Layout(showlegend=False, title=mytitle, margin={ 'l': 0, 'r': 0, 'b': 50, 't': 30 }, scene=go.Scene( xaxis=go.XAxis(title='PCA Dimension<br>Reduced X'), yaxis=go.YAxis(title='PCA Dimension<br>Reduced Y'), zaxis=go.ZAxis(title='PCA Dimension<br>Reduced Z'))) data = [trace] plot_figure = go.Figure(data=data, layout=layout) camera = dict(eye=dict(x=1.5, y=1.5, z=0.1)) plot_figure.update_layout(scene_camera=camera) # plt.tight_layout() # Render the plot. pl = plotly.offline.iplot(plot_figure) pl if not notebook: plotly.offline.plot(plot_figure, filename='file.html') return trace
def cluster_test(): dataSet = getDataSetFromDatabase() labels = cluster(dataSet) # transpose to work with Plotly-format (don't remember if it is sklear.cluster or plotly that is counterintuitive) dataToChart = dataSet.transpose() # create multi dimensional array of data by label segmentedData = [[[] for _ in xrange(3)] for _ in xrange(numClusters)] for num, label in enumerate(labels): print (str(num) + ' ' + str(label)) segmentedData[label][0].append(dataToChart[0][num]) segmentedData[label][1].append(dataToChart[1][num]) segmentedData[label][2].append(dataToChart[2][num]) if debug: print(segmentedData) # create traces for plotly traces = [] baseColor = 100 i = 0 while i < numClusters: trace = go.Scatter3d( x=segmentedData[i][0], y=segmentedData[i][1], z=segmentedData[i][2], mode='markers', marker=dict( size=12, line=dict( color='rgba(baseColor+(i*2), baseColor+(i*2), baseColor+(i*2), 0.14)', width=0.5 ), opacity=0.8 ), # @todo: fix names list for plotly #text=names ) traces.append(trace) i+=1 layout = go.Layout( scene=go.Scene( xaxis=go.XAxis(title='Carbs', tickprefix='Carbs ', showtickprefix='first'), yaxis=go.YAxis(title='Fat', tickprefix='Fat ', showtickprefix='first'), zaxis=go.ZAxis(title='Protein', tickprefix='Protein ', showtickprefix='first') ), height = 800, width = 800, ) fig = go.Figure(data=traces, layout=layout) graphJSON = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder) return render_template('cluster-test.html', graphJSON=graphJSON)
def plotSentiment3D(df2, allDict, notebook=True, topic=""): statTooltips = [] for key in list(df2["UID"]): try: statTooltips.append(tooltipSentiText(allDict[key])) except: print(key, "not found") trace = go.Scatter3d( x=df2['Positive'], y=df2['Negative'], z=df2['Neutral'], mode='markers', marker=dict( size=df2["Topicality"], color=df2['Overall'], colorscale='Inferno', colorbar=dict(title="Compound<br>Sentiment"), # symbol=df["specGroup"], # TODO actually want Feedname showscale=True, opacity=0.7, ), # symbol=df["specGroup"], text=statTooltips, textfont=dict(family="sans serif", size=8, color='crimson'), hoverinfo='text') # Configure the layout. layout = go.Layout(showlegend=False, title="<b>Sentiment Analysis for Topic '" + topic + "'", margin={ 'l': 0, 'r': 0, 'b': 50, 't': 30 }, scene=go.Scene( xaxis=go.XAxis(title='Positive<br>Sentiment'), yaxis=go.YAxis(title='Negative<br>Sentiment'), zaxis=go.ZAxis(title='Neutral<br>Sentiment'))) data = [trace] plot_figure = go.Figure(data=data, layout=layout) camera = dict(eye=dict(x=1.5, y=1.5, z=0.1)) plot_figure.update_layout(scene_camera=camera) #plt.tight_layout() go.FigureWidget(data=data, layout=layout) pl = plotly.offline.iplot(plot_figure) pl if not notebook: plotly.offline.plot(plot_figure, filename='file.html') return
def show(self, title): figs = [] for ind_f in range(len(self.frames)): (points, triangles, signals) = self.frames[ind_f] points = array(points) triangles = array(triangles) signals = array(signals) signals_per_triangle = list( (signals[triangles[i, 0]] + signals[triangles[i, 1]] + signals[triangles[i, 2]]) / 3 for i in range(triangles.shape[0])) signals_per_triangle[0] += 0.001 # Validate colormap my_colormap = FF._validate_colors("Portland", 'tuple') newdata = FF._trisurf(x=points[:, 2], y=points[:, 0], z=points[:, 1], colormap=my_colormap, simplices=triangles, color_func=signals_per_triangle, plot_edges=False, edges_color='rgb(50, 50, 50)', show_colorbar=False, data_list=True) figs += newdata axis = dict(showbackground=True, backgroundcolor='rgb(230, 230, 230)', gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)') xaxis = axis.copy() xaxis['range'] = [-0.08, 0.09] yaxis = axis.copy() yaxis['range'] = [-0.11, 0.05] zaxis = axis.copy() zaxis['range'] = [0.02, 0.18] aspectratio = dict(x=1, y=1, z=1) layout = graph_objs.Layout(title=title, width='100%', height=800, scene=graph_objs.Scene( xaxis=graph_objs.XAxis(xaxis), yaxis=graph_objs.YAxis(yaxis), zaxis=graph_objs.ZAxis(zaxis), aspectratio=dict(x=aspectratio['x'], y=aspectratio['y'], z=aspectratio['z']), )) return my_iplot(graph_objs.Figure(data=figs, layout=layout))
def plot(self): title = self.titlestring % (self.DS.name) if self.DS.d < 3: print("Scatter plot must get at least 3 dimensional dataset") return xaxis = go.XAxis(title=list(self.DS.D)[0]) yaxis = go.YAxis(title=list(self.DS.D)[1]) zaxis = go.ZAxis(title=list(self.DS.D)[2]) layout = dict(title=title, scene=dict(xaxis=xaxis, yaxis=yaxis, zaxis=zaxis)) trace = go.Scatter3d(x=self.DS.D.as_matrix()[:, 0], y=self.DS.D.as_matrix()[:, 1], z=self.DS.D.as_matrix()[:, 2], mode='markers', marker=dict(size=4)) data = [trace] fig = dict(data=data, layout=layout) return self.makeplot(fig, "agg/" + self.shortname)
def trace3d(*f): a1 = a2 = np.arange(10, 70, 1) a2t = a2[:, np.newaxis] trace = [] for fnct in f: z = fnct(a1, a2t) trace.append(go.Surface(z=z, x=a1, y=a2)) data = go.Data(trace) axis = dict( showbackground=True, # (!) show axis background backgroundcolor="rgb(204, 204, 204)", # set background color to grey gridcolor="rgb(255, 255, 255)", # set grid line color zerolinecolor="rgb(255, 255, 255)", # set zero grid line color ) layout = go.Layout(scene=go.Scene( xaxis=go.XAxis(axis, title='alpha1'), # set x-axis style yaxis=go.YAxis(axis, title='alpha2'), # set y-axis style zaxis=go.ZAxis(axis, title='Force') # set z-axis style )) fig = go.Figure(data=data, layout=layout) return iplot(fig, filename='s8_surface')
def scatter_matrix_3d(M, marker=default_marker, width=600, height=600, title=None, x_label=None, y_label=None, z_label=None): data = [go.Scatter3d( x=M[:, 0], y=M[:, 1], z=M[:, 2], mode='markers', marker=marker )] layout = go.Layout( width=width, height=height, title=title, scene=go.Scene( xaxis=go.XAxis(title=x_label), yaxis=go.YAxis(title=y_label), zaxis=go.ZAxis(title=z_label), ) ) return iplot(go.Figure(data=data, layout=layout))
def setup_layout(self): "Setup the axis properties." axis = dict(showbackground=True, backgroundcolor='rgb(230, 230, 230)', gridcolor='rgb(255, 255, 255)', zerolinecolor='rgb(255, 255, 255)') xaxis = axis.copy() xaxis['range'] = [-1.1, 1.1] yaxis = axis.copy() yaxis['range'] = [-1.1, 1.1] zaxis = axis.copy() zaxis['range'] = [-1.1, 1.1] aspectratio = dict(x=1, y=1, z=1) self.layout = go.Layout(title='Kendall Triangles', width='100%', height=800, scene=go.Scene( xaxis=go.XAxis(xaxis), yaxis=go.YAxis(yaxis), zaxis=go.ZAxis(zaxis), aspectratio=dict(x=aspectratio['x'], y=aspectratio['y'], z=aspectratio['z']), ))
def visualize_3d(config, G, node_sizes): keys = G.nodes() values = range(len(G.nodes())) dictionary = dict(zip(keys, values)) inv_map = {v: k for k, v in dictionary.items()} G = nx.relabel_nodes(G, dictionary) edge_trace = go.Scatter3d(x=[], y=[], z=[], mode='lines', line=go.scatter3d.Line( color='rgba(136, 136, 136, .8)', width=1), hoverinfo='none') node_trace = go.Scatter3d( x=[], y=[], z=[], mode='markers', #name='actors', marker=go.scatter3d.Marker( symbol='circle', size=[], color=[], colorscale=config.color_scale, #'Viridis', colorbar=dict(thickness=15, title='Node Connections', xanchor='left', titleside='right'), #line=go.Line(color='rgb(50,50,50)', width=0.5) ), text=[], hoverinfo='text') positions = nx.fruchterman_reingold_layout(G, dim=3, k=0.5, iterations=1000) for edge in G.edges(): x0, y0, z0 = positions[edge[0]] x1, y1, z1 = positions[edge[1]] edge_trace['x'] += ( x0, x1, ) edge_trace['y'] += ( y0, y1, ) edge_trace['z'] += ( z0, z1, ) for node in G.nodes(): x, y, z = positions[node] node_trace['x'] += (x, ) node_trace['y'] += (y, ) node_trace['z'] += (z, ) for i, adjacencies in G.adjacency(): node_trace['marker']['color'] += (len(adjacencies), ) for size in node_sizes: node_trace['marker']['size'] += (abs(math.log(size)) * 10, ) for node in G.nodes(): node_trace['text'] += (inv_map[node], ) axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') layout = go.Layout( title=config.title, width=1000, height=1000, showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest', ) fig = go.Figure(data=[node_trace, edge_trace], layout=layout) offpy(fig, filename=config.out_file_name, auto_open=True, show_link=False)
def main(date, wavelengths, keywords=[], allSpectra=False): """ Plot three wavelengths against each other from a specified set of data. :param date: (string) Data collection date YYYY_MMDD :param wavelengths: (3-tuple) Wavelengths to plot against another :param keywords: (list of strings) Strings which should be included in the filenames of files being plotted :allSpectra: (boolean) Determines where there is one point for every spectra collected, or one point for every leaf file :return: (None) """ # Convert the wavelengths to indices for accessing the data wavelengthIndices = map(wavelengthToIndex, wavelengths) wavelengthIndex1 = wavelengthIndices[0] wavelengthIndex2 = wavelengthIndices[1] wavelengthIndex3 = wavelengthIndices[2] # Get the data files we will be looking at dataPath = DATA_DIRECTORIES[date] filesToPlot = FileIO.getDatafileNames(dataPath, keywords) pointsDR = [] pointsGR = [] pointsSUS = [] for name in filesToPlot: tokens = name[0:-4].split('_') map(lambda x: x.lower(), tokens) plant = tokens[0] resistance = tokens[1] filePath = os.path.join(dataPath, name) data = FileIO.loadCSV(filePath) try: rows, columns = data.shape if columns < 2: continue except: continue if allSpectra: xValues = data[:, wavelengthIndex1] yValues = data[:, wavelengthIndex2] zValues = data[:, wavelengthIndex3] points = np.zeros((rows, 3)) points[:, 0] = xValues points[:, 1] = yValues points[:, 2] = zValues if resistance == SUSCEPTIBLE: if pointsSUS == []: pointsSUS = points else: pointsSUS = np.append(pointsSUS, points, axis=0) elif resistance == DR_RESISTANT: if pointsDR == []: pointsDR = points else: pointsDR = np.append(pointsDR, points, axis=0) elif resistance == GR_RESISTANT: if pointsGR == []: pointsGR = points else: pointsGR = np.append(pointsGR, points, axis=0) else: raise Exception("Unknown resistance type: " + resistance) else: mean = np.mean(data, axis=0) meanValue1 = mean[wavelengthIndex1] meanValue2 = mean[wavelengthIndex2] meanValue3 = mean[wavelengthIndex3] if resistance == SUSCEPTIBLE: pointsSUS.append([meanValue1, meanValue2, meanValue3]) elif resistance == DR_RESISTANT: pointsDR.append([meanValue1, meanValue2, meanValue3]) elif resistance == GR_RESISTANT: pointsGR.append([meanValue1, meanValue2, meanValue3]) else: raise Exception("Unknown resistance type: " + resistance) # Plot the wavelengths pointsDR = np.array(pointsDR) pointsGR = np.array(pointsGR) pointsSUS = np.array(pointsSUS) traceSUS = plotPoints(pointsSUS, RESISTANCE_STRINGS[SUSCEPTIBLE], 'rgba(255, 0, 0, 0)') traceDR = plotPoints(pointsDR, RESISTANCE_STRINGS[DR_RESISTANT], 'rgba(0, 255, 0, 0)') traceGR = plotPoints(pointsGR, RESISTANCE_STRINGS[GR_RESISTANT], 'rgba(0, 0, 255, 0)') layout = go.Layout( title='3D Wavelength Plot', scene=go.Scene( xaxis=go.XAxis(title='Reflectance @ ' + str(wavelengths[0]) + ' nm'), yaxis=go.YAxis(title='Reflectance @ ' + str(wavelengths[1]) + ' nm'), zaxis=go.ZAxis(title='Reflectance @ ' + str(wavelengths[2]) + ' nm') ) ) data = [traceSUS, traceDR, traceGR] fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='3D Wavelength Plot')
title='Amplitude', titlefont=dict( family='Courier New, monospace', size=18, color='#7f7f7f' ) ) ) layout_pulse_evolution = go.Layout( autosize = False, width=800, height=800, title='Pulse Evolution', scene=go.Scene( xaxis=go.XAxis(title='Time'), yaxis=go.YAxis(title='Distance'), zaxis=go.ZAxis(title='Amplitude')) ) trace_input_pulse = go.Scatter(y=np.absolute(uaux)) pulse_evolution = go.Figure(data=[trace_pulse_evolution], layout=layout_pulse_evolution) input_pulse = go.Figure(data=[trace_input_pulse], layout=layout_input_pulse) plot([trace_phase_change], filename='./phase_change.html') plot([trace_pulse_broad], filename='./pulse_broadening.html') plot(pulse_evolution, filename='./pulse_evolution.html') plot(input_pulse, filename='./input_pulse.html')
titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f')), yaxis=dict(title='Number Density', titlefont=dict(family='Courier New, monospace', size=18, color='#7f7f7f'))) layout_pulse_evolution = go.Layout( autosize=False, width=800, height=800, title='Pulse Evolution', scene=go.Scene(xaxis=go.XAxis(title='r(cm)'), yaxis=go.YAxis(title='z(cm)'), zaxis=go.ZAxis(title='Field Amplitude'))) layout_number_density = go.Layout(autosize=False, width=800, height=800, title='Number Density', scene=go.Scene( xaxis=go.XAxis(title='Time&Radius'), yaxis=go.YAxis(title='z(cm)'), zaxis=go.ZAxis(title='Number Density'))) layout_pulse_contour = go.Layout( autosize=False, width=600, height=400, title='Pulse Evolution Near Ionization', xaxis=dict(title='r(cm)',
def update_graph(occupation_data, skill_data): """ Creates the 3D movable graph to visualize the common skills between respective roles """ input_data = occupation_data + skill_data input_data = [a.lower() for a in input_data] if input_data == []: fix_position_list = [ 'web developer', 'ict project manager', 'ict business analyst', 'software developer', 'ict network engineer', 'database administrator', 'ict consultant', 'data analyst', 'ict application developer', 'mobile app developer' ] for key, value in graph_visualization_data.items(): if key == 'nodes': nodes_new = [ k for k in value if k['id'].lower() in fix_position_list ] if key == 'links': links_new = [ k for k in value if k['source'].lower() in fix_position_list or k['target'].lower() in fix_position_list ] new_graph_data = {'nodes': nodes_new, 'links': links_new} G = nx.json_graph.node_link_graph(new_graph_data) pos = nx.spring_layout(G, dim=3) Xn_role = [pos[k][0] for k in pos if k in occupations] Yn_role = [pos[k][1] for k in pos if k in occupations] Zn_role = [pos[k][2] for k in pos if k in occupations] role_labels = [k for k in pos if k in occupations] Xn_skill = [pos[k][0] for k in pos if k in skills_graph] Yn_skill = [pos[k][1] for k in pos if k in skills_graph] Zn_skill = [pos[k][2] for k in pos if k in skills_graph] skill_labels = [k for k in pos if k in skills_graph] Xe = [] Ye = [] Ze = [] for e in G.edges(): Xe.extend([pos[e[0]][0], pos[e[1]][0], None]) Ye.extend([pos[e[0]][1], pos[e[1]][1], None]) Ze.extend([pos[e[0]][2], pos[e[1]][2], None]) trace_nodes_role = go.Scatter3d(x=Xn_role, y=Yn_role, z=Zn_role, mode='markers', marker=dict(size='18', symbol='dot', color='rgb(255,140,0)'), text=role_labels, hoverinfo='text') trace_nodes_skill = go.Scatter3d(x=Xn_skill, y=Yn_skill, z=Zn_skill, mode='markers', marker=dict(symbol='dot', color='rgb(33,188,235)'), text=skill_labels, hoverinfo='text') trace_edges = go.Scatter3d(mode='lines', x=Xe, y=Ye, z=Ze, line=dict(width=0.6, color='rgb(119,136,153)'), hoverinfo='none') axis = dict( showline=False, # hide axis line, grid, ticklabels and title zeroline=False, showgrid=False, showticklabels=False, showbackground=False, title='') layout_skill_graph = go.Layout( title="Skill tree for Jobs", titlefont=dict(color='white'), showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=dict(l=0, r=0, t=40, b=0), hovermode='closest', plot_bgcolor='#131827', paper_bgcolor='#131827', #set background color annotations=go.Annotations([ go.Annotation(showarrow=False, text="", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=go.Font(size=14)) ])) data = go.Data([trace_edges, trace_nodes_role, trace_nodes_skill]) fig = go.Figure(data=data, layout=layout_skill_graph) return fig else: for key, value in graph_visualization_data.items(): if key == 'nodes': nodes_new = [k for k in value if k['id'].lower() in input_data] if key == 'links': links_new = [ k for k in value if k['source'].lower() in input_data or k['target'].lower() in input_data ] new_graph_data = {'nodes': nodes_new, 'links': links_new} G = nx.json_graph.node_link_graph(new_graph_data) pos = nx.spring_layout(G, dim=3) Xn_role = [pos[k][0] for k in pos if k in occupations] Yn_role = [pos[k][1] for k in pos if k in occupations] Zn_role = [pos[k][2] for k in pos if k in occupations] role_labels = [k for k in pos if k in occupations] Xn_skill = [pos[k][0] for k in pos if k in skills_graph] Yn_skill = [pos[k][1] for k in pos if k in skills_graph] Zn_skill = [pos[k][2] for k in pos if k in skills_graph] skill_labels = [k for k in pos if k in skills_graph] Xe = [] Ye = [] Ze = [] for e in G.edges(): Xe.extend([pos[e[0]][0], pos[e[1]][0], None]) Ye.extend([pos[e[0]][1], pos[e[1]][1], None]) Ze.extend([pos[e[0]][2], pos[e[1]][2], None]) trace_nodes_role = go.Scatter3d(x=Xn_role, y=Yn_role, z=Zn_role, mode='markers', marker=dict(size='18', symbol='dot', color='rgb(255,140,0)'), text=role_labels, hoverinfo='text') trace_nodes_skill = go.Scatter3d(x=Xn_skill, y=Yn_skill, z=Zn_skill, mode='markers', marker=dict(symbol='dot', color='rgb(33,188,235)'), text=skill_labels, hoverinfo='text') trace_edges = go.Scatter3d(mode='lines', x=Xe, y=Ye, z=Ze, line=dict(width=0.6, color='rgb(119,136,153)'), hoverinfo='none') axis = dict( showline=False, # hide axis line, grid, ticklabels and title zeroline=False, showgrid=False, showticklabels=False, showbackground=False, title='') layout_skill_graph = go.Layout( title="Skill tree for Jobs", titlefont=dict(color='white'), showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=dict(l=0, r=0, t=40, b=0), hovermode='closest', plot_bgcolor='#131827', paper_bgcolor='#131827', #set background color annotations=go.Annotations([ go.Annotation(showarrow=False, text="", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=go.Font(size=14)) ])) data = go.Data([trace_edges, trace_nodes_role, trace_nodes_skill]) fig = go.Figure(data=data, layout=layout_skill_graph) return fig
def update_graph(dimension_select, xaxis_name, yaxis_name, zaxis_name, rows, selected_indices, old_figure): data_frame = pd.DataFrame(rows) marker_colours = [BLUE] * len(data_frame) if selected_indices is not None: for i in selected_indices: marker_colours[i] = ORANGE if dimension_select == '2D': linear_model = lm.LinearRegression() a1 = array(data_frame[xaxis_name], ndmin=2).transpose() a2 = array(data_frame[yaxis_name], ndmin=2).transpose() linear_model.fit(a1, a2) return { 'data': [ go.Scattergl(name='data', x=data_frame[xaxis_name], y=data_frame[yaxis_name], text="({}, {})".format(xaxis_name, yaxis_name), mode='markers', marker={ 'size': 15, 'opacity': 0.5, 'color': marker_colours }), go.Scattergl( name='Regression Line', x=[ data_frame[xaxis_name].min(), data_frame[xaxis_name].max() ], y=[ linear_model.predict([[data_frame[xaxis_name].min()] ])[0][0], linear_model.predict([[data_frame[xaxis_name].max()] ])[0][0] ], mode='line', line={ 'color': 'black', 'width': 5 }) ], 'layout': go.Layout(height=800, width=1300, title="{} Over {}".format(yaxis_name, xaxis_name), xaxis={ 'title': xaxis_name, 'type': 'linear' }, yaxis={ 'title': yaxis_name, 'type': 'linear' }, margin={ 'l': 40, 'b': 40, 't': 40, 'r': 40 }, hovermode='closest') } else: if old_figure['data'][0]['type'] == 'scatter3d': camera = old_figure['layout']['scene']['camera'] else: camera = DEFAULT_3D_CAMERA return { 'data': [ go.Scatter3d(name='data', x=data_frame[xaxis_name], y=data_frame[yaxis_name], z=data_frame[zaxis_name], text="({}, {}, {})".format( xaxis_name, yaxis_name, zaxis_name), mode='markers', marker={ 'size': 4, 'opacity': 0.5, 'color': marker_colours }) ], 'layout': go.Layout(height=800, width=1300, title="{} Over {} Over {}".format( yaxis_name, xaxis_name, zaxis_name), margin={ 'l': 40, 'b': 40, 't': 40, 'r': 40 }, hovermode='closest', scene=go.Scene( camera=camera, xaxis=go.XAxis(title=xaxis_name), yaxis=go.YAxis(title=yaxis_name), zaxis=go.ZAxis(title=zaxis_name), )) }
def render(self, **kwargs): """ Plots the surface and the control points grid. """ # Calling parent function super(VisSurface, self).render(**kwargs) # Initialize variables plot_data = [] for plot in self._plots: # Plot control points if plot['type'] == 'ctrlpts' and self._config.display_ctrlpts: pts = np.array( utilities.make_quad(plot['ptsarr'], plot['size'][1], plot['size'][0])) cp_z = pts[:, 2] + self._ctrlpts_offset figure = graph_objs.Scatter3d( x=pts[:, 0], y=pts[:, 1], z=cp_z, name=plot['name'], mode='lines+markers', line=dict(color=plot['color'], width=self._config.line_width, dash='solid'), marker=dict( color=plot['color'], size=self._config.line_width * 2, )) plot_data.append(figure) # Plot evaluated points if plot['type'] == 'evalpts': pts = np.array( utilities.make_triangle(plot['ptsarr'], plot['size'][1], plot['size'][0])) figure = graph_objs.Scatter3d( x=pts[:, 0], y=pts[:, 1], z=pts[:, 2], name=plot['name'], mode='lines', line=dict(color=plot['color'], width=self._config.line_width), ) plot_data.append(figure) plot_layout = dict( width=self._config.figure_size[0], height=self._config.figure_size[1], autosize=False, showlegend=self._config.display_legend, scene=graph_objs.Scene( xaxis=graph_objs.XAxis( showgrid=self._config.display_axes, showline=self._config.display_axes, zeroline=self._config.display_axes, showticklabels=self._config.display_axes, title='', ), yaxis=graph_objs.YAxis( showgrid=self._config.display_axes, showline=self._config.display_axes, zeroline=self._config.display_axes, showticklabels=self._config.display_axes, title='', ), zaxis=graph_objs.ZAxis( showgrid=self._config.display_axes, showline=self._config.display_axes, zeroline=self._config.display_axes, showticklabels=self._config.display_axes, title='', ), aspectmode='data', ), ) # Generate the figure fig = graph_objs.Figure(data=plot_data, layout=plot_layout) # Process keyword arguments fig_filename = kwargs.get('fig_save_as', None) fig_display = kwargs.get('display_plot', True) # Display the plot plotly.offline.plot( fig, show_link=False, filename=self._config.figure_filename, image=None if fig_display else self._config.figure_image_format, image_filename=self._config.figure_image_filename if fig_filename is None else fig_filename, auto_open=fig_display)
def sdssGalexTransform2ColorQAPlots3(df, colName_dmag, colName_color1, colName_color2, colName_res, dmagName, colorName1, colorName2, norder, p, rms, outputFileName): # An initial sanity check... if norder > 2: print 'sdssGalexTransform2ColorQAPlots3 can not deal with norder > 2... skipping...' return 1 # Create data from color1, color2, and res... # If the sample size is larger than 1000, # take a random sample of 1000 elements... n_elements = df[colName_res].size if n_elements <= 1000: x = df.loc[:,colName_color1].values y = df.loc[:,colName_color2].values z = df.loc[:,colName_res].values else: df1000 = df.sample(n=1000,axis=0) n_elements = df1000[colName_res].size x = df1000.loc[:,colName_color1].values y = df1000.loc[:,colName_color2].values z = df1000.loc[:,colName_res].values data = np.c_[x,y,z] # Regular grid covering the domain of the data... mn = np.min(data, axis=0) mx = np.max(data, axis=0) X,Y = np.meshgrid(np.linspace(mn[0], mx[0], 20), np.linspace(mn[1], mx[1], 20)) XX = X.flatten() YY = Y.flatten() # Evaluate it on grid... Z = 0.00 + 0.00*X + 0.00*Y # Create data_fit from color1, color2, and the fit parameters p[*]... x_fit = x y_fit = y z_fit = 0.00 + 0.00*x_fit + 0.00*y_fit data_fit = np.c_[x_fit,y_fit,z_fit] # trace1 is the scatter plot of the original points... trace1 = go.Scatter3d( x=data[:,0], y=data[:,1], z=data[:,2], mode='markers', marker=dict(size=1, color='red', line=dict(color='black', width=0.5), opacity=0.5) ) # trace2 is the scatter plot of the fit values at the x,y positions of the original points... trace2 = go.Scatter3d( x=data_fit[:,0], y=data_fit[:,1], z=data_fit[:,2], mode='markers', marker=dict(size=2, color='yellow', line=dict(color='black', width=0.5), opacity=0.8) ) # trace3 is the 2D surface of the fit equation... #trace3 = go.Surface(z=Z, x=X, y=Y, colorscale='RdBu', opacity=0.667) trace3 = go.Surface(z=Z, x=X, y=Y, colorscale='Greys', opacity=0.667) # Package the trace dictionaries into a data object data_go = go.Data([trace1, trace2, trace3]) # Dictionary of style options for all axes axis = dict( showbackground=True, # show axis background backgroundcolor="rgb(204, 204, 204)", # set background color to grey gridcolor="rgb(255, 255, 255)", # set grid line color zerolinecolor="rgb(255, 255, 255)", # set zero grid line color ) # Create a title... if norder == 1: titleText = """%s = %.3f + %.3f*%s + %.3f*%s [rms: %.3f]""" % \ (dmagName, p[0], p[1], colorName1, p[2], colorName2, rms) elif norder == 2: titleText = """%s = %.3f + %.3f*%s + %.3f*%s^2 + %.3f*%s + %.3f*%s^2\n[npts=%d, rms: %.3f]""" % \ (dmagName, p[0], p[1], colorName1, p[2], colorName1, p[3], colorName2, p[4], colorName2, n_elements, rms) else: titleText = '' titleText = titleText.replace('$','') # Make a layout object layout = go.Layout( title=titleText, # set plot title scene=go.Scene( # axes are part of a 'scene' in 3d plots xaxis=go.XAxis(axis), # set x-axis style yaxis=go.YAxis(axis), # set y-axis style zaxis=go.ZAxis(axis)), # set z-axis style ) # Make a figure object fig = go.Figure(data=data_go, layout=layout) # Create interactive plot and save as javascript to html file... #plotly.offline.iplot(fig, filename=outputFileName) plotly.offline.plot(fig, filename=outputFileName, auto_open=False) return 0
eye=dict(x=-0.5, y=2, z=0.9)) points = go.Scatter3d(x=data[:, 0], y=data[:, 1], z=data[:, 2], mode='markers', marker=dict(color='orange', size=10)) true_surface = go.Surface(x=xx, y=yy, z=zz, showscale=False, opacity=0.75, colorscale='Greys', showlegend=True) pca_surface = go.Surface(x=x_pca_plane, y=y_pca_plane, z=z_pca_plane, showscale=False, opacity=0.75, surfacecolor=255 * np.ones(x_pca_plane.shape), colorscale='orange') xaxis = go.XAxis(title='', showaxeslabels=False, showticklabels=False) yaxis = go.YAxis(title='', showaxeslabels=False, showticklabels=False) zaxis = go.ZAxis(title='', showaxeslabels=False, showticklabels=False) scene = go.Scene(camera=camera, xaxis=xaxis, yaxis=yaxis, zaxis=zaxis) layout = go.Layout(scene=scene, width=2000, height=2000) fig = go.Figure(data=[points, true_surface, pca_surface], layout=layout) pyo.plot(fig)
marker=pg.Marker(symbol='dot', size=6, line=pg.Line(color='rgb(50,50,50)', width=0.5), colorscale=[[0, 'rgb(0,0,255)'], [1, 'rgb(255,0,0)']], color=colors), text=nodes, hoverinfo='text') axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') trueLayout = pg.Layout(title='test', width=1000, height=1000, showlegend=False, scene=pg.Scene(xaxis=pg.XAxis(axis), yaxis=pg.YAxis(axis), zaxis=pg.ZAxis(axis)), margin=pg.Margin(t=100), hovermode='closest') plotly.offline.plot(pg.Figure(data=pg.Data([trace1, trace2]), layout=trueLayout), filename=sys.argv[1] + '/' + 'forceGraph.html')
def update_graph(variable, hours, dimension, sun, wind, data): df = pd.read_json(data) df = df.sort_values('time') # df_filtered = df.iloc[int(hours[0]*3600/10):int(hours[1]*3600/10),:] df_filtered = df.loc[(df.time_hr >= hours[0]) & (df.time_hr <= hours[1])] colors = df_filtered[variable] # Try to get pretty colorbar title if variable in units: colortitle = units[variable] else: colortitle = variable if (dimension == '2D'): points = go.Scatter( x=df_filtered['y_km'], y=df_filtered['x_km'], mode='lines+markers', marker=dict( size='7', color=colors, #set color equal to a variable colorbar=go.ColorBar(title=colortitle, titleside='right'), colorscale='Jet', showscale=True), line=dict(color='gray'), text=colors, opacity=0.7) layout = go.Layout(title='Flight Path', xaxis={'title': 'East (km)'}, yaxis={ 'title': 'North (km)', 'scaleanchor': 'x', 'scaleratio': 1 }, hovermode='closest', shapes=[{ 'type': 'circle', 'xref': 'x', 'yref': 'y', 'x0': -3, 'y0': -3, 'x1': 3, 'y1': 3, 'layer': 'below', 'line': { 'color': 'orange', 'dash': 'dash', 'width': 4 } }]) # Plot sun and wind if (sun == 'Show Sun' and wind == 'Show Wind' and 'w_n' in df.columns): df_sun = df_filtered.iloc[::90, :] sun_n = df_sun.sn2 * 5 sun_e = df_sun.sn1 * 5 figure = ff.create_quiver( sun_e, sun_n, -df_sun.sn1 * 3, -df_sun.sn2 * 3, scale=0.2, name='Sun', line={'color': 'rgb(238,180,34,0.8)'}) figure['data'].append(points) wind_x, wind_y = np.meshgrid(np.arange(-4, 5, 1), np.arange(-4, 5, 1)) wind_u = np.ones(wind_x.shape) * df['w_e'][0] wind_v = np.ones(wind_x.shape) * df['w_n'][0] figure_wind = ff.create_quiver( wind_x, wind_y, wind_u, wind_v, scale=0.05, name='Wind', line={'color': 'rgb(220,220,220,0.5)'}) figure['data'].append(figure_wind['data'][0]) figure['layout'] = (layout) # Plot sun vectors if enabled elif (sun == 'Show Sun'): df_sun = df_filtered.iloc[::90, :] sun_n = df_sun.sn2 * 5 sun_e = df_sun.sn1 * 5 figure = ff.create_quiver( sun_e, sun_n, -df_sun.sn1 * 3, -df_sun.sn2 * 3, scale=0.2, name='Sun', line={'color': 'rgb(238,180,34,0.8)'}) figure['data'].append(points) figure['layout'] = (layout) elif (wind == 'Show Wind' and 'w_n' in df.columns): wind_x, wind_y = np.meshgrid(np.arange(-4, 5, 1), np.arange(-4, 5, 1)) wind_u = np.ones(wind_x.shape) * df['w_e'][0] wind_v = np.ones(wind_x.shape) * df['w_n'][0] figure = ff.create_quiver( wind_x, wind_y, wind_u, wind_v, scale=0.05, name='Wind', line={'color': 'rgb(220,220,220,0.5)'}) figure['data'].append(points) figure['layout'] = (layout) else: figure = { 'data': [ go.Scatter( x=df_filtered['y_km'], y=df_filtered['x_km'], mode='lines+markers', marker=dict( size='7', color=colors, #set color equal to a variable colorbar=go.ColorBar(title=colortitle, titleside='right'), colorscale='Jet', showscale=True), line=dict(color='gray'), text=colors, opacity=0.7) ], 'layout': go.Layout(title='Flight Path', xaxis={'title': 'East (km)'}, yaxis={ 'title': 'North (km)', 'scaleanchor': 'x', 'scaleratio': 1 }, hovermode='closest', shapes=[{ 'type': 'circle', 'xref': 'x', 'yref': 'y', 'x0': -3, 'y0': -3, 'x1': 3, 'y1': 3, 'layer': 'below', 'line': { 'color': 'orange', 'dash': 'dash', 'width': 4 } }]) } else: figure = { 'data': [ go.Scatter3d( x=df_filtered['y_km'], y=df_filtered['x_km'], z=df_filtered['h_km'], mode='lines+markers', marker=dict( size='3', color=colors, #set color equal to a variable colorbar=go.ColorBar(title=colortitle, titleside='right'), colorscale='Jet', showscale=True), opacity=0.7, text=colors) ], 'layout': go.Layout(scene=go.Scene(xaxis=go.XAxis(title='East (km)'), yaxis=go.YAxis(title='North (km)'), zaxis=go.ZAxis( title='Altitude (km)', range=[ df_filtered.h_km.min() - 1, df_filtered.h_km.max() + 1 ])), margin=dict(r=10, l=10, b=10, t=10)) } return figure
def render(self): """ Plots the surface and the control points grid. """ if not self._plots: return plot_data = [] for plot in self._plots: # Plot control points if plot['type'] == 'ctrlpts' and self._config.display_ctrlpts: pts = np.array( utilities.make_quad(plot['ptsarr'], plot['size'][1], plot['size'][0])) cp_z = pts[:, 2] + self._ctrlpts_offset figure = graph_objs.Scatter3d( x=pts[:, 0], y=pts[:, 1], z=cp_z, name=plot['name'], mode='lines+markers', line=dict(color=plot['color'], width=self._config.line_width, dash='solid'), marker=dict( color=plot['color'], size=self._config.line_width * 2, )) plot_data.append(figure) # Plot evaluated points if plot['type'] == 'evalpts': pts = np.array( utilities.make_triangle(plot['ptsarr'], plot['size'][1], plot['size'][0])) figure = graph_objs.Scatter3d( x=pts[:, 0], y=pts[:, 1], z=pts[:, 2], name=plot['name'], mode='lines', line=dict(color=plot['color'], width=self._config.line_width), ) plot_data.append(figure) plot_layout = dict( width=self._config.figure_size[0], height=self._config.figure_size[1], autosize=False, showlegend=self._config.display_legend, scene=graph_objs.Scene( xaxis=graph_objs.XAxis( showgrid=self._config.display_axes, showline=self._config.display_axes, zeroline=self._config.display_axes, showticklabels=self._config.display_axes, title='', ), yaxis=graph_objs.YAxis( showgrid=self._config.display_axes, showline=self._config.display_axes, zeroline=self._config.display_axes, showticklabels=self._config.display_axes, title='', ), zaxis=graph_objs.ZAxis( showgrid=self._config.display_axes, showline=self._config.display_axes, zeroline=self._config.display_axes, showticklabels=self._config.display_axes, title='', ), aspectmode='data', ), ) plotly.offline.plot({ "data": plot_data, "layout": plot_layout }, show_link=False)
def create_a_3D_plot(G, st, labels): layt = nx.spring_layout(G, dim=3) # creation of coordinates Edges = list(G.edges) nodes = list(G.nodes) N = len(G) Xn = [layt[k][0] for k in G.nodes] # x-coordinates of nodes Yn = [layt[k][1] for k in G.nodes] # y-coordinates Zn = [layt[k][2] for k in G.nodes] # z-coordinates Xe = [] Ye = [] Ze = [] for e in Edges: Xe += [layt[e[0]][0], layt[e[1]][0], None] # x-coordinates of edge ends Ye += [layt[e[0]][1], layt[e[1]][1], None] Ze += [layt[e[0]][2], layt[e[1]][2], None] # 3 D plot trace1 = go.Scatter3d(x=Xe, y=Ye, z=Ze, mode='lines', line=go.Line(color='rgb(125,125,125)', width=1), hoverinfo='none') trace2 = go.Scatter3d( x=Xn, y=Yn, z=Zn, mode='markers', name='actors', marker=go.Marker(symbol='dot', size=6, color=labels, colorscale=[[0.0, 'rgb(165,0,38)'], [0.1111111111111111, 'rgb(215,48,39)'], [0.2222222222222222, 'rgb(244,109,67)'], [0.3333333333333333, 'rgb(253,174,97)'], [0.4444444444444444, 'rgb(254,224,144)'], [0.5555555555555556, 'rgb(224,243,248)'], [0.6666666666666666, 'rgb(171,217,233)'], [0.7777777777777778, 'rgb(116,173,209)'], [0.8888888888888888, 'rgb(69,117,180)'], [1.0, 'rgb(49,54,149)']], line=go.Line(color='rgb(50,50,50)', width=0.5)), text=list(G.nodes), hoverinfo='text') axis = dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') layout = go.Layout(title="Network of %s (3D visualization)" % st, width=1000, height=1000, showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest') data = go.Data([trace1, trace2]) fig = go.Figure(data=data, layout=layout) py.iplot(fig, filename='3D graph')
def plotly_skeleton_figure(skel3d, skel_desc): meta_fn = _make_joint_metadata_fn(skel_desc) cxs = [] cys = [] czs = [] lxs = [] lys = [] lzs = [] rxs = [] rys = [] rzs = [] xt = list(skel3d[:, 0]) zt = list(-skel3d[:, 1]) yt = list(skel3d[:, 2]) for j, p in enumerate(skel_desc.joint_tree): metadata = meta_fn(j) if metadata['group'] == 'left': xs, ys, zs = lxs, lys, lzs elif metadata['group'] == 'right': xs, ys, zs = rxs, rys, rzs else: xs, ys, zs = cxs, cys, czs xs += [xt[j], xt[p], None] ys += [yt[j], yt[p], None] zs += [zt[j], zt[p], None] points = go.Scatter3d( x=list(skel3d[:, 0]), z=list(-skel3d[:, 1]), y=list(skel3d[:, 2]), text=skel_desc.joint_names, mode='markers', marker=dict(color='grey', size=3, opacity=0.8), ) centre_lines = go.Scatter3d( x=cxs, y=cys, z=czs, mode='lines', line=dict(color='magenta', width=1), hoverinfo='none', ) left_lines = go.Scatter3d( x=lxs, y=lys, z=lzs, mode='lines', line=dict(color='blue', width=1), hoverinfo='none', ) right_lines = go.Scatter3d( x=rxs, y=rys, z=rzs, mode='lines', line=dict(color='red', width=1), hoverinfo='none', ) layout = go.Layout( margin=go.Margin(l=20, r=20, b=20, t=20, pad=0), hovermode='closest', scene=go.Scene( aspectmode='data', yaxis=go.YAxis(title='z'), zaxis=go.ZAxis(title='y'), ), showlegend=False, ) fig = go.Figure(data=[points, centre_lines, left_lines, right_lines], layout=layout) return fig
def main(): # Centrality for graph representation. centrality = Centrality.Centrality(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") centrality.arg.LoadEdgesData() betweenness = centrality.BetweenCentrality() closeness = centrality.ClosenessCentrality() eigenVec = centrality.EigenVector_Centrality() #PredictionModels, Split positive and negative data pos_train = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") neg_train = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") pos_val = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") neg_val = DataReader.DataReader(Graph(), "../FacebookNetwork/dataset/SplitedData/train.edges", "../FacebookNetwork/dataset/SplitedData/val.edges", "../FacebookNetwork/dataset/SplitedData/test.edges", "../FacebookNetwork/dataset/SplitedData/neg_train.edges", "../FacebookNetwork/dataset/SplitedData/neg_val.edges", "../FacebookNetwork/dataset/SplitedData/neg_test.edges") #Load Edges for each graph pos_train.LoadEdgesData() neg_train.LoadEdgesData() pos_val.LoadEdgesData() neg_val.LoadEdgesData() #Split into negative and positive files. pos_train.GenerateTrain() #we will get pos and neg files. pos_val.GenerateValidation() #we will get pos and neg files. #LabelData into dictionnary for files pos_train.LoadGeneratedFiles(pos_train.fluxTr) neg_train.LoadGeneratedFiles(neg_train.fluxTr_neg) pos_val.LoadGeneratedFiles(pos_val.fluxV) neg_val.LoadGeneratedFiles(neg_val.fluxVal_neg) X_train_pos = pos_train.LabelData() X_train_neg = neg_train.LabelData() X_val_pos = pos_val.LabelData() X_val_neg = neg_val.LabelData() print('----------------- Spliting and labeling data X & Y------------------------- \n') Y_train_pos = np.full(shape=(X_train_pos.shape[0],1), fill_value=1) Y_train_neg = np.full(shape=(X_train_neg.shape[0],1), fill_value=0) Y_val_pos = np.full(shape=(X_val_pos.shape[0],1), fill_value=1) Y_val_neg = np.full(shape=(X_val_neg.shape[0],1), fill_value=0) X_train = np.append(X_train_pos,X_train_neg,axis=0) y_train = np.append(Y_train_pos,Y_train_neg,axis=0) X_val = np.append(X_val_pos, X_val_neg, axis=0) y_val = np.append(Y_val_pos, Y_val_neg, axis=0) np.random.shuffle(X_train) np.random.shuffle(y_train) np.random.shuffle(X_val) np.random.shuffle(y_val) print('----------------- Done ------------------------- \n ') print('\n----------------- Linear Model Predictions ------------------------- \n') reg = linear_model.Ridge (alpha = .5) reg.fit(X=X_train[:-1],y=y_train[:-1]) reg.predict(X_train[-1:]) len(reg.predict(X_val)) np.mean((reg.predict(X_val) - y_val)**2) print('Log loss ',log_loss(y_val,reg.predict(X_val))) print('\n ----------------- Linear LinearRegression ------------------------- \n') regressor = LinearRegression() regressor.fit(X_train, y_train) print('Slope',regressor.intercept_) y_pred = regressor.predict(X_val) df = pd.DataFrame({'Actual': y_val.flatten(), 'Predicted': y_pred.flatten()}) #print(df) print('\n ----------------- SVM ------------------------- \n') clf_svm = svm.SVC() clf_svm.fit(X=X_train[:-1],y=y_train[:-1]) print(log_loss(y_val,clf_svm.predict(X_val))) print('\n ------------------------ Implementing Kernel SVM | Polynomial ------------------------ \n') svclassifier2 = svm.SVC(kernel='poly', degree=8,C=150) # this is the degre of the polynomial. svclassifier2.fit(X_train, y_train) #making prediction y_predp = svclassifier2.predict(X_val) #evaluating the poly svm print(confusion_matrix(y_val, y_predp)) print(classification_report(y_val, y_predp)) print('\n --------------------------- Implementing Kernel SVM | Linear -------------------------- \n') svclassifier1 = svm.SVC(kernel='linear') svclassifier1.fit(X_train, y_train) #Make predict y_pred = svclassifier1.predict(X_val) #Evaluating the Algorithm print(svclassifier1.score(X_val, y_val)) print(confusion_matrix(y_val,y_pred)) print(classification_report(y_val,y_pred)) print('\n ------------------------ Implementing Kernel SVM | Sigmoid ------------------------ \n') svclassifier4 = svm.SVC(kernel='sigmoid') svclassifier4.fit(X_train, y_train) #making predict y_preds = svclassifier4.predict(X_val) #Evaluating Algorithm print(confusion_matrix(y_val, y_preds)) print(classification_report(y_val, y_preds)) print('\n------------------------ Implementing Kernel SVM | Gaussian ------------------------\n') svclassifier3 = svm.SVC(kernel='rbf') svclassifier3.fit(X_train, y_train) #making predit y_predg = svclassifier3.predict(X_val) #Evaluating Algorithm print(confusion_matrix(y_val, y_predg)) print(classification_report(y_val, y_predg)) print('\n ------------------------ KNN ------------------------ \n') sc = StandardScaler() sc.fit(X_train) X_train = sc.transform(X_train) X_val = sc.transform(X_val) print('Value for K Math.sqrt(len of X_train) -------> ',math.sqrt(len(X_train))) print("Please wait for graph representation ....") accuracy = [] #We agregate the Accuracy averages for 18 neighbors. f1_scores = [] #Metrics ... index = range(3,81) for i in index: classifier = KNeighborsClassifier(n_neighbors = i, metric= 'euclidean', weights='uniform', leaf_size= 30) #27 classifiers classifier.fit(X_train, y_train) y_pred = classifier.predict(X_val) # Predict the class labels for the provided data conf_matrix = confusion_matrix(y_val, y_pred) # What we predit <VS> what actually is on test data. res = (conf_matrix[0, 0] + conf_matrix[1, 1]) / sum(sum(conf_matrix)) # Calculate Accuracy of our predit. accuracy.append(res) f1_scores.append(list(zip(y_val, y_pred))) print('In the range of 3 to 39 we have this values of accuracy') print(accuracy) # Evaluate the Model. print('We evaluate the Matrix of Confusion') mc = confusion_matrix(y_val, y_pred) print(classification_report(y_val, y_pred)) print(mc) # Graph representation plt.figure(figsize=(10, 6), num='Knn Algorithm Facebook Network Prediction') plt.plot(index, accuracy, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=10) plt.title('Accuracy ratio according to K values') plt.xlabel('K Values') plt.ylabel('Accuracy average') plt.show() #LoadLabels. nodesLabels = [] edges = [] #nodesLabels = [nodesLabels.append(eachEgo['name']) for eachEgo in centrality.arg.graph.vs] for eachEgo in centrality.arg.graph.vs: nodesLabels.append(eachEgo['name']) #edges = [edges.append(edge.tuple) for edge in centrality.arg.graph.es] #ça marche pas je ne sais pas pourquoi. for e in centrality.arg.graph.es: edges.append(e.tuple) layout = centrality.arg.graph.layout('kk', dim=3) #Prepare coordinates for Nodes and Edges. Xn=[layout[n][0] for n in range(len(centrality.arg.graph.vs))]# x-coordinates of nodes Yn=[layout[n][1] for n in range(len(centrality.arg.graph.vs))]# y-coordinates Zn=[layout[n][2] for n in range(len(centrality.arg.graph.vs))]# z-coordinates #Lists of edges. Xe=[] Ye=[] Ze=[] for e in edges: Xe+=[layout[e[0]][0],layout[e[1]][0], None]# x-coordinates of edge ends Ye+=[layout[e[0]][1],layout[e[1]][1], None] Ze+=[layout[e[0]][2],layout[e[1]][2], None] trace1=go.Scatter3d(x=Xe, y=Ye, z=Ze, mode='lines', line=go.Line(color='rgb(125,125,125)', width=1), hoverinfo='none' ) trace2=go.Scatter3d(x=Xn, y=Yn, z=Zn, mode='markers', name='Alters', marker=go.Marker(symbol='circle', color=eigenVec, size=10,colorbar=go.ColorBar( title='Node Degree' ), colorscale='Viridis', line=go.Line(color='rgb(158,18,130)', width=0.5) ), text=nodesLabels, hoverinfo='text' ) axis=dict(showbackground=True, showline=True, zeroline=False, showgrid=True, showticklabels=True, title='' ) plan = go.Layout( title="Facebook Ego-Network", width=1000, height=1000, showlegend=True, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest', annotations=go.Annotations([ go.Annotation( showarrow=True, xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='top', font=go.Font(size=14) ) ]),) data=go.Data([trace1, trace2]) fig=go.Figure(data=data, layout=plan) fig.show()
showline=False, zeroline=False, showgrid=False, showticklabels=False, title='') data = [edge_trace, house_trace, sen_trace, keyword_trace] # create layout for plotly layout = go.Layout( title="Member-Keyword Network from bills proposed since 2017", width=1000, height=1000, showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest') # plot figures fig = go.Figure(data=data, layout=layout) plotly.offline.plot(fig, filename='../Figures/KeywordMemberNetwork_2.html') ## ## ## ##
def get_plotly_networkx_3d(self, varset="all", include_plotlyjs=False): import networkx as nx g, edge_labels = nx.Graph(), {} for i, (name, var) in enumerate(self.items()): #if i == 5: break if varset != "all" and var.varset != varset: continue g.add_node(var, name=name) for parent in var.get_parents(): #print(parent, "is parent of ", name) parent = self[parent] g.add_edge(parent, var) # TODO: Add getters! What about locked nodes! #i = [dep.node for dep in child.deps].index(task) #edge_labels[(task, child)] = " ".join(child.deps[i].exts) # Get positions for all nodes using layout_type. # e.g. pos = nx.spring_layout(g) #pos = getattr(nx, layout_type + "_layout")(g) #, scale=100000, iterations=30) pos = nx.spring_layout(g, dim=3) import plotly.graph_objs as go trace1 = go.Scatter3d(x=[], y=[], z=[], mode='lines', line=go.Line(width=2.0, color='#888'), #, dash="dot"), hoverinfo='none', ) for edge in g.edges(): x0, y0, z0 = pos[edge[0]] x1, y1, z1 = pos[edge[1]] trace1['x'] += [x0, x1, None] trace1['y'] += [y0, y1, None] trace1['z'] += [z0, z1, None] trace2 = go.Scatter3d( x=[], y=[], z=[], name='variable', marker=go.Marker(symbol='dot', size=10, #color=group, colorscale='Viridis', line=go.Line(width=2) ), text=[v.name for v in g], textposition='center', mode='markers+text', hoverinfo='text', hovertext=[v.mnemonics for v in g.nodes()], ) for node in g.nodes(): x, y, z = pos[node] trace2['x'].append(x) trace2['y'].append(y) trace2['z'].append(z) axis=dict(showbackground=False, showline=False, zeroline=False, showgrid=False, showticklabels=False, title='' ) layout = go.Layout( title="Abinit variables (3D visualization)", width=1000, height=1000, showlegend=False, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis), ), margin=go.Margin(t=100), hovermode='closest', annotations=go.Annotations([go.Annotation( showarrow=False, #text="Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1]</a>", text="Hello", xref='paper', yref='paper', x=0, y=0.1, xanchor='left', yanchor='bottom', font=go.Font(size=14), )]), ) data = go.Data([trace1, trace2]) fig = go.Figure(data=data, layout=layout) import plotly s = plotly.offline.plot(fig, include_plotlyjs=include_plotlyjs, output_type='div') return s
def graphlayout3d(): return (go.Layout(showlegend=False, scene=go.Scene(xaxis=go.YAxis(graphaxis()), yaxis=go.XAxis(graphaxis()), zaxis=go.ZAxis(graphaxis())), hovermode='closest'))
vmin, vmax = -3, 3 slice_z.update(cmin=vmin, cmax=vmax) slice_y.update( cmin=vmin, cmax=vmax, showscale=True, colorbar=go.ColorBar(title='Potential [a.u.]'), ) axis = dict( showbackground=True, backgroundcolor="rgb(230, 230,230)", gridcolor="rgb(255, 255, 255)", zerolinecolor="rgb(255, 255, 255)", ) layout = go.Layout( width=700, height=700, scene=go.Scene( xaxis=go.XAxis(axis), yaxis=go.YAxis(axis), zaxis=go.ZAxis(axis, range=[-2, 2]), aspectratio=dict(x=1, y=1, z=1), ), ) fig = dict(data=data, layout=layout) py.iplot(fig)