def get_interactive_vec_plot(visual_df): """ to visualize text data for better access and exploration :param visual_df: pandas dataframe :return: bokeh plot instance """ datasource = ColumnDataSource(visual_df) colour_mapper = CategoricalColorMapper(factors=["0", "1", "2"], palette=["red", "purple", "green"]) TOOLTIPS = [("id", "@index"), ("text", "@reviews"), ("label", "@labels")] hover = HoverTool(tooltips=TOOLTIPS) hover.attachment = 'right' plot = figure(title='2D-Vector Representation of Reviews', plot_width=600, plot_height=400, tools=('pan, wheel_zoom, reset', 'box_zoom', 'undo')) plot.add_tools(hover) plot.circle('x', 'y', source=datasource, color=dict(field="labels", transform=colour_mapper), legend="labels") return plot
def get_interactive_embedding_plot(embeddings: np.ndarray, df: pd.DataFrame): pca = PCA(n_components=2) pca_embeddings = pca.fit_transform(embeddings) df_copy = df.copy() df_copy["pca_x"] = pca_embeddings[:, 0] df_copy["pca_y"] = pca_embeddings[:, 1] hover = HoverTool(tooltips=[ ("product name", "@product_name"), ("barcode", "@code"), ("categories", "@deepest_categories"), ("predicted categories", "@predicted_deepest_categories"), ]) hover.attachment = "right" p = figure( title="Embedding projection", plot_width=1200, plot_height=800, tools=("pan,wheel_zoom,reset", "box_zoom", "undo"), ) p.add_tools(hover) for filter_, name, color in zip( ( "is_correct", "missing_cat_error", "additional_cat_error", "over_pred_cat_error", "under_pred_cat_error", ), [ "correct", "missing", "addition", "over-prediction", "under-prediction" ], Set2[5], ): data_source = ColumnDataSource(df_copy[df_copy[filter_] == True]) p.circle( "pca_x", "pca_y", source=data_source, line_alpha=0, fill_alpha=0.4, size=5, legend=name, color=color, ) p.legend.location = "top_left" p.legend.click_policy = "hide" return p
def create_figure(): G1 = copy.deepcopy(G) nx.set_node_attributes(G1, dict(G1.degree(weight='Weight')), 'WDegree') nx.set_node_attributes(G1, dict(G1.degree()), 'Degree') rem_edg = [] for u,v,d in G1.edges(data=True): if d['Weight'] < edf.value: rem_edg.append((u,v)) G1.remove_edges_from(rem_edg) rem_node = [] for u in G1.nodes(): if G1.node[u]['WDegree'] < ndf.value: rem_node.append(u) rem_node = rem_node+list(nx.isolates(G1)) G1.remove_nodes_from(rem_node) node_df = ColumnDataSource(nodes_df(G1)) edge_df = ColumnDataSource(edges_df(G1)) color_mapper = CategoricalColorMapper(factors=states, palette=Category20b[20] + Category20c[20] + Category20[20]) mapper = LogColorMapper(palette=['#FDFDFD', '#FDF5CA', '#FDF2B1', '#FDEE98', '#FDE665', '#FDDF33', '#FDD700'], low=min(edge_df.data['weight']), high=max(edge_df.data['weight'])) p = figure(title="US Air Flights in 97", toolbar_location="left", plot_width=1100, plot_height=600, match_aspect=True, aspect_scale=0.7, x_range=[-140, -50], y_range=[20, 55]) r1 = p.circle('x', 'y', source=node_df, size='size', level='overlay', color={'field': 'state', 'transform': color_mapper}, alpha=0.8, line_color='#240606', line_width=1, line_alpha=0.3) r2 = p.multi_line('xs', 'ys', line_width='line_width', alpha='alphas', color={'field': 'weight', 'transform': mapper}, source=edge_df) node_hover = HoverTool(tooltips=[('Name', '@Name'), ('State', '@state'), ('No. of Connections', '@Degree'), ('Total Frequency', '@WDegree')], renderers=[r1]) node_hover.attachment = 'right' edge_hover = HoverTool(tooltips=[('Airports', '@city1'), ('', '@city2'), ('Frequency', '@weight')], renderers=[r2]) edge_hover.line_policy = 'interp' edge_hover.attachment = 'left' p.add_tools(node_hover) p.add_tools(edge_hover) return p
def histogram_plot(data, width=600, height=300): ''' ''' if len(data) > 5: data = data[:5] ids = [d['id'] for d in data] topics = ['Topic {}'.format(d['id']) for d in data] dist = [d['dist'] for d in data] labels = [', '.join(d['words']) for d in data] source = ColumnDataSource( data=dict(topics=topics, distribution=dist, ids=ids, labels=labels)) hover = HoverTool(tooltips=""" <div> <div> <span style="font-size: 20px;">Topics: <strong>@ids</strong></span><br> <span style="font-size: 12px;">(Click to see more about the topic)</span><br> <span style="font-size: 20px;">Words: @labels</span> </div> </div> """) hover.attachment = 'right' plot = figure(x_range=topics, width=width, height=height, tools=[hover, 'tap', 'save'], title='Relevant topics') plot.vbar(x='topics', top='distribution', width=0.8, source=source, line_color='white', fill_color=factor_cmap('topics', palette=COLORS, factors=topics)) plot.y_range.start = 0 plot.xgrid.grid_line_color = None plot.xaxis.axis_label = 'Topics' plot.yaxis.axis_label = 'Proportion' plot.title.text_font_size = '1.5em' plot.xaxis.axis_label_text_font_size = "1.5em" plot.yaxis.axis_label_text_font_size = "1.5em" plot.xaxis.major_label_text_font_size = "1em" taptool = plot.select(type=TapTool) taptool.callback = OpenURL(url="/topic/@ids") plot.toolbar.active_inspect = [hover] return components(plot)