def create_soc_piechart(uid,fieldid,img_type): import matplotlib.pyplot as plt import numpy as np import os,sys import cv2 import csv import matplotlib.patches as mpatches plt.rcParams.update({'font.size':12}) lb='SOC: ' lb2 = 'SOC' labels = (lb+'less than 0.05%'),(lb+'0.05% to 0.075%'),(lb+'0.075% to 0.1%'),(lb+'0.1% to 0.112%'),(lb+'0.112% to 0.125%'),(lb+'0.125% to 0.137%'),(lb+'0.137% to 0.15%'),(lb+'0.15% to 0.2%'),(lb+'0.2% to 0.25%'),(lb+' more than 0.25%') cmap = ["#ab0535","#ea4f3b","#f7885a","#fbc07e","#fff0b5","#e6f3a4","#bae383","#81bf6c","#11a75f","#06653d"] file_name = uid + '/' + fieldid + '/IndexValues_'+img_type+'.csv' abc = 0 sqm = [] ind_values = [] explode = (0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2,0.2) with open(file_name,'rt') as f: data = csv.reader(f) for row in data: for values in row: if (abc>0) and (abc<11): sq1 = (float(values)/10000) sq1 = round(sq1,1) ind_values.append(sq1) ss = str(values) + ' sq. m.' sqm.append(ss) if abc==0: avg_ind_value = values if abc==11: field_area = values if abc==12: good_area = values if abc==13: avg_area = values if abc==14: bad_area = values abc = abc+1 fig1,ax1 = plt.subplots() print(labels) print(ind_values) sizes = ind_values #ax1.set_prop_cycle("color",cmap) #ax1.pie(ind_values,labeldistance = 2, explode=explode, labels=labels, autopct='%1.1f%%',shadow=True, startangle=90) ax1.axis('equal') # Equal aspect ratio ensures that pie is drawn as a circle. ax1.set_title(("Field Area (in square meters) with Different SOC%")) patch1 = mpatches.Patch(color="#ab0535",label=(labels[0]+' -> '+sqm[0])) patch2 = mpatches.Patch(color="#ea4f3b",label=(labels[1]+' -> '+sqm[1])) patch3 = mpatches.Patch(color="#f7885a",label=(labels[2]+' -> '+sqm[2])) patch4 = mpatches.Patch(color="#fbc07e",label=(labels[3]+' -> '+sqm[3])) patch5 = mpatches.Patch(color="#fff0b5",label=(labels[4]+' -> '+sqm[4])) patch6 = mpatches.Patch(color="#e6f3a4",label=(labels[5]+' -> '+sqm[5])) patch7 = mpatches.Patch(color="#bae383",label=(labels[6]+' -> '+sqm[6])) patch8 = mpatches.Patch(color="#81bf6c",label=(labels[7]+' -> '+sqm[7])) patch9 = mpatches.Patch(color="#11a75f",label=(labels[8]+' -> '+sqm[8])) patch10 = mpatches.Patch(color="#06653d",label=(labels[9]+' -> '+sqm[9])) ax1.legend(loc = 'center',handles=[patch10,patch9,patch8,patch7,patch6,patch5,patch4,patch3,patch2,patch1],title='Field Area with Different SOC%') plt.show() file_name = uid+ '/' + fieldid +'/'+img_type+'_piechart.png' plt.savefig(file_name) plt.close() pie_c = cv2.imread(file_name) pie_c = pie_c[100:400,120:500] cv2.imwrite(file_name,pie_c) return avg_ind_value,field_area