#!/usr/bin/env python # coding: utf-8 # In[3]: get_ipython().run_line_magic('cd', '..') # In[1]: import pandas as pd import plotly.plotly as py import plotly.tools as tls from plotly.graph_objs import * get_ipython().run_line_magic('matplotlib', 'inline') # In[4]: df = pd.read_csv('data/TR-headlines.csv',usecols=['newsdate','title','twcount'],parse_dates=[0]) df # In[7]: daily = df.groupby('newsdate').sum() daily.plot(); # In[6]: # In[10]: data = Data([Scatter(x=daily.index,y=daily.twcount)]) twc = df.groupby('newsdate').apply(lambda t: t[t.twcount == t.twcount.max()]) titles = twc[twc.newsdate.isin(daily.sort('twcount',ascending=False).head(11).index.values)] titles = titles.sort('twcount',ascending=False) titles = titles.reset_index(drop=True) titles = pd.merge(titles,daily,left_on='newsdate',right_index=True) titles.rename(columns={'twcount_x': 'twcount', 'twcount_y': 'twdaily'}, inplace=True) titles # In[11]: def annotator(r): if r['newsdate'] == pd.to_datetime('2013-09-30'): y = r['twdaily']-10 elif r['newsdate'] == pd.to_datetime('2013-06-02'): y = r['twdaily']-10 else: y=r['twdaily']+40 return Annotation(x=r['newsdate'], y=y, xref='x', yref='y', text=r['title'], showarrow=False) # In[13]: annotations = Annotations(map(annotator,titles.to_dict(orient='records'))) layout = Layout(title="News Commentary Tweets of Turkish Elites (Jan 2013 - Jan 2015)", autosize=True,annotations=annotations,yaxis=YAxis(title='Daily tweet counts')) fig = Figure(data=data, layout=layout) py.iplot(fig,filename="News Commentary Tweets of Turkish Elites") # In[17]: #these tweets belong to... df = pd.read_csv('data/TR-tweets.csv',encoding='utf-8') df.columns # In[18]: tweeps = df.groupby(by='twhandle')['twtext'].count().order(ascending=False).head(100) tweeps.plot(); # In[21]: data = Data([Scatter(x=tweeps.index.values,y=tweeps.values)]) layout = Layout(title="Tweet Counts of Turkish Influencers (Jan 2013 - Jan 2015)", autosize=True,yaxis=YAxis(title='Total tweet counts')) fig = Figure(data=data,layout=layout) py.iplot(fig)