import altair as alt import pandas as pd import matplotlib.cm as cm def altair_gauge(score, max_score, title): source = pd.DataFrame({"category": [1,2], "value": [4,3], "tt": ['hello!',None]}) gauge_theta2 = -1 * 2 * 3.14 * 0.25 + 3.14 * score / float(max_score) c = alt.layer( alt.Chart(source).mark_arc(innerRadius=100, theta=0, thetaOffset=(-1*2*3.14*0.25), theta2=(-1*2*3.14*0.25 + 3.14), color='lightgray', tooltip=None), alt.Chart(source).mark_arc(innerRadius=100, theta=0, thetaOffset=(-1*2*3.14*0.25), theta2=gauge_theta2, tooltip=f'{title}: {int(score)}', color=get_color(score, max_score)), alt.Chart(source).mark_text(text='%.1d' % score, size=80, font='Calibri', dy=-30) ).properties(title=title) return c def get_color(score, max_score): cmap = cm.get_cmap('RdYlGn') color = cmap(score / float(max_score)) color = f'rgba({int(color[0]*256)}, {int(color[1]*256)}, {int(color[2]*256)}, {int(color[3]*256)})' return color def pred_bar_chart(scores, binary_labels=None): bar_df = (pd.DataFrame(scores) .reset_index() .rename(columns={'index': 'Rating', 0: 'Score'})) if binary_labels: bar_df['Rating'].replace(binary_labels, inplace=True) bar = alt.Chart(bar_df).mark_bar().encode( x='Rating:O', y='Score', color=alt.Color('Rating', scale=alt.Scale(scheme='redyellowgreen'), legend=None) ).properties(height=225, title='Prediction Scores') bar.to_html() return bar