Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # FOLLOWER SCATTER PLOT - note that this code depends on the follower CSV
- # being in reverse follow order, which is how the Twitter API currently
- # returns followers. It will not work correctly if the rows in the CSV
- # are rearranged.
- import pandas as pd
- import bokeh.plotting as bk
- def follower_scatter_plot (followers, handle, opacity_norm=5000, bubble_size=4,
- color=(0,90,180), cat_column=None, cat_colors=None,
- start=None, end=None,
- min_date=None, max_date=None,
- max_sample_size=120000):
- followers["createTime"] = pd.to_datetime (followers["createTime"])
- followers["order"] = followers.index
- followers["order"] = followers["order"].max () - followers["order"]
- df = followers[followers["createTime"] > pd.to_datetime ("2005-01-01")]
- zoomed = ""
- if start is not None:
- df = df[df["order"] >= start]
- zoomed = " (zoomed)"
- if end is not None:
- df = df[df["order"] < end]
- zoomed = " (zoomed)"
- if min_date is not None:
- min_date = pd.to_datetime (min_date)
- df = df[df["createTime"] >= min_date]
- zoomed = " (zoomed)"
- if max_date is not None:
- max_date = pd.to_datetime (max_date)
- df = df[df["createTime"] < max_date]
- zoomed = " (zoomed)"
- title = "@" + handle + " followers - follow order by creation date" + zoomed
- p = bk.figure (
- title=title,
- width=800, height=800, y_axis_type="datetime",
- x_axis_label="follow order", y_axis_label="creation date")
- if cat_colors is None or cat_column is None:
- if len (df.index) > max_sample_size:
- df = df.sample (max_sample_size)
- alpha = opacity_norm / len (df.index)
- p.circle (df["order"], df["createTime"], size=bubble_size,
- color=color, alpha=alpha)
- else:
- for label in cat_colors:
- df0 = df[df[cat_column] == label]
- df1 = df.sample (1)
- p.circle (df1["order"], df1["createTime"], size=bubble_size,
- color=cat_colors[label],
- legend=label + " (" + str (len (df0.index)) + " accounts)")
- p.circle (df1["order"], df1["createTime"], size=bubble_size,
- color=(255,255,255))
- if len (df.index) > max_sample_size:
- df = df.sample (max_sample_size)
- alpha = opacity_norm / len (df.index)
- df["color"] = df[cat_column].apply (lambda x: cat_colors[x])
- p.circle (df["order"], df["createTime"], size=4,
- color=df["color"], alpha=alpha)
- p.legend.location = "bottom_center"
- p.xaxis.axis_label_text_font_size = "15pt"
- p.yaxis.axis_label_text_font_size = "15pt"
- p.yaxis.major_label_text_font_size = "12pt"
- p.xaxis.major_label_text_font_size = "12pt"
- p.yaxis[0].formatter.hours = ["%H:%M"]
- p.yaxis[0].formatter.days = ["%Y-%m-%d"]
- p.title.text_font_size = "14pt"
- p.title.align = "center"
- p.xaxis[0].formatter.use_scientific = False
- return p
- #EXAMPLES
- # simple one color plot
- handle = "TwReloaded"
- df = pd.read_csv ( handle + "_followers.csv")
- p = follower_scatter_plot (df, handle, color=(140,0,70))
- bk.show (p)
- # plot with accounts with 0 likes highlighted
- handle = "ARTEM_KLYUSHIN"
- df = pd.read_csv ( handle + "_followers.csv")
- colors = {
- "has liked one or more tweets" : "#80a080",
- "has never liked a tweet" : "#f06000",
- }
- df["cat"] = df["likes"].apply (lambda x: "has never liked a tweet" if x == 0 \
- else "has liked one or more tweets")
- p = follower_scatter_plot (df, handle, cat_column="cat", cat_colors=colors)
- bk.show (p)
- # zoomed plot with accounts belonging to a specific group highlighted
- handle = "realchrisrufo"
- df = pd.read_csv ( handle + "_followers.csv")
- colors = {
- "@rrvvs fake follower network" : "#c00000",
- "other accounts" : "#80a080",
- }
- rrvvs = set (pd.read_csv ("rrvvs_network.csv")["id"])
- df["cat"] = df["id"].apply (lambda x: "@rrvvs fake follower network" \
- if x in rrvvs else "other accounts")
- p = follower_scatter_plot (df, handle, cat_column="cat", cat_colors=colors,
- start=0, end=10000)
- bk.show (p)
- # plot with accounts belonging to multiple groups highlighted
- handle = "AppSame"
- df = pd.read_csv ( handle + "_followers.csv")
- colors = {
- "@rrvvs fake follower network" : "#c00000",
- "@ARTEM_KLYUSHIN fake follower network 2" : "#008020",
- "other accounts" : "#a0a080",
- }
- art = set (pd.read_csv ("artem_network2.csv")["id"])
- df["cat"] = df["id"].apply (lambda x: \
- "@rrvvs fake follower network" if x in rrvvs else
- "@ARTEM_KLYUSHIN fake follower network 2" if x in art else "other accounts")
- p = follower_scatter_plot (df, handle, cat_column="cat", cat_colors=colors)
- bk.show (p)
Advertisement
Add Comment
Please, Sign In to add comment