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