def perfect_subset_overlap_summary(df: DataFrame) -> alt.Chart: """Summarize the count of instances of parent/child overlap by NLA (parent greater, equal, child greater) """ top_bar = alt.Chart(df).mark_bar().encode( x=alt.X('count(parent)', stack='normalize', axis=None, sort=[ 'parent nla greater than child', 'parent and child nla equal', 'parent nla less than child' ]), color=alt.Color('nla_variance', scale=alt.Scale(scheme='greys')), order=alt.Order('nla_variance', sort='ascending')).properties(width=600, height=75) text = alt.Chart(df).mark_text(dx=-15, dy=0, color='white').encode( x=alt.X('count(parent):Q', stack='normalize', axis=None, sort=[ 'parent nla greater than child', 'parent and child nla equal', 'parent nla less than child' ]), detail='nla_variance:N', text=alt.Text('count(parent):Q', format='.0f'), order=alt.Order('nla_variance', sort='ascending')) top_chart = top_bar + text return top_chart
def Joint_Prob_plot(df): year_min = df["cohort"].min() year_max = df["cohort"].max() slider = alt.binding_range(min=year_min, max=year_max, step=1) select = alt.selection_single(name="year", fields=['cohort'], bind=slider, init={'cohort': year_min}) df_new = df.apply(lambda row: gen_plot_df(row), axis=1) df_new = pd.concat(list(df_new)) base = alt.Chart(df_new) # chart on Joint Prob plot_scale = alt.Scale(type="pow", exponent=0.5, scheme="greens", nice=True) color = alt.Color('MR:Q', scale=plot_scale) joint_chart = base.mark_rect().encode( x="p:O", y=alt.Y('k:O', sort=alt.EncodingSortField('k', order='descending')), color=color).add_selection(select).transform_filter(select).properties( height=200, width=200) color_scale = alt.Scale( domain=['1', '2', '3', '4', '5'], range=['#4c78a8', '#f58518', '#e45756', '#72b7b2', '#54a24b']) p_mag_1 = base.mark_bar().encode( x=alt.X("p:O"), y=alt.Y('sum(MR):Q', scale=alt.Scale(domain=(0, 1))), color=alt.Color('k:O', scale=color_scale, legend=None), order=alt.Order(aggregate='sum', type="quantitative", sort='descending')).add_selection( select).transform_filter(select).properties( height=150, width=200) k_mag_1 = base.mark_bar().encode( y=alt.Y("k:O", sort=alt.EncodingSortField('k', order='descending')), x=alt.X('sum(MR):Q', scale=alt.Scale(domain=(0, 1))), color=alt.Color('p:O', scale=color_scale, sort=alt.EncodingSortField('p', order='ascending'), legend=None), order=alt.Order(aggregate='sum', type="quantitative", sort='descending')).add_selection( select).transform_filter(select).properties( height=200, width=150) people_c = people_plot(df, select).properties(width=300, height=300) return (p_mag_1 & (joint_chart | k_mag_1) & people_c).resolve_scale(color='independent')
def get_bar_chart(data: pd.DataFrame, plot_title: str, ax_title: str, marker_col: str, value_col: str, color_col:str, bar_width: int = 0): """Returns a altair barchart object""" tooltips = [value_col, marker_col] if 'Kanton' in data.columns: tooltips.append('Kanton') xax_title = 'Kantone' else: xax_title = '' # remove CH total if y_max == 0: scy = alt.Scale() else: scy = alt.Scale(domain=(0, y_max)) if marker_col == 'Date': xax=alt.X(f'{marker_col}:T', axis=alt.Axis(title='', labelAngle = 30, format="%d.%m")) elif marker_col == 'Sterbedatum': xax=alt.X(f'{marker_col}:T', axis=alt.Axis(title='', labelAngle = 30, format="%d.%m")) else: xax = alt.X(f"{marker_col}:O", axis = alt.Axis(title=xax_title)) if 'Kanton' in data.columns: clr = alt.condition( alt.datum[color_col] == 'BS', alt.value('orange'), alt.value('steelblue')) else: clr = alt.Color(color_col, scale=alt.Scale(scheme=cn.COLOR_SCHEMA)) if bar_width > 0: bar = alt.Chart(data).mark_bar(width = bar_width).encode( x=xax, y=alt.Y(f'{value_col}:Q', title=ax_title, scale=scy ), color = clr, order=alt.Order(color_col, sort='ascending'), tooltip=tooltips) else: bar = alt.Chart(data).mark_bar().encode( x=xax, y=alt.Y(f'{value_col}:Q', title=ax_title, scale=scy ), color = clr, order=alt.Order(color_col, sort='ascending'), tooltip=tooltips) # not sure why the mean is sometime lower than the minimum bar mean # rule = alt.Chart(data).mark_rule(color='red').encode( # y='mean({}):Q'.format(val_par) chart = bar.properties(width=plot_width, height=plot_height, title=plot_title) return chart
def show_genre_platform(df): st.write('## Popular Game Genres on each Platform') st.write( "**Let's see how game trends evolve on each platform. Are gamers' preference changing over the decades?**" ) columns = list(df["Platform"].unique()) select_box = alt.binding_select(options=columns, name='Select a Platform:') sel = alt.selection_single(fields=['Platform'], bind=select_box, init={'Platform': 'Wii'}) # stacked bar chart st.write( "💡 *You can select specific platform with the dropdown menu below*") st.write( "💡 *Hover over the genres to see their popularities in different regions*" ) st.write( alt.Chart(df).transform_filter(sel).mark_bar().encode( x=alt.X('Year:N'), y=alt.Y('sum(Global_Sales):Q', title='Sale (in millions)'), color=alt.Color('Genre', scale=alt.Scale(scheme='tableau20')), order=alt.Order('Genre', sort='ascending'), tooltip=[ 'Genre', 'sum(NA_Sales)', 'sum(EU_Sales)', 'sum(JP_Sales)', 'sum(Other_Sales)' ]).add_selection(sel).properties(width=800, height=500))
def show_game_publisher(df): st.write('## Top 20 Best-selling Game for each Company/Publisher') st.write("**What are the most popular games by each company/publisher?**") columns = list(df["Publisher"].unique()) select_box = alt.binding_select(options=columns, name='Select a Publisher:') sel = alt.selection_single(fields=['Publisher'], bind=select_box, init={'Publisher': 'Nintendo'}) # stacked bar chart st.write( "💡 *You can select specific publisher/company with the dropdown menu below*" ) st.write( "💡 *Hover over the game titles to see their popularities in different regions*" ) st.write( alt.Chart(df).transform_filter(sel).mark_bar().encode( x=alt.X('Year:N'), y=alt.Y('sum(Global_Sales):Q', title='Sale (in millions)'), color=alt.Color('Name', scale=alt.Scale(scheme='tableau20')), order=alt.Order('sum(Global_Sales)', sort='ascending'), tooltip=[ 'Name', 'Genre', 'sum(NA_Sales)', 'sum(EU_Sales)', 'sum(JP_Sales)', 'sum(Other_Sales)' ]).transform_window( rank="rank(Global_Sales)", sort=[alt.SortField("Global_Sales", order="descending") ]).transform_filter( (alt.datum.rank < 20)).add_selection(sel).properties( width=800, height=500))
def stacked_bar_chart(self): print(self.data) chart = ( alt.Chart(self.data, height=450).mark_bar().encode( x=alt.X("sum(percent)", stack="normalize", axis=alt.Axis(format="%")), y=alt.Y("source", axis=None), color="item", tooltip=list(self.data.columns), order=alt.Order( # Sort the segments of the bars by this field "percent", sort="ascending", ), ).configure_title(fontSize=20).configure_axis( labelFontSize=10, titleFontSize=10).configure_legend(labelFontSize=10, titleFontSize=10)) # text = alt.Chart(self.data).mark_text(dx=-5, dy=3, color='white').encode( # x=alt.X("sum(percent)", stack="normalize", axis=alt.Axis(format="%")), # y=alt.Y("source",axis=None), # detail='item:N', # text=alt.Text('item') # ) # full_chart = chart + text return st.altair_chart(chart, use_container_width=True)
def show_business_in_category(yelp_covid_bool_df, business_category_info): cat_info_cnt = pd.DataFrame() feature = st.selectbox("Select a Covid feature you are interested in: ", total_covid_feature) for cate in cate_list_multi: cat_info_cnt[cate] = yelp_covid_bool_df[ business_category_info[cate]].groupby(feature).agg( {'business_id': 'count'})['business_id'] cat_info_cnt.index = ['False', 'True'] cat_info_cnt = cat_info_cnt.stack().reset_index(level=[0, 1]) cat_info_cnt.columns = [feature, 'categories', 'count'] chart = alt.Chart(cat_info_cnt).mark_bar().encode( alt.X('count'), alt.Y('categories:N', sort='-x'), alt.Color(feature + ':N'), alt.Tooltip(['count', feature]), order=alt.Order(feature, sort='descending')).properties(height=200, width=700) st.altair_chart(chart) st.write( "Of course, different types of businesses react differently. For example, you can see a huge part of Reastaurants and Food support **delivery and takeout**, while **request a quote** is nearly exclusively supported by Home Services." )
def plot_play_count_graph(info_content_df: pd.DataFrame, info_userdata_df: pd.DataFrame, log_problem_df: pd.DataFrame): st.header("**♟** Difficulty Frequency **♟**") st.write( "This page contains basic exploratory data analyses for the purpose of getting a general feeling of what the data contains." ) chart = alt.Chart(info_content_df).mark_bar().encode( x='count()', y='learning_stage', color='difficulty', order=alt.Order( # Sort the segments of the bars by this field 'learning_stage', sort='ascending')).properties(width=1500, height=200) st.altair_chart(chart, use_container_width=True) st.header('Log Problem') st.write( 'From the chart below, we could see that the number of users attempt the question is getting lower.' ) datecount_df = (pd.to_datetime( log_problem_df['timestamp_TW'], format='%Y-%m-%d %H:%M:%S %Z').dt.floor('d').value_counts( ).rename_axis('date').reset_index(name='count').sort_values('date')) chart = alt.Chart(datecount_df).mark_line().encode(x='date', y='count') st.altair_chart(chart, use_container_width=True)
def show_game_series(df): st.write('## Popular Game Series') st.write("**Let's explore some of the most popular game series**") game_series = get_game_series(df) game_series_df, columns = get_game_series_df(df, game_series) select_box = alt.binding_select(options=columns, name='Select a Game Series:') sel = alt.selection_single(fields=['Series_Name'], bind=select_box, init={'Series_Name': 'Pokemon'}) # stacked bar chart st.write( "💡 *You can select specific game serie with the dropdown menu below*") st.write( "💡 *Hover over the game titles to see their popularities in different regions*" ) st.write( alt.Chart(game_series_df).transform_filter(sel).mark_bar().encode( x=alt.X('Year:N'), y=alt.Y('sum(Global_Sales):Q', title='Sale (in millions)'), color=alt.Color('Name', scale=alt.Scale(scheme='tableau20')), order=alt.Order('Name', sort='ascending'), tooltip=[ 'Name', 'Genre', 'sum(NA_Sales)', 'sum(EU_Sales)', 'sum(JP_Sales)', 'sum(Other_Sales)' ]).add_selection(sel).properties(width=800, height=500))
def comp_lapi_gest(df_passages_immat_ok, donnees_gest, camera, date_d='2019-01-28', date_f='2019-02-11'): """ Graph de comparaison entr eles donnes gestionnaire et les donnees lapi en entree : df_passages_immat_ok : df des passages valide cf donnees_postraitees.filtre_plaque_non_valable donnees_gest : df des donnes gestionnaires, cf donnees_postraitees.donnees_gest camera : entier : numero de camera en sortie : graph_comp_lapi_gest : chaetr altair de type bar """ # regrouper les données PL validées par jour et par camera nb_pl_j_cam = df_passages_immat_ok.groupby('camera_id').resample( 'D').count()['immat'].reset_index().rename(columns={'immat': 'nb_veh'}) nb_pl_j_cam['type'] = 'lapi' comp_traf_lapi_gest = pd.concat([nb_pl_j_cam, donnees_gest], sort=False) comp_traf_lapi_gest_graph = comp_traf_lapi_gest.loc[ comp_traf_lapi_gest.created.between(pd.to_datetime(date_d), pd.to_datetime(date_f))].copy() return alt.Chart(comp_traf_lapi_gest_graph.loc[ comp_traf_lapi_gest_graph['camera_id'] == camera], title=f'Camera {camera}').mark_bar(opacity=0.8).encode( x=alt.X('monthdate(created):O', axis=alt.Axis(title='date', labelAngle=80)), y=alt.Y('nb_veh:Q', stack=None, axis=alt.Axis(title='Nombre de PL')), color='type', order=alt.Order( "type", sort="ascending")).properties(width=500)
def country_plot(source): alt.themes.enable('dark') combined11 = source selection = alt.selection_single() country_chart = alt.Chart( combined11, title='Spread of Countries - GDP Vs Total Suicide Numbers').mark_point( filled=True).encode( alt.X('gdp_per_capita:Q'), alt.Y('suicides_no'), alt.Size('population:Q', scale=alt.Scale(range=[0, 2000]), legend=None), alt.Order('population:Q', sort='ascending'), tooltip=[ alt.Tooltip('country:N'), alt.Tooltip('suicides_no:Q'), alt.Tooltip('gdp_per_capita:Q'), alt.Tooltip('population') ], color=alt.condition( selection, 'country', alt.value('darkgrey'), scale=alt.Scale(scheme='blueorange'), legend=None)).add_selection(selection).configure_axis( grid=False) return country_chart.to_html()
def _parse_encoding_ecdf(encoding, complementary, sort): """Parse encoding for ECDF.""" if type(encoding) != dict: raise RuntimeError('`encoding` must be specified as a dict.') x, y, val = _make_xy_encoding_ecdf(encoding, complementary) # Need to run through color spec twice to get cat color = _make_color_encoding(encoding, None, sort) if color == Undefined: cat = None else: cat = _get_column_name(color) color = _make_color_encoding(encoding, cat, sort) # Build encoding for export encoding = { key: item for key, item in encoding.items() if key not in ['x', 'y', 'color'] } encoding['x'] = x encoding['y'] = y if cat is not None: encoding['color'] = color encoding['order'] = alt.Order(val + ':Q', sort='ascending') return encoding, cat, val
def graph_TV_jo_cam_norm(df_pct_pl_transit,uvp,coeff_uvp, *cam): """ graph de synthese de la proportion de pl en transit, TV, PL Locaux par heure. Base nb pl dir et pct_pl_transit lapi en entree : df_pct_pl_transit : df du nb de vehicules, issus de resultat.pourcentage_pl_camera uvp : booleen : si on veut le graph ne UVP ou non coeff_uvp : float : coefficientd'equivalence PL- UVP. utilse si uvp=True, sinon eu importe mais doit exister cam : integer : les cameras concernees en sortie : bar_nb_pl_dir_norm : chart altair avec la proportion de pl locaux, pl transit, tv """ concat_dir_trafic_normalisee=PL_transit_dir_jo_cam_normalise(df_pct_pl_transit,coeff_uvp, cam) #creation du titre if [voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)] : titre=f'Proportion de vehicules sur {[voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)][0]}' else : if len(cam)>1 : titre=f'Proportion de vehicules au droit des caméras {cam}' else : titre=f'Proportion de vehicules au droit de la caméra {cam[0]}' bar_nb_pl_dir_norm=alt.Chart(concat_dir_trafic_normalisee, title=titre).mark_bar().encode( x=alt.X('heure:O',axis=alt.Axis(title='Heure',titleFontSize=14,labelFontSize=14)), y=alt.Y('nb_pl:Q',stack='normalize', axis=alt.Axis(title='Proportions de vehicules',titleFontSize=14,labelFontSize=14,format='%')), color=alt.Color('type',sort=['PL locaux','PL en transit','Vehicules Legers'],legend=alt.Legend(title='Type de vehicules',titleFontSize=14,labelFontSize=14)), order=alt.Order('type', sort='ascending')).properties(width=800, height=400).configure_title(fontSize=18) return bar_nb_pl_dir_norm
def make_type_I_error_chart(results): df = pd.DataFrame(results) bars = alt.Chart(df).mark_bar(size=30).encode( y=alt.Y('test:N', title='Type of test', axis=alt.Axis(titleFontSize=18, labelFontSize=15)), x=alt.X('sum(error):Q', title='Probability of Type I error', axis=alt.Axis(titleFontSize=18, labelFontSize=15), stack='zero'), color=alt.Color('direction:N', legend=alt.Legend(title=None, labelFontSize=18, labelLimit=1000)), order=alt.Order('direction:N'), tooltip=alt.Tooltip(['test', 'direction', 'error'])) # text = alt.Chart().mark_text(color='black', size=15, dx=-20).encode( # y=alt.Y('test:N', title='Type of test',), # x=alt.X('error:Q', title='Probability of Type I error', stack='zero'), # text=alt.Text('error:Q', format='.3f'), # order=alt.Order('direction:N'), # tooltip=alt.Tooltip(['test', 'direction', 'error']) # ) rule = alt.Chart(pd.DataFrame( {'alpha': [.05]})).mark_rule(color='black').encode(x='alpha') return alt.layer(bars, rule).properties(height=300, width=600)
def plot_top_users_time(user, types=['RT', 'Mention', 'Text', 'Reply', 'Quoted', 'Like'] ): final = get_user_final_timeline(user, types) all_users_x_month = alt.Chart(final,width=400).mark_bar( cornerRadiusTopLeft=3, cornerRadiusTopRight=3 ).encode( x='yearmonth(created_at):O', y=alt.Y('count():Q'),#, sort=alt.SortField(field="count():Q", op="distinct", order='ascending')), color=alt.Color('screen_name:N',sort='-y'), order=alt.Order('count():Q') ) outlier_transparency = alt.Chart(final, width=400).mark_bar( cornerRadiusTopLeft=3, cornerRadiusTopRight=3, opacity=0.7, color='black' ).encode( x='yearmonth(created_at):O', y=alt.Y('count():Q'),#, sort=alt.SortField(field="count():Q", op="distinct", order='ascending')), opacity=alt.Opacity('outlier:O',sort='-y', scale=alt.Scale(range=[0.35,0])), order='order:N' ).transform_calculate( order="if (datum.outlier, 1, 0)" ) return alt.layer(all_users_x_month,outlier_transparency).resolve_scale(color='independent')
def get_map(self, day_str, title=""): chart_data = self._prepare_dataset(day_str) source = alt.topo_feature(data.world_110m.url, 'countries') background = alt.Chart(source).mark_geoshape( fill="lightgray", stroke="white").properties(width=1000, height=500).project("equirectangular") hover = alt.selection(type='single', on='mouseover', nearest=False, fields=[self.col_lat, self.col_long]) text = background.mark_text(dy=-5, align='right').encode( alt.Text(f'{self.col_name_countries}:N', type='nominal'), opacity=alt.condition(~hover, alt.value(0), alt.value(1))) points = alt.Chart(chart_data).mark_circle().encode( latitude=f"{self.col_lat}:Q", longitude=f"{self.col_long}:Q", size=alt.Size(f"{day_str}:Q", scale=alt.Scale(range=[0, 7000]), legend=None), order=alt.Order(f"{day_str}:Q", sort="descending"), tooltip=[f'{self.col_name_countries}:N', f'{day_str}:Q' ]).add_selection(hover).properties(title=title) chart = alt.layer(background, points, text) return chart
def make_bar_plot(category, damage): """ Generates a bar plot based on user selection of category and damage level Parameters ---------- category - a string; user selected from dropdown and passed by callback damage - a string; user selected from dropdown and passed by callback Return ------ an altair plot converted to html """ alt.themes.register('mds_special', mds_special) alt.themes.enable('mds_special') #alt.themes.enable('none') # to return to default query_string = "" for user_select_damage in damage: query_string += 'damage_level == "' + user_select_damage + '" | ' query_string = query_string[:-2] x_title = category.replace('_', ' ').title() main_title = 'Effect of ' + x_title + ' on Birdstrikes' if len(query_string) != 0: #generate a bar plot bar_plot = alt.Chart( df.query(query_string), title=main_title).mark_bar(opacity=0.3).encode( alt.X(category + ':O', axis=alt.Axis(title=x_title, labelAngle=0), sort=alt.EncodingSortField(field='damage_level_sort', op='count', order='ascending')), alt.Y('count(damage_level):Q', axis=alt.Axis(title="Bird Strikes"), stack=True), alt.Color( 'damage_level', scale=alt.Scale( domain=['Substantial', 'Medium', 'Minor', 'None'], range=['red', 'dodgerblue', 'grey', 'darkgreen']), legend=alt.Legend(orient='bottom', titleOrient='left', title="Damage Level", labelFontSize=15, titleFontSize=15)), alt.Order('damage_level_sort', sort='ascending'), alt.Tooltip(['count(damage_level)'])).properties(width=500, height=400) bar_plot = bar_plot.to_html() else: bar_plot = None return bar_plot
def make_line_plot(date_list, damage): query_string = "" for user_select_damage in damage: query_string += 'damage_level == "' + user_select_damage + '" | ' query_string = query_string[:-2] df_line = df.query('year >= @date_list[0] & year <= @date_list[1]') if len(query_string) != 0: label = alt.selection_single( encodings=['x'], # limit selection to x-axis value on='mouseover', # select on mouseover events nearest=True, # select data point nearest the cursor empty='none' # empty selection includes no data points ) line_plot_base = alt.Chart( df_line.query(query_string), title='Bird Strike Damage over Time' ).mark_area(opacity=0.3, interpolate='monotone').encode( alt.X('year:O', axis=alt.Axis(title="Year", labelAngle=0)), alt.Y('count(damage_level):N', axis=alt.Axis(title="Bird Strikes"), stack=None), alt.Color( 'damage_level', #sort = ['Substantial', 'Medium', 'Minor', 'None'], scale=alt.Scale( domain=['Substantial', 'Medium', 'Minor', 'None'], range=['red', 'dodgerblue', 'grey', 'darkgreen']), legend=alt.Legend(title="Damage Level")), #orient = 'none', #legendX = 675, legendY = 10, #fillColor = 'white')), alt.Order('damage_level_sort', sort='ascending')) line_plot = alt.layer( line_plot_base, alt.Chart().mark_rule(color='grey').encode( x='year:O').transform_filter(label), line_plot_base.mark_circle().encode(opacity=alt.condition( label, alt.value(1), alt.value(0))).add_selection(label), line_plot_base.mark_text( align='left', dx=5, dy=-10, stroke='grey', strokeWidth=1).encode( text='count(damage_level):N').transform_filter(label), line_plot_base.mark_text(align='left', dx=5, dy=-10).encode( text='count(damage_level):N').transform_filter(label), data=df).properties(width=650, height=400) line_plot = line_plot.to_html() else: line_plot = None return line_plot
def make_gdp_perc_chart(year=2018, stat_type='Export'): ''' Create a bar chart that shows Imports/Exports (Dynamic based on switch/callback) as a percentage of GDP in the year selected (based on year slider), and show the highest 15. Parameters ----------- year: integer [1988, 2018] the year for which data is to be displayed - controlled by slider, default is 2018 stat_type: string one of 'Import' or 'Export' determines whether this graph will show imports or exports as a percentage of GDP, default is 'Export', and controlled by switch Returns ----------- gdp_perc_chart: chart bar chart showing stat_type as a percentage of GDP for the specified year Example ----------- > make_gdp_perc_chart(2017, 'Import') ''' countries = ['USA', 'Italy', 'Spain', 'Germany', 'Czech Rep.', 'Brazil', 'Norway', 'Switzerland', 'Turkey', 'Canada', 'Japan', 'Croatia', 'United Kingdom', 'France'] # Wrangling specific to this chart: df_for_perc_of_gdp = arms_gdp[ # (arms_gdp['Country'].isin(countries)) & (arms_gdp['Year'] == year) & (arms_gdp['Direction'] == stat_type)].sort_values(by='percent_GDP', ascending=False).head(15) # df_for_perc_of_gdp['percent_GDP'] = df_for_perc_of_gdp['percent_GDP'] * 100 # Make the chart: gdp_perc_chart = alt.Chart(df_for_perc_of_gdp).mark_bar().encode( alt.X('Country:N', sort=alt.EncodingSortField(field='percent_GDP', order='descending'), title='Country', axis=alt.Axis(labelAngle=45)), alt.Y('percent_GDP:Q', title='Arms Trade as a % of GDP', # scale=alt.Scale(domain=(0, (0.2 if stat_type == 'Import' else 0.5))) ), alt.Color('percent_GDP:Q', scale=alt.Scale(scheme='goldorange'), legend=None), alt.Order(shorthand=['percent_GDP'], sort='descending'), alt.Tooltip(['Country', 'percent_GDP']) ).configure_bar(color='orange' ).properties(width=920, height=230, background='white', title="Arms Trade as a Percentage of GDP for Major " + stat_type + "ers in %d" % ( year)) return gdp_perc_chart
def stack_bar_chart(): rl_vio = doc(0) #rl_vio["YEAR"] = rl_vio["YEAR"].astype("int") source = rl_vio[rl_vio["YEAR"]>2014] crash_type = ["FAILING TO REDUCE SPEED TO AVOID CRASH", "FAILING TO YIELD RIGHT-OF-WAY", "FOLLOWING TOO CLOSELY", "IMPROPER LANE USAGE", "IMPROPER OVERTAKING/PASSING"] st.sidebar.title("What causes the accidents?") select1 = st.sidebar.selectbox("Choose the crash type: ", crash_type) select2 = st.sidebar.selectbox("Choose the year: ", [2015,2016,2017,2018,2019,2020,2021]) st.text("Go back to see all types of causes? Click 'View All'!") if st.button("View All"): cha = alt.Chart(source).mark_bar(size=20).encode( alt.Tooltip(["YEAR:O", "MONTH:O", "sum(RECORDS)"]), alt.Y('YEAR:O', title="Year",axis=alt.Axis(grid=False, labelAngle=0)), alt.X('sum(RECORDS)', axis=alt.Axis(grid=False, labelAngle=0), title="Records"), color="CAUSE", order=alt.Order( # Sort the segments of the bars by this field 'CAUSE', sort='ascending' )).properties( height=400, width=850).transform_filter( alt.FieldOneOfPredicate(field='CAUSE', oneOf=crash_type) ).interactive() else: if select1 in crash_type: cha = alt.Chart(source).mark_bar(size=20).encode( alt.Tooltip(["CAUSE:N", "sum(RECORDS)"]), alt.Y('YEAR:O', axis=alt.Axis(grid=False, labelAngle=0), title="Year"), alt.X('sum(RECORDS)', axis=alt.Axis(grid=False, labelAngle=0,tickMinStep = 1), title="Records"), color=alt.value("#e7ba52") ).properties( height=400, width=600 ).transform_filter( alt.datum.CAUSE == select1 ) if select2: cha = alt.Chart(source).mark_bar(size=20).encode( alt.Tooltip(["YEAR:O", "MONTH:O", "sum(RECORDS)"]), alt.Y('MONTH:O', axis=alt.Axis(grid=False, labelAngle=0), title="Month"), alt.X('sum(RECORDS)', axis=alt.Axis(grid=False, labelAngle=0,tickMinStep = 1), title="Records"), color=alt.value("darkgray") ).properties( height=400, width=600).transform_filter( alt.datum.CAUSE == select1).transform_filter(alt.datum.YEAR == select2) return cha
def plot_stacked_bars(df): rnd = df.essrnd.iloc[0] year = '2002' if rnd == 1 else '2014' var = df.columns[1] annot = questions[var] return alt.Chart(df).mark_bar().encode( alt.X(var, scale=alt.Scale(domain=[0, 1])), y='cntry', order=alt.Order('response', sort='ascending'), color=alt.Color('response:O', legend=alt.Legend(title='Response'), scale=color_scale)).properties( title=f"{annot[0]} * {year} * {annot[1]}")
def kaart_plot(cn6_x): # Title st.subheader("International trade flows to and from Europe in 2019") x = pd.read_csv('data/edges_puhastus2019.csv', sep=';') x = x.loc[:, ~x.columns.str.contains('^Unnamed')] x = remap_cn6(x) countries = pd.read_csv('countries.csv', sep=';') world = alt.topo_feature(data.world_110m.url, 'countries') x = x[x["cn6"] == cn6_x] conns = x select_country = alt.selection_single(on="mouseover", nearest=True, fields=["origin"], empty="none") lookup_data = alt.LookupData(countries, key="country", fields=["name", "latitude", "longitude"]) background = alt.Chart(world).mark_geoshape( fill="lightgray", stroke="white").properties(width=1000, height=800).project(type='naturalEarth1') connections = alt.Chart(conns).mark_rule(opacity=0.35).encode( latitude="latitude:Q", longitude="longitude:Q", latitude2="lat2:Q", longitude2="lon2:Q").transform_lookup( lookup="origin", from_=lookup_data).transform_lookup( lookup="destination", from_=lookup_data, as_=["country", "lat2", "lon2"]).transform_filter(select_country) points = alt.Chart(conns).mark_circle().encode( latitude="latitude:Q", longitude="longitude:Q", size=alt.Size("connections:Q", scale=alt.Scale(range=[0, 400]), legend=None), order=alt.Order("connections:Q", sort="descending"), tooltip=["origin:N", "connections:Q"]).transform_aggregate( connections="count()", groupby=["origin"]).transform_lookup( lookup="origin", from_=lookup_data).add_selection(select_country) st.altair_chart((background + connections + points).configure_view(stroke=None).resolve_scale()) # Description st.markdown(""" International trade flows to and from EU in 2019. The size of the country node shows the number of trade flows to and from other countries. Data: [Comext](https://ec.europa.eu/eurostat/estat-navtree-portlet-prod/BulkDownloadListing?sort=1&dir=comext) yearly data. """)
def plotClassOrder(self): alt.data_transformers.disable_max_rows() chart2 = alt.Chart(self.dataFrame).mark_bar().encode( x=alt.X('sum(number)', type='quantitative', title='Total Number'), y=alt.Y('clasS', type='nominal', title='Class', sort=alt.SortField('number', order='descending')), color='type', order=alt.Order( # Sort the segments of the bars by this field 'type', sort='ascending')) chart2.display()
def make_chart_topic_trends( topic_trends, arxiv_cat_lookup, year_sort=2020, save=True, fig_n=4 ): """Topic trend chart""" # Sort topics by the year of interest topics_sorted = ( topic_trends.loc[[x.year == year_sort for x in topic_trends["date"]]] .groupby("topic_cat")["value"] .sum() .sort_values(ascending=False) .index.tolist() ) topic_trends["order"] = [ [n for n, k in enumerate(topics_sorted) if x == k][0] for x in topic_trends["topic_cat"] ] # Create clean category names topic_trends["topic_cat_clean"] = [ arxiv_cat_lookup[x][:50] + "..." for x in topic_trends["topic_cat"] ] # Create clean topic sorted names topics_sorted_2 = [arxiv_cat_lookup[x][:50] + "..." for x in topics_sorted] evol_sh = ( alt.Chart(topic_trends) .mark_bar(stroke="grey", strokeWidth=0.1) .encode( x="date:T", y=alt.Y("value", scale=alt.Scale(domain=[0, 1])), color=alt.Color( "topic_cat_clean", sort=topics_sorted_2, title="Source category", scale=alt.Scale(scheme="tableau20"), legend=alt.Legend(columns=2), ), order=alt.Order("order", sort="descending"), tooltip=["topic_cat_clean"], ) ).properties(width=400) if save is True: save_altair(evol_sh, f"fig_{fig_n}_topic_trends", driv) return evol_sh
def graph_TV_jo_cam(df_pct_pl_transit,uvp,coeff_uvp, *cam): """ graph de synthese du nombre de pl en trasit, TV et pl totaux par heure. Base nb pl dir et pct_pl_transit lapi en entree : df_pct_pl_transit : df du nb de vehicules, issus de resultat.pourcentage_pl_camera uvp : booleen : si on veut le graph ne UVP ou non coeff_uvp : float : coefficientd'equivalence PL- UVP. utilse si uvp=True, sinon eu importe mais doit exister cam : integer : les cameras concernees en sortie : bar_nb_pl_dir : chart altair avec le nb pl, nb pl transit, tv """ concat_dir_trafic=PL_transit_dir_jo_cam(df_pct_pl_transit,coeff_uvp, cam)[0] #creation du titre if [voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)] : titre=f'Nombre de véhicules sur {[voie for voie, cams in dico_corrsp_camera_site.items() if cams==list(cam)][0]}' else : if len(cam)>1 : titre=f'Nombre de véhicules au droit des caméras {cam}' else : titre=f'Nombre de véhicules au droit de la caméra {cam[0]}' if not uvp : concat_dir_trafic=concat_dir_trafic.loc[concat_dir_trafic.type.isin(['Tous PL','PL en transit','Tous Vehicules'])].copy() bar_nb_pl_dir=alt.Chart(concat_dir_trafic, title=titre).mark_bar().encode( x=alt.X('heure:O',axis=alt.Axis(title='Heure',titleFontSize=14,labelFontSize=14)), y=alt.Y('nb_pl:Q',stack=None, axis=alt.Axis(title='Nombre de vehicules',titleFontSize=14,labelFontSize=14)), color=alt.Color('type',sort=['Tous vehicules', 'Tous PL','PL en transit'],legend=alt.Legend(title='Type de vehicules',titleFontSize=14,labelFontSize=14)), order=alt.Order('type', sort='descending')).properties(width=800, height=400).configure_title(fontSize=18) else : concat_dir_trafic=concat_dir_trafic.loc[concat_dir_trafic.type.isin(['UVP Tous PL','UVP PL en transit','UVP Tous Vehicules'])].copy() bar_nb_pl_dir=alt.Chart(concat_dir_trafic, title=titre).mark_bar().encode( x=alt.X('heure:O',axis=alt.Axis(title='Heure',titleFontSize=14,labelFontSize=14)), y=alt.Y('nb_pl:Q',stack=None, axis=alt.Axis(title='Nombre de vehicules',titleFontSize=14,labelFontSize=14)), color=alt.Color('type',sort=['UVP Tous vehicules', 'UVP Tous PL','UVP PL en transit'],legend=alt.Legend(title='Type de vehicules',titleFontSize=14,labelFontSize=14)), order=alt.Order('type', sort='descending')).properties(width=800, height=400).configure_title(fontSize=18) return bar_nb_pl_dir
def plot_kanban(self, tasks: List[Task], sort: List[str] = None, group: str = None) -> None: logging.info(f"ALTAIR KANBAN PLOT. TASKS #: {len(tasks)}") group = group or 'state' detail = 'owner' if group == 'state' else 'state' task_dict_list = [{ 'weight': 1, **vars(task) } for task in tasks if not task.summary] counts = Counter(task_dict[group] for task_dict in task_dict_list).most_common() _, max_depth = next(iter(counts)) source = pd.DataFrame(task_dict_list) title = f"Kanban Chart | {date.today()}" block_height = 50 block_width = block_height * 8 base = alt.Chart(source).mark_bar(color='black').encode( x=alt.X(group, axis=alt.Axis(orient='top', labelAngle=0, labelFontSize=15), sort=sort), y=alt.Y('sum(weight)', sort='descending', stack='zero'), order=alt.Order('id', sort='ascending')).properties( title=title, width=block_width * len(counts), height=block_height * max_depth) bars = base.encode(color=alt.Color('id:N', legend=None)) text = base.mark_text(dy=-(block_height * 0.33), color='black').encode(text='name') info = base.mark_text(dy=-(block_height * 0.67), dx=(block_width * 0.3), color='#2F4F4F').encode(text=detail) chart = bars + text + info output_file = str(Path(self.plot_dir).joinpath('kanban.html')) chart.save(output_file)
def plot_components(df, placeholder): df = df.melt(id_vars='Timestamp', var_name='Components', value_name='Power') c = alt.Chart(df).transform_joinaggregate( order='sum(Power)', groupby=['Components']).mark_area().encode( x='Timestamp:T', y='Power:Q', color=alt.Color('Components:N', legend=alt.Legend(orient="bottom")), order=alt.Order('order:Q', sort='descending'), tooltip=['Components', 'hoursminutes(Timestamp)', 'Power']) placeholder.altair_chart(c, use_container_width=True) return placeholder
def createStackPlot(df): return alt.Chart(df).mark_bar().encode( x=alt.X('Segundos:Q', axis=alt.Axis(title=None)), #hoursminutes( y=alt.Y('Name:N', axis=alt.Axis(grid=False, title=None), sort=alt.EncodingSortField(field="Segundos", op="sum", order='ascending')), color=alt.Color( 'Classe:N', sort=['SwimN', 'T1N', 'BikeN', 'T2N', 'RunN'], scale=alt.Scale(scheme='tableau20') ), #scale=alt.Scale(range=['#96ceb4', '#BF820E','#4BA55E', '#4FA7A5', '#CBBE00']) tooltip=['Tempo:N'], order=alt.Order('Order', sort='ascending')).configure_view( strokeOpacity=0).properties(height=400, width=900)
def plot_play_count_graph(info_content_df: pd.DataFrame): chart = alt.Chart(info_content_df).mark_bar().encode( x='count()', y='learning_stage', color='difficulty', order=alt.Order( # Sort the segments of the bars by this field 'learning_stage', sort='ascending' ) ).properties( width=1500, height=200 ) return chart
def percapita_trend(highlight, highlight2): total = alt.Chart(df).mark_bar().encode( alt.X('Year:N', title="Year"), alt.Y('CO2 emissions per capita', title='CO2 emissions per capita (kt)'), color=alt.Color('Country Name', scale=alt.Scale(scheme="set3"), title='Countries'), order=alt.Order( # Sort the segments of the bars by this field 'CO2 emissions per capita', sort='ascending'), tooltip=["Country Name", 'CO2 emissions per capita']).properties( width=530, height=350, title='CO2 emissions per capita world trend').transform_filter( highlight | highlight2) return total