functionality to create barchars with matplotlib.pyplot

This commit is contained in:
schrom01 2023-09-04 21:05:05 +02:00
parent 7398bffda6
commit 70c6c0d63d
1 changed files with 51 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import tkinter import tkinter
import threading import threading
import matplotlib.pyplot as plt
import selenium.webdriver.firefox.service import selenium.webdriver.firefox.service
import webdriver_manager.chrome import webdriver_manager.chrome
@ -27,7 +28,7 @@ DropDownLabels = ["Street: ", "Number: ", "Postal code: ", "City: ", "coordinate
file_split_char = "," file_split_char = ","
minRoofSize = 2 minRoofSize = 2
minSuitability = 3 minSuitability = 3
electricityPrice = 0.1 electricityPrice = 0.198
OptionList = [] OptionList = []
exit = False exit = False
stopThread = False stopThread = False
@ -67,12 +68,15 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
image_folder_map = "screenshots_map/" image_folder_map = "screenshots_map/"
image_folder_production = "screenshots_production/" image_folder_production = "screenshots_production/"
image_folder_qrcode = "qrcodes/" image_folder_qrcode = "qrcodes/"
image_folder_barcharts = "barcharts/"
if(not os.path.exists(image_folder_map[:-1])): if(not os.path.exists(image_folder_map[:-1])):
os.makedirs(image_folder_map[:-1], exist_ok=False) os.makedirs(image_folder_map[:-1], exist_ok=False)
if(not os.path.exists(image_folder_production[:-1])): if(not os.path.exists(image_folder_production[:-1])):
os.makedirs(image_folder_production[:-1], exist_ok=False) os.makedirs(image_folder_production[:-1], exist_ok=False)
if (not os.path.exists(image_folder_qrcode[:-1])): if (not os.path.exists(image_folder_qrcode[:-1])):
os.makedirs(image_folder_qrcode[:-1], exist_ok=False) os.makedirs(image_folder_qrcode[:-1], exist_ok=False)
if (not os.path.exists(image_folder_barcharts[:-1])):
os.makedirs(image_folder_qrcode[:-1], exist_ok=False)
startTime = datetime.datetime.now() startTime = datetime.datetime.now()
address_count = len(adress_list) address_count = len(adress_list)
@ -89,6 +93,8 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
found = True found = True
line = adress_list[i] line = adress_list[i]
adress = line.split(file_split_char) adress = line.split(file_split_char)
barChartMonth = [0] * 12
barChartData = [0] * 12
if((line != adress_list[0]) and (adress[columnIndexes[6]] == "") and (((len(adress[columnIndexes[0]]) > 0) and (len(adress[columnIndexes[1]]) > 0) and (len(adress[columnIndexes[2]]) > 0) and (len(adress[columnIndexes[3]]) > 0)) or ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0)))): if((line != adress_list[0]) and (adress[columnIndexes[6]] == "") and (((len(adress[columnIndexes[0]]) > 0) and (len(adress[columnIndexes[1]]) > 0) and (len(adress[columnIndexes[2]]) > 0) and (len(adress[columnIndexes[3]]) > 0)) or ((len(adress[columnIndexes[4]]) > 0) and (len(adress[columnIndexes[5]]) > 0)))):
address_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]] address_string = adress[columnIndexes[0]] + " " + adress[columnIndexes[1]] + " " + adress[columnIndexes[2]] + " " + adress[columnIndexes[3]]
print("searching for Address: " + address_string) print("searching for Address: " + address_string)
@ -166,7 +172,11 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
suitabilities = [] suitabilities = []
bestRoofEnergy = 0 bestRoofEnergy = 0
bestRoof = response_all_roofs_decoded[0] bestRoof = response_all_roofs_decoded[0]
barChartMonth = response_all_roofs_decoded[0]['attributes']['monate']
barChartData = [0] * 12
for roof in response_all_roofs_decoded: for roof in response_all_roofs_decoded:
for i in range(12):
barChartData[i] = barChartData[i] + float(roof['attributes']['monats_ertrag'][i]) * float(roof['attributes']['flaeche']) * electricityPrice
energy = float(roof['attributes']['gstrahlung'])*0.2*0.8 energy = float(roof['attributes']['gstrahlung'])*0.2*0.8
if(energy > bestRoofEnergy): if(energy > bestRoofEnergy):
bestRoofEnergy = energy bestRoofEnergy = energy
@ -175,7 +185,8 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
sumEnergyRoofs = sumEnergyRoofs + energy sumEnergyRoofs = sumEnergyRoofs + energy
sumRoofArea = sumRoofArea + float(roof['attributes']['flaeche']) sumRoofArea = sumRoofArea + float(roof['attributes']['flaeche'])
suitabilities.append(int(roof['attributes']['klasse'])) suitabilities.append(int(roof['attributes']['klasse']))
print(barChartMonth)
print(barChartData)
featureId = str(bestRoof['featureId']) featureId = str(bestRoof['featureId'])
url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + featureId + "&lang=de" url = "https://www.uvek-gis.admin.ch/BFE/sonnendach/index.html?featureId=" + featureId + "&lang=de"
suitability = max(suitabilities) suitability = max(suitabilities)
@ -214,8 +225,11 @@ def search_adresses(adress_list, filename_adresslist, driver, mainText):
if(found and createImages.get() > 0 and roof_is_suitable and (not roof_has_solarpanel)): if(found and createImages.get() > 0 and roof_is_suitable and (not roof_has_solarpanel)):
image_filename = address_string + ".png" image_filename = address_string + ".png"
create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode,
url) barchart_color = "red"
create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, image_folder_barcharts,
url, barChartMonth, barChartData, barchart_color)
else: else:
image_filename = "not-created" image_filename = "not-created"
@ -324,7 +338,7 @@ def create_roof_info(roofs):
info_text = info_text + str(i + 1) + ". Fläche\n" + "Potenzielle Energie: " + str(roofs[i]['attributes']['gstrahlung']*0.2*0.8) + "\nFläche: " + str(roofs[i]['attributes']['flaeche']) + "\nEignung: " + str(roofs[i]['attributes']['klasse']) + " (" + str(roofs[i]['attributes']['klasse_text']).split("##")[0] + ")\n\n" info_text = info_text + str(i + 1) + ". Fläche\n" + "Potenzielle Energie: " + str(roofs[i]['attributes']['gstrahlung']*0.2*0.8) + "\nFläche: " + str(roofs[i]['attributes']['flaeche']) + "\nEignung: " + str(roofs[i]['attributes']['klasse']) + " (" + str(roofs[i]['attributes']['klasse_text']).split("##")[0] + ")\n\n"
return info_text return info_text
def create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, url): def create_images(address_string, driver, image_filename, image_folder_map, image_folder_production, image_folder_qrcode, image_folder_barcharts, url, barChartMonth, barChartData, barchart_color):
driver.get(url) driver.get(url)
time.sleep(2) time.sleep(2)
@ -378,6 +392,38 @@ def create_images(address_string, driver, image_filename, image_folder_map, imag
except: except:
pass pass
try:
barChartMonth.reverse()
barChartMonth = [str(item) for item in barChartMonth]
barChartData.reverse()
max_wert = max(barChartData)
min_wert = min(barChartData)
skala = max_wert + max_wert * 0.1
plt.figure(figsize=(7, 3))
plt.bar(barChartMonth, barChartData, color=barchart_color)
plt.ylabel('Stromproduktion in Franken', fontsize=12)
plt.ylim(0, skala)
# Turn off the frame (border) around the graphic
ax = plt.gca() # Get the current axis
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_visible(False)
plt.xticks(barChartMonth)
# Set the x-axis limits to remove extra space on the right
# plt.xlim(barChartMonth[0], barChartMonth[-1])
plt.savefig(f"{image_folder_barcharts}{image_filename}", bbox_inches='tight')
plt.show()
except:
pass
def read_adresslist(filename_adresslist): def read_adresslist(filename_adresslist):
returnvalues = [] returnvalues = []