"open": 25.93, "high": 27.18, "low": 25.29, "close": 25.35, "signal": "long", "ret": 5.26315789473684 }, { "date": "2009-06-30", "open": 25.36, "high": 27.38, "low": 25.02, "close": 26.35, "signal": "long", "ret": 6.73758865248228 }]) open_close_color = alt.condition("datum.open < datum.close", alt.value("#06982d"), alt.value("#ae1325")) rule = alt.Chart(source).mark_rule().encode(alt.X( 'yearmonthdate(date):T', scale=alt.Scale(domain=[{ "month": 5, "date": 31, "year": 2009 }, { "month": 7, "date": 1, "year": 2009 }]), axis=alt.Axis(format='%m/%d', title='Date in 2009')), alt.Y( 'low',
def main(_): print("Loading data...") dfs = [] for filename in os.listdir(FLAGS.data): if filename.endswith(".csv"): dfs.append( pd.read_csv(os.path.join(FLAGS.data, filename), encoding="utf-8")) data = pd.concat(dfs) print("%d Examples" % (len(set(data["id"])))) print("%d Annotations" % len(data)) if not os.path.isdir(FLAGS.plot_dir): os.makedirs(FLAGS.plot_dir) with open(FLAGS.emotion_file, "r") as f: all_emotions = f.read().splitlines() all_emotions_neutral = all_emotions + ["neutral"] emotion2idx = {e: i for i, e in enumerate(all_emotions)} print("%d emotion Categories" % len(all_emotions)) print("Processing data...") # Remove neutral labels data = data[data["neutral"] == 0] # Remove examples with no ratings (difficult examples) data = data[data[all_emotions_neutral].sum(axis=1) != 0] # Convert into num_examples x num_raters x num_ratings format data = data.groupby("id").filter(lambda x: len(x) >= 3) id_groups = data.groupby("id") worker2examples = {} # dict mapping worker ids to (example, rater id) tuples max_num_raters = data.groupby("id").size().max() ratings = np.zeros( (len(id_groups), max_num_raters, len(all_emotions))) # ignore "neutral" rater_msk = np.zeros( (len(id_groups), max_num_raters)) # for masking out non-existent raters print("Ratings shape", ratings.shape) # Get ratings and rater mask texts = [] for ex_idx, (_, g) in enumerate(id_groups): texts.append(g.iloc[0]["text"]) rater_count = 0 # iterate through workers for _, row in g.iterrows(): for e in all_emotions: ratings[ex_idx, rater_count, emotion2idx[e]] = row[e] rater_msk[ex_idx, rater_count] = 1 worker_id = row["rater_id"] if worker_id in worker2examples: worker2examples[worker_id].append((ex_idx, rater_count)) else: worker2examples[worker_id] = [(ex_idx, rater_count)] rater_count += 1 print("Calculating leave-out (partial) correlations...") partial_corr_per_rater = [] corr_per_rater = [] for worker_id in worker2examples: partial_corrs, corrs = LeaveOut(ratings, rater_msk, worker2examples, worker_id) if len(partial_corrs) < len(all_emotions): continue partial_corr_per_rater.append(partial_corrs) corr_per_rater.append(corrs) corr_per_rater = np.array(corr_per_rater) partial_corr_per_rater = np.array(partial_corr_per_rater) # Verify that there are no NaN values assert np.isnan(corr_per_rater).sum() == 0 # Apply Wilcoxon signed rank test to test significance of each dimension p_vals = np.apply_along_axis(wilcoxon, 0, partial_corr_per_rater)[1] # Apply Bonferroni correction reject, corr_pvals, _, newalpha = multipletests( p_vals, alpha=0.05, method="bonferroni") print("Which dimensions to keep?") print(reject) print(corr_pvals) print(newalpha) print("Running PPCA on all the data...") # Take all raters and split them randomly x = [] y = [] rater_counts = rater_msk.sum(axis=1).astype(int) all_ratings_avg = [] for i, ex in enumerate(ratings): # Get actual raters based on mask keep = [] for worker_rating in ex[:rater_counts[i]]: keep.append(list(worker_rating)) all_ratings_avg.append(list(np.array(keep).mean(axis=0))) # Shuffle raters randomly random.shuffle(keep) num_raters = len(keep) x.append(list(np.array(keep[:int(num_raters / 2)]).mean(axis=0))) y.append(list(np.array(keep[int(num_raters / 2):]).mean(axis=0))) x = np.array(x) y = np.array(y) all_ratings_avg = np.array(all_ratings_avg) w, v = PPCA(x, y) # final components (p-values determine which ones to keep) print("Plotting percentage of covariance explained...") PlotCovar(v) # Apply varimax rotation w_vari = Varimax(w) # Get mapping between ppcs and emotions map_df = pd.DataFrame( w_vari, index=all_emotions, columns=np.arange(len(all_emotions))).round(4) # Sort to move values to diagonal map_df = map_df[list( np.argsort(map_df.apply(lambda x: pd.Series.nonzero(x)[0]).values)[0])] f = plt.figure(figsize=(10, 6), dpi=300) sns.heatmap( map_df, center=0, cmap=sns.diverging_palette(240, 10, n=50), yticklabels=all_emotions) plt.xlabel("Component") plt.savefig( FLAGS.plot_dir + "/component_loadings.pdf", dpi=600, format="pdf", bbox_inches="tight") ppc2emotion = map_df.abs().idxmax().to_dict() emotion2ppc = {e: i for i, e in ppc2emotion.items()} print(ppc2emotion) print("Plotting frequency and mean left-out rater correlations...") corr_mean = corr_per_rater.mean(axis=0) corr_mean_ordered = [corr_mean[emotion2ppc[e]] for e in all_emotions] df_plot = pd.DataFrame({ "emotion": all_emotions, "agreement": corr_mean_ordered }) df_plot["count"] = df_plot["emotion"].map( data[all_emotions].sum(axis=0).to_dict()) df_plot.sort_values("count", ascending=False, inplace=True) df_plot.to_csv(FLAGS.plot_dir + "/emotion_agreements.csv", index=False) # Get colors norm = plt.Normalize(df_plot["agreement"].min(), df_plot["agreement"].max()) sm = plt.cm.ScalarMappable(cmap="BuPu", norm=norm) sm.set_array([]) # Generate figure fig = plt.figure(dpi=600, figsize=(5, 6)) ax = sns.barplot( data=df_plot, y="emotion", x="count", orient="h", hue="agreement", palette="BuPu", dodge=False, edgecolor="black", linewidth=1) ax.get_legend().remove() ax.figure.colorbar(sm) plt.text(18000, 31, "Interrater\nCorrelation", ha="center") plt.xlabel("Number of Examples") plt.ylabel("") plt.draw() labels = [item.get_text() for item in ax.get_xticklabels()] ax.set_xticklabels(["%dk" % (int(int(label) / 1000)) for label in labels]) plt.tight_layout() fig.savefig( FLAGS.plot_dir + "/label_distr_agreement.pdf", dpi=600, format="pdf", bbox_inches="tight") print("Generating t-SNE plot...") # Get PPC scores for all examples all_ratings_avg = Demean(all_ratings_avg) # demean all ratings ppc_scores = all_ratings_avg.dot(w_vari) # project onto ppcs ppc_scores_abs = np.absolute(ppc_scores) # Load maximally distinct colors colors = pd.read_csv( FLAGS.rgb_colors, sep="\t", header=None, names=np.arange(3)) # Set colors (todo(ddemszky): add names to colors in file) palette_rgb = colors.values with open(FLAGS.emotion_color_order) as f: color_order = f.read().splitlines() ppc2color = {emotion2ppc[e]: i for i, e in enumerate(color_order)} # get rgb value for each example based on weighted average of top emotions rgb_vals = [] hex_vals = [] top_categories = [] threshold = 0.5 # exclude points not loading on any of the top 10 categories counter = 0 rgb_max = 255 other_color = palette_rgb[len(all_emotions), :] for i, scores in enumerate(ppc_scores_abs): top_ppcs = [ idx for idx in (-scores).argsort()[:2] if scores[idx] > threshold ] top_emotions = ",".join([ppc2emotion[idx] for idx in top_ppcs ]) if top_ppcs else "other" top_categories.append(top_emotions) if len(top_ppcs) < 1: # doesn't have top emotions from list color = other_color # use grey counter += 1 else: # Weighted average of top emotions (square->weighted average->square root) color_ids = [ppc2color[idx] for idx in top_ppcs] weights = [scores[idx] for idx in top_ppcs] # Need to round, otherwise floating point precision issues will result # in values slightly above 1 avg = np.round( np.sqrt( np.average( np.power(palette_rgb[color_ids] * rgb_max, 2), axis=0, weights=weights)) / rgb_max, 4) if (avg > 1).sum() > 0: print(avg) color = avg rgb_vals.append(list(color)) hex_vals.append("#%02x%02x%02x" % tuple(np.array(color * rgb_max, dtype=int))) rgb_vals = np.array(rgb_vals) # Create t-SNE model tsne_model = TSNE( perplexity=30, n_components=2, n_iter=1000, random_state=23, learning_rate=500, init="pca") new_values = tsne_model.fit_transform(ppc_scores) x = [] y = [] for value in new_values: x.append(value[0]) y.append(value[1]) # Put data in dataframe df = pd.DataFrame({ "x": x, "y": y, "color": hex_vals, "label(s)": top_categories, "text": texts }) df = df[df["label(s)"] != "other"] df["top_label"] = df["label(s)"].str.split(",").str[0] # Two selections: # - a brush that is active on the top panel # - a multi-click that is active on the bottom panel brush = alt.selection(type="interval") click = alt.selection_multi(encodings=["color"]) sample = df.sample(5000) # max 5000 examples can be plotted points = alt.Chart(sample).mark_point( filled=True, size=50).encode( x="x:Q", y="y:Q", color=alt.Color("color", scale=None), tooltip=["label(s)", "text"]).properties( width=700, height=600).add_selection(brush) # Bottom panel is a bar chart bars = alt.Chart(sample).mark_bar().encode( x="count()", y="top_label:N", color=alt.condition(click, alt.Color("color:N", scale=None), alt.value("lightgray")), ).transform_filter(brush.ref()).properties( width=700, selection=click) chart = alt.vconcat( points, bars, data=sample, title="t-SNE Projection of Examples") chart.save(FLAGS.plot_dir + "/tsne.html", format="html")
def _make_manual_legend(self, df, click_selection): groups = df.groupby(self.colorby).first().reset_index().sort_values( self.colorby, ascending=True) group_names = list(groups[self.colorby].values) if len(group_names) > self.MAX_LEGEND_MARKS: raise ValueError( f'max {self.MAX_LEGEND_MARKS} supported for now ({len(group_names)} requested)' ) idx = list(self.MAX_LEGEND_MARKS + 1 - np.arange(len(group_names))) row_type = ['normal'] * len(idx) idx.append(self.MAX_LEGEND_MARKS + 2) row_type.append('title') group_names.append(f'Select {self.get("readable_group_name", "line")}') xs = np.zeros_like(idx) leg_df = pd.DataFrame({ 'idx': idx, 'group_idx': list(groups['group_idx']) + [-1], self._colorby: group_names, 'x': list(xs), 'row_type': row_type, }) axis = alt.Axis(domain=False, ticks=False, orient='right', grid=False, labels=False) base = alt.Chart( leg_df, height=self._height, width=100, ) def _make_base(base, **extra_kwargs): return base.encode( x=alt.X('x:Q', title='', axis=axis, scale=alt.Scale(domain=(-5, 20))), y=alt.Y('idx:Q', title='', axis=axis, scale=alt.Scale(domain=(0, self.MAX_LEGEND_MARKS))), color=self._alt_color, detail=self._alt_detail, **extra_kwargs) legend_points = _make_base(base, opacity=alt.condition( self._click_focused_or_none_selected(), alt.value(1), alt.value(0.4), )).mark_point(shape='diamond', filled=True, size=160) legend_points = legend_points.transform_filter( 'datum.row_type == "normal"') cursor = alt.selection_single(name='legend_hover', nearest=True, on='mouseover', clear='mouseout', fields=['group_idx'], empty='none') layers = [ legend_points, legend_points. mark_text( # fake layer to add the click selection to align='left', ).encode( text=f'padded_text:N', opacity=alt.value(0), ).transform_calculate( padded_text=f'"__" + datum.{self._colorby} + "__"'). add_selection(click_selection), _make_base(base).mark_point(size=0).add_selection(cursor), legend_points.mark_text( align='left', dx=10, font=self._font, ).encode( text=f'{self._colorby}:N', color=alt.value('black'), opacity=alt.condition( self._in_focus_or_none_selected(), alt.value(1), alt.value(0.4), ), ), _make_base(base).mark_text( align='left', dx=-10, dy=-5, font=self._font, fontSize=16, ).encode( text=f'{self._colorby}:N', color=alt.value('black'), ).transform_filter('datum.row_type == "title"') ] return alt.layer(*layers, view=alt.ViewConfig(strokeOpacity=0))
genre_dropdown = alt.binding_select(options=genres) genre_select = alt.selection_single(fields=['Major_Genre'], bind=genre_dropdown, name="Genre") filter_genres = base.add_selection(genre_select).transform_filter( genre_select).properties(title="Dropdown Filtering") #color changing marks rating_radio = alt.binding_radio(options=ratings) rating_select = alt.selection_single(fields=['MPAA_Rating'], bind=rating_radio, name="Rating") rating_color_condition = alt.condition(rating_select, alt.Color('MPAA_Rating:N', legend=None), alt.value('lightgray')) highlight_ratings = base.add_selection(rating_select).encode( color=rating_color_condition) # Boolean selection for format changes input_checkbox = alt.binding_checkbox() checkbox_selection = alt.selection_single(bind=input_checkbox, name="Big Budget Films") size_checkbox_condition = alt.condition( checkbox_selection, alt.SizeValue(25), alt.Size('Hundred_Million_Production:Q')) budget_sizing = base.add_selection(checkbox_selection).encode(
import altair as alt from vega_datasets import data source = data.cars() # Configure common options base = alt.Chart(source) scale = alt.Scale(paddingInner=0) # Configure heatmap heatmap = base.mark_rect().encode( alt.X('Cylinders:O', scale=scale), alt.Y('Origin:O', scale=scale), color='count()' ) # Configure text text = base.mark_text(baseline='middle').encode( x='Cylinders:O', y='Origin:O', text='count()', color=alt.condition( alt.datum['count_*'] > 100, alt.value('black'), alt.value('white') ) ) # Draw the chart heatmap + text
https://bl.ocks.org/amitkaps/fe4238e716db53930b2f1a70d3401701 """ # category: interactive charts import altair as alt from vega_datasets import data source = data.stocks() highlight = alt.selection(type='single', on='mouseover', fields=['symbol'], nearest=True) base = alt.Chart(source).encode( x='date:T', y='price:Q', color='symbol:N' ) points = base.mark_circle().encode( opacity=alt.value(0) ).add_selection( highlight ).properties( width=600 ) lines = base.mark_line().encode( size=alt.condition(~highlight, alt.value(1), alt.value(3)) ) points + lines
def get_timeline_plots(df_scores, selected_score, selected_score_axis, selected_score_desc, use_states, countys): title = { "text": ["", selected_score_desc ], # use two lines as hack so the umlauts at Ö are not cut off "subtitle": "EveryoneCounts.de", "color": "black", "subtitleColor": "lightgray", "subtitleFontSize": 12, "subtitleFontWeight": "normal", "fontSize": 15, "lineHeight": 5, } if use_states: titlestr = "Bundesland" scheme = 'category20' else: titlestr = "Landkreis" scheme = 'category10' if len(countys) > 0 and not use_states: # Landkreise df_scores = df_scores[df_scores["name"].isin(countys)].dropna( axis=1, how="all") df_scores = df_scores[["name", "date", selected_score]].dropna() elif use_states: pass else: return None # county mode, nothing selected # altair selectors highlight = alt.selection_single(empty="none", fields=['name'], on='mouseover', nearest=True, clear="mouseout") highlight_circles = alt.selection_single(empty="none", fields=['date', 'name'], on='mouseover', nearest=True, clear="mouseout") # charts base = alt.Chart(df_scores[[ "name", "date", selected_score ]].dropna()).encode(x=alt.X('date:T', axis=alt.Axis(title='Datum', format=("%d %b"))), y=alt.Y(selected_score + ':Q', title=selected_score_axis), color=alt.Color('name', title=titlestr, scale=alt.Scale(scheme=scheme), legend=alt.Legend(orient="bottom", columns=2)), tooltip=[ alt.Tooltip("name:N", title=titlestr), alt.Tooltip(selected_score + ":Q", title=selected_score_axis), alt.Tooltip("date:T", title="Datum", format=("%A %d %B")), ]) points = base.mark_circle().encode( opacity=alt.value(1), size=alt.condition(~highlight_circles, alt.value(40), alt.value(300)), ).add_selection(highlight).add_selection(highlight_circles).properties( width='container', height=450, title=title) lines = base.mark_line().encode( size=alt.condition(~highlight, alt.value(2), alt.value(6)), opacity=alt.condition(~highlight, alt.value(0.5), alt.value(1))) if selected_score in ["airquality_score", "webcam_score", "tomtom_score"]: return points + lines else: # add horizontal rule at 100% rule = alt.Chart(df_scores).mark_rule(color='lightgray').encode( y="a:Q").transform_calculate(a="100") return rule + points + lines
# left panel: scatter plot points = alt.Chart().mark_point(filled=True, color="black").encode( x='x', y='y' ).transform_filter( pts.ref() ).properties( width=300, height=300 ) # right panel: histogram mag = alt.Chart().mark_bar().encode( x='mbin:N', y="count()", color=alt.condition(pts, alt.value("black"), alt.value("lightgray")) ).properties( selection=pts, width=300, height=300 ) # build the chart: alt.hconcat( points, mag, data=source ).transform_bin( "mbin", field="m", bin=alt.Bin(maxbins=20)
df_corr['correlation_label'] = df_corr['correlation'].map('{:.3f}'.format) if st.checkbox('Show correlation sample'): ''' The pairwise correlation of all attributes in the data set. ''' st.table(df_corr.head()) # Visualize the correlation using a heat map base = alt.Chart(df_corr).encode(x='X:O', y='Y:O') # Text layer with correlation labels # Colors are for easier readability text = base.mark_text().encode(text='correlation_label', color=alt.condition(alt.datum.correlation > 0.5, alt.value('white'), alt.value('black'))) ''' Visualization of the correlation of features using a heat map. The magnitude of correlation between the attributes are strong. ''' # The correlation heatmap itself cor_plot = base.mark_rect().encode(color='correlation:Q') # The '+' means overlaying the text and rect layer st.altair_chart(cor_plot + text, use_container_width=True) ''' Plots showing the distribution of data for each variable. ''' # Histogram plots of all variables df.hist(alpha=0.5, figsize=(15, 15)) plt.tight_layout()
timeseries = timeseries.reset_index().melt('time') # Merge the (x, y) metadata into the long-form view timeseries['id'] = timeseries['id'].astype(int) # make merge not complain data = pd.merge(timeseries, locations, on='id') # Data is prepared, now make a chart selector = alt.selection_single(empty='all', fields=['id']) base = alt.Chart(data).properties( width=250, height=250 ).add_selection(selector) points = base.mark_point(filled=True, size=200).encode( x='mean(x)', y='mean(y)', color=alt.condition(selector, 'id:O', alt.value('lightgray'), legend=None), ).interactive() timeseries = base.mark_line().encode( x='time', y=alt.Y('value', scale=alt.Scale(domain=(-15, 15))), color=alt.Color('id:O', legend=None) ).transform_filter( selector ) points | timeseries
# In[87]: df_comp = pd.concat(file_list) df_comp['intersect'] = df_comp['base_seg_id'].astype(str).isin(intersect) # In[88]: df_comp.head(3) # In[78]: CH1 = alt.Chart(df_comp[df_comp.ad_name == 'cinemax']).mark_bar().encode( x=alt.X('base_seg_id:N', sort='y'), y=alt.Y('Coef_value:Q', title='coefficient'), color=alt.condition(alt.datum.intersect == True, alt.value('orange'), alt.value('steelblue'))).properties(title='Cinemax', width=1000) CH2 = alt.Chart(df_comp[df_comp.ad_name == 'epix']).mark_bar().encode( x=alt.X('base_seg_id:N', sort='y'), y=alt.Y('Coef_value:Q', title='coefficient'), color=alt.condition(alt.datum.intersect == True, alt.value('orange'), alt.value('steelblue'))).properties(title='HBO', width=1000) CH3 = alt.Chart(df_comp[df_comp.ad_name == 'cbs']).mark_bar().encode( x=alt.X('base_seg_id:N', sort='y'), y=alt.Y('Coef_value:Q', title='coefficient'), color=alt.condition(alt.datum.intersect == True, alt.value('orange'), alt.value('steelblue'))).properties(title='CBS', width=1000)
DATA = load_data() st.title("Percentage of (COVID) Doctor Visits by State and County") st.write("In this section, we explore the percentage of doctor visits for COVID by State and County. We begin by hilighting Pennsylvania and as we can see, there are some interesting observatins for the state. ") alt.data_transformers.disable_max_rows() slider = alt.binding_range(min=1, max=31, step=1) select_date = alt.selection_single(name="January", fields=['Date'], bind=slider, init={'Date':1}) state_selector = alt.selection_multi(fields=['statename'], init=[{'statename':'Pennsylvania'}]) States = alt.Chart(DATA).mark_bar().encode( x=alt.X('value:Q', title="% of Visits to Doctor about COVID", aggregate="mean", scale=alt.Scale(domain=[0, 35])), y=alt.Y('statename:N', title="State"), color=alt.condition(state_selector, alt.value("#f76f5c"), alt.value("#451076")), tooltip=[alt.Tooltip("statename:N", title='State'), alt.Tooltip("value:Q", aggregate="mean", title="% of COVID Doctor Visits", format='.2f')] ).add_selection( state_selector ).add_selection( select_date ).transform_filter( select_date).interactive() Counties = alt.Chart(DATA).mark_bar(color='#451076').encode( x=alt.X('value:Q', title="% of Visits to Doctor about COVID", scale=alt.Scale(domain=[0,35])), y=alt.Y('county_name:N', title = "County"), tooltip=[alt.Tooltip("statename:N", title='State'), alt.Tooltip("county_name:N", title='County'),alt.Tooltip("value:Q", aggregate="mean", title="% of COVID Doctor Visits", format='.2f'), alt.Tooltip("puninsured2010:Q", aggregate="mean", title="% Uninsured (as of 2010)", format='.2f')] ).transform_filter( state_selector & select_date).interactive() joint_chart = States | Counties
color = alt.Color('weather:N', scale=scale) # We create two selections: # - a brush that is active on the top panel # - a multi-click that is active on the bottom panel brush = alt.selection_interval(encodings=['x']) click = alt.selection_multi(encodings=['color']) # Top panel is scatter plot of temperature vs time points = alt.Chart().mark_point().encode( alt.X('monthdate(date):T', axis=alt.Axis(title='Date')), alt.Y('temp_max:Q', axis=alt.Axis(title='Maximum Daily Temperature (C)'), scale=alt.Scale(domain=[-5, 40]) ), color=alt.condition(brush, color, alt.value('lightgray')), size=alt.Size('precipitation:Q', scale=alt.Scale(range=[5, 200])) ).properties( width=600, height=300 ).add_selection( brush ).transform_filter( click ) # Bottom panel is a bar chart of weather type bars = alt.Chart().mark_bar().encode( x='count()', y='weather:N', color=alt.condition(click, color, alt.value('lightgray')),
def make_plot(xval='Displacement', yval='Horsepower'): # Don't forget to include imports def mds_special(): font = "Arial" axisColor = "#000000" gridColor = "#DEDDDD" return { "config": { "title": { "fontSize": 24, "font": font, "anchor": "start", # equivalent of left-aligned. "fontColor": "#000000" }, 'view': { "height": 300, "width": 400 }, "axisX": { "domain": True, #"domainColor": axisColor, "gridColor": gridColor, "domainWidth": 1, "grid": False, "labelFont": font, "labelFontSize": 12, "labelAngle": 0, "tickColor": axisColor, "tickSize": 5, # default, including it just to show you can change it "titleFont": font, "titleFontSize": 16, "titlePadding": 10, # guessing, not specified in styleguide "title": "X Axis Title (units)", }, "axisY": { "domain": False, "grid": True, "gridColor": gridColor, "gridWidth": 1, "labelFont": font, "labelFontSize": 14, "labelAngle": 0, #"ticks": False, # even if you don't have a "domain" you need to turn these off. "titleFont": font, "titleFontSize": 16, "titlePadding": 10, # guessing, not specified in styleguide "title": "Y Axis Title (units)", # titles are by default vertical left of axis so we need to hack this #"titleAngle": 0, # horizontal #"titleY": -10, # move it up #"titleX": 18, # move it to the right so it aligns with the labels }, } } # register the custom theme under a chosen name alt.themes.register('mds_special', mds_special) # enable the newly registered theme alt.themes.enable('mds_special') #alt.themes.enable('none') # to return to default typeDict = { 'Displacement': ['quantitative', 'Displacement (mm)'], 'Cylinders': ['ordinal', 'Cylinders (#)'], 'Miles_per_Gallon': ['quantitative', 'Fuel Efficiency (mpg)'], 'Horsepower': ['quantitative', 'Horsepower (hp)'] } # Create a plot from the cars dataset brush = alt.selection(type='interval') chart = alt.Chart(vega_datasets.data.cars.url).mark_point(size=90).encode( alt.X(xval, type=typeDict[xval][0], title=typeDict[xval][1]), alt.Y(yval, type=typeDict[yval][0], title=typeDict[yval][1]), color=alt.condition(brush, 'Origin:N', alt.value('lightgray')), tooltip=[ { "type": typeDict[xval][0], "field": xval }, # {"type":typeDict[yval][0], "field":yval} ]).properties(title='{0} vs. {1}'.format(xval, yval), width=500, height=350).add_selection(brush) bars = alt.Chart(vega_datasets.data.cars.url).mark_bar().encode( y='Origin:N', color='Origin:N', x='count(Origin):Q').transform_filter(brush) return (chart & bars)
fig = go.Figure(data=go.Bar(y=[2, 3, 1])) fig.show() # In[5]: import altair as alt from vega_datasets import data source = data.us_employment() alt.Chart(source).mark_bar().encode( x="month:T", y="nonfarm_change:Q", color=alt.condition( alt.datum.nonfarm_change > 0, alt.value("steelblue"), # The positive color alt.value("orange") # The negative color )).properties(width=600) # In[6]: geo_df = gpd.read_file( r"G:\DeskTop\11_JupyterLabTest\dash_learning\入门\sz_group.geojson") fig = px.choropleth_mapbox(geo_df, geojson=geo_df.geometry, locations=geo_df.index, color='AREA', hover_name="NAME", center={ "lat": 22.5,
""" # category: scatter plots import altair as alt from vega_datasets import data source = data.cars() # Configure the options common to all layers brush = alt.selection(type='interval') base = alt.Chart(source).add_selection(brush) # Configure the points points = base.mark_point().encode( x=alt.X('Miles_per_Gallon', axis=alt.Axis(title='')), y=alt.Y('Horsepower', axis=alt.Axis(title='')), color=alt.condition(brush, 'Origin', alt.value('grey')) ) # Configure the ticks tick_options = dict(labels=False, domain=False, ticks=False) tick_axis = alt.Axis(**tick_options) tick_axis_notitle = alt.Axis(title='', **tick_options) x_ticks = base.mark_tick().encode( alt.X('Miles_per_Gallon', axis=tick_axis), alt.Y('Origin', axis=tick_axis_notitle), color=alt.condition(brush, 'Origin', alt.value('lightgrey')) ) y_ticks = base.mark_tick().encode( alt.X('Origin', axis=tick_axis_notitle),
width=400, height=200 ) # Bar chart - Risk factors bar_factors = alt.Chart(factors).mark_bar().transform_filter( alt.datum.country == selectCountry ).transform_filter( brush ).encode( alt.X('sum(value):Q', title='Total deaths'), y=alt.Y('Risk Factor:O',sort='-x'), tooltip='sum(value):Q', color=alt.condition( alt.datum['Risk Factor'] == 'Smoking', alt.value("red"), # Smoking color alt.value("lightgray") # Other than smoking ) ).properties( width=200, height=400 ) # Visualize st.altair_chart(alt.hconcat(alt.vconcat(base,years) .properties(spacing=20), bar_factors) .configure_legend(orient='top-left', strokeColor='gray', fillColor='#EEEEEE', padding=5, cornerRadius=10) .properties(spacing=20, autosize="pad")
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(source).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 ) alt.vconcat( rect + circ, bar ).resolve_legend( color="independent", size="independent" )
def state_length_vs_timestep_sim(self, chart_type='bar', background_color='#abb2bf', lower_bound=25, upper_bound=200): ''' creates interactive charts showing distributions of on and off times for simulated data chart types: bar: bar chart (histogram) area: area chart (filled line chart) background_color: hex code for chart background color, ex: #abb2bf - grey #ffffff - white ''' full_sim_df = self.full_sim_df chart_type = chart_type.strip().lower() sel_timestep = alt.selection_multi(encodings=['y']) bar_chart = alt.Chart( full_sim_df, height=800, width=250 ).mark_bar( ).encode( alt.X( 'max(duration):Q', title='max packet duration', scale=alt.Scale(type='log') ), alt.Y( 'timestep:N', ), color=alt.condition( sel_timestep, 'timestep:N', alt.value('#96989b'), legend=None ), tooltip = [ alt.Tooltip('timestep:N'), alt.Tooltip('duration:Q', aggregate='max') ] ).add_selection( sel_timestep ) #---------- histograms ---------- detail_bar_chart = alt.Chart( full_sim_df, height=375, width=800 ).mark_bar( opacity=0.5 ).transform_filter( sel_timestep ).transform_filter( datum.duration > lower_bound ).transform_filter( datum.duration < upper_bound ).encode( alt.X('duration:Q'), alt.Y( 'frequency:Q', #scale=alt.Scale(type='log') ), color=alt.Color('timestep:N', legend=None), tooltip=[ alt.Tooltip('duration:Q'), alt.Tooltip('frequency:Q'), alt.Tooltip('timestep:N'), alt.Tooltip('bit:N'), ] ) detail_area_chart = alt.Chart( full_sim_df, height=375, width=800 ).mark_area( opacity=0.5 ).transform_filter( sel_timestep ).transform_filter( datum.duration > lower_bound ).transform_filter( datum.duration < upper_bound ).encode( alt.X('duration:Q'), alt.Y( 'frequency:Q', #scale=alt.Scale(type='log') ), color=alt.Color('timestep:N', legend=None), tooltip=[ alt.Tooltip('duration:Q'), alt.Tooltip('frequency:Q'), alt.Tooltip('timestep:N'), alt.Tooltip('bit:N'), ] ) if chart_type == 'bar': detail_chart = detail_bar_chart elif chart_type == 'area': detail_chart = detail_area_chart else: print(f'unsupported chart type {chart_type}') stacked_bit_details = alt.vconcat( detail_chart.transform_filter(datum.bit == 0).properties(title='off time distributions'), detail_chart.transform_filter(datum.bit == 1).properties(title='on time distributions'), ) full = alt.hconcat( bar_chart, stacked_bit_details, background=background_color ) display(full)
=================== This chart shows an example of using an interval selection to filter the contents of an attached histogram, allowing the user to see the proportion of items in each category within the selection. """ # category: interactive charts import altair as alt from vega_datasets import data source = data.cars() brush = alt.selection(type='interval') points = alt.Chart().mark_point().encode( x='Horsepower:Q', y='Miles_per_Gallon:Q', color=alt.condition(brush, 'Origin:N', alt.value('lightgray')) ).add_selection( brush ) bars = alt.Chart().mark_bar().encode( y='Origin:N', color='Origin:N', x='count(Origin):Q' ).transform_filter( brush ) alt.vconcat(points, bars, data=source)
alt.Tooltip('dvars-per:Q', format='.2f', title='>20 (%)'), alt.Tooltip('dvars-mean:Q', format='.2f', title='Mean'), alt.Tooltip('dvars-std:Q', format='.2f', title='SD'), alt.Tooltip('dvars-max:Q', format='.2f', title='Max'), alt.Tooltip('dvars-min:Q', format='.2f', title='Min')] ).properties( width=700, height=400, ).transform_filter( click ).interactive() threshold_fd = alt.Chart(pd.DataFrame({'y': [0.2]})).mark_rule().encode(y='y') threshold_dvars = alt.Chart(pd.DataFrame( {'y': [20]})).mark_rule().encode(y='y') legend = alt.Chart(data).mark_rect().encode( y=alt.Y('task:N', axis=alt.Axis(title='Select Task')), color=alt.condition(click, 'task:N', alt.value('lightgray'), legend=None), size=alt.value(250) ).properties( width=30, height=180, selection=click ) chart = (scatter_fd + threshold_fd | scatter_dvars + threshold_dvars | legend) chart.save('interactive_plot_headmotion.html')
def seasonEvo1415(): premier1415 = pd.read_csv('App/Data/premier1415.csv') premier1415mod = premier1415.copy() premier1415mod[ 'cumpoints'] = premier1415mod['cumpoints'] - premier1415mod['points'] topteams1415 = [ 'Leicester City', 'Chelsea', 'Manchester City', 'Arsenal', 'Hull City', 'Burnley', 'Queens Park Rangers' ] noLC = [ 'Chelsea', 'Manchester City', 'Arsenal', 'Hull City', 'Burnley', 'Queens Park Rangers' ] LC = ['Leicester City'] premier1415noLC = premier1415[premier1415['team_long_name'].isin(noLC)] premier1415LC = premier1415[premier1415['team_long_name'].isin(LC)] noLClast1415 = premier1415noLC[premier1415noLC['stage'] == 38] LClast1415 = premier1415LC[premier1415LC['stage'] == 38] LClast1415['position'] = '14th' noLClast1415['position'] = ['19th', '1st', '2nd', '18th', '3rd', '20th'] a = {'x': [30], 'y': [19], 'textof': ['➟']} arrow = pd.DataFrame(a) palette = alt.Scale(domain=topteams1415, range=[ '#0062cc', 'green', '#6CABDD', '#EF0107', '#663f3f', '#e1d89f', '#d89216' ]) pointarrow = alt.Chart(arrow).mark_circle(size=100, color='black').encode(x='x', y='y') textarrow = alt.Chart(arrow).mark_text(dx=-20, dy=0, angle=240, fontSize=30).encode(x='x', y='y', text='textof') noLC = [ 'Chelsea', 'Manchester City', 'Arsenal', 'Queens Park Rangers', 'Burnley', 'Hull City' ] LC = ['Leicester City'] nearest = alt.selection(type='single', nearest=True, on='mouseover', fields=['stage'], init={'stage': 20}, empty='none') lineLC = alt.Chart(premier1415LC).mark_line(size=5).encode( x=alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), y=alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), color=alt.Color('team_long_name:N', title='Team'), tooltip='team_long_name:N') line = alt.Chart(premier1415noLC).mark_line(size=2).encode( x=alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), y=alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), color=alt.Color('team_long_name:N', title='Team'), tooltip='team_long_name:N') selectors = alt.Chart(premier1415).mark_point().encode( x='stage', opacity=alt.value(0), ).add_selection(nearest) labelsLC = alt.Chart(LClast1415).mark_text( align='left', dx=3, size=14).encode( alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), alt.Text('position'), alt.Color('team_long_name:N', title='Team', scale=palette), ) labels = alt.Chart(noLClast1415).mark_text( align='left', dx=3, size=12).encode( alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), alt.Text('position'), alt.Color('team_long_name:N', title='Team', scale=palette), ) points = line.mark_circle(size=80).encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0))) pointsLC = lineLC.mark_circle(size=100).encode(opacity=alt.condition( nearest, alt.value(1), alt.value(0)), ) rules = alt.Chart(premier1415).mark_rule(color='gray').encode( x='stage', ).transform_filter(nearest) tick = alt.Chart(premier1415mod).mark_tick( color='black', thickness=2, strokeDash=[1, 1], size=40 * 0.9, # controls width of tick. ).encode( x=alt.X('cumpoints:Q', scale=alt.Scale(domain=[0, 81]), title='Points'), y=alt.Y('team_long_name:N', sort=alt.EncodingSortField("cumpoints", op="max", order='descending'), title='Team'), ).transform_filter(nearest) hist = alt.Chart(premier1415).mark_bar().encode( x=alt.X('cumpoints:Q', scale=alt.Scale(domain=[0, 81]), title='Points'), y=alt.Y('team_long_name:N', sort=alt.EncodingSortField("cumpoints", op="max", order='descending'), title='Team'), color=alt.Color( 'team_long_name:N', scale=alt.Scale(domain=topteams1415))).transform_filter(nearest) text = hist.mark_text( align='left', baseline='middle', dx=3, # Nudges text to right so it doesn't appear on top of the bar fontSize=14).encode(text='cumpoints:Q') text1415 = alt.Chart({ 'values': [{ 'x': 26, 'y': 8 }] }).mark_text( text= 'With only 8 games left, Leicester City\n was at the bottom of the table', lineBreak='\n', align='left', fontSize=14 # , angle=346 ).encode(x='x:Q', y='y:Q') layer = alt.layer(lineLC, line, labels, labelsLC, selectors, points, pointsLC, rules, text1415, pointarrow, textarrow).properties(width=1100, height=300, title='') layer2 = alt.layer(hist, text, tick).properties( width=1120, height=300, title='Points at the selected Gameweek') return alt.VConcatChart(vconcat=(layer, layer2), padding={ "left": 120 }).configure_title(fontSize=16).configure_axis( labelFontSize=12, titleFontSize=12).configure_legend(labelFontSize=12, titleFontSize=12).to_json()
def create_graph(): # retira o número máximo de linhas para pot com Altair alt.data_transformers.disable_max_rows() # faz query no banco de dados autores = queryDB('author', ['ID_author','author']) artigos = queryDB('paper', ['ID_paper','paper']) author_paper = queryDB('author_paper', ['ID_paper','ID_author']) autores['ID_author'] = autores['ID_author'].astype(str) artigos['ID_paper'] = artigos['ID_paper'].astype(str) ### renderiza os gráficos ## Grafo 1 - Autores (authors) print('Preparando grafo dos autores...') graph = nx.Graph() # dataframe com colunas: paper e [lista_autores] group = pd.DataFrame(author_paper.groupby('ID_paper')['ID_author'].apply(list)) # Adicionando "edges" for j,row in group.iterrows(): i=len(row['ID_author']) for i in range(len(row['ID_author'])): for k in range(i,len(row['ID_author'])): graph.add_edge(row['ID_author'][i], row['ID_author'][k]) pos = nx.spring_layout(graph,k=0.2, iterations=50, weight=0.1, center=(0.5,0.5)) # forces graph layout # coletando nodes nodes = to_pandas_nodes(graph,pos) nodes.reset_index(inplace=True) nodes.rename(columns={'index':'ID_author'}, inplace=True) nodes = pd.merge(nodes,autores,on='ID_author') # coletando nome dos autores nodes = pd.merge(nodes,author_paper, on='ID_author') # coletando ID_paper # coletando edges edges = to_pandas_edges(graph,pos) # Gráfico 1 print('Criando interatividade com o Altair (autores) ...') selector = alt.selection_single(empty='all',fields=['ID_author']) # iniciando seletor points = alt.Chart(nodes).add_selection(selector).mark_point(filled=True,size=90).encode( alt.X('x', axis=alt.Axis(title='')), alt.Y('y', axis=alt.Axis(title='')), tooltip='author', opacity=alt.condition(selector,alt.value(0.95),alt.value(0.4),legend=None), color=alt.condition(selector, 'ID_author', alt.value('lightgray'), legend=None) ).properties( selection=selector ).transform_filter(selector) # cria um background para efeitos de transição do seletor bk = alt.Chart(nodes).mark_point(color='lightgray',filled=True,size=90).encode( alt.X('x', axis=alt.Axis(title='')), alt.Y('y', axis=alt.Axis(title='')), tooltip='author', opacity=alt.value(0.4), ) lines = alt.Chart(edges).mark_line(color='salmon').encode( alt.X('x', axis=alt.Axis(title='')), alt.Y('y', axis=alt.Axis(title='')), detail='edge', opacity=alt.value(0.15) ) chart = alt.LayerChart(layer=(lines,bk+points)).properties( height=350, width=450 ).interactive() ## Grafo 2 - Artigos (papers) print('Preparando grafo dos artigos...') graph1 = nx.Graph() group1 = pd.DataFrame(author_paper.groupby('ID_author')['ID_paper'].apply(list)) # Adicionando "edges" for j,row in group1.iterrows(): i=len(row['ID_paper']) for i in range(len(row['ID_paper'])): for k in range(i,len(row['ID_paper'])): graph1.add_edge(row['ID_paper'][i], row['ID_paper'][k]) pos1 = nx.spring_layout(graph1,k=0.2, iterations=50, weight=0.1, center=(0.5,0.5)) # forces graph layout # coletando nodes nodes1 = to_pandas_nodes(graph1, pos1) nodes1.reset_index(inplace=True) nodes1.rename(columns={'index':'ID_paper'}, inplace=True) nodes1 = pd.merge(nodes1,artigos,on='ID_paper') # coletando nome dos papers nodes1 = pd.merge(nodes1,author_paper,on='ID_paper') # coletando ID_author # coletando edges edges1 = to_pandas_edges(graph1,pos1) # Gráfico 2 print('Criando interatividade com o Altair (artigos)...') points1 = alt.Chart(nodes1).add_selection(selector).mark_point(filled=True,size=90).encode( alt.X('x', axis=alt.Axis(title='')), alt.Y('y', axis=alt.Axis(title='')), tooltip='paper', opacity=alt.condition(selector,alt.value(0.95),alt.value(0.4),legend=None), color=alt.condition(selector, 'ID_author', alt.value('lightgray'), legend=None) ).transform_filter(selector) # cria um background para efeitos de transição do seletor bk1 = alt.Chart(nodes1).mark_point(color='lightgray',filled=True,size=90).encode( alt.X('x', axis=alt.Axis(title='')), alt.Y('y', axis=alt.Axis(title='')), tooltip='paper', opacity=alt.value(0.4), ) lines1 = alt.Chart(edges1).mark_line(color='lightblue').encode( alt.X('x', axis=alt.Axis(title='')), alt.Y('y', axis=alt.Axis(title='')), detail='edge', opacity=alt.value(0.2) ) chart1 = alt.LayerChart(layer=(lines1,bk1 + points1)).properties( height=350, width=450 ).interactive() ### Concatenando horizontamnete os gráficos 1 e 2 horiz_chart = alt.hconcat(chart, chart1 ).configure_axis( ticks=False, grid=False, domain=False, labels=False).configure_view( strokeWidth=0 ) return horiz_chart.to_json()
alt.X(alt.repeat("column"), type='quantitative'), alt.Y(alt.repeat("row"), type='quantitative'), color='country', ) nearest = alt.selection(type='single', nearest=True, on='mouseover', fields=['year'], empty='none') selectors = alt.Chart(data).mark_point().encode( x='year', opacity=alt.value(0), ).add_selection(nearest) points = line.mark_point().encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0))) text = line.mark_text(align='left', dx=5, dy=-5).encode(text=alt.condition( nearest, alt.Y(alt.repeat("row"), type='quantitative'), alt.value(' '))) rules = alt.Chart(data).mark_rule(color='gray').encode( x='year', ).transform_filter(nearest) chart = alt.layer(line, selectors, points, rules, text).properties( width=600, height=300).repeat( row=['ticketSold', 'total gross(Billion CNY)', 'screen', 'avgPrice'], column=['year']).interactive() chart.savechart('plot.html')
def graph_spinorama(dfu, graph_params): xmin = graph_params['xmin'] xmax = graph_params['xmax'] ymin = graph_params['ymin'] ymax = graph_params['ymax'] if xmax == xmin: logging.error('Graph configuration is incorrect: xmin==xmax') if ymax == ymin: logging.error('Graph configuration is incorrect: ymin==ymax') # add selectors selectorsMeasurements = alt.selection_multi(fields=['Measurements'], bind='legend') scales = alt.selection_interval(bind='scales') # main charts xaxis = alt.X('Freq:Q', title='Freqency (Hz)', scale=alt.Scale(type='log', base=10, nice=False, domain=[xmin, xmax]), axis=alt.Axis(format='s')) yaxis = alt.Y('dB:Q', title='Sound Pressure (dB)', scale=alt.Scale(zero=False, domain=[ymin, ymax])) # why -10? di_yaxis = alt.Y('dB:Q', title='Sound Pressure DI (dB)', scale=alt.Scale(zero=False, domain=[-5, ymax - ymin - 5])) color = alt.Color('Measurements', type='nominal', sort=None) opacity = alt.condition(selectorsMeasurements, alt.value(1), alt.value(0.2)) line = alt.Chart(dfu).mark_line().transform_filter( alt.FieldOneOfPredicate(field='Measurements', oneOf=[ 'On Axis', 'Listening Window', 'Early Reflections', 'Sound Power' ])).encode(x=xaxis, y=yaxis, color=color, opacity=opacity) circle = alt.Chart(dfu).mark_circle(size=100).transform_filter( alt.FieldOneOfPredicate( field='Measurements', oneOf=[ 'On Axis', 'Listening Window', 'Early Reflections', 'Sound Power' ])).encode(x=xaxis, y=yaxis, color=color, opacity=alt.condition(nearest, alt.value(1), alt.value(0)), tooltip=['Measurements', 'Freq', 'dB']) di = alt.Chart(dfu).mark_line().transform_filter( alt.FieldOneOfPredicate( field='Measurements', oneOf=['Early Reflections DI', 'Sound Power DI'])).encode(x=xaxis, y=di_yaxis, color=color, opacity=opacity) circle_di = alt.Chart(dfu).mark_circle(size=100).transform_filter( alt.FieldOneOfPredicate( field='Measurements', oneOf=['Early Reflections DI', 'Sound Power DI' ])).encode(x=xaxis, y=di_yaxis, color=color, opacity=alt.condition(nearest, alt.value(1), alt.value(0)), tooltip=['Measurements', 'Freq', 'dB']) # assemble elements together spin = alt.layer(circle + line, circle_di + di).resolve_scale( y='independent').add_selection(selectorsMeasurements).add_selection( scales).add_selection(nearest).properties( width=graph_params['width'], height=graph_params['height']) return spin
def draw_legend(global_scales, selection, chart_width): """Draws the interactive group's colors legend for the chart.""" groups = global_scales["color"].domain labels = groups.copy() labels[0] = labels[0] + " [REF]" legend_df = pd.DataFrame({"attribute_value": groups, "label": labels}) # Position the legend to the right of the chart title_text_x_position = chart_width * 1.1 title_text_height = Legend.title_font_size + Legend.title_margin_bottom subtitle_text_height = Legend.font_size + Legend.vertical_spacing entries_circles_x_position = title_text_x_position + Legend.horizontal_spacing entries_text_x_position = ( title_text_x_position + 2 * Legend.circle_radius + Legend.horizontal_spacing ) # Title of the legend. title_text = ( alt.Chart(DUMMY_DF) .mark_text( align="left", baseline="middle", color=Legend.font_color, fontSize=Legend.title_font_size, font=FONT, fontWeight=Legend.title_font_weight, ) .encode( x=alt.value(title_text_x_position), y=alt.value(Legend.margin_top), text=alt.value("Groups"), ) ) # Subtitle text that explains how to interact with the legend. subtitle_text = ( alt.Chart(DUMMY_DF) .mark_text( align="left", baseline="middle", color=Legend.font_color, fontSize=Legend.font_size, font=FONT, fontWeight=Legend.font_weight, ) .encode( x=alt.value(title_text_x_position), y=alt.value(Legend.margin_top + title_text_height), text=alt.value("Click to highlight a group."), ) ) # Conditionally color each legend item # If the group is selected, it is colored according to the group color scale, otherwise it is faded color_encoding = alt.condition( selection, alt.Color("attribute_value:N", scale=global_scales["color"], legend=None), alt.value(Legend.color_faded), ) # Offset the positioning of the legend items after the subitlr text legend_start_y_position = ( Legend.margin_top + title_text_height + subtitle_text_height ) y_scale = alt.Scale( domain=groups, range=[ legend_start_y_position, legend_start_y_position # number of legend elements x text size + (len(groups) * Legend.font_size) # (number of "spacings" + start and end "spacings") x spacing + ((len(groups) + 1) * Legend.vertical_spacing), ], ) # Calculate circle size from radius entries_circle_size = Legend.circle_radius * math.pi ** 2 # Draw color squares for each group entries_circles = ( alt.Chart(legend_df) .mark_point(filled=True, opacity=1, size=entries_circle_size) .encode( x=alt.value(entries_circles_x_position), y=alt.Y("attribute_value:N", scale=y_scale, axis=no_axis()), color=color_encoding, shape=alt.Shape( "attribute_value:N", scale=global_scales["shape"], legend=None ), ) .add_selection(selection) ) trigger_text = alt.selection_multi(empty="all", fields=["attribute_value"]) # Draw colored label for each group entries_text = ( alt.Chart(legend_df) .mark_text( align="left", baseline="middle", font=FONT, fontSize=Legend.font_size, fontWeight=Legend.font_weight, ) .encode( x=alt.value(entries_text_x_position), y=alt.Y("attribute_value:N", scale=y_scale, axis=no_axis()), text=alt.Text("label:N"), color=color_encoding, ) .add_selection(trigger_text) ) return entries_circles + entries_text + subtitle_text + title_text
def graph_compare_cea2034(df, graph_params, speaker1, speaker2): selection1, selection2, selectorsMeasurements, scales = build_selections( df, speaker1, speaker2) # TODO(move to parameters) x_axis = alt.X('Freq:Q', scale=alt.Scale(type="log", domain=[20, 20000], nice=False)) y_axis = alt.Y('dB:Q', scale=alt.Scale(zero=False, domain=[-40, 10])) color = alt.Color('Measurements', type='nominal', sort=None) opacity = alt.condition(selectorsMeasurements, alt.value(1), alt.value(0.2)) line = alt.Chart(df).transform_filter( alt.FieldOneOfPredicate(field='Measurements', oneOf=[ 'On Axis', 'Listening Window', 'Early Reflections', 'Sound Power' ])).encode(x=x_axis, y=y_axis, color=color, opacity=opacity) points = line.mark_circle(size=100).encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0)), tooltip=['Measurements', 'Freq', 'dB']) di_axis = alt.Y('dB:Q', scale=alt.Scale(zero=False, domain=[-10, 40], nice=False)) di = alt.Chart(df).transform_filter( alt.FieldOneOfPredicate( field='Measurements', oneOf=['Early Reflections DI', 'Sound Power DI'])).encode(x=x_axis, y=di_axis, color=color, opacity=opacity) points_di = di.mark_circle(size=100).encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0)), tooltip=['Measurements', 'Freq', 'dB']) spin_full = alt.layer(points + line.mark_line(), points_di + di.mark_line(clip=True)).resolve_scale( y='independent').properties(width=600, height=300) spin_dash = alt.layer( points + line.mark_line(strokeDash=[4, 2]), points_di + di.mark_line(clip=True, strokeDash=[4, 2])).resolve_scale( y='independent').properties(width=600, height=300) line1 = spin_full.add_selection(selection1).transform_filter(selection1) line2 = spin_dash.add_selection(selection2).transform_filter(selection2) points = line.mark_point().encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0))) rules = alt.Chart(df).mark_rule(color='gray').encode( x='Freq:Q').transform_filter(nearest) layers = alt.layer( line2, line1, rules).add_selection(selectorsMeasurements).add_selection( scales).add_selection(nearest).interactive() return layers
st.write(viz0_ActorTimeSmall.add_selection(brush) & viz0_ActorTime.transform_filter(brush) \ & viz0_NumElectionTime.transform_filter(brush)) st.write( "###### Keep your mouse on the chart to zoom in / out and travel through the debate." ) st.write("# \n") st.write(viz1_NumElectionTime.add_selection(scales)) ## Theme word charts brush = alt.selection_single(fields=["broadTheme"]) domain = ["Joe Biden", "Donald J. Trump"] range_ = ["#e45756", "#f58518"] color = alt.condition( brush, alt.Color('name:N', legend=None, scale=alt.Scale(domain=domain, range=range_)), alt.value('lightgray')) broadTheme1 = alt.Chart(themeWordCount).mark_bar().encode( x=alt.X("broadTheme:N", title="Theme", sort=[ "healthcare", "covid", "economy", "environment", "race", "election" ]), y=alt.Y("wordCount:Q", title="Word Count"), color=color, tooltip=[alt.Tooltip('name', title="Actor")]) specificWord1 = alt.Chart(themeWordCount).mark_bar().encode(
def horizon_selector( base: alt.Chart, horizon_selection_brush: alt.MultiSelection, belief_horizon_unit: str, intuitive_forecast_horizon: bool, unique_belief_horizons, ) -> alt.LayerChart: bar_chart = ( base.mark_rule(orient="vertical").transform_filter( time_selection_brush ) # Apply brush before calculating accuracy metrics for the selected events on the fly .transform_calculate( constant=1 + alt.datum.event_start - alt.datum.event_start).transform_calculate( belief_horizon_str='datum.belief_horizon + " %s"' % belief_horizon_unit). encode( opacity=alt.condition( time_selection_brush, alt.Opacity("event_start:T", scale=alt.Scale(domain=(0.9999, 1)), legend=None), alt.value(0), ), # Trick to be able to apply the selection filter for event_start (event_start must be a field in one of the encoding channels) x=alt.X( "belief_horizon:Q", axis=alt.Axis(labelFlush=False), scale=alt.Scale( zero=False, domain=(unique_belief_horizons[0], unique_belief_horizons[-1]), ), title="", ), y=alt.Y( "constant:Q", title=" ", axis=alt.Axis(values=[], domain=False, ticks=False), ), color=alt.condition( horizon_selection_brush | horizon_hover_brush, alt.ColorValue("#c21431"), alt.ColorValue(idle_color), ), size=alt.value(1), tooltip=[ alt.Tooltip( "belief_horizon_str:N", title="Click to select %s" % ("forecast horizon" if intuitive_forecast_horizon else "belief horizon"), ) ], ).properties( height=30, title="Select %s" % ("forecast horizon" if intuitive_forecast_horizon else "belief horizon"), ).transform_filter(time_selection_brush)) circle_chart = (bar_chart.mark_circle().transform_calculate( half_constant=alt.datum.constant / 2).encode( y=alt.Y("half_constant:Q", title="", axis=alt.Axis(values=[])), size=alt.value(100), )) return ( bar_chart.add_selection(horizon_selection_brush, horizon_hover_brush) + circle_chart)
x='date:T', y=alt.Y('value:Q', axis=alt.Axis(title='# of cases')), color='key:N', ) # Transparent selectors across the chart. This is what tells us # the x-value of the cursor selectors = base.mark_point().encode( x='date:T', opacity=alt.value(0), tooltip=[alt.Tooltip('yearmonthdate(date)', title="Date")]).add_selection(nearest) # Draw points on the line, and highlight based on selection points = line.mark_point().encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0))) # Draw text labels near the points, and highlight based on selection text = line.mark_text( align='left', dx=5, dy=-5).encode(text=alt.condition(nearest, 'value:Q', alt.value(' '))) # Draw a rule at the location of the selection rules = alt.Chart(data).mark_rule(color='gray').encode( x='date:T', ).transform_filter(nearest) # Put the five layers into a chart and bind the data chart = alt.layer(line, selectors, points, rules, text).properties(width=900, height=300, title='Global Time Series')
def get_altair_chart(df, x_col, y_cols='ALL', cat_col=None, sel_cols=None, sliders=None, ns_opacity=1.0, chart_title='', scheme='lightmulti', mark_type='line', sort_values=False, y_index=-1, stack=None): if mark_type == 'bar': chart = alt.Chart(df).mark_bar() elif mark_type == 'area': chart = alt.Chart(df).mark_area() else: chart = alt.Chart(df).mark_line(point=True, strokeWidth=2) sort_axis = 'x' x_col_ed = x_col if sort_values: x_col_ed = alt.X(f'{x_col}:N', sort='y') chart = chart.encode( x=x_col_ed, tooltip=list(df.columns), ).properties(width=600, height=400) #.interactive() if sliders: for key, value in sliders.items(): if key == 'min': comparisson = '>=' elif key == 'max': comparisson = '<=' else: print( f"Atenção: a chave '{key}' não é válida para a variável sliders. Usar apenas 'min' ou 'max'" ) continue if type(value) is list: slider_col = value[0] if len(value) > 1: init_value = value[1] else: init_value = eval(f'{key}(df[slider_col])') else: slider_col = value init_value = eval(f'{key}(df[slider_col])') if slider_col in df.columns: slider = alt.binding_range(min=min(df[slider_col]), max=max(df[slider_col]), step=1) slider_selector = alt.selection_single( bind=slider, name=key, fields=[slider_col], init={slider_col: init_value}) chart = chart.add_selection(slider_selector).transform_filter( f'datum.{slider_col} {comparisson} {key}.{slider_col}[0]') if y_cols == 'ALL': index = 1 if cat_col: index += 1 if sel_cols: index += len(sel_cols) y_cols = df.columns[index:].to_list() if len(y_cols) > 1: columns = y_cols y_col_name = 'Y_col' select_box = alt.binding_select(options=columns, name=y_col_name) sel = alt.selection_single(fields=[y_col_name], bind=select_box, init={y_col_name: y_cols[y_index]}) chart = chart.transform_fold(columns, as_=[y_col_name, 'Valor']).transform_filter(sel) if stack == 'normalize': chart = chart.encode(y=alt.Y("Valor:Q", stack="normalize"), ) elif stack == 'sum': chart = chart.encode(y='sum(Valor):Q', ) else: chart = chart.encode(y='Valor:Q', ) chart = chart.add_selection(sel) else: y_col = y_cols[0] chart = chart.encode(y=y_col) # TODO: adicionar filtro de range # lower = chart.properties( # height=60 # ).add_selection(brush) # chart = chart & lower if cat_col: base_cat = cat_col chart = chart.encode( color=alt.Color(base_cat, scale=alt.Scale(scheme=scheme)), #,legend=None), ) sel_base = alt.selection_multi(empty='all', fields=[base_cat], bind='legend') chart = chart.add_selection(sel_base).encode(opacity=alt.condition( sel_base, alt.value(1.0), alt.value(ns_opacity))) bar = alt.Chart(df).mark_bar().encode( y=alt.Y(f'{base_cat}:O', title=None), x='total', # tooltip='total', color=alt.condition( sel_base, alt.Color(f'{base_cat}:N', scale=alt.Scale(scheme=scheme)), alt.ColorValue("lightgrey"), legend=None)).add_selection(sel_base).properties(width=100, height=400) chart = alt.concat(chart, bar) # chart = chart & lower TODO: adicionar fltro de range select_cols = sel_cols if select_cols: options_lists = [ df[cat].dropna().astype(str).sort_values().unique().tolist() for cat in select_cols ] selection = alt.selection_single( name='Selecione', fields=select_cols, init={ cat: options_lists[i][0] for i, cat in enumerate(select_cols) }, bind={ cat: alt.binding_select(options=options_lists[i]) for i, cat in enumerate(select_cols) }) chart = chart.add_selection(selection).transform_filter(selection) return chart
empty='none') # Transparent selectors across the chart. This is what tells us # the x-value of the cursor selectors = alt.Chart(source).mark_point().encode( x='x', opacity=alt.value(0), ).add_selection(nearest) # Draw points on the line, and highlight based on selection points = line.mark_point(point=True, filled=True, color="green", radius=500, size=300, strokeWidth=50).encode(opacity=alt.condition( nearest, alt.value(1), alt.value(0)), ) # Draw text labels near the points, and highlight based on selection text = line.mark_text(align='left', dx=10, dy=5, fontSize=16).encode( text=alt.condition(nearest, 'label' + ":N", alt.value(' '))).transform_calculate( label='datum.y + "= 2 * (" + datum.x + ") + 1"') # Draw a rule at the location of the selection rules = alt.Chart(source).mark_rule(color='gray', size=1).encode( x='x', ).transform_filter(nearest) # Put the five layers into a chart and bind the data chart = alt.layer(line, selectors, points, rules, text).properties( width=900, height=500).properties( title='A line chart for a simple function in altair').interactive(
import altair as alt import pandas as pd penguins_df = pd.read_csv('data/penguins.csv') click = alt.selection_multi(fields=____, bind=____) click_legend = (alt.Chart(penguins_df).mark_bar().encode( alt.X('count()', title='Number of Penguins'), alt.Y('island', title=None, sort=____), alt.Color(____, title="Island"), alt.Row('species', title=None), opacity=alt.condition(____, ____, ____)).add_selection(____)).properties(width=300) click_legend
# US states background background = alt.Chart(states).mark_geoshape( fill='lightgray', stroke='white' ).properties( title='US State Capitols', width=700, height=400 ).project('albersUsa') # Points and text hover = alt.selection(type='single', on='mouseover', nearest=True, fields=['lat', 'lon']) base = alt.Chart(capitals).encode( longitude='lon:Q', latitude='lat:Q' ) text = base.mark_text(dy=-5, align='right').encode( alt.Text('city', type='nominal'), opacity=alt.condition(~hover, alt.value(0), alt.value(1)) ) points = base.mark_point().encode( color=alt.value('black'), size=alt.condition(~hover, alt.value(30), alt.value(100)) ).add_selection(hover) background + points + text
def graph3_4(): def mds_special(): font = "Arial" axisColor = "#000000" gridColor = "#DEDDDD" return { "config": { "title": { "fontSize": 24, "font": font, "anchor": "start", # equivalent of left-aligned. "fontColor": "#000000" }, 'view': { "height": 300, "width": 400 }, "axisX": { "domain": True, #"domainColor": axisColor, "gridColor": gridColor, "domainWidth": 1, "grid": False, "labelFont": font, "labelFontSize": 12, "labelAngle": 0, "tickColor": axisColor, "tickSize": 5, # default, including it just to show you can change it "titleFont": font, "titleFontSize": 16, "titlePadding": 10, # guessing, not specified in styleguide "title": "X Axis Title (units)", }, "axisY": { "domain": False, "grid": True, "gridColor": gridColor, "gridWidth": 1, "labelFont": font, "labelFontSize": 14, "labelAngle": 0, #"ticks": False, # even if you don't have a "domain" you need to turn these off. "titleFont": font, "titleFontSize": 16, "titlePadding": 10, # guessing, not specified in styleguide "title": "Y Axis Title (units)", # titles are by default vertical left of axis so we need to hack this #"titleAngle": 0, # horizontal #"titleY": -10, # move it up #"titleX": 18, # move it to the right so it aligns with the labels }, } } # register the custom theme under a chosen name alt.themes.register('mds_special', mds_special) # enable the newly registered theme alt.themes.enable('mds_special') #alt.themes.enable('none') # to return to default # Wrangling data crime_data_n = crime_data crime_data_n['avg_hatecrimes_fbi_10days'] = ( (crime_data_n['avg_hatecrimes_per_100k_fbi'] / 365) * 10) crime_data_n['prop'] = (crime_data_n['hate_crimes_per_100k_splc'] - crime_data_n['avg_hatecrimes_fbi_10days'] ) / crime_data_n['avg_hatecrimes_fbi_10days'] mean_crime = crime_data_n['avg_hatecrimes_fbi_10days'].mean() conditions = [(crime_data_n['avg_hatecrimes_fbi_10days'] <= mean_crime), (crime_data_n['avg_hatecrimes_fbi_10days'] > mean_crime)] choices = ['low baseline crime rate', 'high baseline crime rate'] crime_data_n['crime_rate_bracket'] = np.select(conditions, choices) crime_data_n['diff_hatecrime'] = ( crime_data_n['hate_crimes_per_100k_splc'] - crime_data_n['avg_hatecrimes_fbi_10days']) crime_data_sorted_trump = crime_data_n.sort_values( by='share_voters_voted_trump') state_selector = alt.selection_multi(fields=['state']) # Create the plots l = alt.Chart( crime_data_n, title="States with low baseline crime rate").mark_bar().encode( alt.X('state:N', title='', axis=alt.Axis(labelAngle=-45)), alt.Y('prop:Q', title='Rate of change of hate crime pre and post election'), color=alt.condition( state_selector, alt.ColorValue("steelblue"), alt.ColorValue("grey"))).transform_filter( (datum.crime_rate_bracket == 'low baseline crime rate')) h = alt.Chart( crime_data_n, title="States with high baseline crime rate").mark_bar().encode( alt.X('state:N', axis=alt.Axis(labelAngle=-45), title=''), alt.Y('prop:Q', title='Rate of change of hate crime pre and post election', scale=alt.Scale(domain=[0, 30])), color=alt.condition( state_selector, alt.ColorValue("steelblue"), alt.ColorValue("grey"))).transform_filter( (datum.crime_rate_bracket == 'high baseline crime rate' )).properties(width=500) heatmap = alt.Chart(crime_data_sorted_trump, width=450).mark_rect().encode( alt.X('state', sort=None, title=" ", axis=alt.Axis(labelAngle=-45)), alt.Y('share_voters_voted_trump', title="Share of Trump voters (%)"), alt.Color('diff_hatecrime', title="Change in hate crime rate (%)"), tooltip=[ alt.Tooltip('state', title='State'), alt.Tooltip('hate_crimes_per_100k_splc', title="Hate crime rate 10 days after election"), alt.Tooltip('avg_hatecrimes_fbi_10days', title="Average rate of hate crime (for 10 days") ]).properties(width=1000).add_selection(state_selector) return alt.vconcat(heatmap, l | h)
""" Bar Chart with Negative Values ============================== This example shows a bar chart with both positive and negative values. """ # category: bar charts import altair as alt from vega_datasets import data source = data.us_employment() alt.Chart(source).mark_bar().encode( x="month:T", y="nonfarm_change:Q", color=alt.condition( alt.datum.nonfarm_change > 0, alt.value("steelblue"), # The positive color alt.value("orange") # The negative color ) ).properties(width=600)
######## Charts start here ######## st.subheader('Start by looking at the overall usage') #weighted usage in log by cost (x), colored by subscribed #adding clickable legend to highlight subscribed categories selection1 = alt.selection_multi(fields=['subscribed'], bind='legend') weighted_vs_cost = alt.Chart(df[filt]).mark_point( size=75, opacity=0.5, filled=True ).encode( alt.X('subscription_cost:Q', axis=alt.Axis(format='$,.2r'), scale=alt.Scale(clamp=True)), alt.Y('usage:Q', scale=alt.Scale(type='log'), title='Weighted Usage (DL + Cit + Auth)'), color=alt.condition(selection1, alt.Color('subscribed:N', scale=subscribed_colorscale), alt.value('lightgray')), #Nominal data type shape=alt.Shape('subscribed:N', scale=subscribed_point_mapping), tooltip=[ 'title', 'downloads', 'citations', 'authorships', 'usage', 'subscription_cost', 'use_oa_percent', 'free_instant_usage_percent', 'cpu', 'cpu_rank', 'subscribed' ], ).interactive().properties( height=500, title={ "text": [ "Total Weighed Usage vs. Cost, color-coded by Subscribed status; clickable legend" ], "subtitle": [
""" Interactive Rectangular Brush ============================= This example shows how to add a simple rectangular brush to a scatter plot. By clicking and dragging on the plot, you can highlight points within the range. """ # category: interactive charts import altair as alt from vega_datasets import data source = data.cars() brush = alt.selection(type='interval') alt.Chart(source).mark_point().encode( x='Horsepower:Q', y='Miles_per_Gallon:Q', color=alt.condition(brush, 'Cylinders:O', alt.value('grey')) ).add_selection(brush)
""" Bar Chart with Highlighted Bar ------------------------------ This example shows a basic bar chart with a single bar highlighted. """ # category: bar charts import altair as alt from vega_datasets import data source = data.wheat.url alt.Chart(source).mark_bar().encode( x='year:O', y="wheat:Q", # The highlight will be set on the result of a conditional statement color=alt.condition( alt.datum.year == 1810, # If the year is 1810 this test returns True, alt.value('orange'), # which sets the bar orange. alt.value( 'steelblue') # And if it's not true it sets the bar steelblue. )).properties(width=600)
""" Multi-panel Scatter Plot with Linked Brushing --------------------------------------------- This is an example of using an interval selection to control the color of points across multiple panels. """ # category: interactive charts import altair as alt from vega_datasets import data source = data.cars() brush = alt.selection(type='interval', resolve='global') base = alt.Chart(source).mark_point().encode( y='Miles_per_Gallon', color=alt.condition(brush, 'Origin', alt.ColorValue('gray')) ).add_selection( brush ).properties( width=250, height=250 ) base.encode(x='Horsepower') | base.encode(x='Acceleration')
def seasonEvolution(): premier1516 = pd.read_csv('App/Data/premier1516.csv') topteams1516 = [ 'Leicester City', 'Arsenal', 'Tottenham Hotspur', 'Manchester City', 'Chelsea' ] noLC = ['Arsenal', 'Tottenham Hotspur', 'Manchester City', 'Chelsea'] LC = ['Leicester City'] premier1516noLC = premier1516[premier1516['team_long_name'].isin(noLC)] premier1516LC = premier1516[premier1516['team_long_name'].isin(LC)] premier1516mod = premier1516.copy() premier1516mod[ 'cumpoints'] = premier1516mod['cumpoints'] - premier1516mod['points'] noLClast = premier1516noLC[premier1516noLC['stage'] == 38] LClast = premier1516LC[premier1516LC['stage'] == 38] LClast['position'] = '1st' noLClast['position'] = ['10th', '4th', '3rd', '2nd'] noLClast['labely'] = [50, 66, 70, 72] a = {'x': [13, 23], 'y': [28, 47], 'textof': ['➟', '➟']} arrow = pd.DataFrame(a) palette = alt.Scale( domain=topteams1516, range=['#0062cc', '#EF0107', 'yellow', '#6CABDD', "green"]) pointarrow = alt.Chart(arrow).mark_circle(size=100, color='black').encode(x='x', y='y') textarrow = alt.Chart(arrow).mark_text(dx=-20, dy=0, angle=45, fontSize=30).encode(x='x', y='y', text='textof') nearest = alt.selection(type='single', nearest=True, on='mouseover', fields=['stage'], init={'stage': 20}, empty='none') lineLC = alt.Chart(premier1516LC).mark_line(size=5).encode( x=alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), y=alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), color=alt.Color('team_long_name:N', title='Team'), tooltip='team_long_name:N') line = alt.Chart(premier1516noLC).mark_line(size=2).encode( x=alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), y=alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), color=alt.Color('team_long_name:N', title='Team'), tooltip='team_long_name:N') selectors = alt.Chart(premier1516).mark_point().encode( x='stage', opacity=alt.value(0)).add_selection(nearest) points = line.mark_circle(size=80).encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0))) labelsLC = alt.Chart(LClast).mark_text(align='left', dx=3, size=14).encode( alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), alt.Y('cumpoints:Q', title='Points', axis=alt.Axis(grid=False)), alt.Text('position'), alt.Color('team_long_name:N', title='Team', scale=palette), ) labels = alt.Chart(noLClast).mark_text(align='left', dx=3, size=12).encode( alt.X('stage', title='Gameweek', axis=alt.Axis(grid=False)), alt.Y('labely:Q', title='Points', axis=alt.Axis(grid=False)), alt.Text('position'), alt.Color('team_long_name:N', title='Team', scale=palette), ) pointsLC = lineLC.mark_circle(size=100).encode(opacity=alt.condition( nearest, alt.value(1), alt.value(0)), ) rules = alt.Chart(premier1516).mark_rule(color='gray').encode( x='stage', ).transform_filter(nearest) hist = alt.Chart(premier1516).mark_bar().encode( x=alt.X('cumpoints:Q', scale=alt.Scale(domain=[0, 81]), title='Points'), y=alt.Y('team_long_name:N', sort=alt.EncodingSortField("cumpoints", op="max", order='descending'), title='Team'), color=alt.Color( 'team_long_name:N', scale=alt.Scale(domain=topteams1516))).transform_filter(nearest) tick = alt.Chart(premier1516mod).mark_tick( color='black', thickness=2, strokeDash=[1, 1], size=60 * 0.9, # controls width of tick. ).encode( x=alt.X('cumpoints:Q', scale=alt.Scale(domain=[0, 81]), title='Points'), y=alt.Y('team_long_name:N', sort=alt.EncodingSortField("cumpoints", op="max", order='descending'), title='Team'), ).transform_filter(nearest) text = hist.mark_text( align='left', baseline='middle', dx=3, # Nudges text to right so it doesn't appear on top of the bar fontSize=14).encode(text='cumpoints:Q') text1516a = alt.Chart({ 'values': [{ 'x': 8.5, 'y': 44 }] }).mark_text( text='Leicester took the lead \n for the first time at Gameweek 13', lineBreak='\n', align='left', fontSize=14 # , angle=346 ).encode(x='x:Q', y='y:Q') text1516b = alt.Chart({ 'values': [{ 'x': 18, 'y': 62 }] }).mark_text( text= 'At Gameweek 23 Leicester City took the lead\n once again and never looked back', lineBreak='\n', align='left', fontSize=14 # , angle=346 ).encode(x='x:Q', y='y:Q') layer = alt.layer(lineLC, line, labelsLC, labels, selectors, points, pointsLC, rules, text1516a, text1516b, pointarrow, textarrow).properties(width=1200, height=300, title='') layer2 = alt.layer(hist, text, tick).properties( width=1200, height=300, title='Points at the selected Gameweek') return alt.VConcatChart(vconcat=(layer, layer2), padding={ "left": 100 }).configure_title(fontSize=16).configure_axis( labelFontSize=12, titleFontSize=12).configure_legend(labelFontSize=12, titleFontSize=12).to_json()
""" Bar Chart with Highlighted Bar ------------------------------ This example shows a basic bar chart with a single bar highlighted. """ # category: bar charts import altair as alt from vega_datasets import data source = data.wheat.url alt.Chart(source).mark_bar().encode( x='year:O', y="wheat:Q", # The highlight will be set on the result of a conditional statement color=alt.condition( alt.datum.year == 1810, # If the year is 1810 this test returns True, alt.value('orange'), # which sets the bar orange. alt.value('steelblue') # And if it's not true it sets the bar steelblue. ) ).properties(width=600)
y='y:Q', color='category:N' ) # Transparent selectors across the chart. This is what tells us # the x-value of the cursor selectors = alt.Chart().mark_point().encode( x='x:Q', opacity=alt.value(0), ).add_selection( nearest ) # Draw points on the line, and highlight based on selection points = line.mark_point().encode( opacity=alt.condition(nearest, alt.value(1), alt.value(0)) ) # Draw text labels near the points, and highlight based on selection text = line.mark_text(align='left', dx=5, dy=-5).encode( text=alt.condition(nearest, 'y:Q', alt.value(' ')) ) # Draw a rule at the location of the selection rules = alt.Chart().mark_rule(color='gray').encode( x='x:Q', ).transform_filter( nearest ) # Put the five layers into a chart and bind the data