Chufolon/data_visualization
0
1import panel as pn2import geopandas as gpd3import altair as alt4 5alt.data_transformers.enable('json')6 7gdf_c = gpd.read_file("data/geo_reviews_per_month.json")8MONTHS=['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']9 10gdfp_c = gdf_c[['neighbourhood', 'geometry']]11df_c = gdf_c[['neighbourhood'] + MONTHS]12df_c = df_c.reset_index()13df_c = df_c.rename(columns={'index':'month'})14for i,m in enumerate(MONTHS):15 df_c = df_c.rename(columns={m:str(i+1)})16df_c = df_c.set_index("month")17 18# For hovering19tooltip = alt.Tooltip(['neighbourhood:N', 'reviews:Q'])20 21# Slider22slider = alt.binding_range(min=1, max=12, step=1)23select_year = alt.selection_single(24 name="month", 25 fields=['month'],26 bind=slider, 27 init={'month': 1},28)29columns = [str(v) for v in range(1,13)]30 31# Update the encoding to include the tooltip32chart = alt.Chart(gdfp_c).mark_geoshape(33 stroke='black',34 strokeWidth=1,35).transform_lookup(36 lookup='neighbourhood',37 from_=alt.LookupData(df_c, 'neighbourhood', columns)38).transform_fold(39 columns, as_=['month', 'reviews']40).transform_calculate(41 month='parseInt(datum.month)',42 reviews='isValid(datum.reviews) ? datum.reviews : -1' 43).encode(44 color = alt.condition(45 'datum.reviews > 0',46 alt.Color('reviews:Q', scale=alt.Scale(scheme='blues', domain=[100, 15000])),47 alt.value('#dbe9f6')48 ),49 tooltip=tooltip50).add_selection(51 select_year52).properties(53 width=700,54 height=40055).transform_filter(56 select_year57).configure_legend(58 gradientLength=400,59 gradientThickness=3060)61 62panel = pn.interact(chart) 63panel.servable(title="London")64 65# import panel as pn66# import hvplot.pandas67 68# # Load Data69# from bokeh.sampledata.autompg import autompg_clean as df70 71# # Make DataFrame Pipeline Interactive72# idf = df.interactive()73 74# # Define Panel widgets75# cylinders = pn.widgets.IntSlider(name='Cylinders', start=4, end=8, step=2)76# mfr = pn.widgets.ToggleGroup(77# name='MFR',78# options=['ford', 'chevrolet', 'honda', 'toyota', 'audi'], 79# value=['ford', 'chevrolet', 'honda', 'toyota', 'audi'],80# button_type='success')81# yaxis = pn.widgets.RadioButtonGroup(82# name='Y axis', 83# options=['hp', 'weight'],84# button_type='success'85# )86 87# # Combine pipeline and widgets88# ipipeline = (89# idf[90# (idf.cyl == cylinders) & 91# (idf.mfr.isin(mfr))92# ]93# .groupby(['origin', 'mpg'])[yaxis].mean()94# .to_frame()95# .reset_index()96# .sort_values(by='mpg') 97# .reset_index(drop=True)98# )99 100# # Pipe to hvplot101# ihvplot = ipipeline.hvplot(x='mpg', y=yaxis, by='origin', color=["#ff6f69", "#ffcc5c", "#88d8b0"], line_width=6, height=400)102 103# # Layout using Template104# template = pn.template.FastListTemplate(105# title='Interactive DataFrame Dashboards with hvplot .interactive', 106# sidebar=[cylinders, 'Manufacturers', mfr, 'Y axis' , yaxis],107# main=[ihvplot.panel()],108# accent_base_color="#88d8b0",109# header_background="#88d8b0",110# )111# template.servable()