SYNC COMMIT

This commit is contained in:
Sebastian Lenzlinger 2024-01-06 14:03:43 +01:00
parent def2784231
commit 96dbb54e50

View File

@ -9,6 +9,7 @@ import numpy as np
logging.getLogger("matplotlib").setLevel(logging.WARNING) logging.getLogger("matplotlib").setLevel(logging.WARNING)
# TODO # TODO
def plt_acc_by_year(db): def plt_acc_by_year(db):
acc_year_sql = """ acc_year_sql = """
@ -18,10 +19,10 @@ def plt_acc_by_year(db):
""" """
result = db.execute_query(acc_year_sql) result = db.execute_query(acc_year_sql)
result_df = pd.DataFrame(result) result_df = pd.DataFrame(result)
plt.barh(result_df['accidentyear'],result_df['count'])
plt.ylabel('Year') fig = px.bar(result_df, y='year', x='count', orientation='h', title='No. of Accidents per Year')
plt.xlabel('No. of Accidents') fig.write_image("fig/acc_by_year.png")
plt.show() fig.write_html("html/acc_by_year.png")
def plt_acc_by_weekday(db): def plt_acc_by_weekday(db):
@ -35,12 +36,18 @@ def plt_acc_by_weekday(db):
result = db.execute_query(acc_weekday_sql) result = db.execute_query(acc_weekday_sql)
result_df = pd.DataFrame(result) result_df = pd.DataFrame(result)
plt.barh(result_df['weekday'], result_df['count']) fig = px.bar(result_df, y='weekday', x='Count', orientation='h', title='No. of Accidents per Weekday')
plt.ylabel('Weekday') fig.write_image("fig/acc_by_weekday.png")
plt.xlabel('No. of Accidents') fig.write_html("html/acc_by_weekday.html")
plt.show()
def plt_acc_by_day_year(db):
acc_year_day_sql = """
SELECT accidentyear AS year, accidentweekday_en AS weekday, COUNT(*) AS count
FROM accidents
GROUP BY weekday, year
ORDER BY year, COUNT(*);
"""
def plt_acc_by_daytime(db): def plt_acc_by_daytime(db):
@ -54,24 +61,27 @@ def plt_acc_by_daytime(db):
result = db.execute_query(acc_weekday_sql) result = db.execute_query(acc_weekday_sql)
result_df = pd.DataFrame(result) result_df = pd.DataFrame(result)
plt.barh(result_df['hour'], result_df['count'])
plt.ylabel('hour')
plt.xlabel('No. of Accidents')
plt.show()
fig = px.bar(result_df, y='hour', x='count', orientation='h') fig = px.bar(result_df, y='hour', x='count', orientation='h')
fig.write_image("fig/acc_by_day.png") fig.write_image("fig/acc_by_day.png")
fig.write_html("html/acc_by_day.html") fig.write_html("html/acc_by_day.html")
# Utilities ===========================================================================================================
def save_as_barplot(df, xname, yname, orientation, file_name):
pass
def save_as_html():
pass
if __name__ == "__main__": if __name__ == "__main__":
remote_db = RemoteDB() remote_db = RemoteDB()
try: try:
#plt_acc_by_year(remote_db) # plt_acc_by_year(remote_db)
#plt_acc_by_weekday(remote_db) # plt_acc_by_weekday(remote_db)
plt_acc_by_daytime(remote_db) plt_acc_by_daytime(remote_db)
except Exception as e: except Exception as e:
print(f"Exception {e} in plots.py") print(f"Exception {e} in plots.py")
finally: finally:
remote_db.close() remote_db.close()