From 7156fe917a06fbcd115c5d08a929a794b01f7ccd Mon Sep 17 00:00:00 2001 From: Wojciech Wojnowski <wojciech.wojnowski@pg.edu.pl> Date: Mon, 8 Jun 2020 15:16:30 +0000 Subject: [PATCH] Added the "save annotated image" functionality --- AGREE.py | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 2 deletions(-) diff --git a/AGREE.py b/AGREE.py index 6accd9b..e422ff7 100644 --- a/AGREE.py +++ b/AGREE.py @@ -1,6 +1,6 @@ # AGREE, Analytical GREennEss Calculator # F. Pena-Pereira, W.Wojnowski, M. Tobiszewski 2020 -# v.0.4 (beta) +# v.0.5 (beta) # imports from tkinter import * @@ -444,6 +444,77 @@ def pieChart(): # weights, labels, colors # print(weight_12.get()) +# ************************************** + +# generate the pie chart (plot) with a circle decal in the middle, together with annotations explaining the numbered fields: +def annotatedPieChart(): # weights, labels, colors + + colors = [colorMapper(var_1), colorMapper(var_2), colorMapper(var_3), colorMapper(var_4), colorMapper(var_5), colorMapper(var_6), colorMapper(var_7), colorMapper(var_8), colorMapper(var_9), + colorMapper(var_10), colorMapper(var_11), colorMapper(var_12)] + + weights = [weight_1.get(), weight_2.get(), weight_3.get(), weight_4.get(), weight_5.get(), weight_6.get(), weight_7.get(), weight_8.get(), weight_9.get(), weight_10.get(), weight_11.get(), + weight_12.get()] + labels = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'] + + index_value = float(final_score.get()) + + fig, ax = plt.subplots(figsize=(5, 3), dpi=150) + ax.clear() + ax.axis('equal') + radius = 1.0 + pie2 = ax.pie(weights, radius=radius, colors=colors, labeldistance=(radius * 0.875), labels=labels, + rotatelabels=False, startangle=90, counterclock=False, + wedgeprops={"edgecolor": "black", 'linewidth': 1}, textprops={'fontsize': (radius * 10)}) + + plt.setp(pie2[1], rotation_mode="anchor", ha="center", va="center") + for tx in pie2[1]: + rot = tx.get_rotation() + tx.set_rotation(rot) + # if you want rotated labels: + # tx.set_rotation(rot+ 90 + (1 - rot // 180) * 180) + + circle = plt.Circle(xy=(0, 0), radius=(radius * 0.75), facecolor=colorMapper(index_value), edgecolor='black', + linewidth=1) + plt.gca().add_artist(circle) + + ax.text(0.5, 0.5, str(index_value), + verticalalignment='center', horizontalalignment='center', + transform=ax.transAxes, + color='black', fontsize=(radius * 40)) + + # create the annotations which are displayed next to the graph + textstr = '\n'.join(( + r'1. Sample treatment', + r'2. Sample amount', + r'3. Device positioning', + r'4. Sample prep. stages', + r'5. Automation, miniaturization', + r'6. Derivatization', + r'7. Waste', + r'8. Analysis throughput', + r'9. Energy consumption', + r'10. Source of reagents', + r'11. Toxicity', + r"12. Operator's safety")) + + props = dict(facecolor='white', alpha=0.0) + + # display the text (1.05 means the object will appear to the right of the plot): + text = ax.text(1.05, 0.83, textstr, transform=ax.transAxes, fontsize=10, verticalalignment='top', bbox=props) + fig.tight_layout() # for exporting a compact figure + + # save the image: + ftypes = [('PNG file', '.png'), ('All files', '*')] + filename = filedialog.asksaveasfilename(filetypes=ftypes, defaultextension='.png') + # save the plot in the specified path; the 'tight' option removes the whitespace from around the figure: + plt.savefig(filename, bbox_inches='tight') + + +# ************************************** +# add the save annotated plot functionality to the file dropdown menu: +FileMenu.add_command(label='Generate annotated result of the assessment', command=annotatedPieChart) + + # ************************************** # define a temporary function to test the printing of global variables: @@ -1322,7 +1393,7 @@ dropDown_10.grid(sticky='w', row=2, column=0, padx=8, pady=8) def change_dropdown_10(*args): global var_10 var_10 = None - if var_3_text.get() == 'select': + if var_10_text.get() == 'select': var_10 = 1.0 else: var_10 = var_10_text_choices[var_10_text.get()] -- GitLab