1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #!/usr/bin/python3
# -*- coding: utf-8 -*-
import re
import sys
from collections import defaultdict, OrderedDict
import matplotlib.pyplot as plt
PATTERN = re.compile(r'([0-9]+) (.+)\[([0-9]+)\] (.+)')
NBHOURS = 8760
COLORS = ['b', 'r', 'y', 'm', 'c']
NBCOLORS = len(COLORS)
def get_one(data):
for x in data:
mobj = re.match(PATTERN, x)
if not mobj:
continue
name = mobj.group(2)
index = int(mobj.group(3))
value = float(mobj.group(4))
yield name, index, value
def write(records, fw=sys.stdout):
towrite = list()
for rec, items in records.items():
for h in range(1, NBHOURS + 1):
towrite.append('{:2s}[{:4d}] {:12.6E}'.format(
rec, h, items.get(h, 0)))
fw.write('\n'.join(towrite))
def plot(records):
leg = list()
for idx_c, (name, values) in enumerate(records.items()):
plt.scatter(*zip(*values.items()), color=COLORS[idx_c % NBCOLORS])
leg.append(name)
plt.legend(leg, fontsize='small')
plt.show()
def main():
filename = 'data_example.txt'
with open(filename, 'r') as fr:
data = fr.read().splitlines()
records = defaultdict(OrderedDict)
for name, index, value in get_one(data):
records[name][index] = value
write(records)
plot(records)
if __name__ == '__main__':
main() |
Partager