title: Python Matplotlib Plot Hourly Data Saved in Defaultdict Line in File
body: <p>I have a file that contains a few thousand lines of data that looks like this:</p>
  
  <pre><code>defaultdict(&lt;type 'int'&gt;, {'2012021310': 76422, '2012021311': 94188, '2012021323': 139363, '2012021312': 111817, '2012021307': 71316, '2012021306': 82418, '2012021305': 65217, '2012021313': 127002, '2012021314': 141099, '2012021315': 147830, '2012021316': 136330, '2012021317': 122252, '2012021318': 118619, '2012021319': 115763, '2012021322': 137658, '2012021321': 130022, '2012021320': 121393, '2012021309': 69406, '2012021308': 66833}) 
  defaultdict(&lt;type 'int'&gt;, {'2012021408': 139745, '2012021409': 143658, '2012021414': 288286, '2012021418': 31216, '2012021416': 268214, '2012021400': 207878, '2012021401': 269296, '2012021402': 270258, '2012021403': 275882, '2012021404': 232521, '2012021405': 195062, '2012021406': 166669, '2012021407': 142855, '2012021417': 245582, '2012021411': 194360, '2012021413': 262078, '2012021410': 158954, '2012021415': 296457, '2012021412': 237083}) 
  defaultdict(&lt;type 'int'&gt;, {'2012021523': 676350, '2012021522': 670147, '2012021521': 650984, '2012021520': 617401, '2012021501': 170448, '2012021503': 246600, '2012021502': 250013, '2012021505': 363866, '2012021504': 300809, '2012021507': 333080, '2012021506': 370454, '2012021509': 343671, '2012021508': 330452, '2012021512': 549736, '2012021513': 622690, '2012021510': 387871, '2012021511': 456171, '2012021516': 647559, '2012021517': 600969, '2012021514': 692257, '2012021515': 706377, '2012021518': 579669, '2012021519': 587969}) 
  defaultdict(&lt;type 'int'&gt;, {'2012021608': 333986, '2012021609': 344126, '2012021602': 651692, '2012021603': 676458, '2012021600': 664484, '2012021601': 686408, '2012021620': 932692, '2012021621': 1065501, '2012021604': 589033, '2012021605': 465191, '2012021623': 1316907, '2012021606': 389669, '2012021607': 342613, '2012021619': 828190, '2012021618': 617836, '2012021622': 1111334, '2012021611': 467532, '2012021610': 387220, '2012021613': 634585, '2012021612': 560227, '2012021615': 718498, '2012021614': 704008, '2012021617': 606396, '2012021616': 665030})
  </code></pre>
  
  <p>The keys are dates/hours like '2012021310' is "02-13-2012 10am" the values are simply counts of an event that occurred. </p>
  
  <p>I'm have a decent understanding for plotting with matplotlib however I'm not sure how to address the data. Is there an easy way to read in each of these lines and address them in a plot similar to the way I would use csv2rec to read in two columns? Or is there a better way?</p>
  
  <p>edit:</p>
  
  <p>This replaces defaultdict(, with "data ="</p>
  
  <pre><code>import fileinput
  
  filein = 'list.txt'
  
  for line in fileinput.input([filein]):
          line = line.replace("defaultdict(&lt;type 'int'&gt;,", "data =")
          line = line.replace(")", " ")
          print line
  </code></pre>
  
  <p>this results in:</p>
  
  <pre><code>data = {'2012021310': 76422, '2012021311': 94188, '2012021323': 139363, '2012021312': 111817, '2012021307': 71316, '2012021306': 82418, '2012021305': 65217, '2012021313': 127002, '2012021314': 141099, '2012021315': 147830, '2012021316': 136330, '2012021317': 122252, '2012021318': 118619, '2012021319': 115763, '2012021322': 137658, '2012021321': 130022, '2012021320': 121393, '2012021309': 69406, '2012021308': 66833}  
  
  data = {'2012021408': 139745, '2012021409': 143658, '2012021414': 288286, '2012021418': 31216, '2012021416': 268214, '2012021400': 207878, '2012021401': 269296, '2012021402': 270258, '2012021403': 275882, '2012021404': 232521, '2012021405': 195062, '2012021406': 166669, '2012021407': 142855, '2012021417': 245582, '2012021411': 194360, '2012021413': 262078, '2012021410': 158954, '2012021415': 296457, '2012021412': 237083}  
  
  data = {'2012021523': 676350, '2012021522': 670147, '2012021521': 650984, '2012021520': 617401, '2012021501': 170448, '2012021503': 246600, '2012021502': 250013, '2012021505': 363866, '2012021504': 300809, '2012021507': 333080, '2012021506': 370454, '2012021509': 343671, '2012021508': 330452, '2012021512': 549736, '2012021513': 622690, '2012021510': 387871, '2012021511': 456171, '2012021516': 647559, '2012021517': 600969, '2012021514': 692257, '2012021515': 706377, '2012021518': 579669, '2012021519': 587969}  
  
  data = {'2012021608': 333986, '2012021609': 344126, '2012021602': 651692, '2012021603': 676458, '2012021600': 664484, '2012021601': 686408, '2012021620': 932692, '2012021621': 1065501, '2012021604': 589033, '2012021605': 465191, '2012021623': 1316907, '2012021606': 389669, '2012021607': 342613, '2012021619': 828190, '2012021618': 617836, '2012021622': 1111334, '2012021611': 467532, '2012021610': 387220, '2012021613': 634585, '2012021612': 560227, '2012021615': 718498, '2012021614': 704008, '2012021617': 606396, '2012021616': 665030}
  </code></pre>
  
  <p>I'm still not sure where to go from here</p>
  
  <p>edit:</p>
  
  <p>I've gotten much closer with a hybrid of everyone's suggestions:</p>
  
  <pre><code>import matplotlib
  matplotlib.use('Agg')
  import matplotlib.pyplot as plt
  import matplotlib.dates as mdates
  from pylab import *
  from matplotlib.mlab import csv2rec
  
      filein = 'list.txt'
      output_name = "image.png"
      dicts = []
      line = ""
  
      for line in fileinput.input([filein]):
              line = line.replace("defaultdict(&lt;type 'int'&gt;,", "data = ")
              line = line.replace(")", "")
              line = line.strip()
              exec(line)
              objects = data.items()
              print objects
  </code></pre>
  
  <p>which returns:</p>
  
  <pre><code>[('2012021307', 71316), ('2012021306', 82418), ('2012021305', 65217), ('2012021309', 69406), ('2012021310', 76422), ('2012021311', 94188), ('2012021312', 111817), ('2012021313', 127002), ('2012021314', 141099), ('2012021315', 147830), ('2012021316', 136330), ('2012021317', 122252), ('2012021318', 118619), ('2012021319', 115763), ('2012021308', 66833), ('2012021321', 130022), ('2012021320', 121393), ('2012021323', 139363), ('2012021322', 137658)]
  [('2012021408', 139745), ('2012021409', 143658), ('2012021403', 275882), ('2012021418', 31216), ('2012021400', 207878), ('2012021416', 268214), ('2012021402', 270258), ('2012021414', 288286), ('2012021404', 232521), ('2012021405', 195062), ('2012021406', 166669), ('2012021407', 142855), ('2012021417', 245582), ('2012021411', 194360), ('2012021401', 269296), ('2012021413', 262078), ('2012021410', 158954), ('2012021415', 296457), ('2012021412', 237083)]
  [('2012021523', 676350), ('2012021522', 670147), ('2012021521', 650984), ('2012021520', 617401), ('2012021501', 170448), ('2012021503', 246600), ('2012021502', 250013), ('2012021505', 363866), ('2012021504', 300809), ('2012021507', 333080), ('2012021506', 370454), ('2012021509', 343671), ('2012021508', 330452), ('2012021512', 549736), ('2012021513', 622690), ('2012021510', 387871), ('2012021511', 456171), ('2012021516', 647559), ('2012021517', 600969), ('2012021514', 692257), ('2012021515', 706377), ('2012021518', 579669), ('2012021519', 587969)]
  [('2012021605', 465191), ('2012021608', 333986), ('2012021609', 344126), ('2012021602', 651692), ('2012021603', 676458), ('2012021600', 664484), ('2012021601', 686408), ('2012021606', 389669), ('2012021607', 342613), ('2012021622', 1111334), ('2012021623', 1316907), ('2012021620', 932692), ('2012021621', 1065501), ('2012021619', 828190), ('2012021618', 617836), ('2012021604', 589033), ('2012021611', 467532), ('2012021610', 387220), ('2012021613', 634585), ('2012021612', 560227), ('2012021615', 718498), ('2012021614', 704008), ('2012021617', 606396), ('2012021616', 665030)]
  [('2012021605', 465191), ('2012021608', 333986), ('2012021609', 344126), ('2012021602', 651692), ('2012021603', 676458), ('2012021600', 664484), ('2012021601', 686408), ('2012021606', 389669), ('2012021607', 342613), ('2012021622', 1111334), ('2012021623', 1316907), ('2012021620', 932692), ('2012021621', 1065501), ('2012021619', 828190), ('2012021618', 617836), ('2012021604', 589033), ('2012021611', 467532), ('2012021610', 387220), ('2012021613', 634585), ('2012021612', 560227), ('2012021615', 718498), ('2012021614', 704008), ('2012021617', 606396), ('2012021616', 665030)]
  </code></pre>
  
  <p>So I'm definitely closer:</p>
  
  <p>edit:</p>
  
  <p>And further yet I have:</p>
  
  <pre><code>import fileinput
  import matplotlib
  matplotlib.use('Agg')
  import matplotlib.pyplot as plt
  import matplotlib.dates as mdates
  from pylab import *
  from matplotlib.mlab import csv2rec 
  
  filein = 'list.txt'
  output_name = "image.png"
  dicts = []
  times = []
  values = []
  
  line = ""
  for line in fileinput.input([filein]):
          line = line.replace("defaultdict(&lt;type 'int'&gt;,", "data = ")
          line = line.replace(")", "")
          line = line.strip()
          exec(line)
          for k in sorted(data.iterkeys()):
                  times.append(k)
                  values.append(data[k])
  
  fig = plt.figure()
  
  ax = fig.add_subplot(111)
  ax.plot(times, values)
  hours = mdates.HourLocator()
  fmt = mdates.DateFormatter('%Y - %M:%D:%H')
  ax.xaxis.set_major_locator(hours)
  ax.xaxis.set_major_formatter(fmt)
  
  fig.autofmt_xdate(bottom=0.2, rotation=90, ha='left')
  
  ax.grid()
  plt.savefig(output_name)
  </code></pre>
  
  <p>This gets me to the point where I've generated a plot at least. The issue now is that x access doesn't generate labels properly from the data being input. I believe that this is due to the somewhat unconventional time-stamp. Any last suggestions?</p>