def CreateGauge(percentage, scaleInfo): gaugeFig = Plot(x_range=Range1d(start=-1.25, end=1.25), y_range=Range1d(start=-1.25, end=1.25), plot_width=250, plot_height=250) gaugeFig.title.text = "Scale " + str(scaleInfo.Num) + ": " + scaleInfo.Name gaugeFig.title.align = 'center' gaugeFig.toolbar_location = None gaugeFig.border_fill_color = "#101010" gaugeFig.background_fill_color = "#101010" gaugeFig.title.text_color = "white" gaugeFig.outline_line_color = None glyph = AnnularWedge(x=0, y=0, inner_radius=.7, outer_radius=1, start_angle=math.pi / 2 - (2 * math.pi), end_angle=math.pi / 2, fill_color="#444444", name="back") glyph2 = AnnularWedge(x=0, y=0, inner_radius=.7, outer_radius=1, start_angle=math.pi / 2 - (2 * math.pi * percentage), end_angle=math.pi / 2, fill_color=gaugeColors[(scaleInfo.Num - 1) % len(gaugeColors)], name="front") PercentageText = Label(text_align='center', text=str(round((percentage * scaleInfo.MaxCapacity), 1)),text_color = 'white', text_font_size="35px") lowerText = Label(text_align='center', y=-0.25, text=scaleInfo.Units, text_color='white') lowerText2 = Label(text_align='center', y=-0.48, text="Of " + str(scaleInfo.MaxCapacity), text_color='white') gaugeFig.add_glyph(glyph) gaugeFig.add_glyph(glyph2) gaugeFig.add_layout(PercentageText) gaugeFig.add_layout(lowerText) gaugeFig.add_layout(lowerText2) return gaugeFig
def test_no_border_or_background_fill(output_file_url, selenium, screenshot): # Have body background-color that should appear through the no-fill plot template = Template(""" <!doctype html> <html lang="en"> <head> {{ bokeh_js }} {{ bokeh_css}} <style> body { background-color: lightblue; } </style> </head> <body> {{ plot_script }} {{ plot_div }} </body> </html> """) plot = Plot(plot_height=HEIGHT, plot_width=WIDTH, x_range=Range1d(0, 10), y_range=Range1d(0, 10), toolbar_location=None) # This is the no-fill that we're testing plot.background_fill_color = None plot.border_fill_color = None plot.add_glyph(Circle(x=3, y=3, size=50, fill_color='#ffffff')) plot.add_glyph(Circle(x=6, y=6, size=50, fill_color='#ffffff')) plot.add_layout( LinearAxis(major_label_text_color='#ffffff', major_label_text_font_size="30pt"), 'left') plot.add_layout( LinearAxis(major_label_text_color='#ffffff', major_label_text_font_size="30pt"), 'below') html = file_html(plot, INLINE, template=template) # filename has to match test function + '.html' light filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_no_border_or_background_fill.html") with io.open(filepath, "w", encoding="utf-8") as f: f.write(decode_utf8(html)) selenium.get(output_file_url) assert has_no_console_errors(selenium) assert screenshot.is_valid()
def test_no_border_or_background_fill(output_file_url, selenium, screenshot): # Have body background-color that should appear through the no-fill plot template = Template(""" <!doctype html> <html lang="en"> <head> {{ bokeh_js }} {{ bokeh_css}} <style> body { background-color: lightblue; } </style> </head> <body> {{ plot_script }} {{ plot_div }} </body> </html> """) plot = Plot(plot_height=HEIGHT, plot_width=WIDTH, x_range=Range1d(0, 10), y_range=Range1d(0, 10), toolbar_location=None) # This is the no-fill that we're testing plot.background_fill_color = None plot.border_fill_color = None plot.add_glyph(Circle(x=3, y=3, size=50, fill_color='#ffffff')) plot.add_glyph(Circle(x=6, y=6, size=50, fill_color='#ffffff')) plot.add_layout(LinearAxis(major_label_text_color='#ffffff', major_label_text_font_size="30pt"), 'left') plot.add_layout(LinearAxis(major_label_text_color='#ffffff', major_label_text_font_size="30pt"), 'below') html = file_html(plot, INLINE, template=template) # filename has to match test function + '.html' light filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_no_border_or_background_fill.html") with io.open(filepath, "w", encoding="utf-8") as f: f.write(decode_utf8(html)) selenium.get(output_file_url) assert has_no_console_errors(selenium) screenshot.assert_is_valid()
def render_minimal_graph(): print('rendering minimal graph') N = 8 node_indices = list(range(N)) plot = Plot( sizing_mode='stretch_both', x_range=(-1.1, 1.1), y_range=(-1.1, 1.1), tools="", toolbar_location=None) graph_renderer = GraphRenderer() graph_renderer.node_renderer.glyph = Oval( height=0.1, width=0.2, fill_color="fill_color", ) graph_renderer.node_renderer.data_source.data = dict( index=node_indices, fill_color=Spectral8) graph_renderer.edge_renderer.data_source.data = dict( start=[0]*N, end=node_indices) # start of layout code (this is the part networkX helps to build! # the layout...) circ = [i*2*math.pi/8 for i in node_indices] x = [math.cos(i) for i in circ] y = [math.sin(i) for i in circ] graph_layout = dict(zip(node_indices, zip(x, y))) graph_renderer.layout_provider = StaticLayoutProvider( graph_layout=graph_layout) plot.axis.axis_line_width = 0 plot.grid.grid_line_width = 0 plot.xaxis.major_tick_line_color = None # turn off x-axis major ticks plot.xaxis.minor_tick_line_color = None # turn off x-axis minor ticks plot.yaxis.major_tick_line_color = None # turn off y-axis major ticks plot.yaxis.minor_tick_line_color = None # turn off y-axis minor ticks plot.xaxis.major_label_text_color = None # Remove label x axis plot.yaxis.major_label_text_color = None # Remove label x axis plot.border_fill_color = None plot.outline_line_color = None plot.renderers.append(graph_renderer) return plot
def render_from_fb_combined(): print('rendering from facebook_combined.txt') df = pd.read_csv('facebook_combined.txt', sep=" ", header=None) G = nx.from_pandas_edgelist(df.head(1000), 0, 1) print(nx.info(G)) plot = Plot(background_fill_color="white", sizing_mode="stretch_both", x_range=Range1d(-0.5, 0.5), y_range=Range1d(-0.5, 0.5)) graph_renderer = from_networkx( G, nx.spring_layout, scale=1, center=(0, 0)) graph_renderer.node_renderer.glyph = Circle( size=15, fill_color=Spectral4[0]) graph_renderer.edge_renderer.glyph = MultiLine( line_alpha=0.8, line_width=1) plot.add_tools(WheelZoomTool()) plot.add_tools(ResetTool()) plot.add_tools(PanTool()) plot.add_tools(HoverTool( tooltips=[("user", "datracka"), ("url", "https://twitter.com/datracka")])) plot.add_tools(PointDrawTool( renderers=[], empty_value='black')) plot.axis.axis_line_width = 0 plot.grid.grid_line_width = 0 plot.xaxis.major_tick_line_color = None # turn off x-axis major ticks plot.xaxis.minor_tick_line_color = None # turn off x-axis minor ticks plot.yaxis.major_tick_line_color = None # turn off y-axis major ticks plot.yaxis.minor_tick_line_color = None # turn off y-axis minor ticks plot.xaxis.major_label_text_color = None # Remove label x axis plot.yaxis.major_label_text_color = None # Remove label x axis plot.border_fill_color = None plot.outline_line_color = None plot.renderers.append(graph_renderer) return plot
from bokeh.io import save from bokeh.models import Circle, LinearAxis, Plot, Range1d template =""" {% block preamble %} <style> body { background-color: lightblue; } </style> {% endblock %} """ plot = Plot(plot_width=600, plot_height=600, x_range=Range1d(0, 10), y_range=Range1d(0, 10), toolbar_location=None) # This is the no-fill that we're testing plot.background_fill_color = None plot.border_fill_color = None plot.add_glyph(Circle(x=3, y=3, size=50, fill_color='#ffffff')) plot.add_glyph(Circle(x=6, y=6, size=50, fill_color='#ffffff')) yaxis = LinearAxis(major_label_text_color='#ffffff', major_label_text_font_size="40px") plot.add_layout(yaxis, 'left') xaxis = LinearAxis(major_label_text_color='#ffffff', major_label_text_font_size="40px") plot.add_layout(xaxis, 'below') save(plot, template=template)
def make_calendar(sp500_data_lst, djia_data_lst, nasdaq_data_lst, twitter_data_lst, holiday_lst, nyt_data_lst, approval_data_lst, generic_dem_lst, generic_rep_lst, plot_wid, plot_ht, year, month, firstweekday="Sun"): firstweekday = list(day_abbrs).index(firstweekday) calendar = Calendar(firstweekday=firstweekday) month_days = [ None if not day else str(day) for day in calendar.itermonthdays(year, month) ] month_weeks = len(month_days) // 7 workday = "linen" weekend = "lightsteelblue" def weekday(date): return (date.weekday() - firstweekday) % 7 def pick_weekdays(days): return [days[i % 7] for i in range(firstweekday, firstweekday + 7)] day_names = pick_weekdays(day_abbrs) week_days = pick_weekdays([workday] * 5 + [weekend] * 2) source = ColumnDataSource(data=dict( days=list(day_names) * month_weeks, weeks=sum([[str(week)] * 7 for week in range(month_weeks)], []), month_days=month_days, day_backgrounds=['white'] * len(month_days), )) djia_data = [(dj_date, DJIA) for (dj_date, DJIA) in djia_data_lst if dj_date.year == year and dj_date.month == month] nasdaq_data = [(nas_date, NASDAQCOM) for (nas_date, NASDAQCOM) in nasdaq_data_lst if nas_date.year == year and nas_date.month == month] sp500_data = [(sp500_date, SP500) for (sp500_date, SP500) in sp500_data_lst if sp500_date.year == year and sp500_date.month == month] holidays = [(holiday_date, Holiday) for (holiday_date, Holiday) in holiday_lst if holiday_date.year == year and holiday_date.month == month] twitter_data = [ (twitter_date, topics) for (twitter_date, topics) in twitter_data_lst if twitter_date.year == year and twitter_date.month == month ] nyt_data = [(nyt_date, headlines) for (nyt_date, headlines) in nyt_data_lst if nyt_date.year == year and nyt_date.month == month] approval_data = [ (approval_date, approve_estimate) for (approval_date, approve_estimate) in approval_data_lst if approval_date.year == year and approval_date.month == month ] approval_data.sort() generic_dem = [(generic_date, dem_estimate) for (generic_date, dem_estimate) in generic_dem_lst if generic_date.year == year and generic_date.month == month ] generic_dem.sort() generic_rep = [(generic_date, rep_estimate) for (generic_date, rep_estimate) in generic_rep_lst if generic_date.year == year and generic_date.month == month ] generic_rep.sort() colors_djia = [DJIA for _, DJIA in djia_data] colors_sp500 = [SP500 for _, SP500 in sp500_data] colors_nasdaq = [NASDAQCOM for _, NASDAQCOM in nasdaq_data] for i in range(len(colors_djia) - 1): avg = np.mean([colors_djia[i], colors_sp500[i], colors_nasdaq[i]]) if 0 < avg <= 11000: colors_djia[i] = '#E52700' elif 11000 < avg <= 11100: colors_djia[i] = '#E33A00' elif 11100 < avg <= 11200: colors_djia[i] = '#E14C00' elif 11200 < avg <= 11300: colors_djia[i] = '#DF5E00' elif 11300 < avg <= 11400: colors_djia[i] = '#DD6F00' elif 11400 < avg <= 11500: colors_djia[i] = '#DB8000' elif 11500 < avg <= 11600: colors_djia[i] = '#D99100' elif 11600 < avg <= 11700: colors_djia[i] = '#D7A100' elif 11700 < avg <= 11800: colors_djia[i] = '#D5B100' elif 11800 < avg <= 11900: colors_djia[i] = '#D3C100' elif 11900 < avg <= 12000: colors_djia[i] = '#D1D000' elif 12000 < avg <= 12100: colors_djia[i] = '#BECF00' elif 12200 < avg <= 12300: colors_djia[i] = '#ABCD00' elif 12300 < avg <= 12400: colors_djia[i] = '#99CB00' elif 12400 < avg <= 12500: colors_djia[i] = '#87C900' elif 12500 < avg <= 12600: colors_djia[i] = '#75C700' elif 12500 < avg <= 12600: colors_djia[i] = '#64C500' else: colors_djia[i] = '#53C300' holiday_source = ColumnDataSource(data=dict( month_djia=[DJIA for _, DJIA in djia_data], month_nasdaq=[NASDAQCOM for _, NASDAQCOM in nasdaq_data], month_sp500=[SP500 for _, SP500 in sp500_data], month_twitter=[topics for _, topics in twitter_data], month_holidays=[Holiday for _, Holiday in holidays], nyt_days=[day_names[weekday(nyt_date)] for nyt_date, _ in nyt_data], nyt_weeks=['0'] + [ str((weekday(nyt_date.replace(day=1)) + nyt_date.day) // 7) for nyt_date, _ in nyt_data ], month_nyt=[headlines for _, headlines in nyt_data], month_approval=[ approve_estimate for _, approve_estimate in approval_data ], month_generic_dem=[dem_estimate for _, dem_estimate in generic_dem], month_generic_rep=[rep_estimate for _, rep_estimate in generic_rep], day_backgrounds=colors_djia, )) xdr = FactorRange(factors=list(day_names)) ydr = FactorRange( factors=list(reversed([str(week) for week in range(month_weeks)]))) x_scale, y_scale = CategoricalScale(), CategoricalScale() plot = Plot(x_range=xdr, y_range=ydr, x_scale=x_scale, y_scale=y_scale, plot_width=plot_wid, plot_height=plot_ht) plot.title.text = month_names[month] + " " + str(year) plot.title.text_font_size = "14pt" plot.title.text_color = "black" plot.title.offset = 25 plot.min_border_left = 5 plot.min_border_top = 5 plot.min_border_bottom = 190 plot.border_fill_color = "white" plot.background_fill_alpha = 0.5 plot.border_fill_alpha = 0.3 rect = Rect(x="days", y="weeks", width=0.9, height=0.9, fill_color="day_backgrounds", line_color="silver") plot.add_glyph(source, rect) rect = Rect(x="nyt_days", y="nyt_weeks", width=0.9, fill_color="day_backgrounds", height=0.9) rect_renderer = plot.add_glyph(holiday_source, rect) text = Text(x="days", y="weeks", text="month_days", text_align="center", text_baseline="middle") plot.add_glyph(source, text) xaxis = CategoricalAxis() xaxis.major_label_text_font_size = "10pt" xaxis.major_label_standoff = 0 xaxis.major_tick_line_color = None xaxis.axis_line_color = None plot.add_layout(xaxis, 'above') TOOLTIPS = """ <div style="height:100%; max-width:300px; min-width:200px;background-color: aliceblue; position:relative;"> <div> <span style="font-size: 17px; font-weight: bold;"> Holiday: @month_holidays</span><br> <span style="font-size: 15px; font-weight: bold; color: darkgrey;"> Trump Approval Rating: @month_approval{0,0.0}%</span><br> <span style="font-size: 15px; font-weight: bold; color: blue;"> Generic Democrat: @month_generic_dem{0,0.0}%</span><br> <span style="font-size: 15px; font-weight: bold; color: red;"> Generic Republican: @month_generic_rep{0,0.0}%</span><br> <span style="font-size: 17px; font-weight: bold;"> NASDAQ: @month_nasdaq{0,0.00}</span><br> <span style="font-size: 17px; font-weight: bold;"> DJIA: @month_djia{0,0.00}</span><br> <span style="font-size: 17px; font-weight: bold;">S&P500: @month_sp500{0,0.00}</span><br> </div> <div> <img src="/static/img/nyt_logo.png" height="15" width="15" style="float: left;"></img> </div> <div> <span style="font-size: 17px; font-weight: bold;">NYT Headlines:</span> <span style="font-size: 15px;">@month_nyt</span> </div> <div> <img src="/static/img/twitter_logo.png" height="15" width="15" style="float: left;"></img> </div> <div> <span style="font-size: 17px; color:blue; font-weight: bold;">Trending Tweets:</span> <span style="font-size: 15px; color:blue;">@month_twitter</span> </div> </div> """ hover_tool = HoverTool(renderers=[rect_renderer], tooltips=TOOLTIPS) # hover_tool = HoverTool(renderers=[rect_renderer], tooltips=[("Holiday", "@month_holidays"),("DJIA", "@month_djia{0,0.00}"), # ("NASDAQ", "@month_nasdaq{0,0.00}"),("S&P500", "@month_sp500{0,0.00}"),("NYT Headlines", "@month_nyt"),("Trending Tweets","@month_twitter")]) plot.tools.append(hover_tool) return plot
def showGraph(self): curdoc().theme = 'dark_minimal' graph_plot = Plot(plot_width=1600, plot_height=900, x_range=Range1d(-1.1, 1.1), y_range=Range1d(-1.1, 1.1)) if self._methood: graph_plot.title.text = ( "Graph %s \nTotal Nodes: %i \nTotal Edges: %i \n Node threshold:" "%i \n edge threshold: %i \n Total COMM: %i \n Method: %s" % (self._path[1], self.G.number_of_nodes(), self.G.number_of_edges(), self.nodeThres, self.edgeThres, len(self.partition), self._methood)) else: graph_plot.title.text = "Graph %s. \nTotal Nodes: %i \nTotal Edges: %i \n Node threshold: %i \n edge threshold: %i" % ( self._path[1], self.G.number_of_nodes(), self.G.number_of_edges(), self.nodeThres, self.edgeThres) graph_renderer = from_networkx(self.G, nx.spring_layout, scale=1, center=(0, 0)) graph_renderer.node_renderer.glyph = Circle(size='size', fill_color='colors') graph_renderer.node_renderer.hover_glyph = Circle(size=18, fill_color='#E84A5F') graph_renderer.node_renderer.glyph.properties_with_values() graph_renderer.edge_renderer.glyph = MultiLine(line_color="#f2f2f2", line_alpha=0.8, line_width=0.4) graph_renderer.edge_renderer.hover_glyph = MultiLine( line_color='#e09e8f', line_width=3) graph_renderer.node_renderer.data_source.data[ 'hashtag'] = self.data.ht.values graph_renderer.node_renderer.data_source.data[ 'frequency'] = self.data.frq.values graph_renderer.node_renderer.data_source.data[ 'degree'] = self.data.degree.values graph_renderer.node_renderer.data_source.data['ix'] = list( self.data.index) graph_renderer.node_renderer.data_source.data[ 'strength'] = self.data.strength.values graph_renderer.node_renderer.data_source.data['size'] = np.log( (self.data.degree.values + 1) * 3) * 6 if self.community: graph_renderer.node_renderer.data_source.data[ 'colors'] = self.data.color.values graph_renderer.node_renderer.data_source.data[ 'community'] = self.data.community.values node_hover_tool = HoverTool(tooltips=[( "hashtag", "@hashtag"), ("freq", "@frequency"), ( 'degree', '@degree'), ('strength', '@strength'), ('community', '@community'), ('ix', '@ix')]) else: graph_renderer.node_renderer.data_source.data['colors'] = [ 'red' ] * self.G.number_of_nodes() node_hover_tool = HoverTool( tooltips=[("hashtag", "@hashtag"), ( "freq", "@frequency"), ('degree', '@degree'), ('strength', '@strength'), ('ix', '@ix')]) graph_plot.add_tools(node_hover_tool, BoxZoomTool(), ResetTool(), WheelZoomTool()) graph_plot.toolbar.active_scroll = graph_plot.select_one(WheelZoomTool) graph_renderer.inspection_policy = NodesAndLinkedEdges() graph_renderer.selection_policy = NodesAndLinkedEdges() graph_plot.toolbar.active_inspect = [node_hover_tool] graph_plot.renderers.append(graph_renderer) graph_plot.background_fill_color = '#0d0d0d' graph_plot.background = 'black' graph_plot.border_fill_color = '#1a1a1a' if self.partition: legend = Legend(items=[ LegendItem(label="coverage: %f" % nx.community.coverage(self.G, self.partition), index=0), LegendItem(label="performance: %f" % nx.community.performance(self.G, self.partition), index=1), LegendItem(label="modularity: %f" % nx.community.modularity(self.G, self.partition), index=2) ]) graph_plot.add_layout(legend) output_file(self._path[0] + "Graph_NT" + str(self.nodeThres) + "ET" + str(self.edgeThres) + ".html") show(graph_plot) return