Hello All,
I’m attempting to set up a simple select menu for a scattergraph. The idea is to filter the data frame and replot based on the market. I’m relatively new to coding and have spent 30+ hours on this to no avail. Please advise =) and thank you in advance!
For the interim, I’m only concerned with pushing to the notebook… I’ll work on the server once I’ve completed all the functionality.
I’ve attached the data.csv
DEPENDENCIES
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import datetime
import matplotlib.colors as colors
% matplotlib inline
from matplotlib.ticker import FuncFormatter
from bokeh.plotting import figure, show, output_notebook, output_file
from bokeh.io import output_file, curdoc, export_png
from bokeh.models import HoverTool, Legend, DataTable, TableColumn, PointDrawTool, ColumnDataSource,\
NumeralTickFormatter, Column, Select, CustomJS, Tabs, Panel, CDSView, NumberFormatter, TextInput
from bokeh.resources import CDN
from bokeh.embed import file_html
from bokeh.application import Application
from bokeh.application.handlers.function import FunctionHandler
CLEAN AND SORT DATA
data = pd.read_csv(‘data.csv’)
data[‘fill’] = (data[’# of Floors (1)’]==1).replace({True:‘bottom’,False:‘full’})
data[‘Marker Size’] =""
data[‘Marker Size’] = data.loc[:,‘Sales Rate (Last 6 Months)’]*125+250
markerscale = data[‘Marker Size’].values
markets = data[‘City’].unique()
df = data.rename(columns={‘Project Name’:‘Neighborhood’,‘Builder Name’:‘Builder’,‘City’:‘Market’,‘Uni Size Min’:‘Min SF’,‘Unit Size Max’:‘Max SF’,‘Typical Lot Size’:‘Lot Size’,‘Total Units Planned’:‘Total Units’,‘Total Units Sold’:‘Units Sold’,‘Total Remaining’:‘Unsold Homes’,‘MinPrice’:‘Min Price’,‘MaxPrice’:‘Max Price’,‘HOA1’:‘HOA’,‘Assessments’:‘Tax Rate’,‘Plan Name (1)’:‘Plan Name’,’# of Beds (1)’:‘Beds’,’# of Baths (1)’:‘Baths’,’# of Floors (1)’:‘Floors’,‘Garage (1)’:‘Garages’,‘Square Footage (1)’:‘SF’,‘Price (1)’:‘Price’,‘color’:‘blue’})
df.columns
CREATE THE BOOLEAN MASK FOR THE MARKET
x = ‘Lake Elsinore’
masked_data = df[df[‘Market’]==x].sort_values(by=‘Market’).dropna(subset=[‘SF’]).dropna(subset=[‘Price’])
neighborhoods = masked_data[‘Neighborhood’].sort_values(ascending = True)
font = {‘family’: ‘serif’,‘blue’: ‘black’,‘weight’: ‘normal’,‘size’: 16,}
p = figure(x_axis_label =‘Home Size (SF)’,y_axis_label=‘Home Price’,plot_width=10, plot_height=6, title="")
for y in neighborhoods:
plot_data = masked_data[masked_data[‘Neighborhood’]==y].sort_values([‘SF’])
source2 = ColumnDataSource(data=plot_data)
p.line(‘SF’,‘Price’, color=‘blue’,alpha=.2,line_color=‘black’,source=source2)
p.circle(‘SF’,‘Price’, color=‘blue’,size = 15,line_color=‘black’, alpha=.65,source=masked_data,legend=None,name=‘DataPoint’)
c1 = p.circle(‘SF’,‘Price’, color=‘blue’,size = 15,line_color=‘black’, alpha=.65,source=masked_data,legend=None,name=‘DataPoint’)
single_source = masked_data[masked_data[‘Floors’]==1]
p.inverted_triangle(‘SF’,‘Price’, color=‘blue’,size = 12,line_color=‘black’, alpha=.65,source=single_source)
CREATE FILTER SELECT
menu = Select(options=list(markets),value=‘Lake Elsinore’, title=“Market”)
def selectcallback(attr,old,new):
print(‘Any sign of the callback being tripped…’)
menu.value == x
masked_data = df[df[‘Market’]==x].sort_values(by=‘Market’)
updated_source_data = ColumnDataSource(masked_data)
p.circle(‘SF’,‘Price’, color=‘blue’,size = 15,line_color=‘black’, alpha=.65,source=updated_source_data)
menu.on_change(‘value’,selectcallback)
p.plot_height=600
p.plot_width=800
output_notebook()
SHOW
show(Column(menu,p))
data.csv (367 KB)