def plot( metrics, x="step", y="mean_episode_return", palette="Set1", kdims=["lr"], kdims_facet=["lstm"], subsample=1000, cols=3, ): hmap = {} model2color = {} colors = hv.Palette(palette).values colors = list(set(colors)) for _facet, models in metrics.items(): for model, _ in models.items(): if model not in model2color: model2color[model] = colors[len(model2color) - 1] for facet, models in metrics.items(): p = plot_facet( models, x, y, palette, model2color, kdims=kdims_facet, subsample=subsample ) if facet not in hmap: hmap[facet] = {} hmap[facet] = p p = hv.HoloMap(hmap, kdims=kdims) p = hv.NdLayout(p).cols(cols) return p
def hv_plot_param(df_tidy, species='H', param='N'): hv_fig = hv.Curve( df_tidy, kdims=['time', species], vdims=[param], ).groupby(param).overlay().opts(frame_height=250, frame_width=250 * 3 // 2) hv_fig.opts(opts.Curve(color=hv.Palette('Viridis'), width=600)) # Take out the Bokeh object p = hv.render(hv_fig) return p
def get_ccaa(complete_df): # preprocessing df = complete_df[['Comunidad Autónoma', 'Fecha', 'Casos']] df = df.sort_values(by='Casos', ascending=False) df.reset_index(drop=True, inplace=True) # plot # for better date representation df.Fecha = df.loc[:, 'Fecha'].dt.strftime('%d-%m') color_cycle = hv.Palette('Inferno') opts_dict = dict(color=color_cycle, width=width, height=450, xrotation=90, invert_xaxis=True, shared_axes=False, tools=['hover'], stacked=True, show_legend=False, colorbar=True, line_color='white', show_grid=True, show_title=False) bars = hv.Bars(df, kdims=['Fecha', 'Comunidad Autónoma'], vdims='Casos', label='Escala lineal') bars.opts(**opts_dict) log_bars = hv.Bars(df, kdims=['Fecha', 'Comunidad Autónoma'], vdims='Casos', label='Escala logarítmica') opts_dict['logy'] = True opts_dict['title'] = 'Escala logarítmica' log_bars.opts(**opts_dict) layout = (bars * log_bars) layout.opts(tabs=True, title='Casos por comunidad') return layout
def get_split(complete_df): # preprocessing df = complete_df.copy() df['Leves'] = abs(df['Casos'] - (df['Hospitalizados'] + df['UCI'])) df = df[[ 'Comunidad Autónoma', 'Fecha', 'Leves', 'Hospitalizados', 'UCI', 'Fallecidos' ]] df = pd.melt(df, id_vars=df.columns[:2], var_name='Estado', value_name='Número') # plot key_dimensions = ['Fecha', 'Estado'] value_dimensions = ['Número'] # for better date representation df.Fecha = df.loc[:, 'Fecha'].dt.strftime('%d-%m') macro = hv.Table(df, key_dimensions, value_dimensions) bars = macro.to.bars(key_dimensions, value_dimensions, 'Comunidad Autónoma') # color_cycle = hv.Cycle(['#FEE5AD', '#F7A541', '#F45D4C', '#2E2633']) color_cycle = hv.Palette('Inferno', reverse=True) bars.opts( opts.Bars(color=color_cycle, show_legend=True, stacked=True, width=900, height=450, shared_axes=False, ylabel='', xlabel='Fecha', responsive=True, tools=['hover'], title='Desglose por estado', legend_position='top_left', xrotation=90, active_tools=['pan', 'wheel_zoom']), ) return bars
def plot_facet( models, x="step", y="mean_episode_return", palette="Set1", model2color=None, **plot_model_args, ): if model2color is None: colors = hv.Palette(palette).values colors = list(set(colors)) model2color = {} hmap = {} for model, runs in models.items(): if model not in model2color: model2color[model] = colors[(len(model2color) - 1) % len(colors)] color = model2color[model] hmap[model] = plot_model(runs, x, y, model, color, **plot_model_args) p = hv.HoloMap(hmap, kdims=get_kdims(next(iter(models)))) p = p.overlay() return p
def plot_facet( models, x="step", y="mean_episode_return", palette="Set1", model2color=None, kdims=["lstm"], subsample=1000, ): if model2color is None: colors = hv.Palette(palette).values colors = list(set(colors)) model2color = {} hmap = {} for model, runs in models.items(): if model not in model2color: model2color[model] = colors[len(model2color) - 1] color = model2color[model] hmap[model] = plot_model(runs, x, y, model, color, subsample) p = hv.HoloMap(hmap, kdims=kdims) p = p.overlay() return p
def plot( metrics, x="step", y="mean_episode_return", palette="Set1", subsample=1000, cols=3, y_min=None, y_max=None, ): hmap = {} model2color = {} colors = hv.Palette(palette).values colors = list(set(colors)) for _facet, models in metrics.items(): for model, _ in models.items(): if model not in model2color: model2color[model] = colors[(len(model2color) - 1) % len(colors)] for facet, models in metrics.items(): p = plot_facet( models, x, y, palette, model2color, subsample=subsample, y_min=y_min, y_max=y_max, ) if facet not in hmap: hmap[facet] = {} hmap[facet] = p kdims = get_kdims(next(iter(metrics))) p = hv.HoloMap(hmap, kdims=kdims) p = hv.NdLayout(p).cols(cols) return p
def plot_scatter(df, x, y, x_round_val=1, y_round_val=1, x_tooltip='', y_tooltip=''): ''' Returns a HoloViews plot layout. Arguments: df - Dataframe to process, must have the column 'Country' adn the columns x and y within. x - Column in Dataframe where values are evaluated for the x-axis y - Column in Dataframe where values are evaluated for the y-axis x_round_val (optional) - single numeric value to set the x axis limits on max found within x y_round_val (optional) - single numeric value to set the y axis limits on max found within y x_tooltip (optional) - tooltip to be set in the plots for the x values y_tooltip (optional) - tooltip to be set in the plots for the y values ''' max_y = np.ceil(np.nanmax(df[y].values)) max_y = max_y - max_y % y_round_val + 2 * y_round_val max_x = np.ceil(np.nanmax(df[x].values)) max_x = max_x - max_x % x_round_val + 2 * x_round_val ''' if max_x > max_y: max_y = max_x else: max_x=max_y ''' if x_tooltip == '': x_tooltip = '@{' + x + '}{0,0.0}' if y_tooltip == '': y_tooltip = '@{' + y + '}{0,0.0}' # Plot settings and parameters hover = HoverTool(tooltips=[('Country', '@Country'), (x, x_tooltip), (y, y_tooltip)]) padding = dict(x=(-1.2, 1.2), y=(-1.2, 1.2)) options_shared = dict(width=700, height=700, xlim=(0, max_x), ylim=(0, max_y), hooks=[axis_not_scientific], active_tools=['wheel_zoom'], padding=(0.1, 0.1), show_grid=True, show_legend=True, legend_position='bottom', legend_cols=3) options = [ opts.Scatter(marker='o', size=10, fill_alpha=0.6, tools=[hover], color=hv.Palette('Set2'), **options_shared), opts.Points(color='Country', cmap=cc.cm.fire, size=8, tools=[hover], **options_shared), opts.Labels(text_font_size='8pt', yoffset=y_round_val / 5), opts.Overlay(**options_shared) ] ds = hv.Table(df) # Create the plot layout = ( #hv.Scatter(df, kdims=[x], vdims=[y, 'Country'])* ds.to(hv.Scatter, x, y, 'Country').overlay() * hv.Labels(ds, kdims=[x, y], vdims=['Country'])).opts(options) return layout