Guest User

Untitled

a guest
Dec 13th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. from bokeh.plotting import figure, show
  2. import datetime
  3. import pandas as pd
  4.  
  5.  
  6. def read(filename):
  7. with open('vin_data.txt') as f:
  8. s = f.read()
  9.  
  10. return s
  11.  
  12.  
  13. def parse_string(s):
  14. x = s.split(',')
  15. year = int(x[0][-4:])
  16. month = int(x[1].split(' - ')[0])
  17. day = int(x[2].split(')')[0])
  18. vin = int(x[3])
  19. date = datetime.date(year, month, day)
  20.  
  21. return (date, vin)
  22.  
  23.  
  24. def parse_data(s):
  25. ls = s[2:-3].split('], [')
  26. lx = [parse_string(s) for s in ls]
  27.  
  28. return lx
  29.  
  30.  
  31. raw_data = read('vin_data.txt')
  32. list_of_tuples = parse_data(raw_data)
  33.  
  34. df_vin = pd.DataFrame(list_of_tuples, columns=['date', 'vin'])
  35. df_vin = df_vin.groupby('vin').apply(min)
  36. df_vin_unique = df_vin.groupby('date').count()
  37. df_vin_unique['cumulative'] = df_vin_unique['vin'].cumsum()
  38.  
  39. df_production = pd.DataFrame(
  40. {'date': [datetime.date(2017, x, 1) for x in [7, 8, 9, 10, 11, 12]],
  41. 'production': [0, 30, 75, 117, 145, 345]
  42. }
  43. )
  44.  
  45. df_production['cumulative'] = df_production['production'].cumsum()
  46.  
  47. f = figure(width=800,
  48. height=600,
  49. x_axis_type='datetime',
  50. title='Model 3 Estimated Sales vs. Unique Sightings Over Time')
  51. f.title.text_font_size = '18pt'
  52.  
  53. f.xaxis.axis_label='date'
  54. f.yaxis.axis_label='cumulative number'
  55.  
  56. f.line(df_vin_unique.index.values, df_vin_unique['cumulative'].values,
  57. legend='Cumulative number of unique sightings (based on VIN) [source: blfire.lima-city.de]',
  58. line_width=2)
  59.  
  60. f.line(df_production['date'].values, df_production['cumulative'].values, color='red',
  61. legend = 'Cumulative number of estimated sales (interpolated) [source: insideevs.com]',
  62. line_width=2)
  63.  
  64. f.legend.location = 'top_left'
  65.  
  66. show(f)
Add Comment
Please, Sign In to add comment