Skip to content

Commit 4be325f

Browse files
Updated code to print pie charts
1 parent 742ac6f commit 4be325f

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

main.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import sys,tweepy,csv,re
22
from textblob import TextBlob
3+
import matplotlib.pyplot as plt
4+
35

46
class SentimentAnalysis:
57

@@ -113,6 +115,7 @@ def DownloadData(self):
113115
print(str(snegative) + "% people thought it was strongly negative")
114116
print(str(neutral) + "% people thought it was neutral")
115117

118+
self.plotPieChart(positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, NoOfTerms)
116119

117120

118121
def cleanTweet(self, tweet):
@@ -121,12 +124,23 @@ def cleanTweet(self, tweet):
121124

122125
# function to calculate percentage
123126
def percentage(self, part, whole):
124-
return 100 * float(part) / float(whole)
127+
temp = 100 * float(part) / float(whole)
128+
return format(temp, '.2f')
125129

130+
def plotPieChart(self, positive, wpositive, spositive, negative, wnegative, snegative, neutral, searchTerm, noOfSearchTerms):
131+
labels = ['Positive [' + str(positive) + '%]', 'Weakly Positive [' + str(wpositive) + '%]','Strongly Positive [' + str(spositive) + '%]', 'Neutral [' + str(neutral) + '%]',
132+
'Negative [' + str(negative) + '%]', 'Weakly Negative [' + str(wnegative) + '%]', 'Strongly Negative [' + str(snegative) + '%]']
133+
sizes = [positive, wpositive, spositive, neutral, negative, wnegative, snegative]
134+
colors = ['yellowgreen','lightgreen','darkgreen', 'gold', 'red','lightsalmon','darkred']
135+
patches, texts = plt.pie(sizes, colors=colors, startangle=90)
136+
plt.legend(patches, labels, loc="best")
137+
plt.title('How people are reacting on ' + searchTerm + ' by analyzing ' + str(noOfSearchTerms) + ' Tweets.')
138+
plt.axis('equal')
139+
plt.tight_layout()
140+
plt.show()
126141

127142

128143

129144
if __name__== "__main__":
130145
sa = SentimentAnalysis()
131146
sa.DownloadData()
132-

0 commit comments

Comments
 (0)