histogram.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. import pandas as pd
  5. plt.rcParams.update({
  6. 'text.usetex': True,
  7. 'font.family': 'sans-serif',
  8. 'font.size': '24',
  9. })
  10. # function that takes names, filepaths and plot plot_id to produce histogram
  11. def plot_names_and_filepaths(names, filepaths, plot_id):
  12. # save all data in a dict
  13. all_data = dict()
  14. # extract data from files
  15. for i, (name, filepath) in enumerate(zip(names, filepaths)):
  16. with open(filepath) as fp:
  17. data = [ line.strip().split(';') for line in fp ]
  18. # notify user of tallied data
  19. print(name)
  20. print(data)
  21. # process list into dict
  22. for group, count in data:
  23. if not group in all_data:
  24. all_data[group] = [int(count)]
  25. else:
  26. all_data[group].append(int(count))
  27. # flatten dict to format fitting for dataframe (list of lists)
  28. all_data_list = []
  29. for key in all_data.keys():
  30. all_data_list.append([key] + all_data[key])
  31. print(all_data_list)
  32. # create dataframe in form of [[group, count1, count2, ...], ...]
  33. df = pd.DataFrame(all_data_list, columns=["Instruction Groups"] + names)
  34. # create actual plot
  35. ax = df.plot( \
  36. x="Instruction Groups", \
  37. kind="bar", \
  38. title="\# Instructions per group across different OS configurations", \
  39. logy=True, \
  40. rot=0, \
  41. #colormap='tab10', \
  42. width=0.7, \
  43. figsize=(20, 16))
  44. # modify plot to have grid in the background
  45. ax.grid(which='major', linestyle='-', linewidth='0.5') #, color='red')
  46. ax.grid(which='minor', linestyle='--', linewidth='0.4') #, color='red')
  47. ax.set_axisbelow(True)
  48. # extract figure and save to file
  49. fig = ax.get_figure()
  50. fig.savefig(f'histogram.{plot_id}.png')
  51. # increment plot id
  52. plot_id += 1
  53. names = [ 'FreeRTOS', 'Zephyr RTOS', 'buildroot 2022.08 Linux 5.17 (glibc + busybox)', 'buildroot 2022.05 Linux 5.18 (uClibc + busybox)' ]
  54. filepaths1 = [ 'count-rv32-freertos-base', 'count-rv32-zephyr-base', 'count-rv32-glibc-busybox-linux-base', 'count-rv32-uclibc-busybox-nommu-linux-base' ]
  55. filepaths2 = [ 'count-rv32-freertos-ext', 'count-rv32-zephyr-ext', 'count-rv32-glibc-busybox-linux-ext', 'count-rv32-uclibc-busybox-nommu-linux-ext' ]
  56. plot_names_and_filepaths(names, filepaths1, 0)
  57. plot_names_and_filepaths(names, filepaths2, 1)