def pie_chart(request): suites = TestSuite.objects.raw('select *, max(date_run) ' 'from reporter_testsuite group by ' 'suite_name') plot_values = {'total': 0, 'passed': 0, 'skipped': 0, 'failed': 0} for suite in suites: plot_values['total'] += suite.num_tests plot_values['passed'] += suite.num_passed plot_values['skipped'] += suite.num_skipped plot_values['failed'] += suite.num_failed labels = 'Passed', 'Skipped', 'Failed' sizes = [ plot_values['passed'], plot_values['skipped'], plot_values['failed'] ] colors = ['#086788', '#FDF0D5', '#EB5E55'] explode = (0, 0.1, 0) fig = pyplot.figure(0) # Plot fig.add_subplot = pyplot.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=False, startangle=140) fig.add_axes = pyplot.axis('equal') figCanvas = FigureCanvas(fig) response = HttpResponse(content_type='image/png') figCanvas.print_png(response) return response
axs[0, 0].xaxis.set_major_locator(fmt_month) axs[0, 0].xaxis.set_major_formatter(date_form) fig.suptitle('Thailand COVID data up to ' + last_updated + '\n Data from https://github.com/djay/covidthailand') if plot_save == True: fig.savefig('full_figure.png') if plot_upload == True: canvas = FigureCanvas(fig) # renders figure onto canvas imdata = io.BytesIO( ) # prepares in-memory binary stream buffer (think of this as a txt file but purely in memory) canvas.print_png( imdata ) # writes canvas object as a png file to the buffer. You can also use print_jpg, alternatively s3.Object('covidviz', 'full_figure.png').put(Body=imdata.getvalue(), ContentType='image/png') s3.ObjectAcl('covidviz', 'full_figure.png').put(ACL='public-read') # narrow plot for mobile screen fig, axs = plt.subplots(6, 1, figsize=(6, 9.5), sharex=True) axs[0].bar(df['Date'], df['Cases'], 1, label='cases') axs[0].set_title(f'Daily New Cases: {latest_new_confirmed:,d}') axs[0].set(ylabel="") axs[0].yaxis.set_major_formatter(ticker.EngFormatter()) axs[1].bar(df['Date'], df['Tested'], 1, label='tested')