In [22]:
import plotly
import numpy as np
import plotly.graph_objs as go
import plotly.offline as py
import math
from plotly.graph_objs import *
plotly.offline.init_notebook_mode()
import time
from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)



from IPython.display import display
from IPython.display import HTML
import IPython.core.display as di # Example: di.display_html('<h3>%s:</h3>' % str, raw=True)

# This line will hide code by default when the notebook is exported as HTML
di.display_html('<script>jQuery(function() {if (jQuery("body.notebook_app").length == 0) { jQuery(".input_area").toggle(); jQuery(".prompt").toggle();}});</script>', raw=True)



def hide_numbers(a):
    return a[0] + ''.join(['x' for i in range(len(a)-1)])

data = {}
for line in open('ceu_kol.dat'):
        
    if 'horizon' not in line:    
        
        fields = line.strip().split('\t')
        user = fields[1]
        horizon = float(fields[5])
        popularity = float(fields[6])
        time = float(fields[-1])
        size = 10*int(math.log(float(fields[2])))
     
        #if horizon > 10000 and popularity > 10 and int(fields[2]) > 7:
        day = int(time/24.0)
        if day not in data:
            data[day] = [horizon]
        else:
            data[day].append(horizon)

            
            
                

    
    
    
#data.sort(key=lambda tup: tup[1])             

x_ = []
y_ = []
z_ = []
for day, hs in data.items():
    x_.append(day)
    z_.append(np.sum(y_))
    y_.append(np.sum(hs))
    



day_date0 = {29 : '03/28\nFirst report of \nlegislation', 30 : '03/29', 31 : '03/30', 32 : '03/31', 
            33 : '04/01', 34 : '04/02\nFirst \ndemonstration', 
            35 : '04/03',  
            36 : '04/04\nLegislation passed', 
            37 : '04/05\Chain of solidarity', 
            38 : '04/06', 39 : '04/07', 
            40 : '04/08', 
            41 : '04/09\nSecond demonstration', 
            42 : '04/10\nLaw signed \ninto effect'}

day_date = {29 : '03/28 First report of legislation', 30 : '03/29', 31 : '03/30', 32 : '03/31', 
            33 : '04/01', 34 : '04/02 First demonstration', 
            35 : '04/03',  
            36 : '04/04 Legislation passed', 
            37 : '04/05 Chain of solidarity', 
            38 : '04/06', 39 : '04/07', 
            40 : '04/08', 
            41 : '04/09 Second demonstration', 
            42 : '04/10 Law signed into effect',
            43 : '04/11',
            44 : '04/12',
            45 : '04/13',
            46 : '04/14',
            47 : '04/15',
            48 : '04/16',
            49 : '04/17',
            50 : '04/18',
            51 : '04/19',
            52 : '04/20',
            53 : '04/21',
            54 : '04/22',
            55 : '04/23',
            56 : '04/24      Comission negotiatins',
            57 : '04/25 Thunderclap, EU',
            58 : '04/26 EU Comission negotiatins',
            59 : '04/27',
            60 : '04/28',
            61 : '04/29',
            62 : '04/30'   
           }


days_x = []
days_all = []
for line in open('kol_days.dat'):
    
    fields = line.strip().split(' ')
    date = int(fields[0])-27

    days_x.append(date)
    days_all.append(int(fields[1]))
  



days_first = []
for line in open('kol_first_days.dat'):
    fields = line.strip().split(' ')
    days_first.append(int(fields[1]))
    

layout=Layout(
    yaxis1=YAxis(
        type = 'log'
    )
)


data = [go.Bar(
        x=days_x,
        y=days_all
)]


datess =  [day_date[i+29] for i in range(len(day_date))]
                                            
timestamps = datess


 
#map(lambda x:x[1], sorted(map(lambda a:[map(int,a.split('/')),a], timestamps)))    
    

total_cum = np.cumsum(days_all)
news_cum = np.cumsum(days_first)

total_full =[]#= [int(total_cum[i+1]) + int(days_first[i])  for i in range(len(days_first)-1)]  
for i in range(len(news_cum)):
   
    new = news_cum[i]
    if i > 0:
        total_full.append( new + total_cum[i-1])
    
    
    
total_full.append(total_cum[-1]+news_cum[-1]-days_first[-1])    


nnn =[0,566309,3512604,10044213,15765560,18664906,33529502,51340426,62933897,69256620,71394147,72692204,74974694,85606424]


datata = []

for i in range(len(days_all)):
    datata.append((days_all[i], day_date[i+29]))

 
datata2 = []
for i in range(len(days_first)):
    datata2.append((days_first[i], day_date[i+29]))
    
trace1 = go.Bar(
    x = [i[1] for i in datata],
    y = [i[0] for i in datata],#nnn,
    marker=dict(
        color='rgba(55, 128, 191, 0.7)',
        line=dict(
            color='rgba(55, 128, 191, 1.0)',
            width=2,
        )
    ),
    name='Old users'
)


trace2 = go.Bar(
    x = [i[1] for i in datata2],
    y = [i[0] for i in datata2],#nnn,
    marker=dict(
        color='rgba(219, 64, 82, 0.7)',
        line=dict(
            color='rgba(219, 64, 82, 1.0)',
            width=2,
        )
    ),
    name='New users'
)


data = [trace1, trace2]
layout = go.Layout(
    title = 'Daily social reach on Twitter  -  #IstandwithCEU',
    barmode='stack',
    bargap = 0.4,
    xaxis = dict(
        tickangle = 27
    ),
    yaxis=dict(
        title ='Potential reach'
    ),
    margin=go.Margin(
    b = 120
    ),
    legend=dict(
        x=0,
        y=1,
        traceorder='normal',
        font=dict(
            family='sans-serif',
            size=12,
            color='#000'
        ),
        bgcolor='rgba(245, 246, 249, 0.8)',
        bordercolor='#FFFFFF',
        borderwidth=2
    ),
    plot_bgcolor='rgba(250, 250, 252, 0.8)'
)
from IPython.display import Image

fig = go.Figure(data=data, layout=layout)  
plotly.plotly.image.save_as(fig, filename='a-simple-plot.png')
py.iplot(fig, filename='axes-range-type')


#31k tweet 
#py.iplot(data, filename='basic-bar')
In [ ]: