task_stats.py 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import matplotlib.pyplot as plt
  2. import os
  3. async def get_task_stats_diagram(task_number, right_answers, all_answers, result):
  4. wrong_answers = all_answers - right_answers
  5. labels = [f'Верный ответ: {right_answers}', f'Неверный ответ: {wrong_answers}']
  6. values = [right_answers, wrong_answers]
  7. # Make figure and axes
  8. fig, ax = plt.subplots(1, 1)
  9. ax.set_title(f"Статистика по задаче {task_number}", fontsize=20)
  10. ax.set_xlabel(result, fontsize=12)
  11. # A standard pie plot
  12. patches, texts, autotexts = ax.pie(values,
  13. autopct='%.0f%%',
  14. textprops={'size': 'larger'},
  15. shadow=False, radius=0.95,
  16. explode=(0, 0.05))
  17. ax.legend(labels, loc='upper left', bbox_to_anchor=(0.7, 0.8))
  18. plt.setp(autotexts, size='x-large')
  19. # getting bytes from image
  20. plt.savefig('data/temp_task_files/task_stats.png')
  21. byte_img = open('data/temp_task_files/task_stats.png', mode='rb').read()
  22. plt.close()
  23. return byte_img