def make_plot(query): # PercentageMicrobe_Success=X_df[Y_df['Results'] ==1 & X_df.columns != "AlphaDiversity"]].sum(axis=1) averageMicrobe_Success = X_df.ix[Y_df['Results'] == 1, X_df.columns != "AlphaDiversity"].mean( axis=0) averageMicrobe_Failure = X_df.ix[Y_df['Results'] == 0, X_df.columns != "AlphaDiversity"].mean( axis=0) averageMicrobe_Success.sort_values(ascending=False, inplace=True) averageMicrobe_Failure.sort_values(ascending=False, inplace=True) donor = X_df.ix[query, X_df.columns != "AlphaDiversity"].sort_values( ascending=False).to_frame() success_donor_Failure = pd.concat( [averageMicrobe_Success, donor, averageMicrobe_Failure], axis=1, join_axes=[averageMicrobe_Success.index]) success_donor_Failure.columns = ['Success', 'donor', 'Failure'] colors = [] for name, hex in matplotlib.colors.cnames.items(): colors.append(name) pie_chart_success = Donut( success_donor_Failure['Success'][0:8], title="Average successful donors' gut microbe composition", palette=colors[8:16]) pie_chart_failure = Donut(success_donor_Failure['Failure'][0:8], title="Average failed donors", palette=colors[8:16]) pie_chart_donor = Donut(success_donor_Failure['donor'][0:8], title='The chosen donor', palette=colors[8:16]) p = row(pie_chart_success, pie_chart_donor, pie_chart_failure) return p
def create_pie_chart(start, end): airline = your_airline() cursor.execute( 'SELECT * FROM purchases NATURAL JOIN ticket WHERE purchase_date BETWEEN %s AND %s AND \ airline_name=%s', (start, end, airline)) puchases = cursor.fetchall() cust_revenue = 0 agent_revenue = 0 for bought in puchases: ticket_id = bought['ticket_id'] cursor.execute( 'SELECT price FROM ticket NATURAL JOIN flight WHERE ticket_id=%s AND airline_name=%s', (ticket_id, airline)) price = cursor.fetchone()['price'] if (bought['booking_agent_id'] == None): cust_revenue += price else: agent_revenue += price total_revenue = agent_revenue + cust_revenue data = pd.Series([ float(agent_revenue / total_revenue), float(cust_revenue / total_revenue) ], index=['Booking Agent', 'Customer']) pie_chart = Donut(data) pie_chart.toolbar_location = None info = [pie_chart, cust_revenue, agent_revenue] return info
def budget_detail(request, budget_id): budget_owner = User.objects.get(id=Budget.objects.get(id=budget_id).user_id).username title = Budget.objects.get(id=budget_id).title base = Cost.objects.filter(budget_id=budget_id, user=request.user).values() total = Cost.objects.filter(budget_id=budget_id, user=request.user).aggregate(Sum('value'))['value__sum'] budget = Budget.objects.get(id=budget_id).value date = [] date_url = [] categories = [] categories_id = [] for item in Cost.objects.filter(budget_id=budget_id, user=request.user).values('title', 'publish', 'category_id'): date.append(str(item['publish'])) date_url.append(str(item['publish']).replace('-', '/')) categories.append(Category.objects.get(id=item['category_id']).title) categories_id.append(Category.objects.get(id=item['category_id']).id) categories_title = [] categories_title_id =[] categories_data = [] for item in Category.objects.values('title', 'id'): val = Cost.objects.filter(budget_id=budget_id, user=request.user, category_id=item['id']).aggregate(Sum('value'))['value__sum'] if val is not None: categories_title.append(item['title']) categories_title_id.append(item['id']) categories_data.append(val) try: total_budget = budget - total except TypeError: total_budget = 0 info = zip(base, date, date_url, categories, categories_id) data = { 'money': [float(x) for x in categories_data], 'labels': categories_title } p1 = Donut(data, values='money', label='labels', plot_width=390, plot_height=400, legend=None, responsive=True) p1.logo = None p1.toolbar_location = None script, div = components(p1, CDN) cat_all = zip(categories_title, categories_data, categories_title_id) return render(request, 'core_sm/costs/budget/budget_detail.html', {'info': info, 'title': title, 'total': total, 'budget': budget, 'total_budget': total_budget, 'script': mark_safe(script), 'div': mark_safe(div), 'cat_all': cat_all, 'categories_data': categories_data, 'budget_owner': budget_owner})
def Donut_Pie(df): pd.options.html.border = 1 percentage = df.percentage.values.tolist() Type = df.Type.values.tolist() data = pd.Series(percentage, index=Type) plot = Donut(data, title="Overall Sentiment % wise") plot.logo = None script, div = components(plot, CDN) return script, div
def fnCreate_Pie(df): pd.options.html.border = 1 Location = df.Location.values.tolist() count = df['count'].tolist() data = pd.Series(count, index=Location) plot = Donut(data, title="Top 5 Country-wise tweets") plot.logo = None script, div = components(plot, CDN) return script, div
def Donut_Pie(df,stS): pd.options.html.border=1 percentage = df.Count.values.tolist() Type= df.Aspects.values.tolist() data = pd.Series(percentage, index = Type) plot = Donut(data, title= stS +" Sentiment Aspects segmentation") plot.logo=None # show(plot) script, div = components(plot,CDN) return script, div
def create_figure(): p = figure(plot_height=400, plot_width=400) count = df.genre.value_counts() colors = [genre_color[x] for x in count.index.sort_values()] p = Donut(count, label='index', color=colors, height=400, width=400, hover_text='#songs') p.title.text = "#Songs = {}".format(len(df)) py = figure(plot_height=400, plot_width=400) count = dfy.genre.value_counts() colors = [genre_color[x] for x in count.index.sort_values()] py = Donut(count, label='index', color=colors, height=400, width=400, hover_text='#songs') py.title.text = "#Songs w/o missing year = {}".format(len(dfy)) pyc = figure(plot_height=400, plot_width=400) count = dfyc.genre.value_counts() colors = [genre_color[x] for x in count.index.sort_values()] pyc = Donut(count, label='index', color=colors, height=400, width=400, hover_text='#songs') pyc.title.text = "#Songs w/o missing year and location = {}".format( len(dfyc)) return p, py, pyc
def mount_graph(): """ Função que monta o gráfico usando os dataframes """ all_df = concat(L_DFS) graph = Donut(all_df, label='state') return components(graph, INLINE)
def plot_variant_summary(data): from bokeh.plotting import figure from bokeh.charts import Donut d = Donut(df, label=['abbr', 'medal'], values='medal_count', text_font_size='8pt', hover_text='medal_count') return d
def get_bokeh_bytes_by_ip(c2s, sent, **kwargs): """ Fetching data """ raw_results = query.get_graph_bytes_by_ip(c2s, sent, **kwargs) aggregate_attr = 'subflow__saddr' if (c2s and sent) or ( not c2s and not sent) else 'subflow__daddr' # Two passes on results total_bytes = 0 for elem in raw_results: total_bytes += elem['bytes_data__sum'] results = [] for elem in raw_results: results.append({ 'label': elem[aggregate_attr] + ' ' + '{:0.2f}'.format(100.0 * elem['bytes_data__sum'] / total_bytes) + '%', 'value': elem['bytes_data__sum'] }) """ Plotting """ sent_text = "sent over" if sent else "received on" c2s_text = "client to server" if c2s else "server to client" plot = Donut(results, title="Number of bytes " + sent_text + " IP address in " + c2s_text + " flow", label="label", values="value") return components(plot)
def annotation_pie(self, pid): df = self.timeBasedDf if pid != 'all': df = df[df['pid'] == int(pid)] data = pd.value_counts(df['annotationsName'].values) p = Donut(data, title='Time Spending on annotations') return p
def locationDistribution(self, pid): location = { "bath": 0, "bed1": 0, "bed2": 0, "hall": 0, "kitchen": 0, "living": 0, "stairs": 0, "study": 0, "toilet": 0, "other": 0 } if pid == 'all': cursor = self.db.location.find({}) title = 'Time Spending in Locations: All(seconds).' th = 500 else: cursor = self.db.location.find({'pid': int(pid)}) title = 'Time Spending in Locations: person #%s (second).' % pid th = 50 for i in cursor: location[i['name']] += float(i['end']) - float(i['start']) data = pd.Series(location) for i in data[data <= th]: data['other'] += i data = data[data > th] p = Donut(data, title=title) return p
def donutLsi(data): TOOLS = 'box_zoom,hover,crosshair,resize,reset' dat = {'object': 'list'} # {'type': 'old', 'contr': -3.5}, {'type': 'cold', 'contr': -4.45} dat['data'] = data df = df_from_json(dat) TITLE = "LSI Topic #"+ str(data[0]['nr']) d = Donut(df, label='type', text_font_size='10pt', hover_text='Test', values='contr', toolbar_location="right", tools=TOOLS, title=TITLE) return d
def plot_chart(df, oxygen, week, replicate): d = Donut(df, label=['class', 'order'], values='abundance', text_font_size='10pt', hover_text='abundance') output_file("donut.html", title=oxygen + "." + str(week) + "." + str(replicate)) return show(d)
def create_donut2(df, col1, col2, title): plot = Donut(df, label=col1, values=col2, color=Spectral11, title=title, plot_width=400, plot_height=400) return plot
def where_did_i_spend_time_m(self, cal, ind, yom=1): '''pie chart''' result = cal.report_doing(ind, yom) result = [get_time_delta(one) for one in result] df = pd.DataFrame(result, columns=['type', 'detail_type', 'duration']) source = ColumnDataSource({ 'type': list(df['type']), 'detail_type': list(df['detail_type']), 'duration': list(df['duration']) }) grouped = df.groupby(['type', 'detail_type']).sum() grouped.to_csv("report/report_time_week_" + str(ind) + ".csv", encoding='utf-8') d = Donut(data=df, label=['type', 'detail_type'], values='duration', text_font_size='6pt') d.plot_width = 800 d.plot_height = 600 return (d)
def pie_chart(Y, names, title='', height=3.8): print(Y, names) i = 0 for i in range(0, len(Y)): names[i] = names[i] + ' = ' + str(Y[i]) data = pd.Series(Y, index=names) pie = Donut(data, title=title, plot_height=int(height * ht), sizing_mode='scale_width') return pie
def draw_piechart(df_allocation): TOOLS = 'box_zoom,box_select,resize,reset' df_allocation['percent'] = [ '{:0.2%}'.format(a / sum(df_allocation['Allocation'])) for a in df_allocation['Allocation'] ] d = Donut(df_allocation, label=['Stocks', 'percent'], values='Allocation', text_font_size='8pt', hover_text='Allocation', tools=TOOLS) script, div = components(d) return script, div
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 createTopicGraph(): words = pd.read_csv( "/Users/christopherhedenberg/Downloads/projects/RapThesaurus/ModelOutput/TopicCountTotal.csv" ) words = words.drop('Unnamed: 0', axis=1) words.columns = ['Topic', 'Count', 'Word'] # original example d = Donut(words, label=['Topic'], values='Count', text_font_size='8pt', hover_text='word_count') script, div = components(d) return script, div
def annotationDistribution(self, pid): annotation = { "a_ascend": 0, "a_descend": 0, "a_jump": 0, "a_loadwalk": 0, "a_walk": 0, "p_bent": 0, "p_kneel": 0, "p_lie": 0, "p_sit": 0, "p_squat": 0, "p_stand": 0, "t_bend": 0, "t_kneel_stand": 0, "t_lie_sit": 0, "t_sit_lie": 0, "t_sit_stand": 0, "t_stand_kneel": 0, "t_stand_sit": 0, "t_straighten": 0, "t_turn": 0, "other": 0 } if pid == 'all': cursor = self.db.annotations.find({}) title = 'Time Spending on annotations: All(seconds).' th = 300 else: cursor = self.db.annotations.find({'pid': int(pid)}) title = 'Time Spending on annotations: person #%s (seconds).' % pid th = 30 for i in cursor: annotation[i['name']] += float(i['end']) - float(i['start']) data = pd.Series(annotation) for i in data[data <= th]: data['other'] += i data = data[data > th] p = Donut(data, title=title) return p
def pie_chart_most_words(self): most_words = pd.DataFrame(sorted(self.most_words), columns=['Word', 'Freq']) # words =list(most_words.Word) # words = self.dataclean.remove_stops(words) pie = Donut(most_words, 'Word', values='Freq', title="Pie Chart For Tweets", color=[ '#f20f32', '#39f20f', '#76dfe7', '#af0132', '#f39f29', '#3FC0CF', '#75D9EF', '#4B96D8', '#33D59F', '#33A9D5', '#94A2F3', '#687EFC', '#68FCCF', '#68FCEC', '#75D3F9' ]) script_pie, div_pie = components(pie) script_pie = Markup(script_pie) div_pie = Markup(div_pie) page = [script_pie, div_pie] return page
def create_figure(): data = song_selected() p = figure(plot_height=400, plot_width=400) if len(data) > 0: #title = 'The {} most {} songs'.format( var_slider.value,type.value) count = data.genre.value_counts() colors = [genre_color[x] for x in count.index.sort_values()] p = Donut(count, label='index', color=colors, height=400, width=400, hover_text='#songs') p.title.text = "#songs = {}".format(len(data)) return p
def bokeh_plot_pie(data, nrDataSource, container_filter=""): title = "Pie chart: " + nrDataSource['name'] + ( (" container: " + container_filter) if container_filter else ", all containers") value_col_names = [d for d in data.columns[1:]] num_rows = len(data[data.columns[0]]) plot = Donut( data, title=title, values=value_col_names[0], label=data.columns[0], text_font_size='8pt', plot_width=800, plot_height=800, responsive=True ) # , color=__get_color_palette(num_rows)) #default palette looks best script, div = components(plot, resources=None, wrap_script=False, wrap_plot_info=True) return (script, div)
def pie_chart(self): data = { 'Class': ['Negative', 'Positive', 'Neutral'], 'Numbers': [ self.negative_tweets_num, self.positive_tweets_num, self.neutral_tweets_num ] } dataframe = pd.DataFrame(data) pie = Donut(dataframe, 'Class', values='Numbers', title="Pie Chart For Tweets", color=['#75D9EF', '#4B96D8', '#33D59F']) script_pie, div_pie = components(pie) script_pie = Markup(script_pie) div_pie = Markup(div_pie) page = [script_pie, div_pie] return page
def plot5pop(): x = np.unique(dressfeatures['Style'].values) for i in x: dressfeatures['Style'][dressfeatures['Style']==i]=str(i).title() # print out the styles x = np.unique(dressfeatures['Style'].values) print('Styles are:') for i in x: print i # grouped_style = dressfeatures.groupby(['Style']) cnt_grouped_style = grouped_style['Dress_ID'].count() cnt_grouped_style.sort_values(ascending=False, inplace=True) # Pick 5 most popular styles and combine the others as others list5style_labels = ['Casual', 'Sexy', 'Party', 'Cute', 'Vintage', 'Others'] valuepopular5 = cnt_grouped_style[:5] valuepopular5['Others']=cnt_grouped_style.values[6:].sum() data = pd.Series(valuepopular5.values,index=list5style_labels) p = figure() pie_chart = Donut(data,title='Popular Styles') output_file("donut.html", title='Popular Styles') show(pie_chart)
from bokeh.sampledata.olympics2014 import data import pandas as pd # utilize utility to make it easy to get json/dict data converted to a dataframe df = df_from_json(data) # filter by countries with at least one medal and sort by total medals df = df[df['total'] > 8] df = df.sort("total", ascending=False) df = pd.melt(df, id_vars=['abbr'], value_vars=['bronze', 'silver', 'gold'], value_name='medal_count', var_name='medal') # original example d = Donut(df, label=['abbr', 'medal'], values='medal_count', text_font_size='8pt', hover_text='medal_count') output_file("donut.html", title="donut.py example") show(d) <nav class="w3-sidebar w3-bar-block w3-animate-left w3-grey" style="display:none", id="my_sidebar"> <button class="w3-bar-item w3-button w3-xlarge" onclick="w3_close()">Close ×</button> <a href="#" class="w3-bar-item w3-button w3-padding-large w3-grey"> <i class="fa fa-home w3-xxlarge"> </i> <p>Home</p> </a> <a href="/Runners.html" class="w3-bar-item w3-button w3-padding-large w3-grey w3-hover-dark-grey">
import pandas as pd import numpy as np from collections import OrderedDict from bokeh.sampledata.olympics2014 import data from bokeh.charts import Donut # we throw the data into a pandas df df = pd.io.json.json_normalize(data['data']) # filter by countries with at least one medal and sort df = df[df['medals.total'] > 8] df = df.sort("medals.total", ascending=False) # then, we get the countries and we group the data by medal type countries = df.abbr.values.tolist() gold = df['medals.gold'].astype(float).values silver = df['medals.silver'].astype(float).values bronze = df['medals.bronze'].astype(float).values # later, we build a dict containing the grouped data medals = OrderedDict(bronze=bronze, silver=silver, gold=gold) # any of the following commented are valid Donut inputs # medals = list(medals.values()) # medals = np.array(list(medals.values())) # medals = pd.DataFrame(medals) donut = Donut(medals, countries, filename="donut.html") donut.title("Medals Donut").xlabel("countries").ylabel("medals") donut.legend(True).width(800).height(800).show()
def index(): reload(sys) #解决中文编码 e_location = "127.0.0.1:9203" e_index = "logstash-*" analysis =action.LogAnalysis(e_location, e_index) c = pycurl.Curl() buf =BytesIO() c.setopt(c.URL, 'http://' + e_location + '/' + e_index + '/_search') c.setopt(pycurl.CUSTOMREQUEST,"GET") c.setopt(c.WRITEFUNCTION, buf.write) c.perform() results = buf.getvalue() results = json.loads(results.decode('utf-8')) c.close if results["hits"]["total"] == 0: flash('you have got no data!') return render_template('auth/upload.html') else: #bar1 actionlist = analysis.actionAgg("action") df=pd.DataFrame(actionlist) bar1 = Bar(df, label='action', values='size', agg='max', color="green", title="sshd-invalid-passwd_IP", plot_width=600, plot_height=322, legend=False) script, div = components(bar1) ''' #dount label,value,res = analysis.USERAgg("action") data = pd.Series(value, index = label) pie_chart = Donut(data, plot_width=400, plot_height=300) script2, div2 = components(pie_chart) ''' #user_ip useriplist = analysis.USERIPAgg("action") a = list() for i in useriplist: data = pd.Series(i['value'], index = i['ip']) pie_chart = Donut(data, title = i['user'] +'\n'+ ",IP總數:" + str(len(i['value'])), plot_width=190, plot_height=190) a.append(pie_chart) b=[] for i in range(0,len(a),5): b.append(a[i:i+5]) p = gridplot(b) script3, div3 = components(p) #INLINE_config js_resources = INLINE.render_js() css_resources = INLINE.render_css() #piechart labels,values,res = analysis.USERAgg("action") colors = [] for i in labels: colors.append("#%06x" % random.randint(0, 0xFFFFFF)) #colors = [ "#F7464A", "#46BFBD", "#FDB45C", "#FEDCBA","#ABCDEF", "#DDDDDD", "#ABCABC", "#AABEEF", "#EFAAED", "#bebece"] count = 0 for a in res: a["color"]=colors[count] if count<9: count+=1 #TableAgg Usertable = analysis.TableAgg("USERNAME","SUPERUSER") return render_template('index.html', plot_script=script, plot_div=div, pies_script=script3, pies_div=div3, js_resources=js_resources, css_resources=css_resources, set=zip(values, labels, colors), res=res, Usertable = Usertable )
def get_memory_hists(self, mode='overview'): """ Plot memory histogram in a row """ access = self.__get_memory_access_types(mode) hists = [] data_dfs = [] col_cycle = [] col_index = [] for key, value in access.iteritems(): sql_query = 'SELECT length FROM memory' sql_query += ' WHERE ' + value sql_query += ' ORDER by line' dataframe = pd.read_sql_query(sql_query, self.get_db()) color = tm.get_random_color() col_sched = [] col_location = [] col_value = [] col_type = [] col_color = [] try: # Some statistics mean = np.round_(dataframe["length"].mean(), 2) median = dataframe["length"].median() sum_len = dataframe["length"].sum() col_cycle.append(sum_len) col_index.append(key) # Plot histogram hist_title = key hist_title += ' / avg ' + str(mean) hist_title += ' / mid ' + str(median) plot_hist = Histogram(dataframe["length"].replace(0, np.nan), 'length', bins=50, color=color, xlabel='Latency of instruction in cycle', ylabel='Count', title=hist_title) except ValueError: continue # Ignore NaN if mean == mean and median == median: # Insert mean col_sched.append(self.get_name_instruction_scheduler()) col_location.append(key) col_value.append(mean) col_type.append('Mean') col_color.append(color) # Insert median col_sched.append(self.get_name_instruction_scheduler()) col_location.append(key) col_value.append(median) col_type.append('Median') col_color.append(color) data = {'sched': col_sched, 'location': col_location, 'value': col_value, 'type': col_type, 'color': col_color} data_df = pd.DataFrame(data, index=col_location) data_dfs.append(data_df) hists.append(plot_hist) # Plot break down of cycles data = {'cycle': col_cycle} cycle_df = pd.DataFrame(data, index=col_index) pie_cycle = Donut(cycle_df.replace(0, np.nan), title='Break down: cycles') hists.append(pie_cycle) info = {'hist': hists, 'info': data_dfs} return info