Advertisement
Guest User

Untitled

a guest
May 21st, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. def tweet_lengths(text):
  2. if len(text) < 100:
  3. return "short"
  4. elif 100 <= len(text) <= 135:
  5. return "medium"
  6. else:
  7. return "long"
  8.  
  9. tweets["tweet_length"] = tweets["text"].apply(tweet_lengths)
  10.  
  11. tl = {}
  12. for candidate in ["clinton", "sanders", "trump"]:
  13. tl[candidate] = tweets["tweet_length"][tweets["candidate"] == candidate].value_counts()
  14.  
  15. fig, ax = plt.subplots()
  16. width = .5
  17. x = np.array(range(0, 6, 2))
  18. ax.bar(x, tl["clinton"], width, color='g')
  19. ax.bar(x + width, tl["sanders"], width, color='b')
  20. ax.bar(x + (width * 2), tl["trump"], width, color='r')
  21.  
  22. ax.set_ylabel('# of tweets')
  23. ax.set_title('Number of Tweets per candidate by length')
  24. ax.set_xticks(x + (width * 1.5))
  25. ax.set_xticklabels(('long', 'medium', 'short'))
  26. ax.set_xlabel('Tweet length')
  27. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement