def createHistogram(key): data = json.load(open('FinishedCourseData/pausePlayBins.json'))[key] x = sorted([float(num) for num in data.keys()]) strX = [str(i) for i in x] y = np.array([data[i] for i in strX], dtype=np.float) output_file('pausePlay.html') p2 = figure(title="PausePlay for " + key, tools="", x_range=strX, y_range=[0, int(max(y))], background_fill='#59636C', plot_width=1600, plot_height=800) xSize = [c + ":0.5" for c in strX] p2.rect(x=xSize, y=y / 2, width=0.2, height=y, color="gold", alpha=0.8) p2.xgrid.grid_line_color = None p2.axis.major_label_text_font_size = "8pt" p2.axis.major_label_standoff = 0 p2.xaxis.major_label_orientation = np.pi / 3 p2.xaxis.major_label_standoff = 5 p2.xaxis.major_tick_out = 0 # show the plots arrayed in a VBox show(VBox(p2))
def uploaded_file(filename): #plik pickle try: plots_data = pickle.load( open(os.path.join(app.config['UPLOAD_FOLDER'], filename), "rb")) except: return render_template('error.html') stops1_tag = None stops2_tag = None stops3_tag = None #wykres stops1 if 'stops1' in plots_data: stops1_plot, stops1_session = plot_bokeh(plots_data['stops1'], normalized_types=False, uniq_id=filename) stops1_tag = autoload_server(stops1_plot, stops1_session).replace( "localhost", "biquad.mimuw.edu.pl") #wykres stops2 if 'stops2' in plots_data: start_pop = plots_data['stops2'][1][0] grid_shape = plots_data['stops2'][0] all_pop_mat = plots_data['stops2'][1][1:] color_dict = plots_data['stops2'][2] stops2_anim_plot, stops2_anim_session = plot_hexagonal_grid.draw_bokeh_plot( start_pop, grid_shape, color_dict, uniq_id=filename) stops2_tag = autoload_server(stops2_anim_plot, stops2_anim_session).replace( "localhost", "biquad.mimuw.edu.pl") thread = Thread(target=update_animation_stops2, args=(stops2_anim_plot, stops2_anim_session, all_pop_mat, color_dict)) thread.start() if 'stops3' in plots_data: all_freqs_after_reception = plots_data['stops3'][0] all_segm_dict = plots_data['stops3'][1] stops3_anim_bar, stops3_anim_plot, stops3_anim_session, geny = plot_segments.draw_plot( all_freqs_after_reception, all_segm_dict, uniq_id=filename) stops3_tag = autoload_server(VBox(stops3_anim_bar, stops3_anim_plot), stops3_anim_session).replace( "localhost", "biquad.mimuw.edu.pl") thread = Thread(target=update_animation_stops3, args=(stops3_anim_bar, stops3_anim_plot, stops3_anim_session, all_freqs_after_reception, all_segm_dict, geny)) thread.start() return render_template('plot.html', tag1=stops1_tag, tag2=stops2_tag, tag3=stops3_tag, filename=filename)
def plot_to_html(self): output_file('scatter_plots.html', title='scatter plots') tools = 'pan,wheel_zoom,box_zoom,reset,save,box_select' width = 800 height = 400 vbox = VBox() for netloc in self.unique_url_netlocs: x, y = self._get_axis(netloc) p = figure( tools=tools, title=netloc, plot_width=width, plot_height=height, y_range=[0.0, 4000.0], x_axis_label='elapsed time in test (seconds)', y_axis_label='ttfb latency (milliseconds)', ) p.scatter(x, y, color='blue') vbox.children.append(p) show(vbox)
y2 = [sin(x) for x in x2] x3 = list(range(11)) y3 = [x**2 for x in x3] # specify and output static HTML file output_file("scatter.html") # EXERCISE: Plot all the sets of points on different plots p1, p2, p3. # Try setting `color` (or `line_color`) and `alpha` (or `line_alpha`). # You can also set `line_dash` and `line_width`. One example is given. p1 = figure(plot_width=300, plot_height=300) p1.line(x1, y1, size=12, color="red", alpha=0.5) p2 = figure(plot_width=300, plot_height=300) p2.line(x2, y2, size=12, color="blue", line_dash=[2, 4]) p3 = figure(plot_width=300, plot_height=300) p3.line(x3, y3, size=12, line_color="orange", line_width=3) # create a figure p4 = figure() # EXERCISE: add all the same renderers above, on this one plot p4.line(x1, y1, size=12, color="red", alpha=0.5) p4.line(x2, y2, size=12, color="blue", line_dash=[2, 4]) p4.line(x3, y3, size=12, line_color="orange", line_width=2) # show the plots arrayed in a VBox show(VBox(HBox(p1, p2, p3), p4))
p1.line(IBM['Date'], IBM['Adj Close'], color='#33A02C', legend='IBM') p1.line(MSFT['Date'], MSFT['Adj Close'], color='#FB9A99', legend='MSFT') # EXERCISE: style the plot, set a title, lighten the gridlines, etc. p1.title = "Stock Closing Prices" p1.grid.grid_line_alpha = 0.3 # EXERCISE: start a new figure p2 = figure(title="AAPL average") # Here is some code to compute the 30-day moving average for AAPL aapl = AAPL['Adj Close'] aapl_dates = AAPL['Date'] window_size = 30 window = np.ones(window_size) / float(window_size) aapl_avg = np.convolve(aapl, window, 'same') # EXERCISE: plot a scatter of circles for the individual AAPL prices with legend # 'close'. Remember to set the x axis type and tools on the first renderer p2.scatter(aapl_dates, aapl, size=4, color='#A6CEE3', legend='close') # EXERCISE: plot a line of the AAPL moving average data with the legeng 'avg' p2.line(aapl_dates, aapl_avg, color='red', legend='avg') # EXERCISE: style the plot, set a title, lighten the gridlines, etc. p2.title = "AAPL One-Month Average" p2.grid.grid_line_alpha = 0.3 show(VBox(p1, p2)) # open a browser
palette=palette, title="categorical heatmap, pd_input", height=400, width=1000) hm_data = df3.values.T hm2 = HeatMap(hm_data, palette=palette, title="Unemployment (Array)", xlabel='Years since 1948', ylabel='Months', height=400, width=1000) simple_df = pd.DataFrame( { 'apples': [4, 5, 8, 12, 4], 'pears': [6, 5, 4, 8, 7], 'bananas': [1, 2, 4, 8, 12] }, index=['2009', '2010', '2011', '2012', '2013']) hm3 = HeatMap(simple_df, palette=palette, title="Fruit comsumption per year", height=400, width=1000) output_file("heatmap.html") show(VBox(hm1, hm2, hm3))
p4.line(x, cdf, line_color="white", line_width=2, alpha=0.7, legend="CDF") p4.legend.orientation = "top_left" p5 = figure(title="Weibull Distribution (λ=1, k=1.25)", background_fill="#E8DDCB", tools="") lam, k = 1, 1.25 # sample the distribution measured = lam * (-np.log(np.random.uniform(0, 1, 1000)))**(1 / k) hist, edges = np.histogram(measured, density=True, bins=50) # compute ideal values x = np.linspace(0, 8, 1000) pdf = (k / lam) * (x / lam)**(k - 1) * np.exp(-(x / lam)**k) cdf = 1 - np.exp(-(x / lam)**k) p5.quad(top=hist, bottom=0, left=edges[:-1], right=edges[1:], fill_color="#036564", line_color="#033649") p5.line(x, pdf, line_color="#D95B43", line_width=8, alpha=0.7, legend="PDF") p5.line(x, cdf, line_color="white", line_width=2, alpha=0.7, legend="CDF") p5.legend.orientation = "top_left" # show all the plots arrayed in a VBox show(VBox(p1, p2, p3, p4, p5))
title="stacked, dict_input", xlabel="countries", ylabel="medals", legend=True, width=800, height=600, stacked=True) df = pd.DataFrame(medals, index=countries) bar3 = Bar(df, title="stacked, df_input", xlabel="countries", ylabel="medals", legend=True, width=800, height=600, stacked=True) medals = np.array([bronze, silver, gold]) bar4 = Bar(df, title="grouped, df_input", xlabel="countries", ylabel="medals", legend=True, width=800, height=600) output_file("bar.html") show(VBox(bar1, bar2, bar3, bar4))
y6_temps = temps_by_tank[6] y7_temps = temps_by_tank[7] y8_temps = temps_by_tank[8] Document = Document() output_file("/var/www/index.html") attrib_dict = dict(width=300, height=300, x_axis_type="datetime") line_dict = dict(size=12, alpha=0.5) plot0 = figure(title="Tank 1", **attrib_dict) plot1 = figure(title="Tank 2", **attrib_dict) plot2 = figure(title="Tank 3", **attrib_dict) plot3 = figure(title="Tank 4", **attrib_dict) plot4 = figure(title="Tank 5", **attrib_dict) plot5 = figure(title="Tank 6", **attrib_dict) plot6 = figure(title="Tank 7", **attrib_dict) plot7 = figure(title="Tank 8", **attrib_dict) plot0.line(x_axis_time_stamps, y0_temps, color="black", **line_dict) plot1.line(x_axis_time_stamps, y1_temps, color="red", **line_dict) plot2.line(x_axis_time_stamps, y2_temps, color="green", **line_dict) plot3.line(x_axis_time_stamps, y3_temps, color="blue", **line_dict) plot4.line(x_axis_time_stamps, y4_temps, color="violet", **line_dict) plot5.line(x_axis_time_stamps, y5_temps, color="blue", **line_dict) plot6.line(x_axis_time_stamps, y6_temps, color="red", **line_dict) plot7.line(x_axis_time_stamps, y7_temps, color="purple", **line_dict) show( VBox(HBox(plot0, plot1, plot2, plot3), HBox(plot4, plot5, plot6, plot7)))
cat=categories, title="All negative input | Grouped", ylabel="Random Number", width=width, height=height) np_custom = Bar(mixed, cat=categories, title="Custom range (start=-3, end=0.4)", ylabel="Random Number", width=width, height=height, continuous_range=Range1d(start=-3, end=0.4)) np_mixed_grouped = Bar(mixed, cat=categories, title="Mixed-sign input | Grouped", ylabel="Random Number", width=width, height=height) # Collect and display output_file("bar.html") show( VBox( HBox(dict_stacked, df_grouped), HBox(np_stacked, np_negative_grouped), HBox(np_mixed_grouped, np_custom), ))
# EXERCISE: add a `rect` renderer to stack the gold medal results # EXERCISE: use grid(), axis(), etc. to style the plot. Some suggestions: # - remove the grid lines # - change the major label standoff, and major_tick_out values # - make the tick labels smaller # - set the x-axis orientation to vertical, or angled # create a new figure p2 = figure(title="Olympic Medals by Country (grouped)", tools="", x_range=countries, y_range=[0, max([gold.max(), silver.max(), bronze.max()])], background_fill='#59636C', plot_width=1000, plot_height=300) # Categorical percentage coordinates can be used for positioning/grouping countries_bronze = [c+":0.3" for c in countries] countries_silver = [c+":0.5" for c in countries] countries_gold = [c+":0.7" for c in countries] # EXERCISE: re create the medal plot, but this time: # - do not stack the bars on the y coordinate # - use countries_gold, etc. to positions the bars on the x coordinate # EXERCISE: use grid(), axis(), etc. to style the plot. Some suggestions: # - remove the axis and grid lines # - remove the major ticks # - make the tick labels smaller # - set the x-axis orientation to vertical, or angled # show the plots arrayed in a VBox show(VBox(p1, p2))
def plot_bokeh(symulacja, normalized_types = False, types_to_plot = None, type_labels = None, ligand_labels = None,uniq_id="no-id"): from bokeh.plotting import figure, VBox, output_server, cursession, push, Session from bokeh.models import Range1d from bokeh.document import Document pops = symulacja[0] ligands = symulacja[1] types = symulacja[2] if types_to_plot == None: types_to_plot = types.keys() if type_labels == None: type_labels = dict([]) for k in types.keys(): type_labels[k] = k if ligand_labels == None: ligand_labels = dict([]) for k in ligands.keys(): ligand_labels[k] = k output_server("stops1"+uniq_id) len_pops = len(pops) step_axis = range(len_pops) TOOLS="resize,pan,wheel_zoom,box_zoom,reset,previewsave,poly_select" xrange1 = Range1d(start=0, end=len_pops-1) if normalized_types: #dwa wykresy - rozmiar populacji i procent komorek fig1 = figure(title="Population size",width=800, height=332,tools=TOOLS,x_range=xrange1) fig1.line(step_axis,pops,line_color = "blue",line_width=2,line_alpha=0.9) fig2 = figure(title="% of cells of specific types in the population",width=724,height=300,tools=TOOLS,x_range=xrange1) for k, v in types.items(): if k in types_to_plot: norm_sizes = [(float(v[i]) / pops[i] * 100) for i in range(len(pops))] color = lambda: random.randint(0,255) rcolor = ('#%02X%02X%02X' % (color(),color(),color())) fig2.line(step_axis, norm_sizes, line_width=2, line_color = rcolor, legend=type_labels[k]) fig2.legend.orientation = "top_right" else: fig1 = figure(title="Population / type size",width=724,height=300,tools=TOOLS,x_range=xrange1) for k, v in types.items(): if k in types_to_plot: color = lambda: random.randint(0,255) rcolor = ('#%02X%02X%02X' % (color(),color(),color())) fig1.line(step_axis, v, line_width = 2, line_color=rcolor, legend = type_labels[k]) fig1.legend.orientation = "top_right" fig1.line(step_axis, pops, line_width = 2.0, line_color = "blue", legend = "population size") fig3 = figure(title="Ligands",width=724,height=300,tools=TOOLS,x_range=xrange1) for k, v in ligands.items(): color = lambda: random.randint(0,255) rcolor = ('#%02X%02X%02X' % (color(),color(),color())) fig3.line(step_axis, v, line_width = 3, line_color = rcolor, legend = ligand_labels[k]) fig3.legend.orientation = "top_right" push() if normalized_types: p = VBox(fig1,fig2,fig3) else: p = VBox(fig1,fig3) session=cursession() return p,session
scatter1 = Scatter(xyvalues, title="iris dataset, dict_input", xlabel="petal_length", ylabel="petal_width", legend='top_left', marker="triangle") groupped_df = flowers[["petal_length", "petal_width", "species"]].groupby("species") scatter2 = Scatter(groupped_df, title="iris dataset, dict_input", xlabel="petal_length", ylabel="petal_width", legend='top_left') pdict = OrderedDict() for i in groupped_df.groups.keys(): labels = groupped_df.get_group(i).columns xname = labels[0] yname = labels[1] x = getattr(groupped_df.get_group(i), xname) y = getattr(groupped_df.get_group(i), yname) pdict[i] = list(zip(x, y)) df = pd.DataFrame(pdict) scatter3 = Scatter(df, title="iris dataset, dict_input", xlabel="petal_length", ylabel="petal_width", legend='top_left') scatter4 = Scatter(list(xyvalues.values()), title="iris dataset, dict_input", xlabel="petal_length", ylabel="petal_width", legend='top_left') output_file("scatter.html") show(VBox(scatter1, scatter2, scatter3, scatter4))