Advertisement
Guest User

cruyff8's barplot

a guest
Oct 9th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.87 KB | None | 0 0
  1. import datetime
  2. import pandas as pd
  3. #import math
  4. import numpy as np
  5. import matplotlib.pyplot as plt
  6.  
  7. # download and process data
  8. relevant = pd.io.parsers.read_csv('http://hasan.d8u.us/links.py.cgi?format=csv')
  9.  
  10. data = np.zeros_like(relevant)
  11. for i, t in enumerate(relevant['Timestamp']):
  12.     data[i] = int(datetime.datetime.fromtimestamp(t).strftime('%H'))
  13.  
  14. # histogram
  15. bins = np.arange(0, 25)    # hours of the day
  16. hist = np.histogram(data, bins)[0]
  17.  
  18. # plot
  19. fig = plt.figure(figsize=(6, 4.5), dpi=120)
  20. ax = fig.add_subplot(111)
  21.  
  22. bar_width = 0.8
  23. hours = bins[:-1] - bar_width * 0.5 # discard last bin boundary and move bars
  24.  
  25. ax.bar(hours, hist, bar_width, color='#2040ff', lw=0.8)
  26. ax.set_xlabel('Hour of day')
  27. ax.set_xlim(-1, 24)
  28. ax.set_xticks(range(0, 24, 3))
  29. ax.set_title('What hour were links sent out?', y=1.03)
  30.  
  31. plt.savefig('barplot.png', dpi=400)
  32. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement