def get_county_choropleth(start_datetime, end_datetime, output='json'): data = d.get_traffic_accident_by_date(start_datetime, end_datetime)['countyId'] county_names = d.get_county() data = data.value_counts() data.index = data.index.map(lambda p: county_names.loc[p]['name']) zeroes = pd.Series(data=0, index=county_names.name) data = data + zeroes data = data.fillna(0) data = data.sort_values() df = pd.DataFrame(dict(county=data.index, count=data.values)) geojson_file = os.path.join(os.path.dirname(__file__), 'regions_epsg_4326.geojson.txt') with open(geojson_file, encoding='utf-8') as file: geo_counties = json.loads(file.read()) fig = px.choropleth(data_frame=df, geojson=geo_counties, featureidkey='properties.NM4', locations='county', color='count', color_continuous_scale='tealrose', range_color=(0, df['count'].mean()*2), projection='sinusoidal', labels={'count':'Počet nehôd'}, hover_data={'county':False}, hover_name=df['county']) fig.update_geos(fitbounds="locations", visible=False) fig.update_layout( margin={"r":0,"t":0,"l":0,"b":0}, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', geo=dict(bgcolor= 'rgba(0,0,0,0)'), coloraxis_showscale=False, ) return plots.encode_plot(fig, output)
def get_plot_total_accidents_by_county(start_datetime, end_datetime, output='json'): acc = d.get_traffic_accident_by_date(start_datetime, end_datetime)['countyId'] acc = acc.value_counts() data = d.get_county() data['count'] = 0 data['count'] += acc data['count'] = data['count'].fillna(0) data.sort_values(by='count', inplace=True) fig = px.bar( data, x='name', y='count', custom_data=[data.index], labels={ 'count': 'Počet nehôd', 'name': 'Kraj' }, ) fig.update_layout( xaxis=dict( title_text='Kraj', titlefont=dict(size=20), ), height=600, yaxis=dict( title_text='Celkový počet nehôd', gridcolor='rgb(140,140,140)', titlefont=dict(size=20), ), dragmode=False, margin={ "r": 0, "t": 0, "l": 0, "b": 0 }, paper_bgcolor='rgba(0,0,0,0)', plot_bgcolor='rgba(0,0,0,0)', font=dict(size=14, ), ) return encode_plot(fig, output)
def get_plot_accident_trend_in_county(county_id, start_datetime=None, end_datetime=None): data = d.get_traffic_accident_by_date(get_start_datetime(start_datetime), get_end_datetime(end_datetime)) data = data.loc[data.countyId == county_id]['overallStartTime'] data = data.map(lambda p: p.date()) data = data.value_counts().sort_index() sns.set_style("whitegrid") fig = plt.figure(figsize=(25, 15)) plt.title("Vývoj nehôd v čase pre " + d.get_county().loc[county_id]['name'], fontsize=30, pad=20) plt.subplots_adjust(left=0.07, right=0.99) g = sns.lineplot(x=data.index, y=data.values) #g.set_xlabel("Deň",fontsize=30, labelpad=15) g.set_ylabel("Počet nehôd", fontsize=30, labelpad=20) #g.set_xticklabels(get_date_xtickslabels(data.index), rotation=90, fontsize=20) g.tick_params(axis='x', labelsize=25, rotation=45) g.tick_params(axis='y', labelsize=25) return encode_plot(fig)
def get_districts_in_groups_by_county(num_in_one_group): df = d.get_county() retval = [] for i, row in df.iterrows(): retval.append([row['name'], get_districts_in_groups(i, 4)]) return retval
def get_counties_in_groups(num_in_one_group): return form_groups(d.get_county(), 4)
def get_county_name(county_id): names = d.get_county() return names.loc[county_id]['name']
def get_county_xticklabels(index): retval = [] counties = d.get_county() for i in index: retval.append(counties.loc[i]['name']) return retval