def make_amount_plot(infile): purchases = infra.pd.read_parquet(infile) purchases = purchases.assign(count=1) purchases = purchases[["amount_bytes", "count" ]].groupby(["amount_bytes"]).sum().reset_index() purchases["amount_mb"] = purchases["amount_bytes"] / 1000**2 alt.Chart(purchases).mark_point().encode( x=alt.X( "amount_mb", title="Session Purchase Amount (MB) (Log Scale)", scale=alt.Scale(type="log", domain=(10, 2000)), ), y=alt.Y( "count", title="Occurrences (Count) (Log Scale)", scale=alt.Scale(type="log", ), ), color=alt.condition( 'datum.count>1000', alt.ColorValue('red'), alt.ColorValue('steelblue'), ), shape=alt.condition( 'datum.count>1000', alt.ShapeValue('diamond'), alt.ShapeValue('triangle'), ), ).properties( width=500, height=300, ).save("renders/purchase_timing_per_user_clumped_amounts.png", scale_factor=2.0) print(purchases)
def available_parking(): data = available_parking_data() person = ( "m 0 0 l 14 -15 a 5 5 0 0 1 9 8 l -14 15 l 9 26 l -4 5 l -12 -22 l -12 12 l 1 5 l -2 3 l -12 -11 l 3 -2 l 5 0 l 9 -14 l -19 -11 l 4 -3 l 22 4" ) bars = alt.Chart(data).transform_calculate( row="ceil(datum.id/5)").transform_calculate( col="datum.id - datum.row*5").mark_point( filled=True, size=30, ).encode( x=alt.X("col:O", axis=None), y=alt.Y("row:O", axis=None), shape=alt.ShapeValue(person), color=alt.condition( alt.datum.occupied == 1, alt.value('orange'), alt.value('steelblue'), ), ).properties(width=700, height=700, title="Available Parking") text = bars.mark_text( align='center', baseline='middle', fontSize=25, dx=-3, dy=-16 # Nudges text to right so it doesn't appear on top of the bar ).encode(text='id:Q') return (bars + text).configure_title( fontSize=25, font='Calibri', #This can be editted anchor='start', color='gray').configure_view(strokeWidth=0)
def labeling_buttons(title): colors = ('#33cc33', '#ace600', '#e6e600', '#ff9900', '#ff3300') data = pd.DataFrame([{'id': i, 'color': c} for i, c in enumerate(colors)]) brush = alt.selection_single(nearest=True, empty='none', fields=['id']) return alt.Chart(data).mark_point(filled=True, size=100).encode( x=alt.X("id:O", axis=None), shape=alt.ShapeValue(PERSON), color=alt.condition( alt.datum.id <= brush.id, alt.Color('color:N', scale=None), alt.value('skyblue'))).properties( width=400, height=100, title=title).configure_view( strokeWidth=0).add_selection(brush).configure_title( fontSize=16)
def predictresults(): h1 = House.query.order_by(House.id.desc()).first() # Creating feature array new_instance = np.array([[h1.livearea, h1.bdrms, h1.baths, h1.one_story, h1.att_garage, h1.basement]]) # Predicting new_instance target prediction = rf.predict(new_instance) # Principal Components lle_new_instance = lle_pipe.transform(new_instance) lle_new_data = pd.DataFrame(lle_new_instance, columns=['pca1', 'pca2']) # Returning text depending on the prediction pred = "not" if prediction[0] == 0 else "" # Creating a sweet altair chart lle_chart = alt.Chart(lle_data).mark_circle(opacity=.4).encode( alt.X("pca1", title="principal component 1"), alt.Y("pca2", title="principal component 2"), alt.Color("target:N", title='Built before 1980', scale=alt.Scale(range=["gray", "orange"])) ) lle_chart = lle_chart + alt.Chart(lle_new_data).mark_point( opacity=1, color="#CA03FF", filled=True, size=150).encode( alt.X("pca1"), alt.Y("pca2"), alt.ShapeValue('cross')).properties(**props).interactive() # Saving chart to file -- probably will be building this inside the app though... lle_chart.save("b41980/static/lle_spec.json") # Creating dictionary to pass into the html posts = { "livearea": h1.livearea, "bdrms": h1.bdrms, "baths": h1.baths, "one_story": h1.one_story, "att_garage": h1.att_garage, "basement": h1.basement, "prediction": pred } return render_template("predictresults.html", title="Prediction Results", posts=posts)
data = pd.DataFrame([dict(id=i) for i in range(1, 101)]) person = ("M1.7 -1.7h-0.8c0.3 -0.2 0.6 -0.5 0.6 -0.9c0 -0.6 " "-0.4 -1 -1 -1c-0.6 0 -1 0.4 -1 1c0 0.4 0.2 0.7 0.6 " "0.9h-0.8c-0.4 0 -0.7 0.3 -0.7 0.6v1.9c0 0.3 0.3 0.6 " "0.6 0.6h0.2c0 0 0 0.1 0 0.1v1.9c0 0.3 0.2 0.6 0.3 " "0.6h1.3c0.2 0 0.3 -0.3 0.3 -0.6v-1.8c0 0 0 -0.1 0 " "-0.1h0.2c0.3 0 0.6 -0.3 0.6 -0.6v-2c0.2 -0.3 -0.1 " "-0.6 -0.4 -0.6z") alt.Chart(data).transform_calculate( row="ceil(datum.id/10)").transform_calculate( col="datum.id - datum.row*10").mark_point(filled=True, size=50).encode( x=alt.X("col:O", axis=None), y=alt.Y("row:O", axis=None), shape=alt.ShapeValue(person)).properties( width=400, height=400).configure_view(strokeWidth=0) ## import altair as alt from vega_datasets import data source = data.stocks() alt.Chart(source).mark_line(point=True).encode(x='date:T', y='price:Q', color='symbol:N') ## import altair as alt from vega_datasets import data
def _plot_isotype_array(baseline_ef, exposed_ef, population_size=100, title="", configure_chart=True, icon_shape=PERSON_SHAPE, icon_size=75, stroke_color="black", stroke_width=1.3, cross_shape=CROSS_SHAPE, cross_width=None, chart_width=350, chart_height=400): if isinstance(baseline_ef, float) or isinstance(exposed_ef, float): warnings.warn( "Can't currently plot (color) fractional icons. Rounding to nearest integer." ) baseline_ef = round(baseline_ef) exposed_ef = round(exposed_ef) data = __generate_chart_source_data(baseline_ef, exposed_ef, population_size) root = round(math.sqrt( population_size)) # Create a square grid of total `population_size` # https://altair-viz.github.io/gallery/isotype_grid.html base_chart = alt.Chart(data).transform_calculate( row=f"ceil(datum.id/{root})", col=f"datum.id - datum.row*{root}", ).encode( x=alt.X("col:O", axis=None), y=alt.Y("row:O", axis=None), ).properties(width=chart_width, height=chart_height, title=title if title else "") icons = base_chart.mark_point( filled=True, stroke=stroke_color, strokeWidth=stroke_width, # 2, size=icon_size, ).encode( color=alt.Color( 'hue:N', scale=alt.Scale( domain=[ 0, 1, 2 ], # Explicitly specify `hue` values or coloring will fail if <3 levels exist in data range=[ "#FFFFFF", # Population (0) "#4A5568", # Baseline (1) "#FA5765", # Exposed (2) "#4078EF" ]), # TODO: add uncertainty using shade: lighter color fill of icons in the 95% CI. legend=None), shape=alt.ShapeValue(icon_shape), ) chart = icons if exposed_ef < baseline_ef: stroke_out = base_chart.mark_point( # shape="cross", filled=True, stroke="#4078EF", # "black" # strokeWidth=cross_width, strokeWidth=math.sqrt(icon_size) / 1.7 if cross_width is None else cross_width, strokeCap="round", size=icon_size, ).encode(shape=alt.ShapeValue(cross_shape), opacity=alt.Opacity( 'reduced:N', legend=None, scale=alt.Scale(domain=[False, True], range=[0, 1]), )) chart += stroke_out if configure_chart: # Configured charts cannot be later concatenated. chart = chart.configure_title( align="left", anchor="start", offset=-10, ).configure_view(strokeWidth=0, ) return chart