# Perform sentiment analysis on the tweets and plot the results
from textblob import TextBlob
import matplotlib.pyplot as plt
import seaborn as sns
# Function to get the sentiment polarity
def get_sentiment(text):
return TextBlob(text).sentiment.polarity
# Apply the function to the tweet column
df['sentiment'] = df['tweet'].apply(get_sentiment)
# Plot the sentiment distribution
plt.figure(figsize=(10, 6))
sns.histplot(df['sentiment'], bins=30, kde=True)
plt.title('Sentiment Distribution of Tweets')
plt.xlabel('Sentiment Polarity')
plt.ylabel('Frequency')
plt.show()
print('Sentiment analysis and plot completed.')
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter