def comparing_chartn(*series) -> alt.layer: series_dict = {} names = [] i = 1 for serie in series: name = "Scenario" + str(i) names.append(name) i += 1 series_dict[name] = serie dat = pd.DataFrame(series_dict) #print(dat) base = alt.Chart(dat.reset_index()).transform_fold(fold=names).encode( x=alt.X("index", title="Días desde hoy"), y=alt.Y("value:Q", title="Total de pacientes"), tooltip=["key:N", "value:Q"], color="key:N", text=alt.Text('max(daily):Q')) text = alt.Chart(dat.reset_index()).transform_fold(fold=names).encode( x=alt.X("index", aggregate={'argmax': 'value'}), y=alt.Y('max(value):Q'), color="key:N", text=alt.Text('max(value):Q')) return ( #.transform_fold(fold=["Scenario1", "Scenario2", "Scenario3"]) alt.layer(base.mark_line(), text.mark_text(dy=-10, fontSize=16)).interactive())
def visualize_is_tested_comparison(df,variable): if variable==None: df_grouped = df.groupby(['date','is_tested']).size().reset_index(name='count') bars = alt.Chart(df_grouped).mark_bar().encode( x = alt.X('date:N'), y = alt.Y('sum(count):Q', stack='zero',title='count') ) group_bars = alt.Chart(df_grouped).mark_bar().encode( x = alt.X('date:N'), y = alt.Y('sum(count):Q', stack='zero',title='count'), tooltip = [alt.Tooltip('sum(count):Q',title='count')], color = alt.Color('is_tested') ) text = bars.mark_text(dy=-10).encode( text = 'sum(count):Q' ) group_text = alt.Chart(df_grouped).mark_text(dy=12,color='white').encode( x = alt.X('date:N'), y = alt.Y('sum(count):Q', stack='zero'), detail = 'is_tested:N', text = alt.Text('sum(count):Q') ) st.altair_chart((group_bars + text + group_text).properties(height=800,width=800,title='Comparison Between Sales of Company X & Y Across Time')) else: for _type in df[variable].unique(): df_temp = df[df[variable]==_type] df_grouped = df_temp.groupby(['date','is_tested']).size().reset_index(name='count') bars = alt.Chart(df_grouped).mark_bar().encode( x = alt.X('date:N'), y = alt.Y('sum(count):Q', stack='zero',title='count') ) group_bars = alt.Chart(df_grouped).mark_bar().encode( x = alt.X('date:N'), y = alt.Y('sum(count):Q', stack='zero',title='count'), tooltip = [alt.Tooltip('sum(count):Q',title='count')], color = alt.Color('is_tested') ) text = bars.mark_text(dy=-10).encode( text = 'sum(count):Q' ) group_text = alt.Chart(df_grouped).mark_text(dy=12,color='white').encode( x = alt.X('date:N'), y = alt.Y('sum(count):Q', stack='zero'), detail = 'is_tested:N', text = alt.Text('sum(count):Q') ) st.altair_chart((group_bars + text + group_text).properties(height=800,width=800,title='Comparison Between Sales of Company X & Y Across Time for value: {}'.format(_type)))
def update_job_name_by_gender(job_name): chart = alt.Chart(jobs).mark_line().encode( alt.X('year:O', title='Year', axis=alt.Axis(labelAngle=-45)), alt.Y('count:Q', axis=alt.Axis(format='~s', title='Number of Employees')), color=alt.Color('sex:N', legend=alt.Legend(title="Genders"))).transform_filter( alt.datum.job == job_name).properties( width=500, height=250, title='Number of Employees by Year') chart_w_interaction = alt.layer( chart, # base line chart alt.Chart().mark_rule(color='#aaa').encode( x='year:O').transform_filter(label), chart.mark_circle().encode(opacity=alt.condition( label, alt.value(1), alt.value(0))).add_selection(label), chart.mark_text( align='left', dx=5, dy=-5, stroke='white', strokeWidth=2).encode( text=alt.Text('count:Q', format='~s')).transform_filter(label), chart.mark_text(align='left', dx=5, dy=-5).encode( text=alt.Text('count:Q', format='~s')).transform_filter(label), data=jobs).configure_axis(labelFontSize=14, titleFontSize=14).configure_legend( labelFontSize=13, titleFontSize=13) # Save html as a StringIO object in memory jobs_by_gender_html = io.StringIO() chart_w_interaction.save(jobs_by_gender_html, 'html') # Return the html from StringIO object return jobs_by_gender_html.getvalue()
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 generate_audit_score_chart(days: typing.List[str], scores: typing.List[float], *, score_color_percentage: float = 80.0) -> alt.Chart: data = pd.DataFrame({'days': days, 'scores': [score / 100.0 for score in scores]}) root_chart = alt.Chart(data).mark_bar().encode( x=alt.X('days', axis=alt.Axis(title='Date', labelAngle=0)), y=alt.Y('scores', title='Audit Score') ) bars = root_chart.mark_bar().encode( color=alt.condition( alt.datum.scores > score_color_percentage / 100.0, alt.value('orange'), alt.value('red') ) ) text = root_chart.mark_text( color='black', fontSize=13, dy=-15 ).encode( text=alt.Text('scores:Q', format='.2%') ) return (bars + text).properties(background='white')
def perfect_subset_overlap_summary(df: DataFrame) -> alt.Chart: """Summarize the count of instances of parent/child overlap by NLA (parent greater, equal, child greater) """ top_bar = alt.Chart(df).mark_bar().encode( x=alt.X('count(parent)', stack='normalize', axis=None, sort=[ 'parent nla greater than child', 'parent and child nla equal', 'parent nla less than child' ]), color=alt.Color('nla_variance', scale=alt.Scale(scheme='greys')), order=alt.Order('nla_variance', sort='ascending')).properties(width=600, height=75) text = alt.Chart(df).mark_text(dx=-15, dy=0, color='white').encode( x=alt.X('count(parent):Q', stack='normalize', axis=None, sort=[ 'parent nla greater than child', 'parent and child nla equal', 'parent nla less than child' ]), detail='nla_variance:N', text=alt.Text('count(parent):Q', format='.0f'), order=alt.Order('nla_variance', sort='ascending')) top_chart = top_bar + text return top_chart
def plot_class_report(learn): alt.renderers.enable('notebook') clsrpt = class_report(learn) charts = [] for key in clsrpt.keys(): fildata = clsrpt[key] fildatax = [] fildatay = [] supp = key + ', Support: ' for xxx in fildata: if xxx == 'support': supp += str(fildata[xxx]) continue fildatax.append(xxx) fildatay.append(fildata[xxx]) df = pd.DataFrame({'x': fildatax, 'y': fildatay}) bars = alt.Chart(df, width=200).mark_bar(size=30).encode( x=alt.X("x", axis=alt.Axis(labelAngle=0, title='')), y=alt.Y('y', axis=alt.Axis(title=''), scale=alt.Scale(domain=(0, 1))), ) text = alt.Chart(df).mark_text(baseline='bottom', dy=-1).encode( x='x', y='y', text=alt.Text('y', format='.2f')) chart = bars + text charts.append(chart.properties(title=supp)) return reduce((lambda x, y: alt.hconcat(x, y)), charts)
def __draw_x_ticks_labels(scales, chart_height): """Draws the numbers in the horizontal axis.""" axis_values = [0, 0.25, 0.5, 0.75, 1] axis_df = pd.DataFrame({"value": axis_values}) x_ticks_labels = ( alt.Chart(axis_df) .mark_text( tooltip="", align="center", fontSize=Axis.label_font_size, font=FONT, fontWeight=Axis.label_font_weight, color=Axis.label_color, ) .encode( text=alt.Text("value:N"), x=alt.X( "value:Q", scale=scales["x"], ), y=alt.value(Metric_Chart.padding_y * chart_height * 0.7), ) ) return x_ticks_labels
def barplot(df, date=None, selectedPlayer=None, kind='total'): """Create activities barplot. kind='total' returns a barplot with x-label indicating the average is for the whole period kind='weekly' returns a barplot with x-label indicating the average is for a specific week """ df = df.reset_index() df['XP da atividade'] = activities['Pontos'] df.columns = ['Atividades', ' ', 'XP da atividade'] if kind == 'total': title = 'Número médio de atividades realizadas em todo o período' elif kind == 'weekly': title = 'Número médio de atividades realizadas na semana de {}'.format( pd.to_datetime(date, format='%d/%m/%y').strftime('%d/%m')) if selectedPlayer is not None: title = 'Atividades realizadas na semana de {} - {}'.format( pd.to_datetime(date, format='%d/%m/%y').strftime('%d/%m'), selectedPlayer) bars = alt.Chart(df, title=title).mark_bar( cornerRadiusTopLeft=3, cornerRadiusTopRight=3).encode( x=alt.X(df.columns[1] + ':Q', axis=alt.Axis(tickMinStep=1)), y=alt.Y(df.columns[0] + ':N', sort='color'), color=alt.Color(df.columns[2] + ':O', scale=alt.Scale(scheme="plasma"))) text = bars.mark_text( align='left', baseline='middle', dx=3 # Nudges text to right so it doesn't appear on top of the bar ).encode(text=alt.Text(df.columns[1] + ':Q', format='.2')) barplot = (bars + text).properties(width=797, height=400) return barplot
def sector_distribution(self, year): title = "Public information requests by sector" chart = AltairChart(name=title, title=title, chart_type="bar") sectors = year.jurisdiction.sectors() chart.header["authority__name"] = "Sector" chart.header["value"] = "Public information requests" year_values = Value.objects.filter(year=year, authority__in=sectors, property__special="PI_ALL") df = chart.apply_query(year_values) pir = df["Public information requests"] df["Percentage"] = pir / pir.sum() chart.set_options(x="Public information requests", y=alt.Y("Sector", sort="-x"), tooltip=[ "Sector", alt.Tooltip("Public information requests", format=","), alt.Tooltip("Percentage", format=",.2%") ]) chart.set_text_options(text=alt.Text('Public information requests', format=".2s"), align='left', baseline='middle', dx=3) chart.custom_settings = lambda x: x.configure_point(size=50) return chart
def plot_heatmap(df, ptitle, x, y, xtitle="", ytitle="", annot_fmt=".3f", ptitle_offset=-5): if ptitle != "": ptitle = alt.TitleParams(ptitle, offset=ptitle_offset) base = (alt.Chart(df, title=ptitle).mark_rect().encode( x=alt.X(x, title=xtitle), y=alt.Y(y, title=ytitle), )) heatmap = base.mark_rect(stroke="white", strokeWidth=2).encode(color=alt.Color( "value:Q", scale=alt.Scale(type="log", scheme="yelloworangered"), legend=None, ), ) text = base.mark_text(baseline="middle").encode( text=alt.Text("value:Q", format=annot_fmt), color=alt.condition( alt.datum.value > df["value"].mean(), alt.value("white"), alt.value("black"), ), ) crchart = heatmap + text return crchart
def plot_heatmap(mat, str_tit): ncat_men, ncat_women = mat.shape mat_arr = np.empty((mat.size, 4)) mat_min, mat_max = np.min(mat), np.max(mat) i = 0 for ix in range(ncat_men): for iy in range(ncat_women): m = mat[ix, iy] s = m - mat_min + 1 mat_arr[i, :] = np.array([ix, iy, m, s]) i += 1 mat_df = pd.DataFrame(mat_arr, columns=['Men', 'Women', 'Value', 'Size']) mat_df = mat_df.astype(dtype={ 'Men': int, 'Women': int, 'Value': float, 'Size': float }) base = alt.Chart(mat_df).encode(x='Men:O', y=alt.Y('Women:O', sort="descending")) mat_map = base.mark_circle(opacity=0.4).encode( size=alt.Size('Size:Q', legend=None, scale=alt.Scale(range=[1000, 10000])), color=alt.Color('Value:Q'), # tooltip=alt.Tooltip('Value', format=".2f") ) text = base.mark_text(baseline='middle', fontSize=16).encode(text=alt.Text('Value:Q', format=".2f"), ) both = (mat_map + text).properties(title=str_tit, width=500, height=500) return both
def heatmap_text(df: pd.DataFrame, color_condition: dict) -> alt.vegalite.v4.api.Chart: """ Function that creates and returns a Text for the Heatmap. This text incorporates all numbers across the chart. Parameters ---------- df : pd.DataFrame Dataframe whose walues will be plotted with a heatmap. color_condition: dict {'condition': {'test': condition, 'value if true': value1}, 'value if false': value2} Returns ------- Text Heatmap Chart: altair.vegalite.v4.api.Chart This is an Altair/Vega-Lite Text Heatmap chart. """ return alt.Chart(df).mark_text(color='white').encode( alt.X('x:O', title='Models'), alt.Y('y:O', title='Hardware Platforms'), text=alt.Text('values:Q', format='.0f'), color=color_condition, tooltip=[ alt.Tooltip('values:Q', format='.0f', title='Input/sec'), alt.Tooltip('x:N', title='Model'), alt.Tooltip('y:N', title='Hardware Platform'), ])
def legend(): text = ( alt.Chart(df) .mark_text(dx=-16, color="white") .encode( x=alt.X("sum(num_speaker):Q", stack="normalize"), y="published_year:O", detail="published_day", text=alt.Text("sum(num_speaker):Q", format=".1f"), ) ) selection = alt.selection_multi(fields=["published_day"], bind="legend") chart = ( alt.Chart(df) .mark_bar() .encode( x=alt.X("sum(num_speaker)", stack="normalize"), y="published_year:O", opacity=alt.condition(selection, alt.value(1), alt.value(0.2)), color="published_day", tooltip=["sum(num_speaker)", "published_day"], ) .interactive() .properties(height=500, width=700) .add_selection(selection) ) st.altair_chart(chart + text)
def text_chart(): text = ( alt.Chart(df) .mark_text(dx=-16, color="white") .encode( x=alt.X("sum(num_speaker):Q", stack="normalize"), y="published_year:O", detail="published_day", text=alt.Text("sum(num_speaker):Q", format=".1f"), ) ) chart = ( alt.Chart(df) .mark_bar() .encode( x=alt.X("sum(num_speaker)", stack="normalize"), y="published_year:O", color="published_day", tooltip=["sum(num_speaker)", "published_day"], ) .interactive() .properties(height=500, width=700) ) st.altair_chart(chart + text)
def unsupervised_metrics(M, metrics_labels, types, filename='metrics_unsuper.html'): rows = [(metrics_labels[i], types[j], m) for (i, j), m in np.ndenumerate(M)] columns = ['m', 't', 'v'] df = pd.DataFrame.from_records(rows, columns=columns) heatmap = alt.Chart(df).encode( alt.X('m:N'), alt.Y('t:N'), ).mark_rect().encode(color='v:Q') text = alt.Chart(df).encode( alt.X('m:N'), alt.Y('t:N'), ).mark_text(baseline='middle').encode( text=alt.Text('v:Q', format='.3f'), color=alt.value('black'), # color=alt.condition( # alt.datum.v < 0.8, # alt.value('black'), # alt.value('white') # ) ) p = (heatmap + text).properties(width=400, height=400) p.save(filename) return p
def make_correlation(data, eda_file_path): """ Creates a pearson's correlation plot of the continuous variables. Parameters: data -- (dataframe) The training data eda_file_path -- (str) The path to specify where the plot is saved """ data_corr = (data.corr().reset_index().rename(columns={ 'index': 'Variable 1' }).melt(id_vars=['Variable 1'], value_name='Correlation', var_name='Variable 2')) base = alt.Chart(data_corr).encode(alt.Y('Variable 1:N'), alt.X('Variable 2:N')) heatmap = base.mark_rect().encode( alt.Color('Correlation:Q', scale=alt.Scale(scheme='viridis'))) text = base.mark_text(baseline='middle').encode( text=alt.Text('Correlation:Q', format='.2'), color=alt.condition(alt.datum.Correlation >= 0.95, alt.value('black'), alt.value('white'))) plot = (heatmap + text).properties( width=400, height=400, title="Pearson's Correlation").configure_axis( labelFontSize=15, titleFontSize=22).configure_title(fontSize=26) plot.save("{}corrplot.png".format(eda_file_path)) print(f"corrplot.png saved to {eda_file_path}")
def popualtion_GDP_inter(dataset, filter): selection = alt.selection_single(on='mouseover', empty='none') point_1 = alt.Chart( dataset, title='Population - GDP per Capita').mark_point( filled=True, size=90).encode( x=alt.X('Population:Q'), y=alt.Y('GDP per Capita:Q'), size='Population', tooltip=['Country', 'Code']).add_selection(selection).transform_filter( alt.FieldOneOfPredicate(field='Country', oneOf=filter)) point_2 = alt.Chart(dataset).mark_point(filled=True, size=90).encode( x=alt.X('Population:Q'), y=alt.Y('GDP per Capita:Q'), size='Population', color=alt.value('red')).transform_filter(alt.datum.Country == 'Norway') text = alt.Chart(dataset).encode( x=alt.X('Population:Q'), y=alt.Y('GDP per Capita:Q'), text=alt.Text('Country'), color=alt.value('black')).mark_text(dy=-4, dx=25).transform_filter( alt.FieldOneOfPredicate(field='Country', oneOf=filter)) return (point_1 + point_2 + text).interactive()
def heatmap(data, vmin=None, vmax=None, annot=None, fmt='.2g'): # We always want to have a DataFrame with semantic information if not isinstance(data, pd.DataFrame): matrix = np.asarray(data) data = pd.DataFrame(matrix) melted = data.stack().reset_index(name='Value') x = data.columns.name y = data.index.name heatmap = alt.Chart(melted).mark_rect().encode( alt.X('{x}:O'.format(x=x), scale=alt.Scale(paddingInner=0)), alt.Y('{y}:O'.format(y=y), scale=alt.Scale(paddingInner=0)), color='Value:Q' ) if not annot: return heatmap # Overlay text text = alt.Chart(melted).mark_text(baseline='middle').encode( x='{x}:O'.format(x=x), y='{y}:O'.format(y=y), text=alt.Text('Value', format=fmt), color=alt.condition(alt.expr.datum['Value'] > 70, alt.value('black'), alt.value('white')) ) return heatmap + text
def make_chart(self, df): base = alt.Chart(df).mark_rect().encode( x=alt.X('yearmonthdate(datum_date):O', title="Fecha evento", sort="descending", axis=alt.Axis(format='%d/%m')), y=alt.Y('yearmonthdate(bulletin_date):O', title=None, sort="descending", axis=alt.Axis(format='%d/%m')), tooltip=['bulletin_date:T', 'datum_date:T', 'value']) heatmap = base.mark_rect().encode(color=alt.Color( 'value:Q', title=None, legend=None, scale=alt.Scale( scheme="redgrey", domainMid=0, # WORKAROUND: Set the domain manually to forcibly # include zero or else we run into # https://github.com/vega/vega-lite/issues/6544 domain=alt.DomainUnionWith(unionWith=[0])))) text = base.mark_text(fontSize=4).encode(text=alt.Text('value:Q'), color=util.heatmap_text_color( df, 'value')) return (heatmap + text).properties(width=585, height=120).facet( row=alt.Row('variable', title=None, sort=['Confirmados', 'Probables', 'Muertes']))
def __draw_x_ticks_labels(scales, chart_height): """Draws the numbers in the horizontal axis.""" # The values to be drawn, we don't want to draw 0 (which corresponds to a ratio of 1) as we later draw an annotation. axis_values = [ x for x in list(range(scales["x"].domain[0], scales["x"].domain[1] + 1)) ] # Given the semantic of the chart, (how many times smaller or larger) we draw absolute values. axis_values_labels = [abs(x) + 1 if x != 0 else "=" for x in axis_values] axis_df = pd.DataFrame({"value": axis_values, "label": axis_values_labels}) tick_labels = (alt.Chart(axis_df).mark_text( tooltip="", align="center", font=FONT, fontSize=Axis.label_font_size, fontWeight=Axis.label_font_weight, color=Axis.label_color, ).encode( text=alt.Text("label:N"), x=alt.X( "value:Q", scale=scales["x"], ), y=alt.value(Disparity_Chart.padding * chart_height * 0.7), )) return tick_labels
def show_produtividade(state): df = state.df.copy() df['Expedido'] = pd.to_datetime(df['Expedido'], dayfirst=True) st.write('## Análises por Mês (exclui Aditamentos)') totais = df.loc[~df['Tipo de Exame'].isin([ 'Aditamento Imunocitoquímica', 'HBA - Aditamento de Imunocitoquímica' ])] number_of_months = (totais.Expedido.max().to_period('M') - totais.Expedido.min().to_period('M')).n bar = alt.Chart(totais).mark_bar().encode( x=alt.X('yearmonth(Expedido):T', axis=alt.Axis(title='Data', tickCount=number_of_months, grid=False)), y=alt.Y('count(Exame)', axis=alt.Axis(title='Número de Análises'))) text = alt.Chart(totais).mark_text(dx=12, dy=-5, color='black').encode( x=alt.X('yearmonth(Expedido):T', axis=alt.Axis(title='Data')), y=alt.Y('count(Exame)', axis=alt.Axis(title='Número de Análises')), text=alt.Text('count(Exame)')) st.altair_chart(bar + text, use_container_width=True)
def plot_covariate_effects(self): """Plot covariate effects """ ce = (self.covariate_effects - 1) * 100 cov_stats = pd.melt(self.covariate_statistics.reset_index(), var_name='condition', id_vars=['covariate'], value_vars=['p5', 'p95', 'other']) cov_stats = cov_stats.replace({ 'p5': '5th', 'p95': '95th' }).set_index(['covariate', 'condition']) ce = ce.join(cov_stats, how='inner') # The left join reorders the index, pandas bug #34133 ce = ce.reorder_levels(['parameter', 'covariate', 'condition']) param_names = list(ce.index.get_level_values('parameter').unique()) plots = [] for parameter in param_names: df = ce.xs(parameter, level=0) df = df.reset_index() error_bars = alt.Chart(df).mark_errorbar(ticks=True).encode( x=alt.X('p5:Q', title='Effect size in percent', scale=alt.Scale(zero=False)), x2=alt.X2('p95:Q'), y=alt.Y('condition:N', title=None), ) rule = alt.Chart(df).mark_rule( strokeDash=[10, 4], color='gray').encode( x=alt.X('xzero:Q')).transform_calculate(xzero="0") points = alt.Chart(df).mark_point(filled=True, color='black').encode( x=alt.X('mean:Q'), y=alt.Y('condition:N'), ) text = alt.Chart(df).mark_text(dy=-15, color="red").encode( x=alt.X("mean:Q"), y=alt.Y("condition:N"), text=alt.Text("value:Q")) plot = alt.layer( error_bars, rule, points, text, data=df, width=800, height=100).facet( columns=1.0, row=alt.Facet('covariate:N', title=None), title=f'{parameter}').resolve_scale(y='independent') plots.append(plot) v = alt.vconcat(*plots).resolve_scale(x='shared') return v
def get_map(self, day_str, title=""): chart_data = self._prepare_dataset(day_str) source = alt.topo_feature(data.world_110m.url, 'countries') background = alt.Chart(source).mark_geoshape( fill="lightgray", stroke="white").properties(width=1000, height=500).project("equirectangular") hover = alt.selection(type='single', on='mouseover', nearest=False, fields=[self.col_lat, self.col_long]) text = background.mark_text(dy=-5, align='right').encode( alt.Text(f'{self.col_name_countries}:N', type='nominal'), opacity=alt.condition(~hover, alt.value(0), alt.value(1))) points = alt.Chart(chart_data).mark_circle().encode( latitude=f"{self.col_lat}:Q", longitude=f"{self.col_long}:Q", size=alt.Size(f"{day_str}:Q", scale=alt.Scale(range=[0, 7000]), legend=None), order=alt.Order(f"{day_str}:Q", sort="descending"), tooltip=[f'{self.col_name_countries}:N', f'{day_str}:Q' ]).add_selection(hover).properties(title=title) chart = alt.layer(background, points, text) return chart
def create(database: sqlite3.Connection, table: str, column: str, name_map: t.Mapping[str, str] = None, transform: t.Optional[t.Callable[[CountPairs], CountPairs]] = None, orientation='vertical') -> altair.Chart: count_pairs = _get_counts(database, table, column) transformed_pairs = transform(count_pairs) if transform else count_pairs named_pairs = _rename(transformed_pairs, name_map) if name_map else transformed_pairs sorted_pairs = sorted(named_pairs) names, counts = zip(*sorted_pairs) # Unzip data = pandas.DataFrame({'x': list(names), 'y': list(counts)}) width = 600 height = 300 x = {'source': 'x', 'title': column} y = {'source': 'y', 'title': 'Count'} if orientation == 'horizontal': height = height + 8 * len(sorted_pairs) x, y = y, x bars = altair.Chart(data, width=width, height=height).mark_bar().encode( altair.X(x['source'], axis=altair.Axis(title=x['title'], labelAngle=0)), altair.Y(y['source'], axis=altair.Axis(title=y['title']))) text = bars.transform_joinaggregate(total='sum(y)').transform_calculate( percentage='datum.y / datum.total').mark_text( align='center', baseline='top', dy=-15).encode(altair.Text('percentage:Q', format='.1%')) chart = bars + text if orientation == 'vertical' else bars return chart
def _build_type_chart(variant_type_counts): """Create a chart of the counts of each variant type.""" width = 400 height = 200 title = 'Variant types' variant_type_data = _dict_to_dataframe(variant_type_counts) type_chart = _placeholder_for_empty_chart('No entries in VCF', width=width, height=height, title=title) if not variant_type_data.empty: bars = alt.Chart(variant_type_data).mark_bar().encode( x=alt.X('label', title=None, sort=ordered_variant_type_labels, axis=alt.Axis(labelAngle=-45)), y=alt.Y('value', axis=alt.Axis(title='Count', format='s')), tooltip=alt.Tooltip('value', format='.4s'), color=alt.Color('label', legend=None, scale=alt.Scale( scheme='set1', domain=ordered_variant_type_labels))) labels = bars.mark_text(dy=-5).encode( text=alt.Text('value', format='.4s')) type_chart = (bars + labels).properties(width=width, height=height, title=title) return type_chart
def _build_tt_chart(titv_counts): """Built chart showing counts of transitions and transversions.""" width = 150 height = 200 ti = titv_counts['Transition'] tv = titv_counts['Transversion'] # Show TiTv ratio with fallback to avoid division by 0 titv_ratio = '%.2f' % (float(ti) / tv) if tv > 0 else '%d / 0' % (ti) title = 'Biallelic Ti/Tv ratio: %s' % (titv_ratio) tt_chart = _placeholder_for_empty_chart('No biallelic SNPs', width=width, height=height, title=title) tt_labels = ['Transition', 'Transversion'] if sum([titv_counts[k] for k in titv_counts]) > 0: tt_data = _dict_to_dataframe(titv_counts) bars = alt.Chart(tt_data).mark_bar().encode( x=alt.X('label', sort=tt_labels, axis=alt.Axis(title=None, labelAngle=0)), y=alt.Y('value', axis=alt.Axis(title='Count', format='s')), tooltip=alt.Tooltip('value', format='.4s'), color=alt.Color('label', legend=None, sort=tt_labels, scale=alt.Scale(scheme='teals', domain=tt_labels))) labels = bars.mark_text(dy=-5).encode( text=alt.Text('value', format='.4s')) tt_chart = (bars + labels).properties(title=title, width=width, height=height) return tt_chart
def metrics(M, metrics_labels, types, filename='metrics.html'): rows = [(metrics_labels[i], types[j], types[k], m) for (i, j, k), m in np.ndenumerate(M)] columns = ['m', 't1', 't2', 'v'] df = pd.DataFrame.from_records(rows, columns=columns) rating_radio = alt.binding_radio(options=metrics_labels) rating_select = alt.selection_single(fields=['m'], bind=rating_radio, name="Metric", empty='none', init={'m': metrics_labels[0]}) heatmap = alt.Chart(df).encode( alt.X('t2:N'), alt.Y('t1:N'), ).mark_rect().encode(color='v:Q') text = alt.Chart(df).encode( alt.X('t2:N'), alt.Y('t1:N'), ).mark_text(baseline='middle').encode( text=alt.Text('v:Q', format='.3f'), color=alt.value('black'), # color=alt.condition( # alt.datum.v < 0.8, # alt.value('black'), # alt.value('white') # ) ) p = (heatmap + text).add_selection(rating_select).transform_filter( rating_select).properties(width=400, height=400) p.save(filename) return p
def visualize_heatmap(self, df): corrMatrix = df.corr().reset_index().melt('index') corrMatrix.columns = ['X', 'Y', 'Correlation'] base = alt.Chart(corrMatrix).transform_filter( alt.datum.X < alt.datum.Y).encode( x='X', y='Y', ).properties( width=self.width, height=self.height ) rects = base.mark_rect().encode( color='Correlation' ) text = base.mark_text( size=30 ).encode( text=alt.Text('Correlation', format='.2f'), color=alt.condition( "datum.Correlation > 0.5", alt.value('white'), alt.value('black') ) ) st.text("") st.text("") st.write(rects + text)
def _create_heatmap(self, y_dummies, y_pred, normalize, zero_diag, size, color_scheme, title): classes = y_dummies.columns.tolist() y_test = y_dummies.values cm = confusion_matrix(y_test.argmax(1), y_pred.argmax(1)) if zero_diag: np.fill_diagonal(cm, 0) if normalize: cm = cm / cm.sum(axis=1)[:, np.newaxis] df = (pd.DataFrame( cm.round(2), columns=classes, index=classes).reset_index().melt(id_vars="index").round(2)) base = alt.Chart(df).encode( x=alt.X("index:N", title="Predicted Label"), y=alt.Y("variable:N", title="True Label"), tooltip=["value"], ) heatmap = base.mark_rect().encode( color=alt.Color("value:Q", scale=alt.Scale(scheme=color_scheme))) text = base.mark_text(size=0.5 * (size / len(classes))).encode( text=alt.Text("value")) return (heatmap + text).properties(width=size, height=size, title=title)