Dotacie¶

Zobraznie dotácii v grafoch¶

In [ ]:
import os
import sqlalchemy
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

db_name= os.environ.get('db_name')
db_pass= os.environ.get('db_pass')
db_host = os.environ.get('db_host')
                       
engine = sqlalchemy.create_engine("mysql+pymysql://" + db_name + ":" + db_pass + "@" + db_host + "/OpenDataLevice")
connection = engine.connect()

rok = "2019"
query = "select RokDotacie, Typ, ZiadatelMeno, SchvaleneProstriedky from subsidy where RokDotacie='"+ rok +"' order by Typ ASC, SchvaleneProstriedky ASC"
df = pd.read_sql_query(query, connection)
In [ ]:
subsidy = {} 
for index, row in df.iterrows():
    if row['Typ'] not in subsidy.keys():
     subsidy[row['Typ']] = {'ZiadatelMeno': [], 'SchvaleneProstriedky': []}

    subsidy[row['Typ']]['ZiadatelMeno'].append(row['ZiadatelMeno'])
    subsidy[row['Typ']]['SchvaleneProstriedky'].append(row['SchvaleneProstriedky'])
In [ ]:
%config InlineBackend.figure_format = 'retina'

matplotlib.style.use('fivethirtyeight')
plt.rcParams.update({'font.sans-serif':'Helvetica Neue'})

i=0
for x in subsidy:
  i = i+ 1
  df = pd.DataFrame(subsidy[x])
  pocet = len(df.index)
  title = x + " (" + rok + ")"
  ax = df.plot(x='ZiadatelMeno', y='SchvaleneProstriedky', xlabel='Suma (EUR)', ylabel='Žiadateľ', legend=False, title=title, kind='barh', figsize=(6,pocet/2.3))
  for rowNum,row in df.iterrows():
    val = int(row['SchvaleneProstriedky'])
    #print(val)
    xpos = 0
    xpos += int(val)
    ax.text(xpos + 1, rowNum-0.15, str(val) + " €", color='black')
    xpos = 0
  fig = ax.get_figure()
  fig.set_size_inches(10, pocet/2.3)
  fig.savefig("dotacie/" + rok + "/graf_"+ str(i) +".png", dpi=300, bbox_inches='tight')