1
+ import matplotlib .pyplot as plt
2
+ import numpy as np
3
+ from instagramy import InstagramUser
4
+ import sys
5
+
6
+ """
7
+ Usage:
8
+ python instalysis.py <text_file_of_usernames>
9
+ """
10
+
11
+ try :
12
+ filename = sys .argv [1 ]
13
+ print ("Extracting..." )
14
+ except (IndexError , KeyError ):
15
+ print ("List of username as textfile in arguement" )
16
+ sys .exit ()
17
+
18
+ usernames = []
19
+ file = open (filename , "r" )
20
+ for line in file :
21
+ if line != "\n " :
22
+ usernames .append (str (line ).strip ())
23
+ followers = []
24
+ following = []
25
+ posts = []
26
+
27
+ for username in usernames :
28
+ user = InstagramUser (username )
29
+ followers .append (user .number_of_followers )
30
+ following .append (user .number_of_followings )
31
+ posts .append (user .number_of_posts )
32
+
33
+ x = np .arange (len (usernames )) # the label locations
34
+ width = 0.25 # the width of the bars
35
+
36
+ fig , ax = plt .subplots ()
37
+ rects1 = ax .bar (x + 0.2 , followers , width , label = "Followers" )
38
+ rects2 = ax .bar (x , following , width , label = "Following" )
39
+ rects3 = ax .bar (x - 0.2 , posts , width , label = "Posts" )
40
+
41
+ # Add some text for labels, title and custom x-axis tick labels, etc.
42
+ ax .set_ylabel ("Popularity" )
43
+ ax .yaxis .set_visible (True )
44
+ ax .set_title ("Username" )
45
+ ax .set_xticks (x )
46
+ ax .set_xticklabels (usernames )
47
+ ax .legend ()
48
+
49
+
50
+ def autolabel (rects ):
51
+ """Attach a text label above each bar in *rects*, displaying its height."""
52
+ for rect in rects :
53
+ height = rect .get_height ()
54
+ ax .annotate (
55
+ "{}" .format (height ),
56
+ xy = (rect .get_x () + rect .get_width () / 2 , height ),
57
+ xytext = (0 , 3 ), # 3 points vertical offset
58
+ textcoords = "offset points" ,
59
+ ha = "center" ,
60
+ va = "bottom" ,
61
+ )
62
+
63
+
64
+ autolabel (rects1 )
65
+ autolabel (rects2 )
66
+ autolabel (rects3 )
67
+ fig .tight_layout ()
68
+ plt .show ()
0 commit comments