LineChart.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http:#www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import matplotlib
  15. # fix can't draw figure with docker
  16. matplotlib.use('Agg')
  17. import matplotlib.pyplot as plt # noqa: E402 - matplotlib.use('Agg') need to be before this
  18. # candidate colors
  19. LINE_STYLE_CANDIDATE = ['b-o', 'r-o', 'k-o', 'm-o', 'c-o', 'g-o', 'y-o',
  20. 'b-s', 'r-s', 'k-s', 'm-s', 'c-s', 'g-s', 'y-s']
  21. def draw_line_chart(file_name, title, x_label, y_label, data_list):
  22. """
  23. draw line chart and save to file.
  24. :param file_name: abs/relative file name to save chart figure
  25. :param title: chart title
  26. :param x_label: x-axis label
  27. :param y_label: y-axis label
  28. :param data_list: a list of line data.
  29. each line is a dict of ("x-axis": list, "y-axis": list, "label": string)
  30. """
  31. plt.figure(figsize=(12, 6))
  32. plt.grid(True)
  33. for i, data in enumerate(data_list):
  34. plt.plot(data["x-axis"], data["y-axis"], LINE_STYLE_CANDIDATE[i], label=data["label"])
  35. plt.xlabel(x_label)
  36. plt.ylabel(y_label)
  37. plt.legend(fontsize=12)
  38. plt.title(title)
  39. plt.tight_layout(pad=3, w_pad=3, h_pad=3)
  40. plt.savefig(file_name)
  41. plt.close()