Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def tweet_lengths(text):
- if len(text) < 100:
- return "short"
- elif 100 <= len(text) <= 135:
- return "medium"
- else:
- return "long"
- tweets["tweet_length"] = tweets["text"].apply(tweet_lengths)
- tl = {}
- for candidate in ["clinton", "sanders", "trump"]:
- tl[candidate] = tweets["tweet_length"][tweets["candidate"] == candidate].value_counts()
- fig, ax = plt.subplots()
- width = .5
- x = np.array(range(0, 6, 2))
- ax.bar(x, tl["clinton"], width, color='g')
- ax.bar(x + width, tl["sanders"], width, color='b')
- ax.bar(x + (width * 2), tl["trump"], width, color='r')
- ax.set_ylabel('# of tweets')
- ax.set_title('Number of Tweets per candidate by length')
- ax.set_xticks(x + (width * 1.5))
- ax.set_xticklabels(('long', 'medium', 'short'))
- ax.set_xlabel('Tweet length')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement