def make_interactive_chart(): ''' ''' pts = alt.selection(type="single", encodings=['x']) rect = alt.Chart(data.movies.url).mark_rect().encode( alt.X('IMDB_Rating:Q', bin=True), alt.Y('Rotten_Tomatoes_Rating:Q', bin=True), alt.Color('count()', scale=alt.Scale(scheme='greenblue'), legend=alt.Legend(title='Total Records'))) circ = rect.mark_point().encode( alt.ColorValue('grey'), alt.Size('count()', legend=alt.Legend( title='Records in Selection'))).transform_filter(pts) bar = alt.Chart(data.movies.url).mark_bar().encode( x='Major_Genre:N', y='count()', color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey"))).properties(selection=pts, width=550, height=200) return alt.vconcat(rect + circ, bar).resolve_legend(color="independent", size="independent")
def make_amount_plot(infile): purchases = infra.pd.read_parquet(infile) purchases = purchases.assign(count=1) purchases = purchases[["amount_bytes", "count" ]].groupby(["amount_bytes"]).sum().reset_index() purchases["amount_mb"] = purchases["amount_bytes"] / 1000**2 alt.Chart(purchases).mark_point().encode( x=alt.X( "amount_mb", title="Session Purchase Amount (MB) (Log Scale)", scale=alt.Scale(type="log", domain=(10, 2000)), ), y=alt.Y( "count", title="Occurrences (Count) (Log Scale)", scale=alt.Scale(type="log", ), ), color=alt.condition( 'datum.count>1000', alt.ColorValue('red'), alt.ColorValue('steelblue'), ), shape=alt.condition( 'datum.count>1000', alt.ShapeValue('diamond'), alt.ShapeValue('triangle'), ), ).properties( width=500, height=300, ).save("renders/purchase_timing_per_user_clumped_amounts.png", scale_factor=2.0) print(purchases)
def pdp_plot(df, rows, columns, values, variables=None, vars_filter=None, clusters=False, cluster_centers=5, columns_type='N', x_title=None, y_title=None, width=700, height=300): ''' Plots a pdp plot for one variable. Parameters ---------- df : pandas.DataFrame Expects the l Returns ------- altair.Chart ''' df = df.copy() if vars_filter and variables: df = df[df[variables] == vars_filter].drop(variables, axis=1) base = alt.Chart(df).properties(width=width, height=height) if clusters: df_clusters = utils.pdp_clusters(cluster_centers, df, rows, columns, values) background = alt.Chart(df_clusters).mark_line(strokeWidth=2).encode( alt.X(f'{columns}:{columns_type}', title=x_title), alt.Y(values, title=y_title), alt.Opacity(rows, legend=None), alt.ColorValue('#468499')).properties(width=width, height=height) else: background = base.mark_line(strokeWidth=1).encode( alt.X(f'{columns}:{columns_type}', title=x_title), alt.Y(values, title=y_title), alt.Opacity(rows, legend=None), alt.ColorValue('#bbbbbb')) df_avg = df.groupby(columns)[values].mean().reset_index() avg_base = alt.Chart(df_avg).encode( alt.X(f'{columns}:{columns_type}', title=x_title), alt.Y(values, title=y_title), ) avg = avg_base.mark_line(strokeWidth=5, color='gold') avg += avg_base.mark_line(strokeWidth=2) avg += avg_base.mark_point(filled=True, size=55) return background + avg
def skyview_cmd(subsample, out_file, data_type='kepler'): brush = alt.selection( type='interval', resolve='global', on="[mousedown[event.shiftKey], window:mouseup] > \ window:mousemove!", zoom='False', translate="[mousedown[event.shiftKey], window:mouseup] > \ window:mousemove!") pan = alt.selection( type='interval', bind='scales', on="[mousedown[!event.shiftKey], window:mouseup] > \ window:mousemove!", translate="[mousedown[!event.shiftKey], window:mouseup] > \ window:mousemove!") if data_type == 'kepler': scale = alt.Scale(domain=['none', 'cand', 'conf'], range=['#4a69bd', '#e55039', '#f6b93b']) color = alt.Color('planet?:N', scale=scale) xscale = alt.Scale(domain=[270, 310]) yscale = alt.Scale(domain=[35, 55]) elif data_type == 'k2': #scale = alt.Scale(domain=['none', 'cand'], # range=['#4a69bd', '#e55039']) #color = alt.Color('k2c_disp:N', scale=scale) color = 'k2c_disp' xscale = alt.Scale(domain=[0, 360]) yscale = alt.Scale(domain=[-90, 90]) else: print("ERROR: data_type not recognized") return chart1 = alt.Chart(subsample).mark_point().encode( alt.X('ra_gaia', scale=xscale, axis=alt.Axis(title='Gaia RA (deg)')), alt.Y('dec_gaia', scale=yscale, axis=alt.Axis(title='Gaia Dec (deg)')), color=alt.condition(brush, color, alt.ColorValue('gray'))).properties( selection=brush + pan, projection={'type': 'gnomonic'}, width=450, height=500) chart2 = alt.Chart(subsample).mark_point().encode( alt.X('gaiamag_minus_kepmag', axis=alt.Axis(title='G - K (mag)')), alt.Y('abs_gmag', axis=alt.Axis(title='abs. G')), color=alt.condition(brush, color, alt.ColorValue('gray'))).properties( selection=brush, width=450, height=500) chart = chart1 | chart2 chart.savechart(out_file)
def linking_tree_with_plots_brush(dataFrame, list_of_data, list_of_titles, color, ToolTip): """Creates a linked brushable altair plot with the tree and the charts appended Parameters ----------- dataframe: Pandas Dataframe dataframe including node data and dimensionality reduction data list_of_data: list list of all the names of the columns in the dataframe for which you want graphs: goes in the order of [x1,y1,x2,y2,x3,y3] etc. list_of_titles: list list of all the TITLES you want for each axis: goes in order of[x1,y1,x2,y2,x3,y3] etc. color: string what the data should be colored by (ex. by clade, by region) ToolTip: list when hovering over the data, what data should be shown Returns --------- A brushable altair plot combining the tree with the plots of columns passed in """ list_of_chart = [] if (len(list_of_data) % 2 != 0 or len(list_of_titles) % 2 != 0): raise Exception( 'The length of list_of_data and the length of list_of_titles should not be odd.' ) else: base = alt.Chart(dataFrame) brush = alt.selection(type='interval', resolve='global') tree_name = base.mark_circle().encode( x=alt.X("date:Q", scale=alt.Scale(domain=(dataFrame["date"].min() - 0.2, dataFrame["date"].max() + 0.2)), title="Date"), y=alt.Y("y:Q", title=""), color=alt.condition(brush, color, alt.ColorValue('gray')), tooltip=ToolTip).add_selection(brush).properties(width=400, height=250) list_of_chart.append(tree_name) for i in range(0, len(list_of_data) - 1, 2): if (i == len(list_of_data)): break chart = base.mark_circle(size=60).encode( x=alt.X(list_of_data[i], title=list_of_titles[i]), y=alt.X(list_of_data[i + 1], title=list_of_titles[i + 1]), color=alt.condition(brush, color, alt.ColorValue('gray')), tooltip=ToolTip).add_selection(brush).properties(width=250, height=250) list_of_chart.append(chart) return list_of_chart
def createChart(data, zipcode): color_expression = "highlight._vgsid_==datum._vgsid_" color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") #rating_selection = alt.selection_single(name="rating", empty="all", encodings=['y']) data = data[data['zipcode'] == zipcode].nlargest(20, 'num') try: maxCount = int(data['num'].max()) except ValueError: maxCount = 10 data = pd.DataFrame([{"name": "undefine", "num": 0}]) return alt.Chart(data)\ .mark_bar(stroke="Black")\ .encode( alt.X("num:Q", axis=alt.Axis(title="Restaurants"), scale=alt.Scale(domain=(0,maxCount))), alt.Y('name:O', axis=alt.Axis(title="cuisine"), sort=alt.SortField(field="num", op="argmax")), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection = highlight_selection, )
def createChart(data, zipcode): color_expression = "highlight._vgsid_==datum._vgsid_" color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") try: data = pd.concat([data['cuisine'], data['perZip' + zipcode]], axis=1).nlargest(20, 'perZip' + zipcode) maxCount = int(data['perZip' + zipcode].max()) except KeyError: maxCount = 1 data = pd.DataFrame([{"cuisine": "", 'perZip' + zipcode: 0}]) return alt.Chart(data) \ .mark_bar(stroke="Black") \ .encode( alt.X('perZip'+zipcode+':Q', axis=alt.Axis(title="Restaurants"), scale=alt.Scale(domain=(0,maxCount))), alt.Y('cuisine:O', axis=alt.Axis(title="cuisine"), sort=alt.SortField(field='perZip'+zipcode, op="argmax")), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection = highlight_selection, )
def createChart(data): color_expression = "highlight._vgsid_==datum._vgsid_" #color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") rating_selection = alt.selection_single(name="rating", empty="all", encodings=['y']) maxCount = int(data['restaurants'].max()) barMean = alt.Chart() \ .mark_bar(stroke="Black") \ .encode( alt.X("mean(restaurants):Q", axis=alt.Axis(title="Restaurants")), alt.Y('cuisine:O', axis=alt.Axis(title="Cuisine"), sort=alt.SortField(field="restaurants", op="mean", order='descending')), alt.ColorValue("LightGrey"),#, condition=color_condition), # Remove color condition ).properties( width=200, height=350, selection = highlight_selection+rating_selection, ) return alt.hconcat(barMean, data=data)
def draw_chart(data, zipcode): # color expression, highlights bar on mouseover color_expression = "highlight._vgsid_==datum._vgsid_" color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") #highlight bar when mouse is hovering highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") try: data = pd.concat([data["cuisine"], data["perZip"+zipcode]], axis=1).nlargest(25, "perZip"+zipcode) maxCount = int(data["perZip"+zipcode].max()) except KeyError: maxCount = 1 data = pd.DataFrame([{"cuisine":"", "perZip"+zipcode:0}]) return alt.Chart(data) \ .mark_bar(stroke="Black") \ .encode( alt.X("perZip"+zipcode+":Q", axis=alt.Axis(title="Number of Restaurants"), scale=alt.Scale(domain=(0,maxCount))), alt.Y("cuisine:O", axis=alt.Axis(title="Cuisine Type"), sort=alt.SortField(field="perZip"+zipcode, op="argmax")), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection = highlight_selection, )
def createChart(data, name=''): color_expression = "highlight._vgsid_==datum._vgsid_" color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") barMean = alt.Chart() \ .mark_bar(stroke="Black") \ .encode( alt.X("rating:Q", axis=alt.Axis(title="The number of restaurants")), alt.Y('name:O', axis=alt.Axis(title="Cuisines".format(name)), sort=alt.SortField(field="rating", op="mean", order='descending')), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection = highlight_selection, ) return alt.hconcat( barMean, data=data, title="The number of restaurants ({} in NYC) - Top 25 cuisines".format( name))
def get_interactive_proportions_plot(gender_balance): source = data_frames[gender_balance] pts = alt.selection(type="multi", encodings=['x']) lin = alt.Chart(source).mark_line().encode( alt.X('year:O', title='Year'), alt.Y('female_prop:Q', title="Proportion of Women", axis=alt.Axis(format='%'), scale=alt.Scale(domain=[0, 1])), alt.Color('job:N', legend=None)).transform_filter(pts).properties( width=500, height=375, title="Proportion of Women by Year") 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 ) lin_w_interaction = alt.layer( lin, # base line chart alt.Chart().mark_rule(color='#aaa').encode( x='year:O').transform_filter(label), lin.mark_circle().encode(opacity=alt.condition(label, alt.value( 1), alt.value(0))).add_selection(label), lin.mark_text( align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode(text=alt.Text( 'female_prop:Q', format='.2%')).transform_filter(label), lin.mark_text(align='left', dx=5, dy=-5).encode(text=alt.Text( 'female_prop:Q', format='.2%')).transform_filter(label), data=source) bar = alt.Chart(source).mark_bar(size=30).encode( y=alt.Y('job:N', title='', sort=alt.EncodingSortField(field="total_prop_female", op="sum", order="descending")), x=alt.X('total_prop_female:Q', title="Proportion of Women", axis=alt.Axis(format='%')), color=alt.condition(pts, alt.Color( 'job:N', legend=None), alt.ColorValue("grey"))).properties( width=250, height=375, title="Jobs by Proportion of Women (For the 10 most " + gender_balance + " jobs)").add_selection(pts) interactive_job_chart = alt.hconcat(lin_w_interaction, bar).resolve_legend( color="independent", size="independent").configure_axis(labelFontSize=13, titleFontSize=14) # Save html as a StringIO object in memory job_gender_proportions_html = io.StringIO() interactive_job_chart.save(job_gender_proportions_html, 'html') # Return the html from StringIO object return job_gender_proportions_html.getvalue()
def _createRules(self, source, tooltip=True, timetext=False, timetexttop=False, timetextheightmod=1, field='value:Q'): selectors = alt.Chart(source.data).mark_point(opacity=0).encode( x='dateandtime:T', ).add_selection(self.nearestTime) rules = alt.Chart(source.data).mark_rule().encode( x='dateandtime:T', color=alt.condition('isValid(datum.value)', alt.ColorValue('gray'), alt.ColorValue('red'))).transform_filter( self.nearestTime) if timetext: if timetexttop: flip = -1 else: flip = 1 time_text_dy = flip * (self.def_height * timetextheightmod / 2 + 11) time_text = rules.mark_text( align='center', dx=0, dy=time_text_dy, fontSize=self.mark_text_font_size + 1).encode(text=alt.condition(self.nearestTime, 'dateandtime:T', alt.value(' '), format='%b %-d, %H:%M')) rules = rules + time_text if tooltip: points = source.mark_point( size=40, filled=True).encode(opacity=alt.condition( self.nearestTime, alt.value(0.8), alt.value(0))) text = source.mark_text( align='left', dx=5, dy=-10, fontSize=self.mark_text_font_size).encode(text=alt.condition( self.nearestTime, field, alt.value(' '), format='.1f')) return selectors + rules + points + text return selectors + rules
def _build_slope(df, values, time, bars, col, text, filter_in, y_pos=10, width=350, height=400, y_title=None): base = alt.Chart(df) if time is None: slope_x_title = '' slope_filter = { 'or': [f'datum.slope_x == "averages"', filter_in.ref()] } else: max_time = df[time].max() slope_x_title = f'{max_time}' slope_filter = { 'and': [{ 'or': [f'datum.slope_x == "averages"', filter_in.ref()] }, f'datum.{time} == {max_time}'] } slope_base = base.encode( x=alt.X('slope_x:N', title=slope_x_title, axis=alt.Axis(labels=False), scale=alt.Scale(domain=['averages', 'measures', ''])), y=alt.Y(values, title=y_title, scale=alt.Scale(domain=[df[values].min(), df[values].max()])), color=alt.Color( col, legend=None)).transform_filter(slope_filter).properties( width=width, height=height) slope_points = slope_base.mark_point(filled=True, size=150) slope_lines = slope_base.mark_line() slope_text_1 = slope_base.mark_text(dx=-25, dy=5).encode( text=values).transform_filter('datum.slope_x == "averages"') slope_text_2 = slope_base.mark_text(align='left', dx=8, dy=5).encode( text='slope_text:N').transform_filter('datum.slope_x == "measures"') slope_title = slope_base.mark_text(size=14, dy=-15).encode( y=alt.value(y_pos), text=bars, color=alt.ColorValue('black')) return slope_points + slope_lines + slope_text_1 + slope_text_2 + slope_title
def time_window_selector(base: alt.Chart, interpolate: bool) -> alt.LayerChart: if interpolate is True: tws = base.mark_area(interpolate="monotone") else: tws = base.mark_bar().encode(x2=alt.X2("event_end:T")) tws = tws.encode( x=alt.X("event_start", title=""), y=alt.Y( "reference_value", title="", axis=alt.Axis(values=[], domain=False, ticks=False), ), color=alt.ColorValue(idle_color), tooltip=alt.TooltipValue("Click and drag to select time window"), ).properties(height=30, title="Select time window") tws = tws.add_selection(time_selection_brush) + tws.transform_filter( time_selection_brush).encode( color=alt.condition(time_selection_brush, alt.ColorValue( "#c21431"), alt.ColorValue(idle_color))) return tws
def plotZip(data, zip_filter): try: data_perzip = pd.concat([data[['cuisine', 'perZip']], data.perZip.apply(pd.Series)], axis=1)[['cuisine', str(zip_filter)]]\ .dropna().rename(index=str, columns={str(zip_filter): "total"}).sort_values(by=['total'], ascending=False) except: data_perzip = pd.DataFrame({'cuisine': [np.nan], 'total': [np.nan]}) return alt.Chart(data_perzip).mark_bar(stroke="Black").encode( alt.X("total:Q", axis=alt.Axis(title="Restaurants")), alt.Y('cuisine:O', sort=alt.SortField(field="total", op="argmax")), alt.ColorValue("LightGrey"), ).properties(width=200)
def showCuisines(data, zipCode): data = data[data['zipCode'] == zipCode] barRest = alt.Chart(data) \ .mark_bar(stroke="Black")\ .encode(alt.X("perZip:Q", axis=alt.Axis(title="Restaurant Count")),\ alt.Y("cuisine:O", axis=alt.Axis(title="Cuisine"),\ sort=alt.SortField(field="perZip", op="sum", order='descending')),\ alt.ColorValue("LightGrey")) return barRest
def single_scatter(y, x): brush = alt.selection(type='interval', resolve='global') return alt.Chart(underlying_display).mark_point().encode( y= y + ':Q', x = x + ':Q', color=alt.condition(brush, 'Sector:N', alt.ColorValue('gray'), legend=None), tooltip=[ 'Ticker', 'Company:O', 'Sector:N'] ).add_selection( brush ).properties( width=500 )
def get_bar_chart(ranks_st: pd.DataFrame, sorted_omic: list, conditionals_1: str, conditionals_2: str, omic_column: str, omic: str, omic1: str, omic2: str, x_size: float, y_size: float, mlt, selector1, selector2): if omic == omic1: x = alt.X('%s:N' % omic1, sort=sorted_omic, axis=None) y = alt.Y('mean(rank):Q', axis=alt.Axis(titleFontSize=8)) width = x_size height = 50 else: x = alt.X('mean(rank):Q', axis=alt.Axis(titleFontSize=8, orient='top')) y = alt.Y('%s:N' % omic2, sort=sorted_omic, axis=None) width = 50 height = y_size if omic_column: color = alt.condition(mlt, omic_column, alt.ColorValue("grey")) else: color = alt.condition(mlt, alt.ColorValue("steelblue"), alt.ColorValue("grey")) tooltips = [ omic, 'mean(rank)', 'stdev(rank)', 'min(%s)' % conditionals_1, 'min(%s)' % conditionals_2 ] if omic_column: tooltips.append(omic_column) bar_omic = alt.Chart(ranks_st).mark_bar( ).encode(x=x, y=y, color=color, tooltip=tooltips).add_selection( mlt, selector1, selector2).transform_filter( alt.FieldEqualPredicate(field='conditional', equal='conditionals') ).transform_filter( alt.datum[conditionals_1] <= selector1.cutoff1).transform_filter( alt.datum[conditionals_2] <= selector2.cutoff2).properties( width=width, height=height) return bar_omic
def createChart(data): maxCount = int(data[0]["count"]) barCount = alt.Chart(pd.DataFrame.from_records(data)) \ .mark_bar(stroke="Black") \ .encode( alt.X("count:Q", axis=alt.Axis(title="Number of Restaurants")), alt.Y("cuisine:O", axis=alt.Axis(title="cuisine"), sort=alt.SortField(field="count", order="descending", op="mean")), alt.ColorValue("LightGrey") ) return barCount
def grafico_scatter_doble(data): source = data brush = alt.selection(type='interval', resolve='global') base = alt.Chart(source).mark_point().encode( y='profitableness_MUS$', color=alt.condition(brush, 'Predominant_genre', alt.ColorValue('gray')), ).add_selection( brush ).properties( width=250, height=250 ) base.encode(x='budget') | base.encode(x='revenue') return base.encode(x='budget') | base.encode(x='revenue')
def generate_plot(element="Production", compared_year=1995): stacked = None stacked2 = None prod = df_sum[df_sum["Element"] == element].reset_index(drop=True) count = 0 items = [ "Cereals", "Citrus Fruit", "Fibre Crops", "Fruit", "Pulses", "Roots and Tubers", "Sugar Crops", "Treenuts", "Vegetables", "Oilcrops" ] areas = ["World", "Africa", "Asia", "Americas", "Europe", "Oceania"] for area in areas: df = prod[prod["Area"] == area] for item in items: year_df = df[df["Year"] == compared_year] year_value = year_df[year_df["Item"] == item]["Value"].reset_index( drop=True)[0] df.loc[df['Item'] == item, 'Value'] = ((df[df["Item"] == item]["Value"] - year_value) / year_value) * 100 base = alt.Chart(df).mark_line().encode( x=alt.X('Year', scale=alt.Scale(domain=[1960, 2020])), y=alt.Y('Value', title=f"{element} % vs {compared_year}"), color=alt.condition(highlight, 'Item', alt.ColorValue('lightgray')), tooltip=['Domain', 'Item', 'Year', 'Value']).properties(title=f"{area} {element}") points = base.mark_circle().encode( opacity=alt.value(0)).add_selection(highlight) lines = base.mark_line().encode( size=alt.condition(~highlight, alt.value(1), alt.value(3))) current = lines + points if count < 3: if stacked is None: stacked = current else: stacked = stacked | current else: if stacked2 is None: stacked2 = current else: stacked2 = stacked2 | current count += 1 final_plot = stacked & stacked2 final_plot.save(f"../html/cashdrop_{element}.html", embed_options={'renderer': 'svg'})
def fixed_viewpoint_selector( base: alt.Chart, active_fixed_viewpoint_selector: bool = False) -> alt.Chart: """Transparent selectors across the chart (visible on hover). This is what tells us the belief time for a given x-value of the cursor. :param active_fixed_viewpoint_selector: if False, return an idle colored version without tooltip """ selector = base.mark_rule().encode( x=alt.X("belief_time:T", scale={"domain": time_selection_brush.ref()}), color=alt.ColorValue(idle_color) if not active_fixed_viewpoint_selector else alt.ColorValue("#c21431"), opacity=alt.condition(nearest_x_hover_brush, alt.value(1), alt.value(0)), tooltip=[ alt.Tooltip( "belief_time:T", timeUnit="yearmonthdatehoursminutes", title="Click to select belief time", ) ] if active_fixed_viewpoint_selector is True else None, ) return selector.add_selection(nearest_x_select_brush).add_selection( nearest_x_hover_brush)
def make_plot(data): pts = alt.selection(type="multi", encodings=['x']) rect = alt.Chart(data).mark_rect().encode( alt.X('series number:N'), alt.Y('grams of sugar', bin=True), alt.Color('count()', scale=alt.Scale(scheme='greenblue'), legend=alt.Legend(title='Recipes') ), tooltip='ingredient' ) bar = alt.Chart(data).mark_bar().encode( x='baker', y='count(recipe)', color=alt.condition(pts, alt.ColorValue("steelblue"), alt.ColorValue("grey")) ).properties( width=550, height=200 ).add_selection(pts) st.altair_chart(rect) st.altair_chart(bar)
def plotWords(dfWords): color_expression = "highlight._vgsid_==datum._vgsid_" color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") return alt.Chart(dfWords) \ .mark_bar(stroke="Black") \ .encode( alt.X("freq", axis=alt.Axis(title="Count")), alt.Y('word:O', axis=alt.Axis(title="Keyword"), sort=alt.SortField(field="freq", op="max", order='descending')), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection = highlight_selection )
def get_lines(data, stroke_w, color, selection=None, **kwargs): lines = alt.Chart(data).mark_line( strokeWidth=stroke_w, **kwargs).encode( alt.X(f'{columns}:{columns_type}', title=x_title, axis=alt.Axis(minExtent=30)), alt.Y(values, title=y_title), alt.Opacity(rows, legend=None), alt.ColorValue(color)).transform_filter(filter_in).properties( width=width, height=height) if selection: lines = lines.encode(size=alt.condition( selection, alt.value(stroke_w * 2), alt.value(stroke_w))).properties( selection=selection) return lines
def rank_bar(y): temp = underlying_display[['Ticker', 'Company',y]] brush3 = alt.selection(type='interval', resolve='global',encodings=['x']) bar = alt.Chart(temp).mark_bar().encode( x=alt.X('Ticker:O',sort='-y'), y=y+':Q', color = alt.condition(brush3, 'Ticker:N', alt.ColorValue('grey'), legend=None) ) rule = alt.Chart(temp).mark_rule(color='yellow').encode( y='mean('+y+'):Q' ) return (bar.add_selection( brush3 ).properties( width=500 ))
def createchart(data): #color_expression = "(indexof(lower(datum.cuisine)) || (highlight._vgsid_==datum._vgsid_)" #color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") #highlight_selection = alt.selection_single(name="highlight", on="mouseover", empty="none") #search_selection = alt.selection_single(name="search", on="mouseover", empty="none", fields=["term"], #bind=alt.VgGenericBinding('input')) chart = alt.Chart(data) \ .mark_bar(stroke="Black") \ .encode( alt.X("total:Q", axis=alt.Axis(title="Restaurants")), alt.Y('cuisine:O', sort=alt.SortField(field="total", op="argmax")), alt.ColorValue("LightGrey"), ) return chart
def pair_scatter(legend_flag): brush = alt.selection(type='interval', resolve='global') base = alt.Chart(underlying_display).mark_point().encode( y=val_c0+':Q', color=alt.condition(brush, 'Sector:N', alt.ColorValue('gray'), legend=legend_flag), size = 'Size:N', tooltip=[ 'Ticker', 'Company:O', 'Sector:N'] ).add_selection( brush ).properties(width='container') base1 = base.encode(x=val_c1+':Q') base2 = base.encode(x=val_c2+':Q') base3 = base.encode(x=val_c3+':Q') return (base1 , base2 , base3)
def showRatingDistribution(df, name=''): zips = name try: data = df.sort_values(by=zips, ascending=False).head(15) except KeyError: return alt.Chart(pd.DataFrame()).mark_bar() else: color_expression = "highlight._vgsid_==datum._vgsid_" color_condition = alt.ConditionalPredicateValueDef( color_expression, "SteelBlue") ## There are two types of selection in our chart: ## (1) A selection for highlighting a bar when the mouse is hovering over highlight_selection = alt.selection_single(name="highlight", empty="all", on="mouseover") ## (2) A selection for updating the rating distribution when the mouse is clicked ## Note the encodings=['y'] parameter is needed to specify that once a selection ## is triggered, it will propagate the encoding channel 'y' as a condition for ## any subsequent filter done on this selection. In short, it means use the data ## field associated with the 'y' axis as a potential filter condition. # rating_selection = alt.selection_single(name="rating", empty="all", encodings=['y']) ## We need to compute the max count to scale our distribution appropriately # maxCount = int(data[zips].max()) ## Our visualization consists of two bar charts placed side by side. The first one ## sorts the apps by their average ratings as below. Note the compound selection ## that is constructed by adding the two selections together. barMean = alt.Chart(data) \ .mark_bar(stroke="Black") \ .encode( alt.X(zips+":Q", axis=alt.Axis(title="Restuarants")), alt.Y('cuisine:O', axis=alt.Axis(title="Cuisine".format(name)), sort=alt.SortField(field=zips, op="max", order='descending')), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection = highlight_selection ) return barMean
def createChart(data, zip): color_expression = "(highlight._vgsid_==datum._vgsid_)" color_condition = alt.ConditionalPredicateValueDef(color_expression, "SteelBlue") highlight_selection = alt.selection_single(name="highlight", on="mouseover", empty="none") vis2 = alt.Chart(data) \ .mark_bar(stroke="Black") \ .encode( alt.X("total:Q", axis=alt.Axis(title="Restaurants")), alt.Y('cuisine:O', sort=alt.SortField(field="total", op="argmax")), alt.ColorValue("LightGrey", condition=color_condition), ).properties( selection=(highlight_selection), ) return vis2