def draw(): data_sources = ['2010YumaAZ.csv', '2010SantaBarbaraCA.csv', '2010SitkaAK.csv'] data_source = np.random.choice(data_sources) csv_path = find_raw_csv_path(data_source) df = pd.read_csv(csv_path) colorscales = [ 'Greys', 'YlGnBu', 'Greens', 'YlOrRd', 'Bluered', 'RdBu', 'Reds', 'Blues', 'Picnic', 'Rainbow', 'Portland', 'Jet', 'Hot', 'Blackbody', 'Earth', 'Electric', 'Viridis', 'Cividis' ] colorscale = np.random.choice(colorscales) data = [go.Heatmap( x=df['DAY'], y=df['LST_TIME'], z=df['T_HR_AVG'], colorscale=colorscale )] layout = go.Layout( title=f"{' '.join((data_source.rsplit('.', 1)[0][:4], data_source.rsplit('.', 1)[0][4:-2], data_source.rsplit('.', 1)[0][-2:]))} Temperature ({colorscale})", yaxis={'title': 'TIME'}, xaxis={'title': 'WEEKDAY'}, hovermode="x" ) fig = go.Figure(data=data, layout=layout) return fig
def draw(): csv_path = find_raw_csv_path('iris.csv') df = pd.read_csv(csv_path) dim = 'petal_length' classes = df['class'].unique() hist_data = [df[df['class'] == c][dim] for c in classes] group_labels = classes bins = [1 for c in classes] fig = ff.create_distplot( hist_data, group_labels=group_labels, bin_size=bins, curve_type='kde', # 'kde' or 'normal' colors=None, rug_text=None, histnorm='probability density', # 'probability density' or 'probability' show_hist=True, show_curve=True, show_rug=True) fig.layout.title = dim.title() return fig
def draw(): csv_path = find_raw_csv_path('abalone.csv') df = pd.read_csv(csv_path) samples = { 'A': np.random.choice(df['rings'], 30, replace=False), 'B': np.random.choice(df['rings'], 20, replace=False) } boxpoints = request_arg('boxpoints', 'all', str, lambda x: x in ('all', 'outliers')) jitter = request_arg('jitter', 0.5, float, lambda x: 0 <= x <= 1) pointpos = request_arg('pointpos', -2, float, lambda x: -2 <= x <= 2) data = [ go.Box(y=samples[sample], name=sample, boxpoints=boxpoints, jitter=jitter, pointpos=pointpos) for sample in samples ] layout = go.Layout(title="Sample of abalone rings values", yaxis={'title': 'Rings'}, hovermode="x") fig = go.Figure(data=data, layout=layout) return fig
def draw(): csv_path = find_raw_csv_path('abalone.csv') # sex,length,diameter,height,whole_weight,shucked_weight,viscera_weight,shell_weight,rings df = pd.read_csv(csv_path) dim = 'length' x_series = df[dim] x_min, x_max = x_series.min(), x_series.max() size = request_arg('size', 0.02, int, lambda x: x in range(int(x_max + 1))) data = [ go.Histogram( x=x_series, xbins=dict(start=min(0, x_min), end=max(1, x_max), size=size), ) ] layout = go.Layout(title="abalone Histogram", yaxis={'title': dim}, xaxis={'title': 'bins'}, hovermode="x") fig = go.Figure(data=data, layout=layout) return fig
def draw(): csv_path = find_raw_csv_path('mpg.csv') df = pd.read_csv(csv_path) # df = df[df.horsepower == '?'] # Bad Data Points Exists in Data Set max_bubble_size = 25 min_value = df['weight'].min() max_value = df['weight'].max() bubble_size = ((df['weight'] - min_value) / (max_value - min_value + 1)) * max_bubble_size data = [ go.Scatter(x=df['horsepower'], y=df['mpg'], text=df['name'], mode="markers", marker=dict(size=bubble_size, color=df['cylinders'], colorscale='Jet', showscale=True)) ] layout = go.Layout(title="MPG Bubble Chart", xaxis={'title': 'horsepower'}, yaxis={'title': 'mpg'}, hovermode="closest") fig = go.Figure(data=data, layout=layout) return fig
def draw(): # create a DataFrame from the .csv file: csv_path = find_raw_csv_path('mpg.csv') df = pd.read_csv(csv_path) max_bubble_size = 30 bubble_size_dimension = 'horsepower' df[bubble_size_dimension] = pd.to_numeric( df[bubble_size_dimension], errors='coerce') # Convert to numbers min_value = df[bubble_size_dimension].min() max_value = df[bubble_size_dimension].max() df[bubble_size_dimension] = df[bubble_size_dimension].fillna( min_value ) # Fill All NaN with min value (Not Zero, otherwise the min size will be too small) bubble_size = ((df[bubble_size_dimension] - min_value) / (max_value - min_value + 1)) * max_bubble_size x_dimension = 'displacement' y_dimension = 'acceleration' color_dimension = 'model_year' text_dimension = 'name' # create traces using a list comprehension: data = [ go.Scatter(x=df[x_dimension], y=df[y_dimension], text=df[text_dimension].str.title(), name="mpg data", mode="markers", marker=dict(size=bubble_size, color=df[color_dimension], showscale=True)) ] # create a layout, remember to set the barmode here layout = go.Layout( title=f"MPG {x_dimension.title()} & {y_dimension.title()}", xaxis={'title': x_dimension}, yaxis={'title': y_dimension}, hovermode="closest") # create a fig from data & layout, and plot the fig. fig = go.Figure(data=data, layout=layout) return fig
def draw(): csv_path = find_raw_csv_path('2018WinterOlympics.csv') df = pd.read_csv(csv_path) trace1 = go.Bar(x=df['NOC'], y=df['Gold'], name='Gold', marker={'color': '#FFFF00'}) trace2 = go.Bar(x=df['NOC'], y=df['Silver'], name='Silver', marker={'color': '#A0A0A0'}) trace3 = go.Bar(x=df['NOC'], y=df['Bronze'], name='Bronze', marker={'color': '#FF8000'}) data = [trace1, trace2, trace3] barmode = request_arg('barmode', 'group', str, lambda x: x in ('group', 'stack', 'relative')) layout = go.Layout(title="Bar Chart 2018 Winter Olympics", yaxis={'title': 'Number of Medals'}, barmode=barmode) fig = go.Figure(data=data, layout=layout) return fig
def draw(): fpath = find_raw_csv_path('nst-est2017-alldata.csv') df = pd.read_csv(fpath) df2 = df.loc[df['DIVISION'] == '1', :] df2.set_index('NAME', inplace=True) df2 = df2.loc[:, [col for col in df2.columns if col.startswith('POP')]] data = [ go.Scatter(x=[col[-4:] for col in df2.columns], y=df2.loc[name, :], mode="lines+markers", name=name) for name in df2.index ] layout = go.Layout(title="Line Chart Example using Pandas") fig = go.Figure(data=data, layout=layout) return fig
def draw(): # Create a pandas DataFrame from 2010YumaAZ.csv csv_path = find_raw_csv_path('2010YumaAZ.csv') df = pd.read_csv(csv_path) # Use a for loop (or list comprehension to create traces for the data list) data = [ go.Scatter(x=df.loc[df['DAY'] == weekday, 'LST_TIME'], y=df.loc[df['DAY'] == weekday, 'T_HR_AVG'], mode="lines+markers", name=weekday) for weekday in df['DAY'].unique() ] # Define the layout layout = go.Layout( title="Daily temperature from June 1-7, 2010 in Yuma, Arizona") # Create a fig from data and layout, and plot the fig fig = go.Figure(data=data, layout=layout) return fig
def draw(): # create a DataFrame from the .csv file: csv_path = find_raw_csv_path('mocksurvey.csv') df = pd.read_csv(csv_path, index_col=0) # df.set_index(df.columns[0], inplace=True) # create traces using a list comprehension: series = request_arg('series', 'q', str, lambda x: x in ('q', 'r')) ori = request_arg('orientation', 'v', str, lambda x: x in ('v', 'h')) if series == 'q': data = [ go.Bar(x=df.columns if ori == 'v' else df.loc[idx, :], y=df.loc[idx, :] if ori == 'v' else df.columns, name=idx, orientation=ori) for idx in df.index ] else: data = [ go.Bar(x=df.index if ori == 'v' else df.loc[:, col], y=df.loc[:, col] if ori == 'v' else df.index, name=col, orientation=ori) for col in df.columns ] # create a layout, remember to set the barmode here barmode = request_arg('barmode', 'group', str, lambda x: x in ('group', 'stack', 'relative')) layout = go.Layout( title="Mocksurvey Result", yaxis={'title': 'Response Score'} if ori == 'v' else None, xaxis={'title': 'Response Score'} if ori == 'h' else None, barmode=barmode) # create a fig from data & layout, and plot the fig. fig = go.Figure(data=data, layout=layout) return fig
def draw(): csv_path = find_raw_csv_path('flights.csv') df = pd.read_csv(csv_path) z_series = df['passengers'] round_by = 5 data = [ go.Heatmap(y=df['month'], x=df['year'], z=z_series, name='passengers', colorscale='Portland', zmin=(int(z_series.min() / round_by) + 1) * round_by, zmax=(int(z_series.max() / round_by) + 1) * round_by) ] layout = go.Layout(title="Passengers by flight Year & Month", yaxis={'title': 'month'}, xaxis={'title': 'year'}, hovermode="x") fig = go.Figure(data=data, layout=layout) return fig
def draw(): csv_path = find_raw_csv_path('mpg.csv') df = pd.read_csv(csv_path) dim = 'mpg' x_series = df[dim] x_min, x_max = x_series.min(), x_series.max() size = request_arg('size', 2, int, lambda x: x in range(int(x_max + 1))) data = [ go.Histogram( x=x_series, xbins=dict(start=min(0, x_min), end=x_max, size=size), ) ] layout = go.Layout(title="MPG Histogram", yaxis={'title': dim}, xaxis={'title': 'bins'}, hovermode="x") fig = go.Figure(data=data, layout=layout) return fig
def draw(): data_sources = [ '2010YumaAZ.csv', '2010SantaBarbaraCA.csv', '2010SitkaAK.csv' ] rows = 3 cols = 1 plot_height = 500 vertical_spacing = 0.04 fig = tools.make_subplots( rows=rows, cols=cols, start_cell='top-left', # 'top-left', 'bottom-left' shared_yaxes=False, shared_xaxes=False, # specs=[[{}, {}, {}]], # vertical_spacing=vertical_spacing, # horizontal_spacing=0.001, subplot_titles=[ ' '.join((name.rsplit('.', 1)[0][:4], name.rsplit('.', 1)[0][4:-2], name.rsplit('.', 1)[0][-2:])) for name in data_sources ]) dfs = [] min_z = 0 max_z = 0 for index, data_source in enumerate(data_sources): df = pd.read_csv(find_raw_csv_path(data_source)) dfs.append(df) min_z = df['T_HR_AVG'].min() if index == 0 else min( min_z, df['T_HR_AVG'].min()) max_z = df['T_HR_AVG'].max() if index == 0 else max( max_z, df['T_HR_AVG'].max()) round_by = 5 min_z = (int(min_z / round_by) + 0) * round_by max_z = (int(max_z / round_by) + 1) * round_by colorscale = np.random.choice([ 'Greys', 'YlGnBu', 'Greens', 'YlOrRd', 'Bluered', 'RdBu', 'Reds', 'Blues', 'Picnic', 'Rainbow', 'Portland', 'Jet', 'Hot', 'Blackbody', 'Earth', 'Electric', 'Viridis', 'Cividis' ]) data = [ go.Heatmap( x=df['DAY'].map(lambda x: x[0:3]), y=df['LST_TIME'], z=df['T_HR_AVG'], colorscale=colorscale, name=data_sources[idx].rsplit('.', 1)[0][4:-2], zmin=min_z, zmax=max_z, ) for idx, df in enumerate(dfs) ] for index, trace in enumerate(data): fig.append_trace(trace, ((index + 1) // (cols + 1)) + 1, cols - ((index + 1) % cols)) fig.layout.update( # height=rows * plot_height + (rows + 1) * vertical_spacing, title=f"Temperature ({colorscale})", ) return fig