def _plot_line(xs, ys_population, title, path=''): """ Plots min, max and mean + standard deviation bars of a population over time """ max_colour, mean_colour, std_colour, transparent = 'rgb(0, 132, 180)', 'rgb(0, 172, 237)', \ 'rgba(29, 202, 255, 0.2)', 'rgba(0, 0, 0, 0)' ys = torch.tensor(ys_population, dtype=torch.float32) ys_min, ys_max, ys_mean, ys_std = ys.min(1)[0].squeeze(), ys.max(1)[0].squeeze(), \ ys.mean(1).squeeze(), ys.std(1).squeeze() ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std trace_max = Scatter(x=xs, y=ys_max.numpy(), line=Line(color=max_colour, dash='dash'), name='Max') trace_upper = Scatter(x=xs, y=ys_upper.numpy(), line=Line(color=transparent), name='+1 Std. Dev.', showlegend=False) trace_mean = Scatter(x=xs, y=ys_mean.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=mean_colour), name='Mean') trace_lower = Scatter(x=xs, y=ys_lower.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=transparent), name='-1 Std. Dev.', showlegend=False) trace_min = Scatter(x=xs, y=ys_min.numpy(), line=Line(color=max_colour, dash='dash'), name='Min') plotly.offline.plot({ 'data': [trace_upper, trace_mean, trace_lower, trace_min, trace_max], 'layout': dict(title=title, xaxis={'title': 'Step'}, yaxis={'title': title}) }, filename=os.path.join(path, title + '.html'), auto_open=False)
def plot_line(xs, ys_population, save_dir): max_colour = 'rgb(0, 132, 180)' mean_colour = 'rgb(0, 172, 237)' std_colour = 'rgba(29, 202, 255, 0.2)' ys = np.array(ys_population) ys_min = np.min(ys, axis=1) ys_max = np.max(ys, axis=1) ys_mean = np.mean(ys, axis=1) ys_std = np.std(ys, axis=1) ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std # 최대 trace_max = Scatter(x=xs, y=ys_max, line=Line(color=max_colour, dash='dash'), name='Max') # 1-sigma trace_upper = Scatter(x=xs, y=ys_upper, line=Line(color='whitesmoke'), name='+1 Std. Dev.', showlegend=False) # fillcolor는 선과 선사이을 채우는 역할 trace_mean = Scatter(x=xs, y=ys_mean, fill='tonexty', fillcolor=std_colour, line=Line(color=mean_colour), name='Mean') # 1- sigam trace_lower = Scatter(x=xs, y=ys_lower, fill='tonexty', fillcolor=std_colour, line=Line(color='whitesmoke'), name='-1 Std. Dev.', showlegend=False) trace_min = Scatter(x=xs, y=ys_min, line=Line(color=max_colour, dash='dash'), name='Min') plotly.offline.plot( { 'data': [trace_upper, trace_mean, trace_lower, trace_min, trace_max], 'layout': dict(title='Rewards', xaxis={'title': 'Step'}, yaxis={'title': 'Average Reward'}) }, filename=os.path.join(save_dir, 'rewards.html'), auto_open=False)
def plot_line(xs, ys_population, save_dir): max_colour = 'rgb(0, 132, 180)' mean_colour = 'rgb(0, 172, 237)' std_colour = 'rgba(29, 202, 255, 0.2)' tmp_colour = 'rgb(255, 0, 0)' ys = torch.tensor(ys_population) ys_min = ys.min(1)[0].squeeze() ys_max = ys.max(1)[0].squeeze() ys_mean = ys.mean(1).squeeze() ys_std = ys.std(1).squeeze() ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std trace_max = Scatter(x=xs, y=ys_max.numpy(), line=Line(color=max_colour, dash='dash'), name='Max') trace_upper = Scatter(x=xs, y=ys_upper.numpy(), line=Line(color=tmp_colour), name='+1 Std. Dev.', showlegend=False) trace_mean = Scatter(x=xs, y=ys_mean.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=mean_colour), name='Mean') trace_lower = Scatter(x=xs, y=ys_lower.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=tmp_colour), name='-1 Std. Dev.', showlegend=False) trace_min = Scatter(x=xs, y=ys_min.numpy(), line=Line(color=max_colour, dash='dash'), name='Min') plt.plot(xs, ys_mean) plt.savefig(os.path.join(save_dir, "plot.png")) plotly.offline.plot( { 'data': [trace_upper, trace_mean, trace_lower, trace_min, trace_max], 'layout': dict(title='Rewards', xaxis={'title': 'Step'}, yaxis={'title': 'Average Reward'}) }, filename=os.path.join(save_dir, 'rewards.html'), auto_open=False)
def get_trace(file_path, normalizer, colours, shw_lgd=False, lgd_name=""): data = {} with open(file_path + '.json') as f: data = json.load(f) # get data to average with # with open(file_path.split('/')[0] + '/' + file_path.split('/')[1] + '/' + file_path.split('/')[2] + '_2/' + # file_path.split('/')[3].split('_data')[0] + '_2_data.json') as f: # data2 = json.load(f) # # # now normalize and get lines to plot: # data['Rewards'] = data['Rewards'] / normalizer # data2['Rewards'] = data2['Rewards'] / normalizer # # data['Rewards'] = np.divide(np.add(np.asarray(data['Rewards']), np.asarray(data2['Rewards'])), 2).tolist() t1_min, t1_lower, t1_mean, t1_upper, t1_max = get_trace_data( data['Rewards']) # get the actual "trace" for plotly trace1_min = Scatter(x=time_scale, y=t1_min, line=Line(color=colours[1], dash='dash'), name='Min') trace1_lower = Scatter(x=time_scale, y=t1_lower, line=Line(color=transparent), name='UpperStdDev', showlegend=False) trace1_mean = Scatter(x=time_scale, y=t1_mean, fill='tonexty', fillcolor=colours[1], line=Line(color=colours[0]), name=lgd_name, showlegend=shw_lgd, orientation="h") trace1_upper = Scatter(x=time_scale, y=t1_upper, fill='tonexty', fillcolor=colours[1], line=Line(color=transparent), name='LowerStdDev', showlegend=False) trace1_max = Scatter(x=time_scale, y=t1_max, line=Line(color=colours[1], dash='dash'), name='Max') return [ trace1_lower, trace1_mean, trace1_upper ] # [trace1_min, trace1_lower, trace1_mean, trace1_upper, trace1_max]
def _plot_line(xs, ys_population, title, name='', path=''): max_colour, mean_colour, std_colour, transparent = 'rgb(0, 132, 180)', 'rgb(0, 172, 237)', 'rgba(29, 202, 255, 0.2)', 'rgba(0, 0, 0, 0)' ys = torch.tensor(ys_population, dtype=torch.float32) ys_min, ys_max, ys_mean, ys_std = ys.min(1)[0].squeeze(), ys.max( 1)[0].squeeze(), ys.mean(1).squeeze(), ys.std(1).squeeze() ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std trace_max = Scatter(x=xs, y=ys_max.numpy(), line=Line(color=max_colour, dash='dash'), name='Max') trace_upper = Scatter(x=xs, y=ys_upper.numpy(), line=Line(color=transparent), name='+1 Std. Dev.', showlegend=False) trace_mean = Scatter(x=xs, y=ys_mean.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=mean_colour), name='Mean') trace_lower = Scatter(x=xs, y=ys_lower.numpy(), fill='tonexty', fillcolor=std_colour, line=Line(color=transparent), name='-1 Std. Dev.', showlegend=False) trace_min = Scatter(x=xs, y=ys_min.numpy(), line=Line(color=max_colour, dash='dash'), name='Min') plotly.offline.plot( { 'data': [trace_upper, trace_mean, trace_lower, trace_min, trace_max], 'layout': dict(title=title, xaxis={'title': 'Step'}, yaxis={'title': title}) }, filename=os.path.join(path, title + "_{}.html".format(name)), auto_open=False) if title == 'Reward': mean = ys_mean.numpy() if len(rewards) != 1: mean = mean[-1] with open('results/{}averages.txt'.format(name), 'a') as f: f.write('{}\n'.format(mean))
def multiple_lineplot(xs, ys, title, line_names=None, path='', xaxis='epoch'): """Allow multiple time series to be plotted. """ xs = np.array(xs, dtype=np.int) ys = np.array(ys, dtype=np.float32) assert xs.shape[0] == ys.shape[0], "{} != {}".format( xs.shape[0], ys.shape[0]) assert len(ys.shape) == 2, ys.shape line_number = ys.shape[1] collors = [ 'rgb({},0,{})'.format(max(255 - i * 50, 0), min(i * 50, 255)) for i in range(line_number) ] if line_names is not None: assert len( line_names ) == line_number, "number of line_names {} does not match with line number {}, {}".format( len(line_names), line_number, line_names) else: line_names = [str(i) for i in range(line_number)] data = [ Scatter(x=xs, y=v, line=Line(color=collors[i]), name=line_names[i]) for i, v in enumerate(ys.T) ] plotly.offline.plot( { 'data': data, 'layout': dict(title=title, xaxis={'title': xaxis}, yaxis={'title': title}) }, filename=os.path.join(path, title + '.html'), auto_open=False)
def plot_act_exp(self, data, title): ''' Plots the actuals vs the expected values Parameters ========== data: pandas dataframe Dataframe that contains the actual (y) and the predicted (y_pred) values with the standard deviation y_std ''' data.iplot(data=[ dict(y=data['y_pred'] - 1.96 * data['y_std'], x=data['Dates'], name='Predicted - 1.96 σ', line=Line(color='Gray', width=0), showlegend=False), dict(y=data['y_pred'] + 1.96 * data['y_std'], x=data['Dates'], name='Predicted + 1.96 σ', fill='tonexty', line=Line(color='Gray', width=0), showlegend=False), dict(y=data['y'], x=data['Dates'], name='Actual', mode='markers', marker=Marker(color='Black', size=3, opacity=1), color='Gray', showlegend=False), dict(y=data['y_pred'], x=data['Dates'], name='Predicted', line=Line(color='Blue', width=0.8), showlegend=False, opacity=0.5), ], title=title, dimensions=(700, 400), vspan=dict(x0=datetime.datetime.strptime( self.testing_cutoff, "%Y%m%d"), x1=data['Dates'].max(), color='Black', width=0, fill=False, opacity=0.1))
def get_trace(file_path, normalizer, colours): data = {} with open(file_path) as json_file: data = json.load(json_file) # now normalize and get lines to plot: data['Rewards'] = data['Rewards'] / normalizer t1_min, t1_lower, t1_mean, t1_upper, t1_max = get_trace_data( data['Rewards']) # get the actual "trace" for plotly trace1_min = Scatter(x=time_scale, y=t1_min, line=Line(color=colours[1], dash='dash'), name='Min') trace1_lower = Scatter(x=time_scale, y=t1_lower, line=Line(color=transparent), name='UpperStdDev', showlegend=False) trace1_mean = Scatter(x=time_scale, y=t1_mean, fill='tonexty', fillcolor=colours[1], line=Line(color=colours[0]), name='Mean', showlegend=False) trace1_upper = Scatter(x=time_scale, y=t1_upper, fill='tonexty', fillcolor=colours[1], line=Line(color=transparent), name='LowerStdDev', showlegend=False) trace1_max = Scatter(x=time_scale, y=t1_max, line=Line(color=colours[1], dash='dash'), name='Max') return [ trace1_lower, trace1_mean, trace1_upper ] # [trace1_min, trace1_lower, trace1_mean, trace1_upper, trace1_max]
def plot_lines(xs, y_lines, line_names, title, xaxis='Iteration', yaxis='Reward', yrange=None): line_colors = ['rgb(60, 180, 75)', 'rgb(145, 30, 180)', 'rgb(230, 25, 75)', 'rgb(0, 172, 237)', 'rgb(170, 110, 40)', 'rgb(240, 50, 230)', 'rgb(255, 255, 25)', 'rgb(128, 0, 0)', 'rgb(128, 128, 128)'] fill_colors = ['rgba(60, 180, 75, 0.2)', 'rgba(145, 30, 180, 0.2)', 'rgba(230, 25, 75, 0.2)', 'rgba(0, 172, 237, 0.2)', 'rgba(170, 110, 40, 0.2)', 'rgba(240, 50, 230, 0.2)', 'rgba(255, 255, 25, 0.2)', 'rgba(128, 0, 0, 0.2)', 'rgba(128, 128, 128, 0.2)'] blank = 'rgba(0, 0, 0, 0)' y_lines = np.array(y_lines, dtype=np.float32) data = [] for i in range(len(line_names)): y = y_lines[i] y_mean, y_std = y[:2] y_upper, y_lower = y_mean + y_std, y_mean - y_std line_color = line_colors[i] fill_color = fill_colors[i] upper_std = Scatter(x=xs, y=y_upper, line=Line(color=blank), showlegend=False) mean = Scatter(x=xs, y=y_mean, fill='tonexty', fillcolor=fill_color, line=Line(color=line_color), name=line_names[i]) lower_std = Scatter(x=xs, y=y_lower, fill='tonexty', fillcolor=fill_color, line=Line(color=blank), showlegend=False) data += [upper_std, mean, lower_std] fig = Figure() for trace in data: fig.add_trace(trace) fig.update_layout(legend=dict( yanchor="top", y=1.23, xanchor="right", # x=0.01 )) fig.update_layout( title=title, xaxis={'title': xaxis}, yaxis={'title': yaxis, 'range': yrange}, autosize=False, width=1000, height=600 ) fig.write_image(f'{title}_plot.png') return fig
def lineplot(xs, ys_population, title, path='', xaxis='epoch'): max_colour, mean_colour, std_colour, transparent = 'rgb(0, 132, 180)', 'rgb(0, 172, 237)', 'rgba(29, 202, 255, 0.2)', 'rgba(0, 0, 0, 0)' if isinstance(ys_population[0], list) or isinstance( ys_population[0], tuple): ys = np.asarray(ys_population, dtype=np.float32) ys_min, ys_max, ys_mean, ys_std, ys_median = ys.min(1), ys.max( 1), ys.mean(1), ys.std(1), np.median(ys, 1) ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std trace_max = Scatter(x=xs, y=ys_max, line=Line(color=max_colour, dash='dash'), name='Max') trace_upper = Scatter(x=xs, y=ys_upper, line=Line(color=transparent), name='+1 Std. Dev.', showlegend=False) trace_mean = Scatter(x=xs, y=ys_mean, fill='tonexty', fillcolor=std_colour, line=Line(color=mean_colour), name='Mean') trace_lower = Scatter(x=xs, y=ys_lower, fill='tonexty', fillcolor=std_colour, line=Line(color=transparent), name='-1 Std. Dev.', showlegend=False) trace_min = Scatter(x=xs, y=ys_min, line=Line(color=max_colour, dash='dash'), name='Min') trace_median = Scatter(x=xs, y=ys_median, line=Line(color=max_colour), name='Median') data = [ trace_upper, trace_mean, trace_lower, trace_min, trace_max, trace_median ] else: data = [Scatter(x=xs, y=ys_population, line=Line(color=mean_colour))] plotly.offline.plot( { 'data': data, 'layout': dict(title=title, xaxis={'title': xaxis}, yaxis={'title': title}) }, filename=os.path.join(path, title + '.html'), auto_open=False)
def get_trace_from_baseline_data(data, lgd_name, shw_lgd, colours): t1_min, t1_lower, t1_mean, t1_upper, t1_max = get_trace_data(data) # get the actual "trace" for plotly trace1_min = Scatter(x=time_scale, y=t1_min, line=Line(color=colours[1], dash='dash'), name='Min') trace1_lower = Scatter(x=time_scale, y=t1_lower, line=Line(color=transparent), name='UpperStdDev', showlegend=False) trace1_mean = Scatter(x=time_scale, y=t1_mean, fill='tonexty', fillcolor=colours[1], line=Line(color=colours[0]), name=lgd_name, showlegend=shw_lgd, orientation="h") trace1_upper = Scatter(x=time_scale, y=t1_upper, fill='tonexty', fillcolor=colours[1], line=Line(color=transparent), name='LowerStdDev', showlegend=False) trace1_max = Scatter(x=time_scale, y=t1_max, line=Line(color=colours[1], dash='dash'), name='Max') return [ trace1_lower, trace1_mean, trace1_upper ] # [trace1_min, trace1_lower, trace1_mean, trace1_upper, trace1_max]
def make_observation(obs, index): """ :param obs: DataFrame with columns 'value', 'error' and 'name' index: value for x-axis :returns: a line representing the error of the given observation. """ return Scatter( y=[obs['value'] + obs['error'], obs['value'] - obs['error']], x=[index, index], showlegend=False, hoverinfo='none', text='', mode='lines', line=Line(color='#000', width=1))
def init_confidence_band(y, mean, x, line, color): """ Makes a confidence band between mean and the corresponding confidence values (i.e. max, min, p90, p10). :param y: the confidence values. mean: mean value to be drawn backwards to fill area x: x-values both for mean and y values. line: id for belonging group color: color of line :returns: A trace representing a confidence band. """ return Scatter(y=(y + mean[::-1]), x=(x + x[::-1]), legendgroup=line, name=line, fill='toself', mode='lines', hoverinfo='none', showlegend=False, fillcolor=color, line=Line(width=0))
def __init__(self, data, observations=(), colors=DefaultFanChartColors(), alpha_values=default_alpha_values, *args, **kwargs): """Fan chart figure. :param data: Iterable of tuples in the following format: ( name, { 'p10': p10, 'p90': p90, 'mean': mean, 'max': max, 'min': min' } ) :param observations: Iterable of touples in the following format: (name, {'value': v, 'error': err}) """ colors = iter(colors) layout = Layout() traces = [] color_lookup = defaultdict(lambda: next(colors)) xs = defaultdict(list) lines = defaultdict(lambda: defaultdict(list)) for index, row in data: xs[row['name']].append(index) for column in ['p90', 'mean', 'p10', 'min', 'max']: lines[row['name']][column].append(row[column]) for name, columns in lines.items(): traces.append( Scatter( y=columns['mean'], x=xs[name], legendgroup=name, name=name, mode='lines', line=Line(color=rgba(*color_lookup[name][0:3], a=1.0)))) for column in ['p90', 'p10', 'min', 'max']: traces.append( init_confidence_band(y=columns[column], mean=columns['mean'], x=xs[name], line=name, color=rgba(*color_lookup[name][0:3], a=alpha_values[column]))) if observations is not None: for i, row in observations: traces.append(make_observation(row, i)) traces.append(make_marker( obs=row, index=i, )) super(FanChart, self).__init__(data=traces, layout=layout, *args, **kwargs)
def _plot_line(reward_buffer, title, path=""): Tab_T_actors = reward_buffer.Tab_T_actors Tab_T_learner = reward_buffer.Tab_T_learner Tab_mean_length_episode = reward_buffer.Tab_mean_length_episode Tab_longest_episode = reward_buffer.Tab_longest_episode tab_rewards_plot = reward_buffer.tab_rewards_plot max_colour, mean_colour, std_colour, transparent = ( "rgb(0, 132, 180)", "rgb(0, 172, 237)", "rgba(29, 202, 255, 0.2)", "rgba(0, 0, 0, 0)", ) ys = torch.tensor(tab_rewards_plot, dtype=torch.float32) ys_min, ys_max, ys_mean, ys_std = ( ys.min(1)[0].squeeze(), ys.max(1)[0].squeeze(), ys.mean(1).squeeze(), ys.std(1).squeeze(), ) ys_upper, ys_lower = ys_mean + ys_std, ys_mean - ys_std text_learner_step = [] assert len(Tab_T_learner) == len(Tab_mean_length_episode) == len( Tab_longest_episode) for indice_Tab in range(len(Tab_T_learner)): T_learner = Tab_T_learner[indice_Tab] length_episode = Tab_mean_length_episode[indice_Tab] length_longest_episode = Tab_longest_episode[indice_Tab][0] score_longest_episode = Tab_longest_episode[indice_Tab][1] text_learner_step.append( f"Longest episode last {length_longest_episode} steps and score was " f"{score_longest_episode}.\n " f"Mean length episode = {length_episode}.\n " f"Nb step learner : {T_learner:.2e}") trace_max = Scatter( x=Tab_T_actors, y=ys_max.numpy(), line=Line(color=max_colour, dash="dash"), name="Max", text=text_learner_step, ) trace_upper = Scatter( x=Tab_T_actors, y=ys_upper.numpy(), line=Line(color=transparent), name="+1 Std. Dev.", showlegend=False, ) trace_mean = Scatter( x=Tab_T_actors, y=ys_mean.numpy(), fill="tonexty", fillcolor=std_colour, line=Line(color=mean_colour), name="Mean", ) trace_lower = Scatter( x=Tab_T_actors, y=ys_lower.numpy(), fill="tonexty", fillcolor=std_colour, line=Line(color=transparent), name="-1 Std. Dev.", showlegend=False, ) trace_min = Scatter(x=Tab_T_actors, y=ys_min.numpy(), line=Line(color=max_colour, dash="dash"), name="Min") plotly.offline.plot( { "data": [trace_upper, trace_mean, trace_lower, trace_min, trace_max], "layout": dict(title=title, xaxis={"title": "Step"}, yaxis={"title": title}), }, filename=os.path.join(path, title + ".html"), auto_open=False, )
def create_trafo_trace(net, trafos=None, color='green', width=5, infofunc=None, cmap=None, trace_name='trafos', cmin=None, cmax=None, cmap_vals=None, use_line_geodata=None): """ Creates a plotly trace of pandapower trafos. INPUT: **net** (pandapowerNet) - The pandapower network OPTIONAL: **trafos** (list, None) - The trafos for which the collections are created. If None, all trafos in the network are considered. **width** (int, 5) - line width **infofunc** (pd.Series, None) - hoverinfo for trafo elements. Indices should correspond to the pandapower element indices **trace_name** (String, "lines") - name of the trace which will appear in the legend **color** (String, "green") - color of lines in the trace **cmap** (bool, False) - name of a colormap which exists within plotly (Greys, YlGnBu, Greens, YlOrRd, Bluered, RdBu, Reds, Blues, Picnic, Rainbow, Portland, Jet, Hot, Blackbody, Earth, Electric, Viridis) **cmap_vals** (list, None) - values used for coloring using colormap **cbar_title** (String, None) - title for the colorbar **cmin** (float, None) - colorbar range minimum **cmax** (float, None) - colorbar range maximum """ color = get_plotly_color(color) # defining lines to be plot trafos = net.trafo.index.tolist() if trafos is None else list(trafos) if len(trafos) == 0: return [] trafo_buses_with_geodata = net.trafo.hv_bus.isin(net.bus_geodata.index) & \ net.trafo.lv_bus.isin(net.bus_geodata.index) trafos_mask = net.trafo.index.isin(trafos) trafos_to_plot = net.trafo[trafo_buses_with_geodata & trafos_mask] if infofunc is not None: if not isinstance(infofunc, pd.Series) and isinstance(infofunc, Iterable) and \ len(infofunc) == len(trafos): infofunc = pd.Series(index=trafos, data=infofunc) assert isinstance(infofunc, pd.Series), \ "infofunc should be a pandas series with the net.trafo.index to the infofunc contents" infofunc = infofunc.loc[trafos_to_plot.index] cmap_colors = [] if cmap is not None: cmap = 'jet' if cmap is None else cmap cmin = 0 if cmin is None else cmin cmax = 100 if cmin is None else cmax if cmap_vals is not None: cmap_vals = cmap_vals else: if net.res_trafo.shape[0] == 0: logger.error( "There are no power flow results for lines which are default for line colormap coloring..." "set cmap_vals input argument if you want colormap according to some specific values..." ) cmap_vals = net.res_trafo.loc[trafos_to_plot.index, 'loading_percent'].values cmap_colors = get_plotly_cmap(cmap_vals, cmap_name=cmap, cmin=cmin, cmax=cmax) trafo_traces = [] for col_i, (idx, trafo) in enumerate(trafos_to_plot.iterrows()): if cmap is not None: color = cmap_colors[col_i] trafo_trace = dict(type='scatter', text=[], line=Line(width=width, color=color), hoverinfo='text', mode='lines', name=trace_name) trafo_trace[ 'text'] = trafo['name'] if infofunc is None else infofunc.loc[idx] from_bus = net.bus_geodata.loc[trafo.hv_bus, 'x'] to_bus = net.bus_geodata.loc[trafo.lv_bus, 'x'] trafo_trace['x'] = [from_bus, (from_bus + to_bus) / 2, to_bus] from_bus = net.bus_geodata.loc[trafo.hv_bus, 'y'] to_bus = net.bus_geodata.loc[trafo.lv_bus, 'y'] trafo_trace['y'] = [from_bus, (from_bus + to_bus) / 2, to_bus] trafo_traces.append(trafo_trace) center_trace = create_edge_center_trace(trafo_traces, color=color, infofunc=infofunc, use_line_geodata=use_line_geodata) trafo_traces.append(center_trace) return trafo_traces
def create_line_trace(net, lines=None, use_line_geodata=True, respect_switches=False, width=1.0, color='grey', infofunc=None, trace_name='lines', legendgroup=None, cmap=None, cbar_title=None, show_colorbar=True, cmap_vals=None, cmin=None, cmax=None, cpos=1.1): """ Creates a plotly trace of pandapower lines. INPUT: **net** (pandapowerNet) - The pandapower network OPTIONAL: **lines** (list, None) - The lines for which the collections are created. If None, all lines in the network are considered. **width** (int, 1) - line width **respect_switches** (bool, False) - flag for consideration of disconnected lines **infofunc** (pd.Series, None) - hoverinfo for line elements. Indices should correspond to the pandapower element indices **trace_name** (String, "lines") - name of the trace which will appear in the legend **color** (String, "grey") - color of lines in the trace **legendgroup** (String, None) - defines groups of layers that will be displayed in a legend e.g. groups according to voltage level (as used in `vlevel_plotly`) **cmap** (String, None) - name of a colormap which exists within plotly if set to True default `Jet` colormap is used, alternative colormaps : Greys, YlGnBu, Greens, YlOrRd, Bluered, RdBu, Reds, Blues, Picnic, Rainbow, Portland, Jet, Hot, Blackbody, Earth, Electric, Viridis **cmap_vals** (list, None) - values used for coloring using colormap **show_colorbar** (bool, False) - flag for showing or not corresponding colorbar **cbar_title** (String, None) - title for the colorbar **cmin** (float, None) - colorbar range minimum **cmax** (float, None) - colorbar range maximum **cpos** (float, 1.1) - position of the colorbar """ color = get_plotly_color(color) # defining lines to be plot lines = net.line.index.tolist() if lines is None else list(lines) if len(lines) == 0: return [] if infofunc is not None: if not isinstance(infofunc, pd.Series) and isinstance(infofunc, Iterable) and \ len(infofunc) == len(lines): infofunc = pd.Series(index=lines, data=infofunc) if len(infofunc) != len(lines) and len(infofunc) != len(net.line): raise UserWarning( "Different amount of hover info than lines to plot") assert isinstance(infofunc, pd.Series), \ "infofunc should be a pandas series with the net.line.index to the infofunc contents" no_go_lines = set() if respect_switches: no_go_lines = set(lines) & set(net.switch.element[ (net.switch.et == "l") & (net.switch.closed == 0)]) lines_to_plot = net.line.loc[set(net.line.index) & (set(lines) - no_go_lines)] no_go_lines_to_plot = None use_line_geodata = use_line_geodata if net.line_geodata.shape[ 0] > 0 else False if use_line_geodata: lines_to_plot = lines_to_plot.loc[set(lines_to_plot.index) & set(net.line_geodata.index)] else: lines_with_geodata = lines_to_plot.from_bus.isin(net.bus_geodata.index) & \ lines_to_plot.to_bus.isin(net.bus_geodata.index) lines_to_plot = lines_to_plot.loc[lines_with_geodata] cmap_lines = None if cmap is not None: # workaround: if colormap plot is used, each line need to be separate scatter object because # plotly still doesn't support appropriately colormap for line objects # TODO correct this when plotly solves existing github issue about Line colorbar cmap = 'jet' if cmap is True else cmap if cmap_vals is not None: if not isinstance(cmap_vals, np.ndarray): cmap_vals = np.asarray(cmap_vals) else: if net.res_line.shape[0] == 0: logger.error( "There are no power flow results for lines which are default for line colormap coloring..." "set cmap_vals input argument if you want colormap according to some specific values..." ) cmap_vals = net.res_line.loc[lines_to_plot.index, 'loading_percent'].values cmap_lines = get_plotly_cmap(cmap_vals, cmap_name=cmap, cmin=cmin, cmax=cmax) if len(cmap_lines) == len(net.line): # some lines are not plotted although cmap_value were provided for all lines line_idx_map = dict( zip(net.line.loc[lines].index.tolist(), range(len(lines)))) cmap_lines = [ cmap_lines[line_idx_map[idx]] for idx in lines_to_plot.index ] else: assert len(cmap_lines) == len(lines_to_plot), \ "Different amounts of cmap values and lines to plot were supplied" line_traces = [] for col_i, (idx, line) in enumerate(lines_to_plot.iterrows()): line_color = color line_info = line['name'] if cmap is not None: try: line_color = cmap_lines[col_i] line_info = line['name'] if infofunc is None else infofunc.loc[ idx] except IndexError: logger.warning( "No color and info for line {:d} (name: {}) available". format(idx, line['name'])) line_trace = dict(type='scatter', text=[], hoverinfo='text', mode='lines', name=trace_name, line=Line(width=width, color=color)) line_trace['x'], line_trace['y'] = _get_line_geodata_plotly( net, lines_to_plot.loc[idx:idx], use_line_geodata) line_trace['line']['color'] = line_color line_trace['text'] = line_info line_traces.append(line_trace) if show_colorbar and cmap is not None: cmin = cmap_vals.min() if cmin is None else cmin cmax = cmap_vals.max() if cmax is None else cmax try: # TODO for custom colormaps cbar_cmap_name = 'Jet' if cmap == 'jet' else cmap # workaround to get colorbar for lines (an unvisible node is added) # get x and y of first line.from_bus: x = [net.bus_geodata.x[net.line.from_bus[net.line.index[0]]]] y = [net.bus_geodata.y[net.line.from_bus[net.line.index[0]]]] lines_cbar = dict(type='scatter', x=x, y=y, mode='markers', marker=Marker( size=0, cmin=cmin, cmax=cmax, color='rgb(255,255,255)', opacity=0, colorscale=cbar_cmap_name, colorbar=ColorBar(thickness=10, x=cpos), )) if cbar_title: lines_cbar['marker']['colorbar']['title'] = cbar_title lines_cbar['marker']['colorbar']['title']['side'] = 'right' line_traces.append(lines_cbar) except: pass if len(no_go_lines) > 0: no_go_lines_to_plot = net.line.loc[no_go_lines] for idx, line in no_go_lines_to_plot.iterrows(): line_color = color line_trace = dict(type='scatter', text=[], hoverinfo='text', mode='lines', name='disconnected lines', line=Line(width=width / 2, color='grey', dash='dot')) line_trace['x'], line_trace['y'] = _get_line_geodata_plotly( net, no_go_lines_to_plot.loc[idx:idx], use_line_geodata) line_trace['line']['color'] = line_color try: line_trace['text'] = infofunc.loc[idx] except (KeyError, IndexError, AttributeError): line_trace["text"] = line['name'] line_traces.append(line_trace) if legendgroup: line_trace['legendgroup'] = legendgroup # sort infofunc so that it is the correct order lines_to_plot + no_go_lines_to_plot if infofunc is not None: if not isinstance(infofunc, pd.Series) and isinstance(infofunc, Iterable) and \ len(infofunc) == len(net.line): infofunc = pd.Series(index=net.line.index, data=infofunc) assert isinstance(infofunc, pd.Series), \ "infofunc should be a pandas series with the net.line.index to the infofunc contents" sorted_idx = lines_to_plot.index.tolist() if no_go_lines_to_plot is not None: sorted_idx += no_go_lines_to_plot.index.tolist() infofunc = infofunc.loc[sorted_idx] center_trace = create_edge_center_trace(line_traces, color=color, infofunc=infofunc, use_line_geodata=use_line_geodata) line_traces.append(center_trace) return line_traces
def _create_branch_trace(net, branches=None, use_branch_geodata=True, respect_separators=False, width=1.0, color='grey', infofunc=None, trace_name='lines', legendgroup=None, cmap=None, cbar_title=None, show_colorbar=True, cmap_vals=None, cmin=None, cmax=None, cpos=1.1, branch_element='line', separator_element='switch', node_element='bus', cmap_vals_category='loading_percent'): """ Creates a plotly trace of branch elements. The rather generic, non-power net specific names were introduced to make it usable in other packages, e.g. for pipe networks. INPUT: **net** (pandapowerNet) - The network OPTIONAL: **branches** (list, None) - The branches for which the collections are created. If None, all branches in the network are considered. **use_branch_geodata** (bool, True) - whether the geodata of the branch tables should be used **respect_separators** (bool, True) - whether separating elements like switches should be considered **width** (int, 1) - branch width **color** (String, "grey") - color of lines in the trace **infofunc** (pd.Series, None) - hoverinfo for line elements. Indices should correspond to the pandapower element indices **trace_name** (String, "lines") - name of the trace which will appear in the legend **legendgroup** (String, None) - defines groups of layers that will be displayed in a legend e.g. groups according to voltage level (as used in `vlevel_plotly`) **cmap** (String, None) - name of a colormap which exists within plotly if set to True default `Jet` colormap is used, alternative colormaps : Greys, YlGnBu, Greens, YlOrRd, Bluered, RdBu, Reds, Blues, Picnic, Rainbow, Portland, Jet, Hot, Blackbody, Earth, Electric, Viridis **cmap_vals** (list, None) - values used for coloring using colormap **show_colorbar** (bool, False) - flag for showing or not corresponding colorbar **cbar_title** (String, None) - title for the colorbar **cmin** (float, None) - colorbar range minimum **cmax** (float, None) - colorbar range maximum **cpos** (float, 1.1) - position of the colorbar **branch_element** (str, "line") - name of the branch element in the net. In a pandapower net, this is alwas "line" **separator_element** (str, "switch") - name of the separator element in the net. In a pandapower net, this is alwas "switch" **node_element** (str, "bus") - name of the node element in the net. In a pandapower net, this is alwas "bus" (net.bus) """ color = get_plotly_color(color) # defining branches (lines) to be plot branches = net[branch_element].index.tolist( ) if branches is None else list(branches) if len(branches) == 0: return [] if infofunc is not None: if not isinstance(infofunc, pd.Series) and isinstance(infofunc, Iterable) and \ len(infofunc) == len(branches): infofunc = pd.Series(index=branches, data=infofunc) if len(infofunc) != len(branches) and len(infofunc) != len( net[branch_element]): raise UserWarning("Different amount of hover info than {}s to " "plot".format(branch_element)) assert isinstance(infofunc, pd.Series), \ "infofunc should be a pandas series with the net.{}.index to the infofunc " \ "contents".format(branch_element) no_go_branches = set() if respect_separators: if separator_element == "switch": no_go_branches = set(branches) & \ set(net[separator_element].element[(net[separator_element].et == "l") & (net[separator_element].closed == 0)]) elif separator_element == "valve": no_go_branches = set(branches) & \ set(net[separator_element][(~net[separator_element].in_service) | (net[separator_element].opened)]) else: raise NotImplementedError( "respect separtors is only implements for switches, " "not for {}s.".format(separator_element)) branches_to_plot = net[branch_element].loc[set(net[branch_element].index) & (set(branches) - no_go_branches)] no_go_branches_to_plot = None branch_geodata = branch_element + "_geodata" node_geodata = node_element + "_geodata" use_branch_geodata = use_branch_geodata if net[branch_geodata].shape[ 0] > 0 else False if use_branch_geodata: branches_to_plot = branches_to_plot.loc[ set(branches_to_plot.index) & set(net[branch_geodata].index)] else: branches_with_geodata = branches_to_plot['from_'+node_element].isin( net[node_geodata].index) & \ branches_to_plot['to_'+node_element].isin(net[node_geodata].index) branches_to_plot = branches_to_plot.loc[branches_with_geodata] cmap_branches = None if cmap is not None: # workaround: if colormap plot is used, each line need to be separate scatter object because # plotly still doesn't support appropriately colormap for line objects # TODO correct this when plotly solves existing github issue about Line colorbar cmap = 'jet' if cmap is True else cmap if cmap_vals is not None: if not isinstance(cmap_vals, np.ndarray): cmap_vals = np.asarray(cmap_vals) else: if net['res_' + branch_element].shape[0] == 0: logger.error( "There are no simulation results for branches which are default for {}" "colormap coloring..." "set cmap_vals input argument if you want colormap according to some specific " "values...".format(branch_element)) cmap_vals = net['res_' + branch_element].loc[branches_to_plot.index, cmap_vals_category].values cmap_branches = get_plotly_cmap(cmap_vals, cmap_name=cmap, cmin=cmin, cmax=cmax) if len(cmap_branches) == len(net[branch_element]): # some branches are not plotted although cmap_value were provided for all branches branch_idx_map = dict( zip(net[branch_element].loc[branches].index.tolist(), range(len(branches)))) cmap_branches = [ cmap_branches[branch_idx_map[idx]] for idx in branches_to_plot.index ] else: assert len(cmap_branches) == len(branches_to_plot), \ "Different amounts of cmap values and branches to plot were supplied" branch_traces = [] for col_i, (idx, branch) in enumerate(branches_to_plot.iterrows()): line_color = color line_info = branch['name'] if cmap is not None: try: line_color = cmap_branches[col_i] line_info = branch[ 'name'] if infofunc is None else infofunc.loc[idx] except IndexError: logger.warning( "No color and info for {} {:d} (name: {}) available". format(branch_element, idx, branch['name'])) line_trace = dict(type='scatter', text=[], hoverinfo='text', mode='lines', name=trace_name, line=Line(width=width, color=color)) line_trace['x'], line_trace['y'] = _get_branch_geodata_plotly( net, branches_to_plot.loc[idx:idx], use_branch_geodata, branch_element, node_element) line_trace['line']['color'] = line_color line_trace['text'] = line_info branch_traces.append(line_trace) if show_colorbar and cmap is not None: cmin = cmap_vals.min() if cmin is None else cmin cmax = cmap_vals.max() if cmax is None else cmax try: # TODO for custom colormaps cbar_cmap_name = 'Jet' if cmap == 'jet' else cmap # workaround to get colorbar for branches (an unvisible node is added) # get x and y of first line.from_bus: x = [ net[node_geodata].x[net[branch_element]["from_" + node_element] [net[branch_element].index[0]]] ] y = [ net[node_geodata].y[net[branch_element]["from_" + node_element] [net[branch_element].index[0]]] ] branches_cbar = dict(type='scatter', x=x, y=y, mode='markers', marker=Marker( size=0, cmin=cmin, cmax=cmax, color='rgb(255,255,255)', opacity=0, colorscale=cbar_cmap_name, colorbar=ColorBar(thickness=10, x=cpos), )) if cbar_title: branches_cbar['marker']['colorbar']['title'] = cbar_title branches_cbar['marker']['colorbar']['title']['side'] = 'right' branch_traces.append(branches_cbar) except: pass if len(no_go_branches) > 0: no_go_branches_to_plot = net[branch_element].loc[no_go_branches] for idx, branch in no_go_branches_to_plot.iterrows(): line_color = color line_trace = dict(type='scatter', text=[], hoverinfo='text', mode='lines', name='disconnected branches', line=Line(width=width / 2, color='grey', dash='dot')) line_trace['x'], line_trace['y'] = _get_branch_geodata_plotly( net, no_go_branches_to_plot.loc[idx:idx], use_branch_geodata, branch_element, node_element) line_trace['line']['color'] = line_color try: line_trace['text'] = infofunc.loc[idx] except (KeyError, IndexError, AttributeError): line_trace["text"] = branch['name'] branch_traces.append(line_trace) if legendgroup: line_trace['legendgroup'] = legendgroup # sort infofunc so that it is the correct order lines_to_plot + no_go_lines_to_plot if infofunc is not None: if not isinstance(infofunc, pd.Series) and isinstance(infofunc, Iterable) and \ len(infofunc) == len(net[branch_element]): infofunc = pd.Series(index=net[branch_element].index, data=infofunc) assert isinstance(infofunc, pd.Series), \ "infofunc should be a pandas series with the net.{}.index to the infofunc contents" \ .format(branch_element) sorted_idx = branches_to_plot.index.tolist() if no_go_branches_to_plot is not None: sorted_idx += no_go_branches_to_plot.index.tolist() infofunc = infofunc.loc[sorted_idx] center_trace = create_edge_center_trace( branch_traces, color=color, infofunc=infofunc, use_line_geodata=use_branch_geodata) branch_traces.append(center_trace) return branch_traces
def show_map(M, start=None, goal=None, path=None): G = M._graph pos = nx.get_node_attributes(G, 'pos') edge_trace = go.Scatter( x=[], y=[], line=Line(width=0.5, color='#888'), hoverinfo='none', mode='lines') for edge in G.edges(): x0, y0 = G.nodes[edge[0]]['pos'] x1, y1 = G.nodes[edge[1]]['pos'] edge_trace['x'] += tuple([x0, x1, None]) edge_trace['y'] += tuple([y0, y1, None]) node_trace = go.Scatter( x=[], y=[], text=[], mode='markers', hoverinfo='text', marker=go.scatter.Marker( showscale=False, # colorscale options # 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' | # Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu' colorscale='Hot', reversescale=True, color=[], size=10, colorbar=dict( thickness=15, title='Node Connections', xanchor='left', titleside='right' ), line=dict(width=2))) for node in G.nodes(): x, y = G.nodes[node]['pos'] node_trace['x'] += tuple([x]) node_trace['y'] += tuple([y]) for node, adjacencies in enumerate(G.adjacency()): color = 0 if path and node in path: color = 2 if node == start: color = 3 elif node == goal: color = 1 # node_trace['marker']['color'].append(len(adjacencies)) node_trace['marker']['color'] += tuple([color]) node_info = "Intersection " + str(node) node_trace['text'] += tuple([node_info]) fig = go.Figure(data=[edge_trace, node_trace], layout=go.Layout( title='<br>Network graph made with Python', titlefont=dict(size=16), showlegend=False, hovermode='closest', margin=dict(b=20, l=5, r=5, t=40), xaxis=go.layout.XAxis( showgrid=False, zeroline=False, showticklabels=False), yaxis=go.layout.YAxis(showgrid=False, zeroline=False, showticklabels=False))) iplot(fig)