Advertisement
Guest User

plot drillinge

a guest
Jan 31st, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. import re
  2.  
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import matplotlib.cm as cm
  6.  
  7. def jahr_zweistellig(jahr):
  8.     if jahr is None:
  9.         return None
  10.  
  11.     return int(jahr) if len(jahr) > 2 else int('20'+jahr)
  12.  
  13.  
  14. data = []
  15. with open('data.txt', 'r') as f:
  16.     for line in f:
  17.         line = line.rstrip()
  18.  
  19.         m = re.match(
  20.             '(?P<slot>[0-9]+)[.]?[ ]+' +
  21.             '(?P<user>[^,;]*)[,;]\s*' +
  22.             '((?P<modell>.*?)[,;]\s*)+?' +
  23.             '(B[jJ][.]?\s*((?P<baujahr_monat>[0-9]{2,4})/)?(?P<baujahr_jahr>[0-9]{2,4}),\s*)?' +
  24.             '(EZ[.]?\s*((?P<ez_monat>[0-9]{2,4})/)?(?P<ez_jahr>[0-9]{2,4}),\s*)?' +
  25.             '((?P<zellen>(80|88))(\s*Zellen|er),\s*)?'
  26.             '((?P<km>[0-9.,]+)\s*(km)?,?\s*)?'
  27.             '((?P<ah>[0-9.,]+)\s*([aA][hH]),?\s*)?'
  28.             '.*', line)
  29.  
  30.         if m:
  31.             g = m.groupdict()
  32.             data.append({
  33.                     'Ah': None if g['ah'] is None else float(g['ah'].replace(',', '.')),
  34.                     'BJ': jahr_zweistellig(g.get('baujahr_jahr')),
  35.                     'EZ': jahr_zweistellig(g.get('ez_jahr')),
  36.                     'km': int(re.sub('[^0-9]', '', g.get('km'))) if g.get('km') is not None else None,
  37.             })
  38.             #print('    matched: '+line)
  39.         else:
  40.             print('Not matched: '+line)
  41.  
  42.  
  43.  
  44. x = {}
  45. y = {}
  46. for d in data:
  47.     if d['Ah'] is None or d['km'] is None:
  48.         continue
  49.  
  50.     jahr = d['BJ'] if d['BJ'] is not None else d['EZ']
  51.  
  52.     if jahr not in x:
  53.         x[jahr] = []
  54.         y[jahr] = []
  55.  
  56.     x[jahr].append(d['km']/1000)
  57.     y[jahr].append(d['Ah'])
  58.  
  59.  
  60. jahre = set(x.keys())
  61. jahre.remove(None)
  62. jahre = list(jahre)
  63. jahre.sort()
  64.  
  65. farben = cm.rainbow(np.linspace(0, 1, len(jahre)))
  66.  
  67. for jahr, farbe in zip(jahre, farben):
  68.     plt.scatter(x[jahr], y[jahr], color=farbe, label=jahr)
  69.  
  70. plt.xticks(np.arange(0,
  71.                      max(max(x.values())) + 1,
  72.                      10.0))
  73.  
  74. plt.xlabel('Kilometerstand * 1000')
  75. plt.ylabel('Kapazität (Ah)')
  76.  
  77. plt.legend()
  78.  
  79. plt.title('Drillinge')
  80. plt.savefig('plot.png', bbox_inches='tight', dpi=300)
  81.  
  82. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement