def graph(id): project = Project.query.get_or_404(id) logs = project.logs log_number = len([1 for _ in logs]) if project.logs and log_number > 1: logs = project.logs.order_by(ProjectLog.log_date) x = [ datetime.combine(log.log_date, datetime.min.time()).timestamp() * 1000 for log in logs ] y = [log.previous_score * score_multiplier for log in logs] multi_iter = {'x': x, 'data': y} line = vincent.Line(multi_iter, iter_idx='x') line.scales['x'] = vincent.Scale(name='x', type='time', range='width', domain=vincent.DataRef( data='table', field="data.idx")) line.scales['y'] = vincent.Scale(name='y', range='height', nice=True, domain=[0, score_multiplier]) line.scales['color'] = vincent.Scale(name='color', range=['#12897D'], type='ordinal') line.axes['y'].ticks = 3 line.axes['x'].ticks = 7 if line_style: line.marks['group'].marks[ 0].properties.enter.interpolate = vincent.ValueRef( value=line_style) return jsonify({"status": "success", "data": line.grammar()}) else: return failure_response("No history for this project", 404)
def plotrwresult(G): visits = [G.node[node]['visits'] for node in G.nodes_iter()] norm_visits = np.array(visits) / float(G.graph['total_visits']) deaths = [G.node[node]['deaths'] for node in G.nodes_iter()] norm_deaths = np.array(deaths) / float(sum(deaths)) if G.is_directed(): degrees = [G.in_degree(node) for node in G.nodes_iter()] norm_degrees = np.array(degrees) / float(G.size()) else: degrees = [G.degree(node) for node in G.nodes_iter()] aorm_degrees = np.array(degrees) / float(2 * G.size()) multi_iter1 = { 'index': range(G.order()), 'Visits': norm_visits, 'Deaths': norm_deaths, 'Degree': norm_degrees } line = vincent.Scatter(multi_iter1, iter_idx='index') line.axis_titles(x='Vertex', y='Juice') line.legend(title='Results') line.width = 400 line.height = 300 line.marks[0].marks[0].properties.enter.opacity = vincent.ValueRef(value=1) line.marks[0].marks[0].properties.update = vincent.PropertySet() line.marks[0].marks[0].properties.update.size = vincent.ValueRef(value=100) line.marks[0].marks[0].properties.hover = vincent.PropertySet() line.marks[0].marks[0].properties.hover.size = vincent.ValueRef(value=200) line.marks[0].marks[0].properties.update.size = vincent.ValueRef(value=100) line.marks[0].marks[0].properties.hover = vincent.PropertySet() line.marks[0].marks[0].properties.hover.size = vincent.ValueRef(value=200) line.scales['shape'] = vincent.Scale( name='shape', type='ordinal', domain=vincent.DataRef(data='table', field='data.col'), range=["square", "circle", "triangle-down", "triangle-up"]) line.marks[0].marks[0].properties.enter.shape = vincent.ValueRef( scale="shape", field="data.col") line.legends[0].shape = "shape" return line
df = pd.read_csv('data/us_county_data_vincent.csv', na_values=[' ']) df['FIPS_Code'] = df['FIPS'].astype(str) #Perform an inner join, pad NA's with data from nearest county merged = pd.merge(df, county_df, on='FIPS', how='inner') merged = merged.fillna(method='pad') # <codecell> geo_data = [{'name': 'counties', 'url': 'files/vincent_map_data/us_counties_intfips.topo.json', 'feature': 'us_counties.geo'}] vis = vincent.Map(data=merged, geo_data=geo_data, scale=1100, projection='albersUsa', data_bind='Employed_2011', data_key='FIPS', map_key={'counties': 'properties.FIPS'}) vis.marks[0].properties.enter.stroke_opacity = vincent.ValueRef(value=0.5) #Change our domain for an even inteager vis.scales['color'].domain = [0, 189000] vis.legend(title='Number Employed 2011') vis.to_json('unemployed.json', html_out=True, html_path='unemployed_map.html') # <codecell> vis.display() # <codecell>
import vincent as vince from pandasql import sqldf import matplotlib.pyplot as plt import numpy as np import json import csv freq_data = pd.read_csv('freq_data.csv', delimiter=',') pysqldf = lambda q: sqldf(q, globals()) ## use pandas dataframe to create Vincent frequency histogram vizualization to be passed to Vega later. q = """SELECT * FROM freq_data ORDER BY frequency DESC LIMIT 20;""" viz_data = pysqldf(q) vis = vince.Bar(viz_data, columns=['frequency'], key_on='word', height=400) vis.axes[0].properties = vince.AxisProperties(labels=vince.PropertySet( angle=vince.ValueRef(value=45), align=vince.ValueRef(value='left'))) #vis.padding['bottom'] = 90 vis.axis_titles(y='Frequency (%)', x='Word') vis.legend('20 Most Frequent Words in Tweets (%)') vis.to_json('vega.json') vis.colors(brew='RdPu') words = viz_data['word'] frequency = viz_data['frequency'] ##create matplotlib frequency histogram ind = np.arange(20) # the x locations for the groups width = 0.5 fig, ax = plt.subplots() rects1 = ax.bar(ind, frequency, width, color='#98FB98') ax.set_ylabel('(%) Frequency') ax.set_xticks(ind + width)
def plotrwtraversal(G, expensiveedges=[], time=None, countfrogs=False): data = [] index = [] for e in G.edges_iter(): key = edgetokey((e[0], e[1])) sr = pd.Series(G.edge[e[0]][e[1]]['timeline']) if not countfrogs: sr[sr > 0] = 1 data.append(sr) index.append(key) if time: data.append(pd.Series(time * [0])) index.append('hack') df = pd.DataFrame(data, index=index).T if time: del df['hack'] dfexp = df[expensiveedges].copy() dfcheap = df.copy() dfcheap.drop(expensiveedges, 1, inplace=True) dfexpcopy = dfexp.copy() dfexpcopy[dfexp > 0] = 1 cost = (dfexpcopy.sum(axis=1)).sum() G.graph['cost'] = cost if len(expensiveedges) > 0: datacost = [] indexcost = [] datacost.append(dfexp.sum(axis=1)) indexcost.append('Expensive') datacost.append(dfcheap.sum(axis=1)) indexcost.append('Cheap') dfcost = pd.DataFrame(datacost, index=indexcost).T finaldf = dfcost else: finaldf = df showteleportations = (len(G.graph['teleportations']) > 0) and countfrogs if showteleportations: # Count teleportations finaldf['Teleportation'] = pd.Series(G.graph['teleportations']) try: if countfrogs and len(G.graph['waiting']) > 0: finaldf['Waiting'] = pd.Series(G.graph['waiting']) except: pass line = vincent.StackedArea(finaldf) if not countfrogs: line.axis_titles(x='Rounds', y='# edges traversed') else: line.axis_titles(x='Rounds', y='# frogs') line.legend(title='Edge') line.width = 400 line.height = 300 if not countfrogs: line.scales[1].domain = [0, G.size()] line.marks[0].marks[0].properties.enter.opacity = vincent.ValueRef( value=0.8) if len(expensiveedges) > 0: if showteleportations: line.colors(range_=['#ff0000', '#50aa50', '#6060aa', '#eeeeee']) else: line.colors(range_=['#ff0000', '#50aa50', '#eeeeee']) line.marks[0].marks[0].properties.update = vincent.PropertySet() line.marks[0].marks[0].properties.update.size = vincent.ValueRef(value=100) line.marks[0].marks[0].properties.hover = vincent.PropertySet() line.marks[0].marks[0].properties.hover.size = vincent.ValueRef(value=200) line.marks[0].marks[0].properties.update.opacity = vincent.ValueRef( value=0.8) line.marks[0].marks[0].properties.hover.opacity = vincent.ValueRef(value=1) return line, finaldf
return res def word_occurences(data): #Define stopwords punctuation = list(string.punctuation) stop = stopwords.words('english') + stopwords.words('french') stop += ['les'] stop += punctuation stop += ['rt', 'via'] stop += list(string.ascii_letters) stop += ['...','…',"'",'’'] count_all = collections.Counter() count_all.update([word for tweet in tweets for word in tweet if word not in stop]) return count_all.most_common(25) if __name__ == '__main__': load_online_data() tweets = load_and_tokenize() top_occurences = word_occurences(tweets) labels, freq = zip(*top_occurences) data = {'data': freq, 'x': labels} bar = vincent.Bar(data, iter_idx='x') #rotate x axis labels ax = vincent.AxisProperties(labels = vincent.PropertySet(angle=vincent.ValueRef(value=90))) bar.axes[0].properties = ax #Export visu to JSON file bar.to_json('term_freq.json')