def bball(): os.chdir('C:\\Users\\weinfz18\\Documents\\NBA_current') ## switch to correct directory imgo = np.loadtxt('imgo.csv', delimiter=',') imgd = np.loadtxt('imgd.csv', delimiter=',') ## court image players = pd.read_csv('C:\\Users\\weinfz18\\Documents\\NBA_current\\all_players.csv', names=['players'],delimiter='$') oplayer = "Whiteside, Hassan" dplayer = "Whiteside, Hassan" dplayername = ''.join([x for x in dplayer.lower() if x in 'abcdefghijklmnopqrstuvwxyz']) oplayername = ''.join([x for x in oplayer.lower() if x in 'abcdefghijklmnopqrstuvwxyz']) odata = pd.read_csv('C:\\Users\\weinfz18\\Documents\\NBA_current\\player_shots\\{}\\odata.csv'.format(oplayername)) ddata = pd.read_csv('C:\\Users\\weinfz18\\Documents\\NBA_current\\player_shots\\{}\\ddata.csv'.format(dplayername)) odata = odata[odata['dense']!=0] ddata = ddata[ddata['dense']!=0] ddata = ddata.reset_index(drop=True) odata = odata.reset_index(drop=True) odata = odata.to_dict("list") ddata = ddata.to_dict("list") oplayer_select = Select(value="Rubio, Ricky", title='Offensive player', options=sorted(players['players'].tolist())) dplayer_select = Select(value="Rubio, Ricky", title='Defensive player', options=sorted(players['players'].tolist())) oplayer_select.on_change('value', update_plot) dplayer_select.on_change('value', update_plot) op, odata_table, dp, ddata_table = Make_Plot(ddata,odata,imgo,imgd,oplayer,dplayer) oscript, odiv = components(op) otscript, otdiv = components(odata_table) dscript, ddiv = components(dp) dtscript, dtdiv = components(ddata_table) script = [oscript, otscript, dscript, dtscript] div = [odiv, otdiv, ddiv, dtdiv] return render_template('index.html', script=script, div=div)
def simple_chart(request): plot = figure() import numpy as np # x = np.arange(-10,10,0.1) # y = np.arange(-10,10,0.1) # plot.circle([1,2], [3,4]) script, div = components(plot, CDN) tp, hp = np.loadtxt('/Users/sebastian/phd/data/phenEOB-data/phenP/hp.dat').T tc, hc = np.loadtxt('/Users/sebastian/phd/data/phenEOB-data/phenP/hc.dat').T # select the tools we want # TOOLS="reset,pan,wheel_zoom,box_zoom,save" # p1 = figure(tools=TOOLS, plot_width=300*2, plot_height=300) p1 = figure(plot_width=300*2, plot_height=300) p1.line(tp, hp, color="red", alpha=0.5) # p2 = figure(tools=TOOLS, plot_width=300*2, plot_height=300, x_range=p1.x_range, y_range=p1.y_range,) p2 = figure(plot_width=300*2, plot_height=300, x_range=p1.x_range, y_range=p1.y_range,) p2.line(tc, hc, color="blue", alpha=0.5) from bokeh.plotting import gridplot p = gridplot([[p1],[p2]]) # plots = {'Red': p1, 'Blue': p2} # script, div = components(plots) script, div = components(p) return render(request, "bokplot/simple_chart.html", {"the_script": script, "the_div": div})
def bcplot(self, crime, dist, norm_dist='neither', freq = '6M'): frq = freq nd = norm_dist districts = list(dist) crime = crime fname = 'app/static/data/perc/{}-{}.csv'.format(crime.upper(),frq.upper()) df = pd.read_csv(fname) dsdf = df.set_index(pd.to_datetime(df['Unnamed: 0'])) dsdf.index.rename('Date', inplace=True) dsdf.drop('Unnamed: 0', axis=1, inplace=True) color_list = ['blue','red','green','orange','purple','teal','brown'] dcl = zip(districts[:6],color_list) x = dsdf.index #Pull all city crime and house file fname = 'app/static/data/perd/{}-{}.csv'.format('ALL',frq.upper()) df = pd.read_csv(fname) sfdf = df.set_index(pd.to_datetime(df['Unnamed: 0'])) sfdf.index.rename('Date', inplace=True) sfdf.drop('Unnamed: 0', axis=1, inplace=True) #Pull all district house file fname = 'app/static/data/AllDistHouse-{}.csv'.format(frq.upper()) df = pd.read_csv(fname) adh = df.set_index(pd.to_datetime(df['weekEndingDate'])) adh.index.rename('Date', inplace=True) #adh.drop('Unnamed: 0', axis=1, inplace=True) p = figure(plot_width=1000, plot_height=600, title='Plot of {} in District(s)'.format(crime.title()), x_axis_label='Date', x_axis_type='datetime' #y_axis_label='Count of {} / 6mo'.format(crime.title()) ) for i in dcl: if nd=='dist_deal': print "plotting dist deal" district_crime = dsdf[i[0]] city_crime = sfdf[crime.upper()] dh = adh[i[0]] ch = sfdf['averageListingPrice'] y = 1/((district_crime*dh)/(city_crime*ch)) p.yaxis.axis_label = "Deal Index for {} / {}".format(crime.title(),frq) if nd=='neither': print "plotting neither" y = dsdf[i[0]] p.yaxis.axis_label = "Count of {} / {}".format(crime.title(),frq) #p.y_axis_label='Count of {} / {}'.format(crime.title(),frq) p.line(x, y, line_width=4, color=i[1], alpha = .7, legend="{}".format(i[0].title())) p.legend.orientation = "bottom_left" # create the HTML elements to pass to template figJS,figDiv = components(p,CDN) script, div = components(p) return script, div
def MonthGraph(request): if request.method =='GET' : if request.user.is_authenticated(): now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)}) else: return redirect('/authen/') elif request.method == 'POST': now = datetime.now() now = formats.date_format(now,"SHORT_DATETIME_FORMAT") #data = DeviceUsage.objects.values('date').annotate(sumusage=Sum('usage'))# Group by date y = [0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0] head="" if request.POST.get('deviceid') is not None: id = request.POST.get('deviceid') if request.POST.get('month') is not None: month = request.POST.get('month') do = DeviceProfile.objects.get(device_id=id) data = DeviceUsage.objects.filter(device_id=do,date__month=month).values('date').annotate(sumusage=Sum('usage'),sumtime=Sum('time')) #print data # data[0].get('date').strftime("%d") get only date from year-month-date for d in data: hr = (float(d.get('sumtime'))/3600) kw = float(d.get('sumusage')) y[int(d.get('date').strftime("%d"))] = kw * hr head = "usage of device name: "+ do.device_name +" at "+request.POST.get('month')+"th Month" else: print "Don't select data" head="" plot = Bar(y,title=head, xlabel='date', ylabel='Unit', width=800, height=400) plot.toolbar_location = None plot.outline_line_color = None script, div = components(plot, CDN) return render(request, "monthgraph.html", {"the_script": script, "the_div": div,'devices': DeviceProfile.objects.filter(owner=request.user),"date":str(now)})
def test_output_is_without_script_tag_when_wrap_script_is_false(self, mock_make_id): script, div = embed.components(_embed_test_plot) html = bs4.BeautifulSoup(script) scripts = html.findAll(name='script') self.assertEqual(len(scripts), 1) script_content = scripts[0].getText() rawscript, div = embed.components(_embed_test_plot, wrap_script=False) self.maxDiff = None self.assertEqual(rawscript.strip(), script_content.strip())
def test_plot_dict_returned_when_wrap_plot_info_is_false(self, mock_make_id): plot = _embed_test_plot expected_plotdict = {"modelid": plot.ref["id"], "elementid": "ID", "docid": "ID"} script, plotdict = embed.components(_embed_test_plot, wrap_plot_info=False) self.assertEqual(plotdict, expected_plotdict) script, plotids = embed.components((_embed_test_plot, _embed_test_plot), wrap_plot_info=False) self.assertEqual(plotids, (expected_plotdict, expected_plotdict)) script, plotiddict = embed.components({'p1': _embed_test_plot, 'p2': _embed_test_plot}, wrap_plot_info=False) self.assertEqual(plotiddict, {'p1': expected_plotdict, 'p2': expected_plotdict})
def index_Main(): if request.method =='GET': #conn=sqlite3.connect('misodata.db') #A=pd.read_sql('SELECT * FROM LMPdata LIMIT 5',conn) #conn.close() #B=A.loc[0]['NODE'] return render_template('/Milestone_Main.html', Nodename="",node1n="",node1s="",node1t="") else: node=request.form['nodename'] NODE_info=pd.read_csv('N_info.csv') #node1=request.args.get("node1") #Hval2=Hval.loc[0]['NODE_NAME'] #NODE_info=pd.DataFrame({'NODE':['AMIL.EDWARDS2','AMMO.LABADIE1'],'STATE':['IL','MO'],'TYPE':['GEN','GEN']}) if any(NODE_info.NODE_NAME==node)==False: nodeout=node+' is not a Node name' return render_template('Milestone_Main.html', Nodename=nodeout) else: nodefind=NODE_info.loc[NODE_info['NODE_NAME']==node].index.tolist()[0] node1n=NODE_info.loc[nodefind]['NODE_NAME'] node1s=NODE_info.loc[nodefind]['STATE'] node1t=NODE_info.loc[nodefind]['TYPE'] #if nodenum=='1nodes': nodenum=request.form['nodenum'] if nodenum=='1nodes': dfprice=plotbokeh(node1n) bdate=np.array(dfprice['DATE'], dtype=np.datetime64) bprice=np.array(dfprice['PRICE']) p1=figure(x_axis_type='datetime') p1.line(bdate,bprice) #np.array( #,dtype=np.datetime64) p1.title = ' Energy Prices for ' + node1n p1.xaxis.axis_label = "Date" p1.yaxis.axis_label = "Price/MWh" script, div = components(p1) else: node2=request.form['nodename2'] dfprice=plotbokehcomp(node1n,node2) bdate=np.array(dfprice['DATE'], dtype=np.datetime64) bprice=np.array(dfprice['DIFF_COST']) p1=figure(x_axis_type='datetime') p1.line(bdate,bprice) p1.title = "Temporal Energy Price Differences" p1.xaxis.axis_label = "Date" p1.yaxis.axis_label = "Price/MWh (+Node1,-Node2)" script, div = components(p1) #script=bdate #div=bprice #return render_template('/Milestone_Main.html',Nodename="",node1n=node1n,node1s=node1s,node1t=node1t) #else: # script='empty' # div='empty' return render_template('Onenode_plot.html',node1n=node1n, script=script, div=div)
def test_return_type(self): r = embed.components(_embed_test_plot) self.assertEqual(len(r), 2) script, divs = embed.components((_embed_test_plot, _embed_test_plot)) self.assertTrue(isinstance(divs, tuple)) script, divs = embed.components([_embed_test_plot, _embed_test_plot]) self.assertTrue(isinstance(divs, tuple)) script, divs = embed.components({"Plot 1": _embed_test_plot, "Plot 2": _embed_test_plot}) self.assertTrue(isinstance(divs, dict) and all(isinstance(x, string_types) for x in divs.keys()))
def test_output_is_without_script_tag_when_wrap_script_is_false(self, mock_uuid): mock_uuid.uuid4 = mock.Mock() mock_uuid.uuid4.return_value = "uuid" script, div = embed.components(_embed_test_plot) html = bs4.BeautifulSoup(script) scripts = html.findAll(name="script") self.assertEqual(len(scripts), 1) script_content = scripts[0].getText() rawscript, div = embed.components(_embed_test_plot, wrap_script=False) self.maxDiff = None self.assertEqual(rawscript.strip(), script_content.strip())
def figures(): stock = request.form['Stock'] today = date.today() end_date = today - timedelta(days=1) end_date end_date = today - timedelta(days=1) end_date month = timedelta(days=30) start_date = end_date - month start_date end_date = end_date.isoformat() end_date start_date = start_date.isoformat() start_date api_key = 'YuzEP1riVHHxHDx7BSyz' url = "https://www.quandl.com/api/v3/datasets/WIKI/%s/data.csv?column_index=4&order=asc&start_date=%s&end_date=%s&api_key=%s" % (stock, start_date, end_date, api_key) source = urllib2.urlopen(url).read() open("data.csv", "wb").write(source) nt = pd.read_csv('data.csv', parse_dates=['Date'], index_col='Date') nt = nt.set_index(pd.to_datetime(nt.index)) # generate some random integers, sorted x = nt.index y = nt['Close'] p = figure(plot_width=800, plot_height=500, title='Closing Price %s for Past 30 Days' % (stock), x_axis_label='Date', x_axis_type='datetime', y_axis_label='Closing Price', ) p.line(x, y, line_width=2, color="blue", alpha = .5) p.legend.orientation = "bottom_left" # create the HTML elements to pass to template figJS,figDiv = components(p,CDN) script, div = components(p) return render_template('figure.html', script=script, div=div)
def test_plot_dict_returned_when_wrap_plot_info_is_false(self, mock_uuid): mock_uuid.uuid4 = mock.Mock() mock_uuid.uuid4.return_value = 'uuid' plot = _embed_test_plot expected_plotdict = {"modelid": plot.ref["id"], "elementid": "uuid", "modeltype": "Plot"} script, plotdict = embed.components(_embed_test_plot, wrap_plot_info=False) self.assertEqual(plotdict, expected_plotdict) script, plotids = embed.components((_embed_test_plot, _embed_test_plot), wrap_plot_info=False) self.assertEqual(plotids, (expected_plotdict, expected_plotdict)) script, plotiddict = embed.components({'p1': _embed_test_plot, 'p2': _embed_test_plot}, wrap_plot_info=False) self.assertEqual(plotiddict, {'p1': expected_plotdict, 'p2': expected_plotdict})
def admin(): if current_user.is_authenticated and Permission.MANAGE_USER_ACCOUNT in current_user.permissions: # never_logged_in = User.query.filter(User.last_seen == None).filter(User.status_id != 2).all() # logged_in = User.query.filter(User.last_seen != None).filter(User.status_id != 2).all() # all_active_user = User.query.filter(User.status_id != 2).all() from bokeh.embed import components from bokeh.charts import Donut, Bar from bokeh.charts.operations import blend from bokeh.charts.attributes import cat, color import pandas as pd from sqlalchemy import text never_logged_in = User.query.filter(User.last_seen == None).filter(User.status_id != 2).count() logged_in = User.query.filter(User.last_seen != None).filter(User.status_id != 2).count() total = never_logged_in + logged_in d = {'user': pd.Series(['Never logged in', 'Logged in'], index=[0, 1]), 'user_count': pd.Series([never_logged_in, logged_in], index=[0, 1]) } df = pd.DataFrame(d) pie_chart = Donut(df, label=['user'], values='user_count', hover_text='user_count') script, div = components(pie_chart) df2 = pd.read_sql(UserSurvey.query.statement, db.session.bind) def completed(row): val = "Not Completed" if row['completed_on']: val="Completed" return val df2['completed'] = df2.apply(completed, axis=1) b = Bar(df2, label='survey_id', values='user_id', agg='count', stack='completed', legend='top_right') bar_script, bar_div = components(b) return render_template('/admin.html', user=current_user, script=script, div=div, bar_script=bar_script, bar_div=bar_div) else: return redirect(url_for('index'))
def plot_display(): today = date.today() if today.month == 1: new_month = 12 new_year = today.year-1 else: new_month = today.month-1 new_year = today.year m_onemonth = today.replace(month=new_month, year = new_year) data = np.array(app.vars['D']['dataset']['data'])[:,1:].astype(float) dates = np.array(app.vars['D']['dataset']['data'])[:,0].astype('M8[D]') c_header = np.array(app.vars['D']['dataset']['column_names']).astype(str) S = Series(data.T.tolist(), index = c_header[1:]).to_dict() DF = DataFrame(S, index=dates).sort_index(ascending=False) x = DF[today:m_onemonth].index y = DF[today:m_onemonth]["Close"][::-1] p = figure(title=app.vars['ticker']+" closing price previous month", plot_height=300, plot_width = 600, x_axis_type="datetime") p.line(x,y,color = "#2222aa", line_width = 3) #js_res=INLINE.render_js() #css_res=INLINE.render_css() script, div = components(p)#, INLINE) print(div) return render_template('plot.html',ticker=app.vars['ticker'], div=div, script=script)#, js_resources=js_res, css_resources=css_res)
def plot(): models = map(str,request.args.getlist('model')) total_HD = db.engine.execute("SELECT model, COUNT(serial_number) FROM HDmodel GROUP BY model;") failed_HD = db.engine.execute("SELECT model, COUNT(serial_number) FROM HDmodel WHERE sum>0 GROUP BY model;") total=[] for row in total_HD: total.append([row[0],float(row[1])]) total=pd.DataFrame(total,columns=['model','drives']) failed=[] for row in failed_HD: failed.append([row[0],float(row[1])]) failed=pd.DataFrame(failed,columns=['model','drives']) #failure_rate = pd.DataFrame(failed[failed['model'].str.contains('WDC')].drives/(total[total['model'].str.contains('WDC')].drives)) #model = list(failed[failed['model'].str.contains('WDC')].model) p = Bar(failed[failed['model'].str.contains('|'.join(models))].drives,cat=list(failed[failed['model'].str.contains('|'.join(models))].model)) script, div = embed.components(p,CDN) return render_template("bar.html",script=script,div=div)
def get_plot_comp(df, player_name): values = blend( 'pass', 'shoot', 'turnover', name='action probability', labels_name='play_action' ) labels = cat(columns='region', sort=False) stacks = cat(columns='play_action', sort=False) colors = color( columns='play_action', palette=['DeepSkyBlue', 'LightGreen', 'LightPink'], sort=False ) title = player_name + ' Action Probability per Region' hover_info = [('action', '@play_action'), ('prob', '@height')] bar = Bar( df, values=values, label=labels, stack=stacks, color=colors, legend='bottom_left', title=title, tooltips=hover_info ) bar.title.text_font_size = '14pt' bar.legend.orientation = 'horizontal' script, div = components(bar) # output_file("~/repos/Triple-Triple/triple_triple/simulator_analytics/plot_app/templates/test_bar.html") return script, div, bar
def doPlot_Box(data, nrDataSource): p = BoxPlot(data, values=data.columns[1], label=data.columns[0], marker='square', color=data.columns[0], title="BoxPlot: " + nrDataSource['name']) c = components(p, resources=None, wrap_script=False, wrap_plot_info=True) return c
def build_stock_plot(symbol, dates, prices): average_price = float(sum(prices)) / len(prices) average_dates = [0 for n in prices] first_price = prices[-1] p = figure( width=610, plot_height=300, tools="pan,box_zoom,reset", title="1 month period", x_axis_label=None, x_axis_type="datetime", y_axis_label="$ per share", toolbar_location="below", ) p.line(dates, prices, color="navy", alpha=0.9, line_width=2, legend=symbol.upper()) p.line(dates, average_price, color="orange", legend="Average", alpha=0.4, line_width=1.5) # p.line(dates, first_price, color='red', legend='Starting', alpha=0.4, line_width=1.5) p.ygrid.minor_grid_line_color = "navy" p.ygrid.minor_grid_line_alpha = 0.1 p.legend.orientation = "top_left" p.legend.label_text_font = "Helvetica" script, div = components(p) return script, div
def doPlot2(data, nrDataSource): plots = [] for thisColumn in data.columns[1:]: plots.append(Bar(data, data.columns[0], values=thisColumn, title="Bar graph: " + nrDataSource['name'], xlabel=data.columns[0], ylabel=thisColumn, responsive=True)) c = components(vplot(*plots), resources=None, wrap_script=False, wrap_plot_info=True) return c
def plot_count_by_hour(date): date_format_str = '%Y-%m-%d %H:%M:%S' date_parser = lambda u: pd.datetime.strptime(u, date_format_str) # read it again name_list = ['datetime','vehicle_id','latitude','longitude'] df = pd.read_csv('cash/bus_data.csv',names = name_list, parse_dates=True, date_parser=date_parser,index_col = 0, header=0) pstart = pd.datetime.strptime(date[0], '%Y/%m/%d/%H') pend = pd.datetime.strptime(date[1], '%Y/%m/%d/%H') try: df = df.ix[pstart:pend] except: pass bus_count = df['vehicle_id'].groupby(pd.TimeGrouper(freq='30min')).nunique() # bokeh plot plot = figure(x_axis_type = 'datetime') plot.title = 'bus count' plot.grid.grid_line_alpha=0.3 plot.xaxis.axis_label = 'Time' plot.yaxis.axis_label = 'Bus Count' plot.line(np.array(bus_count.index, dtype=np.datetime64), bus_count.values, line_color='blue', line_width=2) #slider = Slider(start=0.1, end=4, value=1, step=.1, title='power', callback=callback) #output_file('plot.html') #save(plot) script, div = components(plot) return script, div
def doPlot11(data, nrDataSource): print(data) print(data.columns) p = Line(data, title="Line graph: " + nrDataSource['name'], xlabel=data.columns[0], ylabel=data.columns[1], responsive=True) c = components(p, resources=None, wrap_script=False, wrap_plot_info=True) return c
def polinet(): error = None if request.method == 'POST': try: app_polinet.vars['pol_name'] = get_pol_name(request.form['pol_name']) except: app_polinet.vars['pol_name'] = 'Not a valid name' else: app_polinet.vars['pol_name'] = 'Bernard Sanders' app.vars['pol_data'] = pd.DataFrame([[1,2,3,1,3,2,4,1]]) fig = TimeSeries(app.vars['pol_data']) plot_resources = RESOURCES.render( js_raw=INLINE.js_raw, css_raw=INLINE.css_raw, js_files=INLINE.js_files, css_files=INLINE.css_files ) script, div = components(fig, INLINE) return render_template('polinet.html', name=app_polinet.vars['pol_name'], data=app_polinet.vars['pol_data'], plot_script=script, plot_div=div, plot_resources=plot_resources)
def index(): if request.method == 'GET': return render_template('index.html', op = app.OPTIONS, names = app.NAMES) if request.method == 'POST': options = [val.replace("_",". ") for val in app.OPTIONS if request.form.get(val) is not None] ticker = request.form.get("ticker") notValid, fig = make_html(ticker, options) app.script, app.div = components(fig) #output_file("test.html") ''' TOOLS = "resize,crosshair,box_zoom,reset,box_select,save" output_file("test.html") fig=figure(title="Sensor data", tools = TOOLS) fig.line([1,2,3,4],[2,4,6,8]) #global script #global div script, div=components(fig) ''' if len(notValid) > 0: notValidstr = 'These are not valid tickers: ' + ', '.join(notValid) else: notValidstr = "" return render_template('plot.html', op = app.OPTIONS, names = app.NAMES,\ script = app.script, div = json.dumps(app.div), notValid = notValidstr)
def index(): if request.method == 'GET': return render_template('index.html') else: ticker = request.form['ticker'].upper() features = request.form.getlist('features') api_url = 'https://www.quandl.com/api/v1/datasets/WIKI/%s.json?api_key=5wY62Y1qbsMcJA9qVjBz' % ticker session = requests.Session() session.mount('http://', requests.adapters.HTTPAdapter(max_retries=3)) raw_data = session.get(api_url) if len(raw_data.json()) <= 1: return render_template('error.html', ticker=ticker) else: if not features: return render_template('blank.html') else: cols = raw_data.json()[u'column_names'] data = raw_data.json()[u'data'] df = pd.DataFrame(data) df.columns = cols df['Date'] = pd.to_datetime(df['Date']) p = figure(title='Data from Quandl WIKI set', x_axis_label='date', x_axis_type='datetime') for (f, c) in zip(features, Spectral4): p.line(df['Date'], df[f], line_color=c, legend=ticker + ": " + f) script, div = components(p) return render_template('graph.html', script=script, div=div, ticker=ticker)
def deconv_summary_page(filename, cost_data, deconv_data): fig_dict = dict() cost_key = "cost_plot" fig_dict[cost_key] = cost_fig(cost_data, 300, 533, epoch_axis=True) vis_keys = dict() img_keys = dict() for layer, layer_data in deconv_data: lyr_vis_keys, lyr_img_keys, lyr_fig_dict = deconv_figs(layer, layer_data, fm_max=4) vis_keys[layer] = lyr_vis_keys img_keys[layer] = lyr_img_keys fig_dict.update(lyr_fig_dict) script, div = components(fig_dict) template = Template(''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>{{page_title}}</title> <style> div{float: left;} </style> <link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.9.0.min.css" type="text/css" /> <script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.9.0.min.js"></script> {{ script }} </head> <body> <div id=cost_plot style="width:100%; padding:10px"> {{ div[cost_key]}} </div> {% for layer in sorted_layers %} <div id=Outer{{layer}} style="padding:20px"> <div id={{layer}} style="background-color: #C6FFF1; padding:10px"> Layer {{layer}}<br> {% for fm in vis_keys[layer].keys() %} <div id={{fm}} style="padding:10px"> Feature Map {{fm}}<br> {{ div[vis_keys[layer][fm]] }} {{ div[img_keys[layer][fm]] }} </div> {% endfor %} </div> </div> <br><br> {% endfor %} </body> </html> ''') with open(filename, 'w') as htmlfile: htmlfile.write(template.render(page_title="Deconv Visualization", script=script, div=div, cost_key=cost_key, vis_keys=vis_keys, img_keys=img_keys, sorted_layers=sorted(vis_keys)))
def getPlot(self): """using bokeh output glyphs """ from bokeh.embed import components # import pdb # pdb.set_trace() context = aq_inner(self.context) uid = IUUID(context,None) py,html = tmp_file_name(uid) # druge py file is exist? if os.path.isfile(py): pass else: # create self.create_tmp_file(context,py) try: bokeh = load_from_file(py) except: return {"js":"","div":"<div class='text-error'>the uploaded python file has syntax errors!</div>"} script, div = components(bokeh.p) out = {} out['js'] = script out['div'] = div return out
def plot_data(): # get data x = [] y = [] colors = [] i = 0 with open("data.csv", "r") as f: while True: line = f.readline() if len(line) == 0: break if i == 6: break items = line.split(',') x.append(float(items[0])) y.append(float(items[1])) #z = int(items[2][:-1]) colors.append("#%02x%02x%02x" % (255, 0, 0)) i += 1 fig = figure(title="Data", plot_width=600, plot_height=400, toolbar_location="below") fig.scatter(x, y, radius=0.1, fill_color=colors, fill_alpha=0.6, line_color=None) script, div = components(fig, INLINE) html = flask.render_template( 'figure.html', #'layout.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
def sys_plot(pk): job = Job.objects.get(id=pk) jh = job.host_set.all().values_list('name', flat=True).distinct() hover = HoverTool(tooltips = [ ("host", "@x-@y") ]) plot = figure(title = "System Plot", tools = [hover], toolbar_location = None, x_range = racks, y_range = nodes, plot_height = 800, plot_width = 1000) import time start = time.time() ctr = 0 for rack in racks: for node in nodes: name = str(rack)+'-'+str(node) if name in jh: sys_color[ctr] = ["#bf0a30"] ctr+=1 print("sys",time.time()-start) plot.xaxis.major_label_orientation = "vertical" plot.rect(xrack, yrack, color = sys_color, width = 1, height = 1) return components(plot)
def create(self): self.source = self.update_source() p = figure(plot_width=400, plot_height=400, title="Harvest Plot", x_axis_type='datetime', tools='pan, wheel_zoom, box_zoom, reset, resize, save, hover') p.line(x="timestamp", y="relevant_pages", color=GREEN, width=0.2, legend="relevant", source=self.source) p.scatter(x="timestamp", y="relevant_pages", fill_alpha=0.6, color=GREEN, source=self.source) p.line(x="timestamp", y="downloaded_pages", color=DARK_GRAY, width=0.2, legend="downloaded", source=self.source) p.scatter(x="timestamp", y="downloaded_pages", fill_alpha=0.6, color=DARK_GRAY, source=self.source) hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ("harvest_rate", "@harvest_rate"), ]) p.legend.orientation = "top_left" # Save ColumnDataSource model id to database model script, div = components(p, INLINE) return (script, div)
def makeBokehPlot_text(bokehDataSource,clusTitles): clusTitles = ['ALL TOPICS'] +clusTitles allPlots = [__makeBokehPlot_text(bokehDataSource[i]) for i,v in enumerate(clusTitles)] allTabs = [Panel(child=p, title=clusTitles[i]) for i,p in enumerate(allPlots)] tabs = Tabs(tabs=allTabs) script,div = components(tabs) return script, div
def show_plot(): ticker = request.form['ticker'] today_string = time.strftime("%x") today_list = today_string.split("/") year = int(today_list[2]) + 2000 month = int(today_list[0])-1 if month == 0: month = 12 year -= 1 start_date=(str(year) + '-' + str(month) + '-' + today_list[1]) url = "https://www.quandl.com/api/v3/datasets/WIKI/" + ticker + ".json?start_date=" + start_date r = requests.get(url) if r.status_code == 404: return redirect('/error') if r.status_code == 400: return redirect('/bad-request') j = json.loads(r.text) data = pd.DataFrame(data=j['dataset']['data'], columns=j['dataset']['column_names']) data = data.set_index(['Date']) data.index = pd.to_datetime(data.index) output_file("test.html") p = figure(plot_width=400, plot_height=400, x_axis_type="datetime") p.line(data.index, data['Close'], line_width=2) script, div = components(p) return render_template('show-plot.css', name=j['dataset']['name'], stock=ticker, script=script, div=div)
def test_return_type(self): r = embed.components(_embed_test_plot, CDN) self.assertEqual(len(r), 2)
TOOLS = "hover" p = figure(x_range=name, plot_height=400, title="Most Popular Dog Names", toolbar_location=None, tools=TOOLS) hover = p.select(dict(type=HoverTool)) hover.tooltips = OrderedDict([ ('Amount', '@top'), ]) print("name", type(name)) print("top", type(count)) p.vbar(x=name, top=count, width=0.9) p.xgrid.grid_line_color = None p.y_range.start = 0 p.xaxis.major_label_text_font_size = "8pt" p.yaxis.major_label_text_font_size = "10pt" p.axis.major_label_standoff = 0 p.xaxis.major_label_orientation = 1.5 show(p) script, div = components(p)
def plot(): # Web-based Financial Graph # Collecting and presenting Stock Market Data using Python import datetime from pandas_datareader import data from bokeh.plotting import figure, show, output_file from bokeh.embed import components from bokeh.resources import CDN start = datetime.datetime(2020, 12, 10) end = datetime.datetime(2021, 2, 10) # Grabbing Stock data from Yahoo df = data.DataReader(name="GOOG", data_source="yahoo", start=start, end=end) # Indexing date_increase = df.index[df.Close > df.Open] date_decrease = df.index[df.Close < df.Open] # determining whether a stocks price went up or down for the day def inc_dec(close_c, open_c): if close_c > open_c: value = "Increase" elif close_c < open_c: value = "Decrease" else: value = "Equal" return value # Adding a new Status column to our dataset with the info from inc_dec df["Status"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)] # Calculating middle of the box chart (Average between open and close) df["Middle"] = (df.Open + df.Close) / 2 # Height of the rectangle for the data frame df["Height"] = abs(df.Close - df.Open) # building figure object p = figure(x_axis_type='datetime', width=1000, height=300, sizing_mode="scale_width") p.title.text = "Candlestick Chart" # Chart grid line attributes p.grid.grid_line_alpha = 0.3 # 12 Hours in milliseconds hours_12 = 12 * 60 * 60 * 1000 # Creating lines for the Candlestick p.segment(df.index, df.High, df.index, df.Low, color="black") # When price closes higher than when it started p.rect(df.index[df.Status == "Increase"], df.Middle[df.Status == "Increase"], hours_12, df.Height[df.Status == "Increase"], fill_color="#CCFFFF", line_color="black") # When price closes lower than when it started p.rect(df.index[df.Status == "Decrease"], df.Middle[df.Status == "Decrease"], hours_12, df.Height[df.Status == "Decrease"], fill_color="#FF3333", line_color="black") script, div1 = components(p) cdn_js = CDN.js_files return render_template("plot.html", script=script, div1=div1, cdn_js=cdn_js[0])
def gen_plots(refDat, regionList, strCounts, strNames, ambigDict, outDir): (ref_chr, pos_offset, pos_end) = refDat refStr = humanChr(ref_chr) + ': {0:,} - {1:,}'.format(pos_offset, pos_end) TRI_HEIGHT = 1 PLOT_BUFFER = 200 triDict = { regionList[i][0]: (regionList[i][2] - regionList[i][1] + 1, i) for i in xrange(len(regionList)) } N = len(triDict) # # PLOT 1: REFERENCE REGION PARTITIONS # TOOLS = [ PanTool(dimensions='width'), WheelZoomTool(dimensions='width'), HoverTool(), ResetTool() ] p1 = figure(plot_width=900, plot_height=220, tools=TOOLS, title=refStr) colorList = [ "#%02x%02x%02x" % (int(255 * getColor(i, N)[0]), int( 255 * getColor(i, N)[1]), int(255 * getColor(i, N)[2])) for i in xrange(N) ] triList_x = [] triList_y = [] nameList = [] arrowList = [] x_adj = 0 for i in xrange(len(regionList)): n = regionList[i] triList_x.append([x_adj, x_adj, x_adj + triDict[n[0]][0]]) triList_y.append([-TRI_HEIGHT, TRI_HEIGHT, 0]) nameList.append(n[0]) x_adj += triDict[n[0]][0] if regionList[i][0] in ambigDict: ambigVal = ambigDict[regionList[i][0]] if ambigVal[1]: arrowList.append((x_adj + ambigVal[0], x_adj + 1, i + 1)) else: arrowList.append((x_adj + ambigVal[0] - triDict[n[0]][0], x_adj + 1 - triDict[n[0]][0], i)) pos_s_list = [n[0] + pos_offset for n in triList_x] pos_e_list = [n[2] + pos_offset for n in triList_x] source = ColumnDataSource(data=dict(x=triList_x, y=triList_y, name=nameList, color=colorList, sPos=pos_s_list, ePos=pos_e_list)) p1.patches('x', 'y', source=source, color='color', alpha=0.7, line_color="white", line_width=0.5) for i in xrange(len(arrowList)): p1.add_layout( Arrow(x_start=arrowList[i][1], x_end=arrowList[i][0], y_start=0.8 * TRI_HEIGHT, y_end=0.8 * TRI_HEIGHT, end=NormalHead(fill_color=colorList[arrowList[i][2]], line_color=colorList[arrowList[i][2]], fill_alpha=0.5, line_alpha=0.5, size=8), line_width=4, line_color=colorList[arrowList[i][2]], line_alpha=0.6)) #p1.patches(triList, [[-TRI_HEIGHT,TRI_HEIGHT,0] for n in xrange(N)], color=colorList, alpha=[1.0]*N, line_width=1, hover_alpha=[0.6]*N) start = triList_x[0][0] end = triList_x[-1][2] p1.yaxis.major_label_text_color = None p1.xgrid.grid_line_color = None p1.ygrid.grid_line_color = None p1.x_range.bounds = (start - PLOT_BUFFER, end + PLOT_BUFFER) p1.y_range.bounds = (-TRI_HEIGHT - 1, TRI_HEIGHT + 1) p1.xaxis.axis_label = 'Reference position (relative)' p1.select_one(HoverTool).point_policy = "follow_mouse" p1.select_one(HoverTool).tooltips = [("Name", "@name"), ("Ref Start", "@sPos"), ("Ref End", "@ePos")] # # PLOT 2+: OBSERVED JUNCTIONS # observed_junction_plots = [] for i in xrange(len(strCounts)): TOOLS = [ PanTool(dimensions='width'), WheelZoomTool(dimensions='width'), HoverTool(), ResetTool() ] observed_junction_plots.append( figure(plot_width=900, plot_height=200, tools=TOOLS, title="Multiplicity: " + str(strCounts[i][0]) + ", Type: " + strNames[i])) myColorList = [] triList_x = [] triList_y = [] nameList = [] x_adj = 0 for m in strCounts[i][1]: myInd = triDict[m[2].replace('*', '')] myColorList.append(colorList[myInd[1]]) nameList.append(m[2]) if m[1]: # is forward strand triList_x.append([x_adj, x_adj, x_adj + myInd[0]]) triList_y.append([-TRI_HEIGHT, TRI_HEIGHT, 0]) else: # is reverse strand triList_x.append([x_adj, x_adj + myInd[0], x_adj + myInd[0]]) triList_y.append([0, TRI_HEIGHT, -TRI_HEIGHT]) x_adj += myInd[0] # add a novel sequence segment, if necessary if m[3][1]: myColorList.append('#AAAAAA') nameList.append('novel sequence') triList_x.append([x_adj, x_adj, x_adj + m[3][0]]) triList_y.append([-TRI_HEIGHT, TRI_HEIGHT, 0]) x_adj += m[3][0] source = ColumnDataSource(data=dict( x=triList_x, y=triList_y, name=nameList, color=myColorList)) observed_junction_plots[-1].patches('x', 'y', source=source, color='color', alpha=0.7, line_color="white", line_width=0.5) start = triList_x[0][0] end = triList_x[-1][2] observed_junction_plots[-1].yaxis.major_label_text_color = None observed_junction_plots[-1].xgrid.grid_line_color = None observed_junction_plots[-1].ygrid.grid_line_color = None observed_junction_plots[-1].x_range.bounds = (start - PLOT_BUFFER, end + PLOT_BUFFER) observed_junction_plots[-1].y_range.bounds = (-TRI_HEIGHT - 1, TRI_HEIGHT + 1) observed_junction_plots[-1].select_one( HoverTool).point_policy = "follow_mouse" observed_junction_plots[-1].select_one(HoverTool).tooltips = [ ("Name", "@name") ] # # GENERATE HTML # plots = [p1] + observed_junction_plots script, div = components(plots) build_html(outDir + 'results.html', refStr, script, div)
def enkeyword(keyword): print("Keyword:", keyword) en_tweet_df = pd.DataFrame(retrieve_sentiment_data_en()) en_tweet_df['sentiment'] = en_tweet_df['sentiment'].values.astype(float) # print(en_tweet_df['keyword']) en_keywordtweet_df = en_tweet_df[en_tweet_df['keyword'] == keyword] #ENGLISH SENTIMENT BARCHART positive = len([ element for element in (list(en_keywordtweet_df['sentiment'])) if element > 0 and element < 999 ]) negative = len([ element for element in (list(en_keywordtweet_df['sentiment'])) if element < 0 ]) neutral = len([ element for element in (list(en_keywordtweet_df['sentiment'])) if element == 0 ]) none = len([ element for element in (list(en_keywordtweet_df['sentiment'])) if element == 999 ]) sentiment = ['Positive', 'Negative', 'Neutral', 'None'] counts = [positive, negative, neutral, none] colors = ["#82E0AA", "#e84d60", "#ccccff", "#FCA15A"] source = ColumnDataSource( data=dict(sentiment=sentiment, counts=counts, color=colors)) en_bar = figure(x_range=sentiment, plot_height=400, toolbar_location=None, tools="hover", tooltips="@sentiment @counts") en_bar.vbar(x='sentiment', top='counts', width=0.9, color='color', legend_field="sentiment", source=source) en_bar.add_layout(en_bar.legend[0], 'above') en_bar.xgrid.grid_line_color = None en_bar.y_range.start = 0 en_bar.legend.orientation = "horizontal" en_bar.legend.location = "top_right" en_bar.add_layout(Title(text=keyword, text_font_style="italic"), 'above') en_bar.add_layout( Title(text="English Sentiment Counts", text_font_size="12pt"), 'above') en_script, en_div = components(en_bar) #ENGLISH EMOTION en_emo_tweet_df = pd.DataFrame(retrieve_emotion_data_en()) # print(en_emo_tweet_df['keyword'].unique()) en_emo_keywordtweet_df = en_emo_tweet_df[en_emo_tweet_df['keyword'] == keyword] en_emo_keywordtweet_df['anger'] = en_emo_keywordtweet_df[ 'anger'].values.astype(int) en_emo_keywordtweet_df['fear'] = en_emo_keywordtweet_df[ 'fear'].values.astype(int) en_emo_keywordtweet_df['anticipation'] = en_emo_keywordtweet_df[ 'anticipation'].values.astype(int) en_emo_keywordtweet_df['trust'] = en_emo_keywordtweet_df[ 'trust'].values.astype(int) en_emo_keywordtweet_df['surprise'] = en_emo_keywordtweet_df[ 'surprise'].values.astype(int) en_emo_keywordtweet_df['sadness'] = en_emo_keywordtweet_df[ 'sadness'].values.astype(int) en_emo_keywordtweet_df['joy'] = en_emo_keywordtweet_df[ 'joy'].values.astype(int) en_emo_keywordtweet_df['disgust'] = en_emo_keywordtweet_df[ 'disgust'].values.astype(int) en_anger = (en_emo_keywordtweet_df['anger']).sum() en_fear = (en_emo_keywordtweet_df['fear']).sum() en_anticipation = (en_emo_keywordtweet_df['anticipation']).sum() en_trust = (en_emo_keywordtweet_df['trust']).sum() en_surprise = (en_emo_keywordtweet_df['surprise']).sum() en_sadness = (en_emo_keywordtweet_df['sadness']).sum() en_joy = (en_emo_keywordtweet_df['joy']).sum() en_disgust = (en_emo_keywordtweet_df['disgust']).sum() en_emotions = [ 'anger', 'fear', 'anticipation', 'trust', 'surprise', 'sadness', 'joy', 'disgust' ] count = [ en_anger, en_fear, en_anticipation, en_trust, en_surprise, en_sadness, en_joy, en_disgust ] colors = [ "#FF5722", "#FFCC80", "#FFEB3B", "#D4E157", "#66BB6A", "#009688 ", "#80DEEA", "#2196F3" ] source = ColumnDataSource(data=dict( y=en_emotions, right=count, line_color=colors, fill_color=colors)) en_emotionbarc = figure(y_range=en_emotions, plot_height=250, toolbar_location=None, tools="hover", tooltips="@y @right") en_emotionbarc.hbar(y='y', right='right', line_color='line_color', fill_color='fill_color', height=0.9, source=source, legend_field="y") en_emotionbarc.add_layout(en_emotionbarc.legend[0], 'right') en_emotionbarc.legend.items.reverse() en_emotionbarc.y_range.range_padding = 0.1 en_emotionbarc.ygrid.grid_line_color = None en_emotionbarc.axis.minor_tick_line_color = None en_emotionbarc.outline_line_color = None en_emotionbarc.add_layout(Title(text=keyword, text_font_style="italic"), 'above') en_emotionbarc.add_layout( Title(text="English Emotion Counts", text_font_size="12pt"), 'above') script_en, div_en = components(en_emotionbarc) ##PIECHART total = en_anger + en_fear + en_anticipation + en_trust + en_surprise + en_sadness + en_joy + en_disgust per_en_anger = en_anger / total * 100 per_en_fear = en_fear / total * 100 per_en_anticipation = en_anticipation / total * 100 per_en_trust = en_trust / total * 100 per_en_surprise = en_surprise / total * 100 per_en_sadness = en_sadness / total * 100 per_en_joy = en_joy / total * 100 per_en_disgust = en_disgust / total * 100 x = { 'Anger': per_en_anger, 'Fear': per_en_fear, 'Anticipation': per_en_anticipation, 'Trust': per_en_trust, 'Surprise': per_en_surprise, 'Sadness': per_en_sadness, 'Joy': per_en_joy, 'Disgust': per_en_disgust } data = pd.Series(x).reset_index(name='value').rename( columns={'index': 'emotion'}) data['color'] = Category20c[len(x)] data['angle'] = data['value'] / data['value'].sum() * 2 * pi p = figure(plot_height=350, toolbar_location=None, tools="hover", tooltips="@emotion: @value{0.2f} %") p.wedge( x=0, y=1, radius=0.4, # use cumsum to cumulatively sum the values for start and end angles start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='emotion', source=data) p.axis.axis_label = None p.axis.visible = False p.grid.grid_line_color = None p.add_layout(Title(text=keyword, text_font_style="italic"), 'above') p.add_layout( Title(text="Percentage of English Emotions", text_font_size="12pt"), 'above') script2, div2 = components(p) return render_template( 'enkeyword.html', enbar_script=en_script, enbar_div=en_div, enemobar_script=script_en, enemobar_div=div_en, enpie_script=script2, enpie_div=div2, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def plot(): from pandas_datareader import data import datetime from bokeh.plotting import figure, show, output_file from bokeh.embed import components from bokeh.resources import CDN #CDN = Content Delivery Network start = datetime.datetime(2019, 11, 2) end = datetime.datetime(2020, 3, 10) df = data.DataReader(name="GOOG", data_source="yahoo", start=start, end=end) #indermediate data to avoid bohek confusion def inc_dec(c, o): if c > o: value = "Increase" elif c < o: value = "Decrease" else: value = "Equal" return value #creating new column df["Status"] = [inc_dec(c, o) for c, o in zip(df.Close, df.Open)] df["Middle"] = (df.Open + df.Close) / 2 df["Height"] = abs(df.Open - df.Close) #sizing_mode="scale_width" resizes the chart based on window size p = figure(x_axis_type="datetime", width=1000, height=300, sizing_mode="scale_width") p.title.text = "Candlestick Chart" p.grid.grid_line_alpha = 0.3 #alpha denotes the level of transperancy og the grid horizontal lines hours_12 = 12 * 60 * 60 * 1000 p.segment(df.index, df.High, df.index, df.Low, color="black") p.rect(df.index[df.Status == "Increase"], df.Middle[df.Status == "Increase"], hours_12, df.Height[df.Status == "Increase"], fill_color="#CCFFFF", line_color="black") p.rect(df.index[df.Status == "Decrease"], df.Middle[df.Status == "Decrease"], hours_12, df.Height[df.Status == "Decrease"], fill_color="#FF3333", line_color="black") script1, div1 = components(p) cdn_js = CDN.js_files[0] return render_template("plot.html", script1=script1, div1=div1, cdn_js=cdn_js)
def mskeyword(keyword): print("Keyword:", keyword) ms_tweet_df = pd.DataFrame(retrieve_sentiment_data_ms()) ms_tweet_df['sentiment'] = ms_tweet_df['sentiment'].values.astype(int) ms_keywordtweet_df = ms_tweet_df[ms_tweet_df['keyword'] == keyword] # print(ms_keywordtweet_df) positive = len([ element for element in (list(ms_keywordtweet_df['sentiment'])) if element > 0 and element < 999 ]) negative = len([ element for element in (list(ms_keywordtweet_df['sentiment'])) if element < 0 ]) neutral = len([ element for element in (list(ms_keywordtweet_df['sentiment'])) if element == 0 ]) none = len([ element for element in (list(ms_keywordtweet_df['sentiment'])) if element == 999 ]) sentiment = ['Positive', 'Negative', 'Neutral', 'None'] counts = [positive, negative, neutral, none] colors = ["#82E0AA", "#e84d60", "#ccccff", "#FCA15A"] source = ColumnDataSource( data=dict(sentiment=sentiment, counts=counts, color=colors)) ms_bar = figure(x_range=sentiment, plot_height=400, toolbar_location=None, tools="hover", tooltips="@sentiment @counts") ms_bar.vbar(x='sentiment', top='counts', width=0.9, color='color', legend_field="sentiment", source=source) #ms_bar.add_layout(ms_bar.legend[0], 'above') ms_bar.xgrid.grid_line_color = None ms_bar.y_range.start = 0 ms_bar.legend.orientation = "horizontal" ms_bar.legend.location = "top_right" ms_bar.add_layout(Title(text=keyword, text_font_style="italic"), 'above') ms_bar.add_layout( Title(text="Malay Sentiment Counts", text_font_size="12pt"), 'above') ms_script, ms_div = components(ms_bar) ##MALAY EMOTION ms_emo_tweet_df = pd.DataFrame(retrieve_emotion_data_ms()) ms_emo_keywordtweet_df = ms_emo_tweet_df[ms_emo_tweet_df['keyword'] == keyword] ms_emo_keywordtweet_df['anger'] = ms_emo_keywordtweet_df[ 'anger'].values.astype(int) ms_emo_keywordtweet_df['fear'] = ms_emo_keywordtweet_df[ 'fear'].values.astype(int) ms_emo_keywordtweet_df['happy'] = ms_emo_keywordtweet_df[ 'happy'].values.astype(int) ms_emo_keywordtweet_df['love'] = ms_emo_keywordtweet_df[ 'love'].values.astype(int) ms_emo_keywordtweet_df['sadness'] = ms_emo_keywordtweet_df[ 'sadness'].values.astype(int) ms_emo_keywordtweet_df['surprise'] = ms_emo_keywordtweet_df[ 'surprise'].values.astype(int) ms_anger = (ms_emo_keywordtweet_df['anger']).sum() ms_fear = (ms_emo_keywordtweet_df['fear']).sum() ms_happy = (ms_emo_keywordtweet_df['happy']).sum() ms_love = (ms_emo_keywordtweet_df['love']).sum() ms_sadness = (ms_emo_keywordtweet_df['sadness']).sum() ms_surprise = (ms_emo_keywordtweet_df['surprise']).sum() ms_emotions = ['anger', 'fear', 'happy', 'love', 'sadness', 'surprise'] count = [ms_anger, ms_fear, ms_happy, ms_love, ms_sadness, ms_surprise] colors = [ "#FF5722", "#FFCC80", "#FFEB3B", "#D4E157", "#66BB6A", "#009688 " ] source = ColumnDataSource(data=dict( y=ms_emotions, right=count, line_color=colors, fill_color=colors)) ms_emotionbarc = figure(y_range=ms_emotions, plot_height=250, toolbar_location=None, tools="hover", tooltips="@y @right") ms_emotionbarc.hbar(y='y', right='right', line_color='line_color', fill_color='fill_color', height=0.9, source=source, legend_field="y") ms_emotionbarc.add_layout(ms_emotionbarc.legend[0], 'right') ms_emotionbarc.legend.items.reverse() ms_emotionbarc.y_range.range_padding = 0.1 ms_emotionbarc.ygrid.grid_line_color = None ms_emotionbarc.axis.minor_tick_line_color = None ms_emotionbarc.outline_line_color = None ms_emotionbarc.add_layout(Title(text=keyword, text_font_style="italic"), 'above') ms_emotionbarc.add_layout( Title(text="Malay Emotion Counts", text_font_size="12pt"), 'above') script_ms, div_ms = components(ms_emotionbarc) ##PIECHART total = ms_anger + ms_fear + ms_happy + ms_love + ms_sadness + ms_surprise per_ms_anger = ms_anger / total * 100 per_ms_fear = ms_fear / total * 100 per_ms_happy = ms_happy / total * 100 per_ms_love = ms_love / total * 100 per_ms_sadness = ms_sadness / total * 100 per_ms_surprise = ms_surprise / total * 100 x = { 'Anger': per_ms_anger, 'Fear': per_ms_fear, 'Happy': per_ms_happy, 'Love': per_ms_love, 'Sadness': per_ms_sadness, 'Surprise': per_ms_surprise } data = pd.Series(x).reset_index(name='value').rename( columns={'index': 'emotion'}) data['color'] = Category20c[len(x)] data['angle'] = data['value'] / data['value'].sum() * 2 * pi p = figure(plot_height=350, toolbar_location=None, tools="hover", tooltips="@emotion: @value{0.2f} %") p.wedge( x=0, y=1, radius=0.4, # use cumsum to cumulatively sum the values for start and end angles start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='emotion', source=data) p.axis.axis_label = None p.axis.visible = False p.grid.grid_line_color = None p.add_layout(Title(text=keyword, text_font_style="italic"), 'above') p.add_layout( Title(text="Percentage of Malay Emotions", text_font_size="12pt"), 'above') script2, div2 = components(p) return render_template( 'mskeyword.html', msbar_script=ms_script, msbar_div=ms_div, msemobar_script=script_ms, msemobar_div=div_ms, mspie_script=script2, mspie_div=div2, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def emotion(): #ENGLISH EMOTION HORIZONTAL BARCHART en_tweet_df = pd.DataFrame(retrieve_emotion_data_en()) en_tweet_df['anger'] = en_tweet_df['anger'].values.astype(int) en_tweet_df['fear'] = en_tweet_df['fear'].values.astype(int) en_tweet_df['anticipation'] = en_tweet_df['anticipation'].values.astype( int) en_tweet_df['trust'] = en_tweet_df['trust'].values.astype(int) en_tweet_df['surprise'] = en_tweet_df['surprise'].values.astype(int) en_tweet_df['sadness'] = en_tweet_df['sadness'].values.astype(int) en_tweet_df['joy'] = en_tweet_df['joy'].values.astype(int) en_tweet_df['disgust'] = en_tweet_df['disgust'].values.astype(int) en_anger = (en_tweet_df['anger']).sum() en_fear = (en_tweet_df['fear']).sum() en_anticipation = (en_tweet_df['anticipation']).sum() en_trust = (en_tweet_df['trust']).sum() en_surprise = (en_tweet_df['surprise']).sum() en_sadness = (en_tweet_df['sadness']).sum() en_joy = (en_tweet_df['joy']).sum() en_disgust = (en_tweet_df['disgust']).sum() en_emotions = [ 'anger', 'fear', 'anticipation', 'trust', 'surprise', 'sadness', 'joy', 'disgust' ] count = [ en_anger, en_fear, en_anticipation, en_trust, en_surprise, en_sadness, en_joy, en_disgust ] colors = [ "#FF5722", "#FFCC80", "#FFEB3B", "#D4E157", "#66BB6A", "#009688 ", "#80DEEA", "#2196F3" ] source = ColumnDataSource(data=dict( y=en_emotions, right=count, line_color=colors, fill_color=colors)) en_emotionbarc = figure( y_range=en_emotions, plot_height=250, title="Types of Emotion in English Tweets by Count", toolbar_location=None, tools="hover", tooltips="@y @right") en_emotionbarc.hbar(y='y', right='right', line_color='line_color', fill_color='fill_color', height=0.9, source=source, legend_field="y") en_emotionbarc.add_layout(en_emotionbarc.legend[0], 'right') en_emotionbarc.legend.items.reverse() en_emotionbarc.y_range.range_padding = 0.1 en_emotionbarc.ygrid.grid_line_color = None en_emotionbarc.axis.minor_tick_line_color = None en_emotionbarc.outline_line_color = None script_en, div_en = components(en_emotionbarc) #ENGLISH PERCENTAGE PIECHART total = en_anger + en_fear + en_anticipation + en_trust + en_surprise + en_sadness + en_joy + en_disgust per_en_anger = en_anger / total * 100 per_en_fear = en_fear / total * 100 per_en_anticipation = en_anticipation / total * 100 per_en_trust = en_trust / total * 100 per_en_surprise = en_surprise / total * 100 per_en_sadness = en_sadness / total * 100 per_en_joy = en_joy / total * 100 per_en_disgust = en_disgust / total * 100 x = { 'Anger': per_en_anger, 'Fear': per_en_fear, 'Anticipation': per_en_anticipation, 'Trust': per_en_trust, 'Surprise': per_en_surprise, 'Sadness': per_en_sadness, 'Joy': per_en_joy, 'Disgust': per_en_disgust } data = pd.Series(x).reset_index(name='value').rename( columns={'index': 'emotion'}) data['color'] = Category20c[len(x)] data['angle'] = data['value'] / data['value'].sum() * 2 * pi en_pchart = figure(plot_height=350, title="Percentage of Emotion among English Tweets", toolbar_location=None, tools="hover", tooltips="@emotion: @value{0.2f} %") en_pchart.wedge( x=0, y=1, radius=0.4, # use cumsum to cumulatively sum the values for start and end angles start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='emotion', source=data) en_pchart.axis.axis_label = None en_pchart.axis.visible = False en_pchart.grid.grid_line_color = None script_enPc, div_enPc = components(en_pchart) #MALAY EMOTION HORIZONTAL BARCHART ms_tweet_df = pd.DataFrame(retrieve_emotion_data_ms()) ms_tweet_df['anger'] = ms_tweet_df['anger'].values.astype(int) ms_tweet_df['fear'] = ms_tweet_df['fear'].values.astype(int) ms_tweet_df['happy'] = ms_tweet_df['happy'].values.astype(int) ms_tweet_df['love'] = ms_tweet_df['love'].values.astype(int) ms_tweet_df['sadness'] = ms_tweet_df['sadness'].values.astype(int) ms_tweet_df['surprise'] = ms_tweet_df['surprise'].values.astype(int) ms_anger = (ms_tweet_df['anger']).sum() ms_fear = (ms_tweet_df['fear']).sum() ms_happy = (ms_tweet_df['happy']).sum() ms_love = (ms_tweet_df['love']).sum() ms_sadness = (ms_tweet_df['sadness']).sum() ms_surprise = (ms_tweet_df['surprise']).sum() ms_emotions = ['anger', 'fear', 'happy', 'love', 'sadness', 'surprise'] count = [ms_anger, ms_fear, ms_happy, ms_love, ms_sadness, ms_surprise] colors = [ "#FF5722", "#FFCC80", "#FFEB3B", "#D4E157", "#66BB6A", "#009688 " ] source = ColumnDataSource(data=dict( y=ms_emotions, right=count, line_color=colors, fill_color=colors)) ms_emotionbarc = figure(y_range=ms_emotions, plot_height=250, title="Types of Emotion in Malay Tweets by Count", toolbar_location=None, tools="hover", tooltips="@y @right") ms_emotionbarc.hbar(y='y', right='right', line_color='line_color', fill_color='fill_color', height=0.9, source=source, legend_field="y") ms_emotionbarc.add_layout(ms_emotionbarc.legend[0], 'right') ms_emotionbarc.legend.items.reverse() ms_emotionbarc.y_range.range_padding = 0.1 ms_emotionbarc.ygrid.grid_line_color = None ms_emotionbarc.axis.minor_tick_line_color = None ms_emotionbarc.outline_line_color = None script_ms, div_ms = components(ms_emotionbarc) #MALAY PERCENTAGE PIECHART total = ms_anger + ms_fear + ms_happy + ms_love + ms_sadness + ms_surprise per_ms_anger = ms_anger / total * 100 per_ms_fear = ms_fear / total * 100 per_ms_happy = ms_happy / total * 100 per_ms_love = ms_love / total * 100 per_ms_sadness = ms_sadness / total * 100 per_ms_surprise = ms_surprise / total * 100 x = { 'Anger': per_ms_anger, 'Fear': per_ms_fear, 'Happy': per_ms_happy, 'Love': per_ms_love, 'Sadness': per_ms_sadness, 'Surprise': per_ms_surprise } data = pd.Series(x).reset_index(name='value').rename( columns={'index': 'emotion'}) data['color'] = Category20c[len(x)] data['angle'] = data['value'] / data['value'].sum() * 2 * pi p = figure(plot_height=350, title="Percentage of Emotions among Malay Tweets", toolbar_location=None, tools="hover", tooltips="@emotion: @value{0.2f} %") p.wedge( x=0, y=1, radius=0.4, # use cumsum to cumulatively sum the values for start and end angles start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='emotion', source=data) p.axis.axis_label = None p.axis.visible = False p.grid.grid_line_color = None script2, div2 = components(p) return render_template( 'emotion.html', enbar_script=script_en, enbar_div=div_en, en_piechart_script=script_enPc, en_piechart_div=div_enPc, msbar_script=script_ms, msbar_div=div_ms, piechart_script=script2, piechart_div=div2, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def plot_system_stats(stats_file, filebytype, sizebytype): """Read in the file of saved stats over time and plot them. Parameters ----------- stats_file : str file containing information of stats over time filebytype : str file containing information of file counts by type over time sizebytype : str file containing information on file sizes by type over time """ # get path for files settings = get_config() outputs_dir = os.path.join(settings['outputs'], 'monitor_filesystem') # read in file of statistics date, f_count, sysize, frsize, used, percent = np.loadtxt(stats_file, dtype=str, unpack=True) fits_files, uncalfiles, calfiles, ratefiles, rateintsfiles, i2dfiles, nrcfiles, nrsfiles, nisfiles, mirfiles, fgsfiles = np.loadtxt(filebytype, dtype=str, unpack=True) fits_sz, uncal_sz, cal_sz, rate_sz, rateints_sz, i2d_sz, nrc_sz, nrs_sz, nis_sz, mir_sz, fgs_sz = np.loadtxt(sizebytype, dtype=str, unpack=True) logging.info('Read in file statistics from {}, {}, {}'.format(stats_file, filebytype, sizebytype)) # put in proper np array types and convert to GB sizes dates = np.array(date, dtype='datetime64') file_count = f_count.astype(float) systemsize = sysize.astype(float) / (1024.**3) freesize = frsize.astype(float) / (1024.**3) usedsize = used.astype(float) / (1024.**3) fits = fits_files.astype(int) uncal = uncalfiles.astype(int) cal = calfiles.astype(int) rate = ratefiles.astype(int) rateints = rateintsfiles.astype(int) i2d = i2dfiles.astype(int) nircam = nrcfiles.astype(int) nirspec = nrsfiles.astype(int) niriss = nisfiles.astype(int) miri = mirfiles.astype(int) fgs = fgsfiles.astype(int) fits_size = fits_sz.astype(float) / (1024.**3) uncal_size = uncal_sz.astype(float) / (1024.**3) cal_size = cal_sz.astype(float) / (1024.**3) rate_size = rate_sz.astype(float) / (1024.**3) rateints_size = rateints_sz.astype(float) / (1024.**3) i2d_size = i2d_sz.astype(float) / (1024.**3) nircam_size = nrc_sz.astype(float) / (1024.**3) nirspec_size = nrs_sz.astype(float) / (1024.**3) niriss_size = nis_sz.astype(float) / (1024.**3) miri_size = mir_sz.astype(float) / (1024.**3) fgs_size = fgs_sz.astype(float) / (1024.**3) # plot the data # Plot filecount vs. date p1 = figure( tools='pan,box_zoom,reset,wheel_zoom,save', x_axis_type='datetime', title="Total File Counts", x_axis_label='Date', y_axis_label='Count') p1.line(dates, file_count, line_width=2, line_color='blue') p1.circle(dates, file_count, color='blue') # Plot system stats vs. date p2 = figure( tools='pan,box_zoom,wheel_zoom,reset,save', x_axis_type='datetime', title='System stats', x_axis_label='Date', y_axis_label='GB') p2.line(dates, systemsize, legend='Total size', line_color='red') p2.circle(dates, systemsize, color='red') p2.line(dates, freesize, legend='Free bytes', line_color='blue') p2.circle(dates, freesize, color='blue') p2.line(dates, usedsize, legend='Used bytes', line_color='green') p2.circle(dates, usedsize, color='green') # Plot fits files by type vs. date p3 = figure( tools='pan,box_zoom,wheel_zoom,reset,save', x_axis_type='datetime', title="Total File Counts by Type", x_axis_label='Date', y_axis_label='Count') p3.line(dates, fits, legend='Total fits files', line_color='black') p3.circle(dates, fits, color='black') p3.line(dates, uncal, legend='uncalibrated fits files', line_color='red') p3.diamond(dates, uncal, color='red') p3.line(dates, cal, legend='calibrated fits files', line_color='blue') p3.square(date, cal, color='blue') p3.line(dates, rate, legend='rate fits files', line_color='green') p3.triangle(dates, rate, color='green') p3.line(dates, rateints, legend='rateints fits files', line_color='orange') p3.asterisk(dates, rateints, color='orange') p3.line(dates, i2d, legend='i2d fits files', line_color='purple') p3.x(dates, i2d, color='purple') p3.line(dates, nircam, legend='nircam fits files', line_color='midnightblue') p3.x(dates, nircam, color='midnightblue') p3.line(dates, nirspec, legend='nirspec fits files', line_color='springgreen') p3.x(dates, nirspec, color='springgreen') p3.line(dates, niriss, legend='niriss fits files', line_color='darkcyan') p3.x(dates, niriss, color='darkcyan') p3.line(dates, miri, legend='miri fits files', line_color='dodgerblue') p3.x(dates, miri, color='dodgerblue') p3.line(dates, fgs, legend='fgs fits files', line_color='darkred') p3.x(dates, fgs, color='darkred') # plot size of total fits files by type p4 = figure( tools='pan,box_zoom,wheel_zoom,reset,save', x_axis_type='datetime', title="Total File Sizes by Type", x_axis_label='Date', y_axis_label='GB') p4.line(dates, fits_size, legend='Total fits files', line_color='black') p4.circle(dates, fits_size, color='black') p4.line(dates, uncal_size, legend='uncalibrated fits files', line_color='red') p4.diamond(dates, uncal_size, color='red') p4.line(dates, cal_size, legend='calibrated fits files', line_color='blue') p4.square(date, cal_size, color='blue') p4.line(dates, rate_size, legend='rate fits files', line_color='green') p4.triangle(dates, rate_size, color='green') p4.line(dates, rateints_size, legend='rateints fits files', line_color='orange') p4.asterisk(dates, rateints_size, color='orange') p4.line(dates, i2d_size, legend='i2d fits files', line_color='purple') p4.x(dates, i2d_size, color='purple') p4.line(dates, nircam_size, legend='nircam fits files', line_color='midnightblue') p4.x(dates, nircam_size, color='midnightblue') p4.line(dates, nirspec_size, legend='nirspec fits files', line_color='springgreen') p4.x(dates, nirspec_size, color='springgreen') p4.line(dates, niriss_size, legend='niriss fits files', line_color='darkcyan') p4.x(dates, niriss_size, color='darkcyan') p4.line(dates, miri_size, legend='miri fits files', line_color='dodgerblue') p4.x(dates, miri_size, color='dodgerblue') p4.line(dates, fgs_size, legend='fgs fits files', line_color='darkred') p4.x(dates, fgs_size, color='darkred') # create a layout with a grid pattern to save all plots grid = gridplot([[p1, p2], [p3, p4]]) outfile = os.path.join(outputs_dir, "filesystem_monitor.html") output_file(outfile) save(grid) set_permissions(outfile) logging.info('Saved plot of all statistics to {}'.format(outfile)) # Save each plot's components plots = [p1, p2, p3, p4] plot_names = ['filecount', 'system_stats', 'filecount_type', 'size_type'] for plot, name in zip(plots, plot_names): plot.sizing_mode = 'stretch_both' script, div = components(plot) div_outfile = os.path.join(outputs_dir, "{}_component.html".format(name)) with open(div_outfile, 'w') as f: f.write(div) f.close() set_permissions(div_outfile) script_outfile = os.path.join(outputs_dir, "{}_component.js".format(name)) with open(script_outfile, 'w') as f: f.write(script) f.close() set_permissions(script_outfile) logging.info('Saved components files: {}_component.html and {}_component.js'.format(name, name)) logging.info('Filesystem statistics plotting complete.') # Begin logging: logging.info("Completed.")
df = pd.DataFrame.from_dict(response_json['Time Series (Daily)'], orient='index').sort_index(axis=1) df = df.rename( columns={ '1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. adjusted close': 'Adj Close', '6. volume': 'Volume', '7. dividend amount': 'Dividend Amount', '8. split coefficient': 'Split Coefficient' }) df.reset_index(inplace=True) df['index'] = pd.to_datetime(df['index']) mask = (df['index'] >= '2020-01-01') df = df.loc[mask] df1 = df[['index', 'Open', 'Close', 'Adj Close']] p = figure(plot_width=800, plot_height=800, x_axis_type="datetime") p.background_fill_color = "#f5f5f5" p.grid.grid_line_color = "white" p.title.text = 'Monthly Stock Data of %s % APPL.upper' p.xaxis.axis_label = "Date and Month of 2020" p.yaxis.axis_label = "Price" p.axis.axis_line_color = None p.title.text_font = "Times" p.title.text_font_size = "20px" components(p)
def bokeh(): co2_emissions_all = pd.read_csv( 'Total_carbon_dioxide_emissions_from_all_sectors_all_fuels_United_States.csv', index_col=False) co2_emissions_all.index = (pd.date_range(start='1980-01-01', periods=len(co2_emissions_all), freq='A'))[::-1] co2_emissions_all.sort_index(inplace=True) co2_emissions_all.drop(['Year'], axis=1, inplace=True) co2_emissions_all.index.name = 'Year' co2_emissions_all = co2_emissions_all[ co2_emissions_all.index > '1990-01-01'] co2_emissions_all = co2_emissions_all.rename(columns={ 'Series ID: EMISS.CO2-TOTV-TT-TO-US.A million metric tons CO2': 'co2_all' }) #(co2_emissions_all['co2_all']/co2_emissions_all['co2_all'][0]).plot(); co2_emissions_ng = pd.read_csv( 'Total_carbon_dioxide_emissions_from_all_sectors_natural_gas_United_States.csv', index_col=False) co2_emissions_ng.index = (pd.date_range(start='1980-01-01', periods=len(co2_emissions_ng), freq='A'))[::-1] co2_emissions_ng.sort_index(inplace=True) co2_emissions_ng.drop(['Year'], axis=1, inplace=True) co2_emissions_ng.index.name = 'Year' co2_emissions_ng = co2_emissions_ng[co2_emissions_ng.index > '1990-01-01'] co2_emissions_ng = co2_emissions_ng.rename(columns={ 'Series ID: EMISS.CO2-TOTV-TT-NG-US.A million metric tons CO2': 'co2_Ngas' }) #(co2_emissions_ng['co2_Ngas']/co2_emissions_ng['co2_Ngas'][0]).plot(); co2_emissions_oil = pd.read_csv( 'Total_carbon_dioxide_emissions_from_all_sectors_petroleum_United_States.csv', index_col=False) co2_emissions_oil.index = (pd.date_range(start='1980-01-01', periods=len(co2_emissions_oil), freq='A'))[::-1] co2_emissions_oil.sort_index(inplace=True) co2_emissions_oil.drop(['Year'], axis=1, inplace=True) co2_emissions_oil.index.name = 'Year' co2_emissions_oil = co2_emissions_oil[ co2_emissions_oil.index > '1990-01-01'] co2_emissions_oil = co2_emissions_oil.rename(columns={ 'Series ID: EMISS.CO2-TOTV-TT-PE-US.A million metric tons CO2': 'co2_oil' }) #(co2_emissions_oil['co2_oil']/co2_emissions_oil['co2_oil'][0]).plot(); plot_8_data=pd.concat([co2_emissions_oil.co2_oil/co2_emissions_oil.co2_oil[0],co2_emissions_ng.co2_Ngas/co2_emissions_ng.co2_Ngas[0],\ co2_emissions_all.co2_all/co2_emissions_all.co2_all[0],],axis=1)#.plot(figsize=(16,9)) plot_8_data.index.name = 'Month' source = ColumnDataSource(plot_8_data) p = figure(title='normalized co2_emissions', plot_width=600, plot_height=600, x_axis_type="datetime") p.xaxis.axis_label = "Months**" p.yaxis.axis_label = "petroleum_co2+gas_co2+all_co2" p.line(x='Month', y='co2_oil', line_width=2, source=source, color="black", legend='petroleum_co2_emis+') p.line(x='Month', y='co2_Ngas', line_width=2, source=source, color="green", legend='gas_co2_emis+') p.line(x='Month', y='co2_all', line_width=2, source=source, color="navy", legend='all_co2_emis+') # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() # render template script, div = components(p) html = render_template( 'index.html', plot_script=script, plot_div=div, js_resources=js_resources, css_resources=css_resources, ) return encode_utf8(html)
tools=TOOLS, plot_width=300, plot_height=300) p2.scatter(x2, y2, size=12, color="blue", alpha=0.5) p3 = figure(x_range=xr2, y_range=yr2, tools=TOOLS, plot_width=300, plot_height=300) p3.scatter(x3, y3, size=12, color="green", alpha=0.5) # plots can be a single Bokeh model, a list/tuple, or even a dictionary plots = {'Red': p1, 'Blue': p2, 'Green': p3} script, div = components(plots) template = Template('''<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Bokeh Scatter Plots</title> <style> div{float: left;} </style> {{ js_resources }} {{ css_resources }} {{ script }} </head> <body> {% for key in div.keys() %} {{ div[key] }} {% endfor %}
def lab_work_5_post(): x = [] y = [] form_data = request.form.to_dict() x_size = int(form_data['x']) y_size = int(form_data['y']) matrix = np.zeros((x_size-1, y_size-1), dtype=int) del form_data['x'] del form_data['y'] for k, v in form_data.items(): i, j = list(map(int, k.split('x'))) if v == '': continue if i == 0 and j != 0: y.append(to_int_if_can(v)) elif i != 0 and j == 0: x.append(to_int_if_can(v)) else: matrix[i-1][j-1] = to_int_if_can(v) sum_rows = [to_int_if_can(sum(matrix[i])) for i in range(x_size-1)] sum_cols = [to_int_if_can(sum(matrix[:, i])) for i in range(y_size-1)] sum_n = sum(sum_rows) averages_y_for_x = [] averages_x_for_y = [] res = 0 for i in range(0, x_size-1): for index, v in enumerate(matrix[i]): if v == 0: continue res += v * y[index] averages_y_for_x.append( to_int_if_can(round(res/sum_rows[i], 3)) ) res = 0 for j in range(0, y_size-1): for index, v in enumerate(matrix[:, j]): if v == 0: continue res += v * x[index] averages_x_for_y.append( to_int_if_can(round(res/sum_cols[j], 3)) ) res = 0 selective_averages_x = get_selective_averages(x, sum_rows, sum_n) selective_averages_x_2 = get_selective_averages(x, sum_rows, sum_n, 2) sigma_x = math.sqrt( selective_averages_x_2 - math.pow(selective_averages_x, 2) ) selective_averages_y = get_selective_averages(y, sum_cols, sum_n) selective_averages_y_2 = get_selective_averages(y, sum_cols, sum_n, 2) sigma_y = math.sqrt( selective_averages_y_2 - math.pow(selective_averages_y, 2) ) sum_nxy_x_y = 0 for i in range(0, x_size-1): for index, v in enumerate(matrix[i]): if v == 0: continue sum_nxy_x_y += v * y[index] * x[i] a = sum_nxy_x_y - sum_n * selective_averages_x * selective_averages_y b = sum_n * sigma_x * sigma_y selective_correlation_coefficient = a/b f_x_1 = selective_correlation_coefficient * sigma_y / sigma_x f_x_2 = selective_averages_y + ((selective_averages_x * -1) * f_x_1) distr_conditional_averages_x = [] distr_conditional_averages_y = [] for e in x: distr_conditional_averages_x.append({ 'x': e, 'y': round(f_x_1*e+f_x_2, 3) }) # for e in y: # distr_conditional_averages_y.append({ # 'y': e, # 'x': round((f_x_2+e)/(f_x_1*-1), 3) # }) js_resources = INLINE.render_js() css_resources = INLINE.render_css() fig = get_reg_line_figure( x, averages_y_for_x, [e['x'] for e in distr_conditional_averages_x], [e['y'] for e in distr_conditional_averages_x], 'x', 'y' ) plot_1 = {} plot_1['script'], plot_1['div'] = components(fig) # fig2 = get_figure(y, averages_x_for_y, 'x', 'y') # plot_2 = {} # plot_2['script'], plot_2['div'] = components(fig2) html = render_template( 'lab_work_5/lab_work_5_res.html', action_url='/lab_work_5', js_resources=js_resources, css_resources=css_resources, plot_1=plot_1, x=x, y=y, matrix=matrix, sum_n=sum_n, sum_cols=sum_cols, sum_rows=sum_rows, averages_y_for_x=averages_y_for_x, averages_x_for_y=averages_x_for_y, selective_averages_x=selective_averages_x, selective_averages_y=selective_averages_y, sigma_x=round(sigma_x, 3), sigma_y=round(sigma_y, 3), selective_correlation_coefficient=round( selective_correlation_coefficient, 3 ), f_x_1=round(f_x_1, 3), f_x_2=round(f_x_2, 3), distr_conditional_averages_x=distr_conditional_averages_x, distr_conditional_averages_y=distr_conditional_averages_y ) return encode_utf8(html)
def __plot_bubble_plot(self, data, params, input_values): output_file("bubble_plot.html") curdoc().theme = 'dark_minimal' TOOLS = "hover,crosshair,pan,wheel_zoom,zoom_in,zoom_out,box_zoom,undo,tap,box_select,poly_select,reset,save" if input_values: df = data else: df = data[0:0] p = figure(x_range=df['Name'], plot_height=450, title="Heatmap", tools=TOOLS, toolbar_location="below") if input_values: if not "All" in input_values[self.resposible_unit]: if len(input_values[self.resposible_unit]) <= 1: df = df[df['Verantwortliche Organisationseinheit'] == input_values[self.resposible_unit][0]] else: def remove_bu(x): for input_ in input_values[self.resposible_unit]: if not input_ in x and not x in input_values[ self.resposible_unit]: return 'REMOVE' else: return x df['Verantwortliche Organisationseinheit'] = df[ 'Verantwortliche Organisationseinheit'].apply( lambda x: remove_bu(x)) df = df[ df['Verantwortliche Organisationseinheit'] != 'REMOVE'] if not "All" in input_values[self.supported_bc]: def remove_bc(x): for input_ in input_values[self.supported_bc]: if not input_ in x and not x in input_values[ self.supported_bc]: return 'REMOVE' else: return x df = df[df['Unterstützte Geschäftsfähigkeiten'].notna()] df['Unterstützte Geschäftsfähigkeiten'] = df[ 'Unterstützte Geschäftsfähigkeiten'].apply( lambda x: remove_bc(x)) df = df[df['Unterstützte Geschäftsfähigkeiten'] != 'REMOVE'] # Sort after bubble size if input_values[self.sorting] == 'Ascending': df.sort_values(by=input_values[self.bubble_size_axis], inplace=True) else: df.sort_values(by=input_values[self.bubble_size_axis], inplace=True, ascending=False) if not input_values[self.top] == 'All': df = df.head(int(input_values[self.top])) colors = np.array([[ r, g, 150 ] for r, g in zip(50 + 2 * df[input_values[self.bubble_size_axis]], 30 + 2 * df[input_values[self.bubble_y_axis]])], dtype="uint8") p = figure(x_range=df['Name'], plot_height=500, title="Heatmap Analysis", tools=TOOLS, toolbar_location="below", y_axis_label=input_values[self.bubble_y_axis], x_axis_label=input_values[self.bubble_size_axis], x_minor_ticks=-10) if input_values[self.bubble_size_axis] == 'Anzahl Nutzer': df[input_values[self.bubble_size_axis]] = df[input_values[ self.bubble_size_axis]].apply(lambda x: x + 500 if x <= 500 and x > 0 else x) df_size = df[input_values[self.bubble_size_axis]] / 50 elif input_values[ self. bubble_size_axis] == 'Anzahl unterstützter Geschäftsfähigkeiten': df_size = df[input_values[self.bubble_size_axis]] * 4 else: df_size = df[input_values[self.bubble_size_axis]] * 4 p.scatter(x=df['Name'], y=df[input_values[self.bubble_y_axis]], fill_alpha=0.6, size=df_size, fill_color=colors, line_color=None) layout = column(p, sizing_mode="stretch_both") script, div = components(layout, wrap_script=False, theme='dark_minimal') return {'div': div, 'script': script} layout = column(p, sizing_mode="stretch_both") script, div = components(p, wrap_script=False, theme='dark_minimal') return {'div': div, 'script': script}
def tplot(name, var_label = None, auto_color=True, interactive=False, combine_axes=True, nb=False, save_file=None, gui=False, qt=True, pyqtgraph=False): """ This is the function used to display the tplot variables stored in memory. The default output is to show the plots stacked on top of one another inside a GUI window. The GUI window has the option to export the plots in either PNG or HTML formats. .. note:: This plotting routine uses the python Bokeh library, which creates plots using HTML and Javascript. Bokeh is technically still in beta, so future patches to Bokeh may require updates to this function. Parameters: name : str / list List of tplot variables that will be plotted var_label : str, optional The name of the tplot variable you would like as a second x axis. auto_color : bool, optional Automatically color the plot lines. interactive : bool, optional If True, a secondary interactive plot will be generated next to spectrogram plots. Mousing over the spectrogram will display a slice of data from that time on the interactive chart. combine_axis : bool, optional If True, the axes are combined so that they all display the same x range. This also enables scrolling/zooming/panning on one plot to affect all of the other plots simultaneously. nb : bool, optional If True, the plot will be displayed inside of a current Jupyter notebook session. save_file : str, optional A full file name and path. If this option is set, the plot will be automatically saved to the file name provided in an HTML format. The plots can then be opened and viewed on any browser without any requirements. gui : bool, optional If True, then this function will output the 2 HTML components of the generated plots as string variables. This is useful if you are embedded the plots in your own GUI. For more information, see http://bokeh.pydata.org/en/latest/docs/user_guide/embed.html qt : bool, optional If True, then this function will display the plot inside of the Qt window. From this window, you can choose to export the plots as either an HTML file, or as a PNG. Returns: None Examples: >>> #Plot a single line >>> import pytplot >>> x_data = [2,3,4,5,6] >>> y_data = [1,2,3,4,5] >>> pytplot.store_data("Variable1", data={'x':x_data, 'y':y_data}) >>> pytplot.tplot("Variable1") >>> #Display two plots >>> x_data = [1,2,3,4,5] >>> y_data = [[1,5],[2,4],[3,3],[4,2],[5,1]] >>> pytplot.store_data("Variable2", data={'x':x_data, 'y':y_data}) >>> pytplot.tplot(["Variable1", "Variable2"]) >>> #Display 2 plots, using Variable1 as another x axis >>> x_data = [1,2,3] >>> y_data = [ [1,2,3] , [4,5,6], [7,8,9] ] >>> v_data = [1,2,3] >>> pytplot.store_data("Variable3", data={'x':x_data, 'y':y_data, 'v':v_data}) >>> pytplot.options("Variable3", 'spec', 1) >>> pytplot.tplot(["Variable2", "Variable3"], var_label='Variable1') >>> #Plot all 3 tplot variables, sending the output to an HTML file >>> pytplot.tplot(["Variable1", "Variable2", "Variable3"], save_file='C:/temp/pytplot_example.html') >>> #Plot all 3 tplot variables, sending the HTML output to a pair of strings >>> div, component = pytplot.tplot(["Variable1", "Variable2", "Variable3"], gui=True) """ #Check a bunch of things if(not isinstance(name, list)): name=[name] num_plots = 1 else: num_plots = len(name) for i in range(num_plots): if isinstance(name[i], int): name[i] = list(pytplot.data_quants.keys())[name[i]] if name[i] not in pytplot.data_quants.keys(): print(str(i) + " is currently not in pytplot") return if isinstance(var_label, int): var_label = list(pytplot.data_quants.keys())[var_label] if pyqtgraph: layout = QtPlotter.generate_stack(name, var_label=var_label, auto_color=auto_color, combine_axes=combine_axes, mouse_moved_event=pytplot.hover_time.change_hover_time) pytplot.pytplotWindow.newlayout(layout) pytplot.pytplotWindow.resize(pytplot.tplot_opt_glob['window_size'][0], pytplot.tplot_opt_glob['window_size'][1]) pytplot.pytplotWindow.show() pytplot.pytplotWindow.activateWindow() if not (hasattr(sys, 'ps1')) or not hasattr(QtCore, 'PYQT_VERSION'): QtGui.QApplication.instance().exec_() return else: layout = HTMLPlotter.generate_stack(name, var_label=var_label, auto_color=auto_color, combine_axes=combine_axes, interactive=interactive) #Output types if gui: script, div = components(layout) return script, div elif nb: output_notebook() show(layout) return elif save_file != None: output_file(save_file, mode='inline') save(layout) return elif qt: dir_path = os.path.dirname(os.path.realpath(__file__)) output_file(os.path.join(dir_path, "temp.html"), mode='inline') save(layout) new_layout = WebView() pytplot.pytplotWindow.resize(pytplot.tplot_opt_glob['window_size'][0]+100,pytplot.tplot_opt_glob['window_size'][1]+100) new_layout.resize(pytplot.tplot_opt_glob['window_size'][0],pytplot.tplot_opt_glob['window_size'][1]) dir_path = os.path.dirname(os.path.realpath(__file__)) new_layout.setUrl(QtCore.QUrl.fromLocalFile(os.path.join(dir_path, "temp.html"))) pytplot.pytplotWindow.newlayout(new_layout) pytplot.pytplotWindow.show() pytplot.pytplotWindow.activateWindow() if not (hasattr(sys, 'ps1')) or not hasattr(QtCore, 'PYQT_VERSION'): QtGui.QApplication.instance().exec_() return else: dir_path = os.path.dirname(os.path.realpath(__file__)) output_file(os.path.join(dir_path, "temp.html"), mode='inline') show(layout) return
def deconv_summary_page(filename, cost_data, deconv_data): fig_dict = dict() cost_key = "cost_plot" fig_dict[cost_key] = cost_fig(cost_data, 300, 533, epoch_axis=True) vis_keys = dict() img_keys = dict() for layer, layer_data in deconv_data: lyr_vis_keys, lyr_img_keys, lyr_fig_dict = deconv_figs(layer, layer_data, fm_max=4) vis_keys[layer] = lyr_vis_keys img_keys[layer] = lyr_img_keys fig_dict.update(lyr_fig_dict) script, div = components(fig_dict) template = Template(''' <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>{{page_title}}</title> <style> div{float: left;} </style> <link rel="stylesheet" href="http://cdn.pydata.org/bokeh/release/bokeh-0.9.0.min.css" type="text/css" /> <script type="text/javascript" src="http://cdn.pydata.org/bokeh/release/bokeh-0.9.0.min.js"></script> {{ script }} </head> <body> <div id=cost_plot style="width:100%; padding:10px"> {{ div[cost_key]}} </div> {% for layer in sorted_layers %} <div id=Outer{{layer}} style="padding:20px"> <div id={{layer}} style="background-color: #C6FFF1; padding:10px"> Layer {{layer}}<br> {% for fm in vis_keys[layer].keys() %} <div id={{fm}} style="padding:10px"> Feature Map {{fm}}<br> {{ div[vis_keys[layer][fm]] }} {{ div[img_keys[layer][fm]] }} </div> {% endfor %} </div> </div> <br><br> {% endfor %} </body> </html> ''') with open(filename, 'w') as htmlfile: htmlfile.write( template.render(page_title="Deconv Visualization", script=script, div=div, cost_key=cost_key, vis_keys=vis_keys, img_keys=img_keys, sorted_layers=sorted(vis_keys)))
def figure_as_html(builds_data, nodes=None, title=None, with_labels=False): ''' For every build in builds_data draw rectangle with data: :center_x: = left(=timestamp) + width/2 :center y: = builtOn :width: = duration :height: = 100 :color: = result_to_color(result) :label: = fullDisplayName ''' # Data center_x = [] center_y = [] width = [] left = [] height = 0.95 label = [] color = [] max_time = datetime_to_timestamp(datetime.datetime.now()) for build_data in builds_data: logger.debug("Processing build %s" % build_data) build_left = build_data['timestamp'] if build_data['duration'] == 0 and not build_data['result']: logger.debug("Build %s is still running" % build_data['fullDisplayName']) duration = max_time - build_data['timestamp'] build_width = duration else: build_width = build_data['duration'] build_label = build_data['fullDisplayName'] build_color = get_build_color(build_data) left.append(build_left) width.append(build_width) label.append(build_label) center_x.append(build_left + build_width/2) center_y.append(build_data['builtOn']) color.append(build_color) source = ColumnDataSource( data=dict( x=center_x, y=center_y, width=width, left=left, color=color, label=label, ) ) if not nodes: nodes = sorted(list(set(center_y))) # Plot properties TOOLS="pan,xwheel_zoom,box_zoom,reset,previewsave,hover,resize" p = figure(x_axis_type="datetime", y_range=nodes, width=900, tools=TOOLS, title=title, toolbar_location="below", height=max(min(len(nodes)* 40, 800),100), ) hover = p.select(dict(type=HoverTool)) hover.tooltips = '<font color="@color">•</font> @label' # Draw data p.rect('x', 'y', 'width', height, color='color', source=source, line_width=1, line_color='white', line_alpha=0.4, fill_alpha=0.4, ) if with_labels: # Add labels layer p.text('left', 'y', 'label', source=source, angle=0.3, name="Labels", text_font_size='6pt', text_baseline='middle', text_alpha=0.8, x_offset=map(lambda x: x % 3 * 20, xrange(len(label))), y_offset=map(lambda y: y % 2 * 10 -5 , xrange(len(label))), ) # To HTML script, div = components(p) return script + div
def plotGraph(df, color): source = ColumnDataSource(df) hover = HoverTool(tooltips=[("Total Cases", "@totalconfirmed{0,0}"), ("Date", '@date{%d-%b}')], formatters={"@date": "datetime"}) p = figure(x_axis_type='datetime', y_range=(int(df.iloc[0]['totalconfirmed']) - 100, int(df.iloc[-1]['totalconfirmed']) + 10000), tools=[hover], plot_width=800, plot_height=400, x_axis_label='Date', y_axis_label='No. of cases', sizing_mode='scale_width') p.line(x='date', y='totalconfirmed', source=source, color=color, line_width=3, line_alpha=0.2) renderer = p.circle(x='date', y='totalconfirmed', source=source, color=color, size=5, legend_label="Total confirmed cases") p.axis.axis_label_text_font_style = 'normal' p.axis.axis_label_text_color = color #x_axis attributes p.xaxis.formatter = DatetimeTickFormatter(months=['%d-%b']) p.xaxis.axis_line_color = color p.xaxis.major_label_text_color = color p.xaxis.axis_line_width = 1 p.xgrid.grid_line_color = None #y_axis attributes p.yaxis.formatter = NumeralTickFormatter(format="0 a") p.yaxis.axis_line_color = color p.yaxis.major_label_text_color = color p.yaxis.axis_line_width = 1 p.ygrid.grid_line_color = color p.ygrid.grid_line_alpha = 0.1 #ticks styling p.axis.minor_tick_line_color = None #p.axis.minor_tick_line_width = 2 p.axis.major_tick_line_color = color #Plot background attribute p.background_fill_color = color p.background_fill_alpha = 0.1 #Legend p.legend.location = "top_left" p.legend.label_text_color = color p.legend.background_fill_alpha = 0 p.legend.border_line_color = color return components(p)
def sentiment(): #ENGLISH SENTIMENT BAR CHART en_tweet_df = pd.DataFrame(retrieve_sentiment_data_en()) en_tweet_df['sentiment'] = en_tweet_df['sentiment'].values.astype(float) positive = len([ element for element in (list(en_tweet_df['sentiment'])) if element > 0 and element < 999 ]) negative = len([ element for element in (list(en_tweet_df['sentiment'])) if element < 0 ]) neutral = len([ element for element in (list(en_tweet_df['sentiment'])) if element == 0 ]) none = len([ element for element in (list(en_tweet_df['sentiment'])) if element == 999 ]) sentiment = ['Positive', 'Negative', 'Neutral', 'None'] counts = [positive, negative, neutral, none] colors = ["#82E0AA", "#e84d60", "#ccccff", "#FCA15A"] source = ColumnDataSource( data=dict(sentiment=sentiment, counts=counts, color=colors)) en_bar = figure(x_range=sentiment, plot_height=400, title="English Sentiment Counts", toolbar_location=None, tools="hover", tooltips="@sentiment @counts") en_bar.vbar(x='sentiment', top='counts', width=0.9, color='color', legend_field="sentiment", source=source) en_bar.add_layout(en_bar.legend[0], 'above') en_bar.xgrid.grid_line_color = None en_bar.y_range.start = 0 en_bar.legend.orientation = "horizontal" en_bar.legend.location = "top_right" en_script, en_div = components(en_bar) #MALAY SENTIMENT BAR CHART ms_tweet_df = pd.DataFrame(retrieve_sentiment_data_ms()) ms_tweet_df['sentiment'] = ms_tweet_df['sentiment'].values.astype(int) positive = len([ element for element in (list(ms_tweet_df['sentiment'])) if element > 0 and element < 999 ]) negative = len([ element for element in (list(ms_tweet_df['sentiment'])) if element < 0 ]) neutral = len([ element for element in (list(ms_tweet_df['sentiment'])) if element == 0 ]) none = len([ element for element in (list(ms_tweet_df['sentiment'])) if element == 999 ]) sentiment = ['Positive', 'Negative', 'Neutral', 'None'] counts = [positive, negative, neutral, none] colors = ["#82E0AA", "#e84d60", "#ccccff", "#FCA15A"] source = ColumnDataSource( data=dict(sentiment=sentiment, counts=counts, color=colors)) ms_bar = figure(x_range=sentiment, plot_height=400, title="Malay Sentiment Counts", toolbar_location=None, tools="hover", tooltips="@sentiment @counts") ms_bar.vbar(x='sentiment', top='counts', width=0.9, color='color', legend_field="sentiment", source=source) ms_bar.add_layout(ms_bar.legend[0], 'above') ms_bar.xgrid.grid_line_color = None ms_bar.y_range.start = 0 ms_bar.legend.orientation = "horizontal" ms_bar.legend.location = "top_right" ms_script, ms_div = components(ms_bar) #CHINESE BAR CHART zh_tweet_df = pd.DataFrame(retrieve_sentiment_data_zh()) zh_tweet_df['sentiment'] = zh_tweet_df['sentiment'].values.astype(int) positive = len([ element for element in (list(zh_tweet_df['sentiment'])) if element > 0 and element < 999 ]) negative = len([ element for element in (list(zh_tweet_df['sentiment'])) if element < 0 ]) neutral = len([ element for element in (list(zh_tweet_df['sentiment'])) if element == 0 ]) none = len([ element for element in (list(zh_tweet_df['sentiment'])) if element == 999 ]) sentiment = ['Positive', 'Negative', 'Neutral', 'None'] counts = [positive, negative, neutral, none] colors = ["#82E0AA", "#e84d60", "#ccccff", "#FCA15A"] source = ColumnDataSource( data=dict(sentiment=sentiment, counts=counts, color=colors)) zh_bar = figure(x_range=sentiment, plot_height=400, toolbar_location=None, tools="hover", tooltips="@sentiment @counts") zh_bar.vbar(x='sentiment', top='counts', width=0.9, color='color', legend_field="sentiment", source=source) zh_bar.add_layout(zh_bar.legend[0], 'above') zh_bar.xgrid.grid_line_color = None zh_bar.y_range.start = 0 zh_bar.legend.orientation = "horizontal" zh_bar.legend.location = "top_right" zh_bar.add_layout( Title(text="Chinese Sentiment Counts", text_font_size="10pt"), 'above') zh_script, zh_div = components(zh_bar) return render_template( 'sentiment.html', enbar_script=en_script, enbar_div=en_div, msbar_script=ms_script, msbar_div=ms_div, zhbar_script=zh_script, zhbar_div=zh_div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def createMap(): #pd.options.display.float_format = '{:,.0f}'.format shapefile = '/Users/Aditya/Desktop/project/geoData/ne_110m_admin_0_countries.shp' datafile = '/Users/Aditya/Desktop/project/output.csv' #Read shapefile using Geopandas gdf = gpd.read_file(shapefile)[['ADMIN', 'ADM0_A3', 'geometry']] #Rename columns. gdf.columns = ['country', 'country_code', 'geometry'] #Drop row corresponding to 'Antarctica' #gdf.at[43, 'country'] = "French Guiana" #drop antartica, takes too much space gdf = gdf.drop(gdf.index[159]) #Read csv file using pandas df = pd.read_csv(datafile, names=[ 'Country', 'Deaths', 'Deaths /1M pop', 'New Deaths', 'Tests', 'Confirmed Cases', 'Confirmed Case/Fatality Rate', 'Confirmed Cases/1M pop', 'EstimatedCases', 'Seasonal Flu Deaths1(CDC/WHO 2017)' ], skiprows=2) merged = gdf.merge(df, left_on='country', right_on='Country', how='left') merged['Deaths /1M pop'].fillna("No Data", inplace=True) merged.round({'Deaths /1M pop': 2}) #print(merged.head(merged['Deaths /1M pop'])) #Read data to json. merged_json = json.loads(merged.to_json()) #Convert to String like object. json_data = json.dumps(merged_json) #Input GeoJSON source that contains features for plotting. geosource = GeoJSONDataSource(geojson=json_data) #Define a sequential multi-hue color palette. palette = brewer['YlOrRd'][7] #Reverse color order so that dark blue is highest obesity. palette = palette[::-1] #Instantiate LinearColorMapper that linearly maps numbers in a range, into a sequence of colors. color_mapper = LinearColorMapper(palette=palette, low=0, high=350, nan_color='#d9d9d9') #Define custom tick labels for color bar. tick_labels = { '0': '0', '50': '50', '100': '100', '150': '150', '200': '200', '250': '250', '300': '300', '350': '>350' } #Add hover tool TOOLTIPS = """ <div> <div> <span style="font-size: 14px; color: black;"> Country: @country </span> </div> <div> <span style="font-size: 14px; color: black;"> Deaths /1M: @{Deaths /1M pop}{0.00 a} </span> </div> </div> """ hover = HoverTool(tooltips=TOOLTIPS) #Create color bar. color_bar = ColorBar(color_mapper=color_mapper, label_standoff=9, width=20, height=555, border_line_color=None, location=(0, -10), orientation='vertical', major_label_overrides=tick_labels, background_fill_color="#1e1e2f", major_label_text_color="white") #Create figure object. p = figure(title='Deaths per Million', plot_height=610, plot_width=1100, toolbar_location=None, tools=[hover]) p.title.text_color = "white" p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.background_fill_color = "#3690c0" p.border_fill_color = "#1e1e2f" p.axis.visible = False today = date.today() d2 = today.strftime("%B %d, %Y") title = "Data is from https://www.realclearpolitics.com/coronavirus. Last updated %s" % d2 p.add_layout(Title(text=title, align="left", text_color="white"), "below") #Add patch renderer to figure. p.patches('xs', 'ys', source=geosource, fill_color={ 'field': 'Deaths /1M pop', 'transform': color_mapper }, line_color='black', line_width=0.25, fill_alpha=1) #Specify figure layout. p.add_layout(color_bar, 'right') script, div = components(p) #Display figure. save(p) return p
def plot(): if request.method == 'POST': symbol = request.form['symbol'] # Get Stock DataFrame # msft = yf.Ticker("MSFT") msft = yf.Ticker(symbol) hist = msft.history(period='max') # Define constants W_PLOT = 1000 H_PLOT = 400 TOOLS = 'pan,wheel_zoom,hover,reset' VBAR_WIDTH = 0.2 RED = Category20[7][6] GREEN = Category20[5][4] BLUE = Category20[3][0] BLUE_LIGHT = Category20[3][1] ORANGE = Category20[3][2] PURPLE = Category20[9][8] BROWN = Category20[11][10] def get_symbol_df(symbol=None): df = pd.DataFrame(hist)[-50:] df.reset_index(inplace=True) df["Date"] = pd.to_datetime(df["Date"]) return df def plot_stock_price(stock): p = figure(plot_width=W_PLOT, plot_height=H_PLOT, tools=TOOLS, title="Stock price", toolbar_location='above') inc = stock.data['Close'] > stock.data['Open'] dec = stock.data['Open'] > stock.data['Close'] view_inc = CDSView(source=stock, filters=[BooleanFilter(inc)]) view_dec = CDSView(source=stock, filters=[BooleanFilter(dec)]) # map dataframe indices to date strings and use as label overrides p.xaxis.major_label_overrides = { i + int(stock.data['index'][0]): date.strftime('%b %d') for i, date in enumerate(pd.to_datetime(stock.data["Date"])) } p.xaxis.bounds = (stock.data['index'][0], stock.data['index'][-1]) p.segment(x0='index', x1='index', y0='Low', y1='High', color=RED, source=stock, view=view_inc) p.segment(x0='index', x1='index', y0='Low', y1='High', color=GREEN, source=stock, view=view_dec) p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=BLUE, line_color=BLUE, source=stock, view=view_inc, name="price") p.vbar(x='index', width=VBAR_WIDTH, top='Open', bottom='Close', fill_color=RED, line_color=RED, source=stock, view=view_dec, name="price") p.legend.location = "top_left" p.legend.border_line_alpha = 0 p.legend.background_fill_alpha = 0 p.legend.click_policy = "mute" p.yaxis.formatter = NumeralTickFormatter(format='$ 0,0[.]000') p.x_range.range_padding = 0.05 p.xaxis.ticker.desired_num_ticks = 40 p.xaxis.major_label_orientation = 3.14 / 4 # Select specific tool for the plot price_hover = p.select(dict(type=HoverTool)) # Choose, which glyphs are active by glyph name price_hover.names = ["price"] # Creating tooltips price_hover.tooltips = [("Datetime", "@Date{%Y-%m-%d}"), ("Open", "@Open{$0,0.00}"), ("Close", "@Close{$0,0.00}"), ("Volume", "@Volume{($ 0.00 a)}")] price_hover.formatters = {"Date": 'datetime'} return p stock = ColumnDataSource( data=dict(Date=[], Open=[], Close=[], High=[], Low=[], index=[])) #stock = AjaxDataSource(data=dict(Date=[], Open=[], Close=[], High=[], Low=[],index=[]),data_url='http://127.0.0.1:5000/plot',polling_interval=1000,mode='append') #symbol = 'msft' df = get_symbol_df(symbol) stock.data = stock.from_df(df) elements = list() # update_plot() p_stock = plot_stock_price(stock) elements.append(p_stock) curdoc().add_root(column(elements)) curdoc().title = 'Bokeh stocks historical prices' #show(p_stock) script, div = components(p_stock) kwargs = {'script': script, 'div': div} #kwargs['title'] = 'bokeh-with-flask' return render_template('index.html', **kwargs) #return redirect(url_for('index', **kwargs)) #return kwargs #return json.dumps(json_item(p_stock,"myplot")) #return redirect(url_for('index')) return "OK"
def report(): if not request.args.get("tweets") and not request.args.get( "retweets") and not request.args.get("replies"): return redirect( url_for( 'index', error= "Error: At least one \"include\" option need to be checked.")) nickname = request.args.get('nickname') try: user = api.GetUser(screen_name=nickname) except TwitterError: return redirect(url_for('index', error="Error: User not found.")) timeline = api.GetUserTimeline( screen_name=nickname, count=200, trim_user=True, include_rts=request.args.get("retweets"), exclude_replies=not request.args.get("replies")) if not request.args.get("tweets"): timeline = Tools.cut_tweets(timeline) if request.args.get("links"): old_timeline = timeline.copy() i = 0 for tweet in old_timeline: timeline[i].full_text = Tools.clean_links(tweet.full_text) if timeline[i].full_text == "": del timeline[i] else: i += 1 if len(timeline) < 3: return redirect( url_for('index', error="Error: Too few tweets with given criteria found.")) replies_script, replies_div = components(create_replies_graph(timeline)) favorites_script, favorites_div = components( create_favorites_graph(timeline, 10)) posts_in_days_script, posts_in_days_div = components( create_posts_in_days_graph(timeline)) length_script, length_div = components(create_length_graph(timeline, 10)) posts_in_hours_script, posts_in_hours_div = components( create_posts_in_hours_graph(timeline)) tfidf_script, tfidf_div = components(create_tfidf_graph(timeline)) wordcloud(timeline, user.id_str) # grab the static resources js_resources = INLINE.render_js() css_resources = INLINE.render_css() avatar_url = "https://twitter.com/" + nickname + "/profile_image?size=bigger" r = requests.get(avatar_url) avatar_path = "static/temp/avatar" + user.id_str + ".png" with open(avatar_path, 'wb') as f: f.write(r.content) try: following_tooltip = len(api.GetFriendIDs(user.id)) following = Tools.human_format(following_tooltip) except TwitterError: following = "???" following_tooltip = "Reached api limit" tweets = user.statuses_count followers = user.followers_count likes = user.favourites_count posts = len(timeline) date_last = Tools.human_format_date(timeline[0].created_at) date_first = Tools.human_format_date(timeline[len(timeline) - 1].created_at) # render template html = render_template( "report.html", nickname=nickname, tweets=Tools.human_format(tweets), following=following, followers=Tools.human_format(followers), likes=Tools.human_format(likes), tweets_tooltip=tweets, following_tooltip=following_tooltip, followers_tooltip=followers, likes_tooltip=likes, replies_script=replies_script, replies_div=replies_div, favorites_script=favorites_script, favorites_div=favorites_div, posts_in_days_script=posts_in_days_script, posts_in_days_div=posts_in_days_div, posts_in_hours_script=posts_in_hours_script, posts_in_hours_div=posts_in_hours_div, js_resources=js_resources, css_resources=css_resources, length_script=length_script, length_div=length_div, tfidf_script=tfidf_script, tfidf_div=tfidf_div, pid="temp/WC" + user.id_str + ".png", avatar="temp/avatar" + user.id_str + ".png", tweets_checked=request.args.get("tweets"), replies_checked=request.args.get("replies"), retweets_checked=request.args.get("retweets"), links_checked=request.args.get("links"), posts=Tools.human_format(posts), date_interval=date_first + " - " + date_last, ) return encode_utf8(html)
def home(): models = [] set_brands = get_brands() full_site_items_list, set_site_items = get_site_items_list() get_site_items = request.form.get('site_item') saving_name = request.form.get('input_name') if get_site_items is None: get_brand_1, get_model_1 = request.form.get( 'dropdown-brand_1'), request.form.get('dropdown-model_1') set_models_1 = get_models(get_brand_1) if get_model_1 not in set_models_1: get_model_1 = None get_brand_2, get_model_2 = request.form.get( 'dropdown-brand_2'), request.form.get('dropdown-model_2') set_models_2 = get_models(get_brand_2) if get_model_2 not in set_models_2: get_model_2 = None get_brand_3, get_model_3 = request.form.get( 'dropdown-brand_3'), request.form.get('dropdown-model_3') set_models_3 = get_models(get_brand_3) if get_model_3 not in set_models_3: get_model_3 = None get_brand_4, get_model_4 = request.form.get( 'dropdown-brand_4'), request.form.get('dropdown-model_4') set_models_4 = get_models(get_brand_4) if get_model_4 not in set_models_4: get_model_4 = None else: site_data = get_models_from_site_list(get_site_items) get_brand_1, get_model_1 = site_data[0][0], site_data[0][1] set_models_1 = get_models(get_brand_1) if get_model_1 not in set_models_1: get_model_1 = None get_brand_2, get_model_2 = site_data[1][0], site_data[1][1] set_models_2 = get_models(get_brand_2) if get_model_2 not in set_models_2: get_model_2 = None get_brand_3, get_model_3 = site_data[2][0], site_data[2][1] set_models_3 = get_models(get_brand_3) if get_model_3 not in set_models_3: get_model_3 = None get_brand_4, get_model_4 = site_data[3][0], site_data[3][1] set_models_4 = get_models(get_brand_4) if get_model_4 not in set_models_4: get_model_4 = None models_set = [get_model_1, get_model_2, get_model_3, get_model_4] if models_set != [None, None, None, None]: save_to_temp(models_set) else: if saving_name != None and saving_name != 'temp': profiles = get_temp_profile() profiles.insert(0, saving_name) add_new_site_item(profiles) else: pass models.extend((get_model_1, get_model_2, get_model_3, get_model_4)) models = clear_models_list(models) if models: records = get_records(models) date_series, models_prices, models = convert_to_pandas(records, models) script_price_graph, div_price_graph = components( create_graph(date_series, models_prices, models)) else: script_price_graph, div_price_graph = components( create_graph([1], [1], [','])) return render_template('home.html', set_brands=set_brands, get_brand_1=get_brand_1, get_model_1=get_model_1, set_models_1=set_models_1, get_brand_2=get_brand_2, get_model_2=get_model_2, set_models_2=set_models_2, get_brand_3=get_brand_3, get_model_3=get_model_3, set_models_3=set_models_3, get_brand_4=get_brand_4, get_model_4=get_model_4, set_models_4=set_models_4, div_price_graph=div_price_graph, script_price_graph=script_price_graph, get_site_items=get_site_items, set_site_items=set_site_items)
p = figure(plot_width=1000, plot_height=300, x_axis_type="datetime", title="Finance candlestick", sizing_mode="scale_width") #p.grid.grid_line_color=None p.grid.grid_line_alpha = 0.3 p.segment(df.index, df.High, df.index, df.Low, color="blue") #color codes are available in w3 school css color p.rect(df.index[df.Status == "Increase"], df.Middle[df.Status == "Increase"], hour_12, df.Height[df.Status == "Increase"], fill_color="#8FBC8F", line_color="black") p.rect(df.index[df.Status == "Decrease"], df.Middle[df.Status == "Decrease"], hour_12, df.Height[df.Status == "Decrease"], fill_color="#A52A2A", line_color="black") script1, div1 = components(p) cdn_js = CDN.js_files #cdn_css=CDN.css_files #output_file("candlestick.html") #show(p)
def generate_graph(self): """ Generate the graph; return a 2-tuple of strings, script to place in the head of the HTML document and div content for the graph itself. :return: 2-tuple (script, div) :rtype: tuple """ logger.debug('Generating graph for %s', self._graph_id) # tools to use tools = [ PanTool(), BoxZoomTool(), WheelZoomTool(), SaveTool(), ResetTool(), ResizeTool() ] # generate the stacked area graph try: g = Area(self._data, x='Date', y=self._y_series_names, title=self._title, stack=True, xlabel='Date', ylabel='Downloads', tools=tools, # note the width and height will be set by JavaScript plot_height=400, plot_width=800, toolbar_location='above', legend=False ) except Exception as ex: logger.error("Error generating %s graph", self._graph_id) logger.error("Data: %s", self._data) logger.error("y=%s", self._y_series_names) raise ex lines = [] legend_parts = [] # add a line at the top of each Patch (stacked area) for hovertool for renderer in g.select(GlyphRenderer): if not isinstance(renderer.glyph, Patches): continue series_name = renderer.data_source.data['series'][0] logger.debug('Adding line for Patches %s (series: %s)', renderer, series_name) line = self._line_for_patches(self._data, g, renderer, series_name) if line is not None: lines.append(line) legend_parts.append((series_name, [line])) # add the Hovertool, specifying only our line glyphs g.add_tools( HoverTool( tooltips=[ (self._y_name, '@SeriesName'), ('Date', '@FmtDate'), ('Downloads', '@Downloads'), ], renderers=lines, line_policy='nearest' ) ) # legend outside chart area legend = Legend(legends=legend_parts, location=(0, 0)) g.add_layout(legend, 'right') return components(g)
def admin_dashboard(request): if request.user.is_superuser: # filter without 'org_name' orgs = OrgProfile.objects.all() myFilter_orgs = OrgsFilter(request.GET, queryset=orgs) orgs_count = myFilter_orgs.qs.count() researchs = OrgResearch.objects.filter( publish=True).order_by('-created_at') myFilter = OrgsFilter(request.GET, queryset=researchs) researchs_count = myFilter.qs.count() # filter with 'org_name' news = OrgNews.objects.filter( Q(publish=True) & ~Q(org_name__name='khalil')).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=news) news_count = myFilter.qs.count() rapports = OrgRapport.objects.filter( publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=rapports) rapports_count = myFilter.qs.count() datas = OrgData.objects.filter(publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=datas) datas_count = myFilter.qs.count() medias = OrgMedia.objects.filter(publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=medias) medias_count = myFilter.qs.count() jobs = OrgJob.objects.filter(publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=jobs) jobs_count = myFilter.qs.count() fundings = OrgFundingOpp.objects.filter( publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=fundings) fundings_count = myFilter.qs.count() Capacitys = OrgCapacityOpp.objects.filter( publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=Capacitys) Capacitys_count = myFilter.qs.count() devs = DevOrgOpp.objects.filter(publish=True).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=devs) devs_count = myFilter.qs.count() our_news = OrgNews.objects.filter( Q(publish=True) & Q(org_name__name='khalil')).order_by('-created_at') myFilter = OrgsNewsFilter(request.GET, queryset=our_news) our_news_count = myFilter.qs.count() # data visualation sdate = str(datetime.now().date() - timedelta(days=6)) edate = str(datetime.now().date()) if request.GET: sdate = request.GET.get('start_date_pub') edate = request.GET.get('end_date_pub') if sdate == '': sdate = str(datetime.now().date() - timedelta(days=6)) if edate == '': edate = str(datetime.now().date()) days = [] delta = datetime.strptime(edate, '%Y-%m-%d').date() - datetime.strptime( sdate, '%Y-%m-%d').date() for i in range(delta.days + 1): day = datetime.strptime(sdate, '%Y-%m-%d').date() + timedelta(days=i) days.append(day) # news per day days_to_present = [] counts = [] if request.GET: org_name = request.GET.get('org_name', None) if org_name == '': for i in range(len(days)): days_to_present.append(str(days[i])) counts.append( OrgNews.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) else: for i in range(len(days)): days_to_present.append(str(days[i])) counts.append( OrgNews.objects.filter( Q(publish=True) & Q(published_at__date=days[i]) & Q(org_name__id=org_name)).count()) else: for i in range(len(days)): days_to_present.append(str(days[i])) counts.append( OrgNews.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) source = ColumnDataSource( data=dict(days_to_present=days_to_present, counts=counts)) factor_cmap('', palette=Spectral6, factors=days_to_present) TOOLTIPS = [ ('date', "@days_to_present"), ('count', "@counts"), ] p = figure( x_range=days_to_present, plot_height=250, title="عدد الأخبار المنشورة باليوم", tools="pan,wheel_zoom,box_zoom,save,zoom_in,hover,zoom_out,reset", tooltips=TOOLTIPS) p.vbar(x='days_to_present', top='counts', width=0.9, source=source, legend_field="days_to_present", line_color='white', fill_color=factor_cmap('days_to_present', palette=Spectral6, factors=days_to_present)) p.xgrid.grid_line_color = None p.ygrid.grid_line_color = None p.y_range.start = 0 p.background_fill_color = "rgb(255, 255, 255)" p.border_fill_color = "rgb(255, 255, 255)" # p.background_fill_color = "rgba(23, 103, 140, 0.1)" # p.border_fill_color = "rgba(23, 103, 140, 0.1)" p.title.align = 'center' p.legend.visible = False script, div = components(p) # orgs by days counts_org = [] for i in range(len(days)): counts_org.append( OrgProfile.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) source = ColumnDataSource( data=dict(days_to_present=days_to_present, counts_org=counts_org)) factor_cmap('days_to_present', palette=Spectral6, factors=days_to_present) TOOLTIPS = [ ('name', "@days_to_present"), ('count', "@counts_org"), ] p_org = figure( x_range=days_to_present, plot_height=250, title="عدد المنظمات المنشورة باليوم", tools="pan,wheel_zoom,box_zoom,save,zoom_in,hover,zoom_out,reset", tooltips=TOOLTIPS) p_org.vbar(x='days_to_present', top='counts_org', width=0.9, source=source, legend_field="days_to_present", line_color='white', fill_color=factor_cmap('days_to_present', palette=Spectral6, factors=days_to_present)) p_org.xgrid.grid_line_color = None p_org.ygrid.grid_line_color = None p_org.y_range.start = 0 p_org.background_fill_color = "rgb(255, 255, 255)" p_org.border_fill_color = "rgb(255, 255, 255)" p_org.title.align = 'center' p_org.legend.visible = False script_org, div_org = components(p_org) # reports by days counts_report = [] if request.GET: org_name = request.GET.get('org_name', None) if org_name == '': for i in range(len(days)): counts_report.append( OrgRapport.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) else: for i in range(len(days)): counts_report.append( OrgRapport.objects.filter( Q(publish=True) & Q(published_at__date=days[i]) & Q(org_name__id=org_name)).count()) else: for i in range(len(days)): counts_report.append( OrgRapport.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) source = ColumnDataSource(data=dict(days_to_present=days_to_present, counts_report=counts_report)) factor_cmap('', palette=Spectral6, factors=days_to_present) TOOLTIPS = [ ('name', "@days_to_present"), ('count', "@counts_report"), ] p_report = figure( x_range=days_to_present, plot_height=250, title="عدد التقارير المنشورة باليوم", tools="pan,wheel_zoom,box_zoom,save,zoom_in,hover,zoom_out,reset", tooltips=TOOLTIPS) p_report.vbar(x='days_to_present', top='counts_report', width=0.9, source=source, legend_field="days_to_present", line_color='white', fill_color=factor_cmap('days_to_present', palette=Spectral6, factors=days_to_present)) p_report.xgrid.grid_line_color = None p_report.ygrid.grid_line_color = None p_report.y_range.start = 0 p_report.background_fill_color = "rgb(255, 255, 255)" p_report.border_fill_color = "rgb(255, 255, 255)" p_report.title.align = 'center' p_report.legend.visible = False script_report, div_report = components(p_report) # jobs per days counts_jobs = [] if request.GET: org_name = request.GET.get('org_name', None) if org_name == '': for i in range(len(days)): counts_jobs.append( OrgJob.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) else: for i in range(len(days)): counts_jobs.append( OrgJob.objects.filter( Q(publish=True) & Q(published_at__date=days[i]) & Q(org_name__id=org_name)).count()) else: for i in range(len(days)): counts_jobs.append( OrgJob.objects.filter( Q(publish=True) & Q(published_at__date=days[i])).count()) source = ColumnDataSource(data=dict(days_to_present=days_to_present, counts_jobs=counts_jobs)) factor_cmap('', palette=Spectral6, factors=days_to_present) TOOLTIPS = [ ('name', "@days_to_present"), ('count', "@counts_jobs"), ] p_jobs = figure( x_range=days_to_present, plot_height=250, title="عدد التقارير المنشورة باليوم", tools="pan,wheel_zoom,box_zoom,save,zoom_in,hover,zoom_out,reset", tooltips=TOOLTIPS) p_jobs.vbar(x='days_to_present', top='counts_jobs', width=0.9, source=source, legend_field="days_to_present", line_color='white', fill_color=factor_cmap('days_to_present', palette=Spectral6, factors=days_to_present)) p_jobs.xgrid.grid_line_color = None p_jobs.ygrid.grid_line_color = None p_jobs.y_range.start = 0 p_jobs.background_fill_color = "rgb(255, 255, 255)" p_jobs.border_fill_color = "rgb(255, 255, 255)" p_jobs.title.align = 'center' p_jobs.legend.visible = False script_jobs, div_jobs = components(p_jobs) # v_count = OrgNews.objects.filter(Q(publish=True)& Q(published_at__date='2020-11-05')).count() # for pro in profs: # org_type = pro.get_org_type_display() # position_work = pro.get_position_work_display() # # city_work = pro.get_city_work_display() # work_domain = pro.get_work_domain_display() # target_cat = pro.get_target_cat_display() # org_registered_country = pro.get_org_registered_country_display() # w_polic_regulations = pro.get_w_polic_regulations_display() context = { # 'org_type': org_type, # 'position_work': position_work, # # 'city_work': city_work, # 'work_domain': work_domain, # 'target_cat': target_cat, # 'org_registered_country': org_registered_country, # 'w_polic_regulations': w_polic_regulations, 'news_count': news_count, 'myFilter': myFilter, 'orgs_count': orgs_count, 'myFilter_orgs': myFilter_orgs, 'rapports_count': rapports_count, 'datas_count': datas_count, 'medias_count': medias_count, 'researchs_count': researchs_count, 'jobs_count': jobs_count, 'fundings_count': fundings_count, 'Capacitys_count': Capacitys_count, 'devs_count': devs_count, 'our_news_count': our_news_count, # 'sdate':sdate, # 'edate':edate, 'days': days, 'delta': delta, 'script': script, 'div': div, 'script_org': script_org, 'div_org': div_org, 'script_report': script_report, 'div_report': div_report, 'script_jobs': script_jobs, 'div_jobs': div_jobs, # 'org_name':org_name } else: return HttpResponse( 'You dont have the permitions to entro this page :) ') return render(request, 'profiles/layout_profile.html', context)
label_opts2 = dict(x=-84, y=47, x_units='screen', y_units='screen') label_opts3 = dict(x=612, y=64, x_units='screen', y_units='screen', text_align='right', text_font_size='9pt') msg1 = 'By Exoplots' # when did the data last get updated modtimestr = get_update_time().strftime('%Y %b %d') msg3 = 'Data: NASA Exoplanet Archive' caption1 = Label(text=msg1, **label_opts1) caption2 = Label(text=modtimestr, **label_opts2) caption3 = Label(text=msg3, **label_opts3) fig.add_layout(caption1, 'below') fig.add_layout(caption2, 'below') fig.add_layout(caption3, 'below') plotting.save(fig) # save the individual pieces so we can just embed the figure without the whole # html page script, div = components(fig, theme=theme) with open(embedfile, 'w') as ff: ff.write(script) ff.write(div)
def test_result_attrs(self): script, div = embed.components(_embed_test_plot, CDN) html = bs4.BeautifulSoup(script) scripts = html.findAll(name='script') self.assertEqual(len(scripts), 1) self.assertTrue(scripts[0].attrs, {'type': 'text/javascript'})
def zhkeyword(keyword): print("Keyword:", keyword) #CHINESE BAR CHART zh_tweet_df = pd.DataFrame(retrieve_sentiment_data_zh()) zh_tweet_df['sentiment'] = zh_tweet_df['sentiment'].values.astype(int) positive = len([ element for element in (list(zh_tweet_df['sentiment'])) if element > 0 and element < 999 ]) negative = len([ element for element in (list(zh_tweet_df['sentiment'])) if element < 0 ]) neutral = len([ element for element in (list(zh_tweet_df['sentiment'])) if element == 0 ]) none = len([ element for element in (list(zh_tweet_df['sentiment'])) if element == 999 ]) sentiment = ['Positive', 'Negative', 'Neutral', 'None'] counts = [positive, negative, neutral, none] colors = ["#82E0AA", "#e84d60", "#ccccff", "#FCA15A"] source = ColumnDataSource( data=dict(sentiment=sentiment, counts=counts, color=colors)) zh_bar = figure(x_range=sentiment, plot_height=400, toolbar_location=None, tools="hover", tooltips="@sentiment @counts") zh_bar.vbar(x='sentiment', top='counts', width=0.9, color='color', legend_field="sentiment", source=source) zh_bar.add_layout(zh_bar.legend[0], 'above') zh_bar.xgrid.grid_line_color = None zh_bar.y_range.start = 0 zh_bar.legend.orientation = "horizontal" zh_bar.legend.location = "top_right" zh_bar.add_layout(Title(text=keyword, text_font_style="italic"), 'above') zh_bar.add_layout( Title(text="Chinese Sentiment Counts", text_font_size="12pt"), 'above') zh_script, zh_div = components(zh_bar) #CHINESE SENTIMENT PIECHART ##PIECHART total = positive + negative + neutral + none per_positive = positive / total * 100 per_negative = negative / total * 100 per_neutral = neutral / total * 100 per_ms_none = none / total * 100 x = { 'Positive': per_positive, 'Negative': per_negative, 'Neutral': per_neutral, 'None': per_ms_none } data = pd.Series(x).reset_index(name='value').rename( columns={'index': 'sentiment'}) data['color'] = Category20c[len(x)] data['angle'] = data['value'] / data['value'].sum() * 2 * pi p = figure(plot_height=350, toolbar_location=None, tools="hover", tooltips="@sentiment: @value{0.2f} %") p.wedge( x=0, y=1, radius=0.4, # use cumsum to cumulatively sum the values for start and end angles start_angle=cumsum('angle', include_zero=True), end_angle=cumsum('angle'), line_color="white", fill_color='color', legend_field='sentiment', source=data) p.axis.axis_label = None p.axis.visible = False p.grid.grid_line_color = None p.add_layout(Title(text=keyword, text_font_style="italic"), 'above') p.add_layout( Title(text="Percentage of Keyword Sentiment", text_font_size="12pt"), 'above') pie_script, pie_div = components(p) return render_template( 'zhkeyword.html', zhbar_script=zh_script, zhbar_div=zh_div, zhpie_script=pie_script, zhpie_div=pie_div, js_resources=INLINE.render_js(), css_resources=INLINE.render_css(), ).encode(encoding='UTF-8')
def plot(): import numpy as np import pandas as pd import geoviews as gv import holoviews as hv import panel as pn from holoviews.operation.datashader import rasterize hv.extension("bokeh") from bokeh.models.callbacks import CustomJS # Some points defining a triangulation over (roughly) Britain. xy = np.asarray([ [-0.101, 0.872], [-0.080, 0.883], [-0.069, 0.888], [-0.054, 0.890], [-0.045, 0.897], [-0.057, 0.895], [-0.073, 0.900], [-0.087, 0.898], [-0.090, 0.904], [-0.069, 0.907], [-0.069, 0.921], [-0.080, 0.919], [-0.073, 0.928], [-0.052, 0.930], [-0.048, 0.942], [-0.062, 0.949], [-0.054, 0.958], [-0.069, 0.954], [-0.087, 0.952], [-0.087, 0.959], [-0.080, 0.966], [-0.085, 0.973], [-0.087, 0.965], [-0.097, 0.965], [-0.097, 0.975], [-0.092, 0.984], [-0.101, 0.980], [-0.108, 0.980], [-0.104, 0.987], [-0.102, 0.993], [-0.115, 1.001], [-0.099, 0.996], [-0.101, 1.007], [-0.090, 1.010], [-0.087, 1.021], [-0.069, 1.021], [-0.052, 1.022], [-0.052, 1.017], [-0.069, 1.010], [-0.064, 1.005], [-0.048, 1.005], [-0.031, 1.005], [-0.031, 0.996], [-0.040, 0.987], [-0.045, 0.980], [-0.052, 0.975], [-0.040, 0.973], [-0.026, 0.968], [-0.020, 0.954], [-0.006, 0.947], [0.003, 0.935], [0.006, 0.926], [0.005, 0.921], [0.022, 0.923], [0.033, 0.912], [0.029, 0.905], [0.017, 0.900], [0.012, 0.895], [0.027, 0.893], [0.019, 0.886], [0.001, 0.883], [-0.012, 0.884], [-0.029, 0.883], [-0.038, 0.879], [-0.057, 0.881], [-0.062, 0.876], [-0.078, 0.876], [-0.087, 0.872], [-0.030, 0.907], [-0.007, 0.905], [-0.057, 0.916], [-0.025, 0.933], [-0.077, 0.990], [-0.059, 0.993]]) # Make lats + lons x = abs(xy[:, 0] * 180 / 3.14159) y = xy[:, 1] * 180 / 3.14159 # A selected triangulation of the points. triangles = np.asarray([ [67, 66, 1], [65, 2, 66], [1, 66, 2], [64, 2, 65], [63, 3, 64], [60, 59, 57], [2, 64, 3], [3, 63, 4], [0, 67, 1], [62, 4, 63], [57, 59, 56], [59, 58, 56], [61, 60, 69], [57, 69, 60], [4, 62, 68], [6, 5, 9], [61, 68, 62], [69, 68, 61], [9, 5, 70], [6, 8, 7], [4, 70, 5], [8, 6, 9], [56, 69, 57], [69, 56, 52], [70, 10, 9], [54, 53, 55], [56, 55, 53], [68, 70, 4], [52, 56, 53], [11, 10, 12], [69, 71, 68], [68, 13, 70], [10, 70, 13], [51, 50, 52], [13, 68, 71], [52, 71, 69], [12, 10, 13], [71, 52, 50], [71, 14, 13], [50, 49, 71], [49, 48, 71], [14, 16, 15], [14, 71, 48], [17, 19, 18], [17, 20, 19], [48, 16, 14], [48, 47, 16], [47, 46, 16], [16, 46, 45], [23, 22, 24], [21, 24, 22], [17, 16, 45], [20, 17, 45], [21, 25, 24], [27, 26, 28], [20, 72, 21], [25, 21, 72], [45, 72, 20], [25, 28, 26], [44, 73, 45], [72, 45, 73], [28, 25, 29], [29, 25, 31], [43, 73, 44], [73, 43, 40], [72, 73, 39], [72, 31, 25], [42, 40, 43], [31, 30, 29], [39, 73, 40], [42, 41, 40], [72, 33, 31], [32, 31, 33], [39, 38, 72], [33, 72, 38], [33, 38, 34], [37, 35, 38], [34, 38, 35], [35, 37, 36]]) z = np.random.uniform(0, 16, 74) # print("x",x) # print("y",y) # print("z",z) def plotthis(z, regions='w'): if regions is 'O': z = np.where(((x >= 0) & (x <= 4) & (y >= 50) & (y <= 56)), z, np.nan).flatten() elif regions is 'A': z = np.where(((x >= 3) & (x <= 4) & (y >= 54) & (y <= 57)), z, np.nan).flatten() elif regions is 'T': z = np.where(((x >= -2) & (x <= 3) & (y >= 50) & (y <= 57)), z, np.nan).flatten() # else: # # data = get_data4region(data,**odisha) # z=z.flatten() print("lx:", len(x), "ly:", len(y), "lz:", len(z)) print("z", z) verts = pd.DataFrame(np.stack((x, y, z)).T, columns=['X', 'Y', 'Z']) # #openStreet Background. # tri_sub = cf.apply(lambda x: x - 1) # tri_sub=tri_sub[:10] tri = gv.TriMesh((triangles, gv.Points(verts))) return tri allplot = {(k, r): plotthis(k, r) for k, r in zip(z, ['O', 'A', 'T'])} df_div = hv.Div(""" <figure> <img src="https://i.ibb.co/5h74S9n/python.png" height='80' width='90' vspace='-10'> """) colorbar_opts = { 'major_label_overrides': { 0: '0', 1: '1', 2: '2', 3: '3', 4: '4', 5: '5', 6: '6', 7: '7', 8: '8', 9: '9', 10: '10', 11: '11', 12: '12', 13: '13', 14: '>14', 15: '15' }, 'major_label_text_align': 'left', 'major_label_text_font_style': 'bold', } logo1 = hv.RGB.load_image("https://i.ibb.co/5h74S9n/python.png") def absolute_position(plot, element): glyph = plot.handles['glyph'] x_range, y_range = plot.handles['x_range'], plot.handles['y_range'] glyph.dh_units = 'screen' glyph.dw_units = 'screen' glyph.dh = 60 glyph.dw = 90 glyph.x = x_range.start glyph.y = y_range.start xcode = CustomJS(code="glyph.x = cb_obj.start", args={'glyph': glyph}) plot.handles['x_range'].js_on_change('start', xcode) ycode = CustomJS(code="glyph.y = cb_obj.start", args={'glyph': glyph}) plot.handles['y_range'].js_on_change('start', ycode) def plot_limits(plot, element): plot.handles['x_range'].min_interval = 100 plot.handles['x_range'].max_interval = 55000000 # 3000000 plot.handles['y_range'].min_interval = 500 plot.handles['y_range'].max_interval = 900000 opts = dict(width=700, height=700, tools=['hover', 'save', 'wheel_zoom'], active_tools=['wheel_zoom'], hooks=[plot_limits], colorbar=True, color_levels=15, colorbar_opts=colorbar_opts, cmap=['#000080', '#0000cd', '#0008ff', '#004cff', '#0090ff', '#00d4ff', '#29ffce', '#60ff97', '#97ff60', '#ceff29', '#ffe600', '#ffa700', '#ff2900', '#cd0000', '#800000', ], clim=(0, 15), title="\t\t\t\t\t\t\t\t\t Mean Period (s) ", fontsize={'title': 18, 'xlabel': 15, 'ylabel': 15, 'ticks': 12}) #tiles = gv.tile_sources.Wikipedia hmap1 = hv.HoloMap(allplot, kdims=['Select D :', 'Select State']) dd = df_div.opts(width=70, height=70) finalplot = pn.Column(pn.Row(dd), rasterize(hmap1).options(**opts) * logo1.opts(hooks=[absolute_position], apply_ranges=False)) from bokeh.embed import components from bokeh.resources import CDN from bokeh.io import curdoc doc = curdoc() def modify_doc(doc): sea = (name='Sea Surface') doc.add_root(sea.panel().get_root(doc)) # Put all the tabs into one application #tabs = Tabs(tabs=[tab1, tab2, tab3, tab4, tab5]) # Put the tabs in the current document for display # doc=curdoc().add_root(finalplot) script, div = components(finalplot.get_root(doc)) cdn_js0=CDN.js_files[0] cdn_js = CDN.js_files[1] cdn_css=CDN.css_files print("cdn_js:",cdn_js) print("cdn_css",cdn_css) return render_template("plot.html", script=script, div=div, cdn_css=cdn_css, cdn_js=cdn_js, cdn_js0=cdn_js0)