Advertisement
patryk

KCK - Zad1 na 3.0

Oct 7th, 2015
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import glob
  3. import csv
  4.  
  5. def str_to_float(string):
  6.     try:
  7.         string = float(string)
  8.     except ValueError:
  9.         pass
  10.     return string
  11.  
  12.  
  13. def read_file(filename):
  14.     arr = []
  15.     with open(filename, newline='') as csvfile:
  16.         handler = csv.reader(csvfile, delimiter=',')
  17.         for row in handler:
  18.             arr.append(row)
  19.     return arr 
  20.  
  21.  
  22. def prepare_array(arr):
  23.     new_arr = []
  24.     for row in arr:
  25.         temp = []
  26.         for cell in row:
  27.             temp.append(str_to_float(cell))
  28.         new_arr.append(temp)
  29.     return new_arr
  30.  
  31.  
  32. def prepare_plot(arr, lb):
  33.     x, y = [], []
  34.     for row in arr[1:]:
  35.         x.append(row[1])
  36.         y.append(sum(row[2:]) / float(len(row[2:])))
  37.     plt.plot(x, y, label=lb)
  38.  
  39.  
  40. def draw_plot():
  41.     plt.xlabel('Rozegranych gier')
  42.     plt.ylabel('Odsetek wygranych gier')
  43.     plt.title('Wizualizacja na 3.0')
  44.     plt.legend(loc=4)
  45.     plt.xlim([0,500000])
  46.     plt.show()
  47.  
  48.  
  49. def main():
  50.     for file_name in glob.glob('*.csv'):
  51.         arr = read_file(file_name)
  52.         new_arr = prepare_array(arr)
  53.         prepare_plot(new_arr, file_name[:-4])
  54.     draw_plot()
  55.  
  56.  
  57. if __name__ == '__main__':
  58.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement