Guest User

Twitter follower scatter plot

a guest
Jun 25th, 2021
3,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. # FOLLOWER SCATTER PLOT - note that this code depends on the follower CSV
  2. # being in reverse follow order, which is how the Twitter API currently
  3. # returns followers. It will not work correctly if the rows in the CSV
  4. # are rearranged.
  5.  
  6. import pandas as pd
  7. import bokeh.plotting as bk
  8.  
  9.  
  10. def follower_scatter_plot (followers, handle, opacity_norm=5000, bubble_size=4,
  11. color=(0,90,180), cat_column=None, cat_colors=None,
  12. start=None, end=None,
  13. min_date=None, max_date=None,
  14. max_sample_size=120000):
  15. followers["createTime"] = pd.to_datetime (followers["createTime"])
  16. followers["order"] = followers.index
  17. followers["order"] = followers["order"].max () - followers["order"]
  18. df = followers[followers["createTime"] > pd.to_datetime ("2005-01-01")]
  19. zoomed = ""
  20. if start is not None:
  21. df = df[df["order"] >= start]
  22. zoomed = " (zoomed)"
  23. if end is not None:
  24. df = df[df["order"] < end]
  25. zoomed = " (zoomed)"
  26. if min_date is not None:
  27. min_date = pd.to_datetime (min_date)
  28. df = df[df["createTime"] >= min_date]
  29. zoomed = " (zoomed)"
  30. if max_date is not None:
  31. max_date = pd.to_datetime (max_date)
  32. df = df[df["createTime"] < max_date]
  33. zoomed = " (zoomed)"
  34. title = "@" + handle + " followers - follow order by creation date" + zoomed
  35. p = bk.figure (
  36. title=title,
  37. width=800, height=800, y_axis_type="datetime",
  38. x_axis_label="follow order", y_axis_label="creation date")
  39. if cat_colors is None or cat_column is None:
  40. if len (df.index) > max_sample_size:
  41. df = df.sample (max_sample_size)
  42. alpha = opacity_norm / len (df.index)
  43. p.circle (df["order"], df["createTime"], size=bubble_size,
  44. color=color, alpha=alpha)
  45. else:
  46. for label in cat_colors:
  47. df0 = df[df[cat_column] == label]
  48. df1 = df.sample (1)
  49. p.circle (df1["order"], df1["createTime"], size=bubble_size,
  50. color=cat_colors[label],
  51. legend=label + " (" + str (len (df0.index)) + " accounts)")
  52. p.circle (df1["order"], df1["createTime"], size=bubble_size,
  53. color=(255,255,255))
  54. if len (df.index) > max_sample_size:
  55. df = df.sample (max_sample_size)
  56. alpha = opacity_norm / len (df.index)
  57. df["color"] = df[cat_column].apply (lambda x: cat_colors[x])
  58. p.circle (df["order"], df["createTime"], size=4,
  59. color=df["color"], alpha=alpha)
  60. p.legend.location = "bottom_center"
  61. p.xaxis.axis_label_text_font_size = "15pt"
  62. p.yaxis.axis_label_text_font_size = "15pt"
  63. p.yaxis.major_label_text_font_size = "12pt"
  64. p.xaxis.major_label_text_font_size = "12pt"
  65. p.yaxis[0].formatter.hours = ["%H:%M"]
  66. p.yaxis[0].formatter.days = ["%Y-%m-%d"]
  67. p.title.text_font_size = "14pt"
  68. p.title.align = "center"
  69. p.xaxis[0].formatter.use_scientific = False
  70. return p
  71.  
  72.  
  73. #EXAMPLES
  74.  
  75. # simple one color plot
  76.  
  77. handle = "TwReloaded"
  78. df = pd.read_csv ( handle + "_followers.csv")
  79. p = follower_scatter_plot (df, handle, color=(140,0,70))
  80. bk.show (p)
  81.  
  82.  
  83. # plot with accounts with 0 likes highlighted
  84.  
  85. handle = "ARTEM_KLYUSHIN"
  86. df = pd.read_csv ( handle + "_followers.csv")
  87. colors = {
  88. "has liked one or more tweets" : "#80a080",
  89. "has never liked a tweet" : "#f06000",
  90. }
  91. df["cat"] = df["likes"].apply (lambda x: "has never liked a tweet" if x == 0 \
  92. else "has liked one or more tweets")
  93. p = follower_scatter_plot (df, handle, cat_column="cat", cat_colors=colors)
  94. bk.show (p)
  95.  
  96.  
  97. # zoomed plot with accounts belonging to a specific group highlighted
  98.  
  99. handle = "realchrisrufo"
  100. df = pd.read_csv ( handle + "_followers.csv")
  101. colors = {
  102. "@rrvvs fake follower network" : "#c00000",
  103. "other accounts" : "#80a080",
  104. }
  105. rrvvs = set (pd.read_csv ("rrvvs_network.csv")["id"])
  106. df["cat"] = df["id"].apply (lambda x: "@rrvvs fake follower network" \
  107. if x in rrvvs else "other accounts")
  108. p = follower_scatter_plot (df, handle, cat_column="cat", cat_colors=colors,
  109. start=0, end=10000)
  110. bk.show (p)
  111.  
  112.  
  113. # plot with accounts belonging to multiple groups highlighted
  114.  
  115. handle = "AppSame"
  116. df = pd.read_csv ( handle + "_followers.csv")
  117. colors = {
  118. "@rrvvs fake follower network" : "#c00000",
  119. "@ARTEM_KLYUSHIN fake follower network 2" : "#008020",
  120. "other accounts" : "#a0a080",
  121. }
  122. art = set (pd.read_csv ("artem_network2.csv")["id"])
  123. df["cat"] = df["id"].apply (lambda x: \
  124. "@rrvvs fake follower network" if x in rrvvs else
  125. "@ARTEM_KLYUSHIN fake follower network 2" if x in art else "other accounts")
  126. p = follower_scatter_plot (df, handle, cat_column="cat", cat_colors=colors)
  127. bk.show (p)
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment