def network_expansion(network, method = 'rel', ext_min=0.1,
                      ext_width=False, filename=None, boundaries=[]):
    """Plot relative or absolute network extension of AC- and DC-lines.
    
    Parameters
    ----------
    network: PyPSA network container
        Holds topology of grid including results from powerflow analysis
    method: str
        Choose 'rel' for extension relative to s_nom and 'abs' for 
        absolute extensions. 
    ext_min: float
        Choose minimum relative line extension shown in plot in p.u..
    ext_width: float or bool
        Choose if line_width respects line extension. Turn off with 'False' or
        set linear factor to decremise extension line_width.
    filename: str or None
        Save figure in this direction
    boundaries: array
       Set boundaries of heatmap axis
    
    """

    cmap = plt.cm.jet

    overlay_network = network.copy()
    overlay_network.lines = overlay_network.lines[
                            overlay_network.lines.s_nom_extendable & ((
                                    overlay_network.lines.s_nom_opt -
                                  overlay_network.lines.s_nom_min) /
                                overlay_network.lines.s_nom >= ext_min)]
    overlay_network.links = overlay_network.links[
                            overlay_network.links.p_nom_extendable & ((
                                    overlay_network.links.p_nom_opt -
                                  overlay_network.links.p_nom_min)/
                                 overlay_network.links.p_nom >= ext_min)]

    for i, row in overlay_network.links.iterrows():
        linked = overlay_network.links[(row['bus1'] ==
                overlay_network.links.bus0) & (
                        row['bus0'] == overlay_network.links.bus1)]
        if not linked.empty:
            if row['p_nom_opt'] < linked.p_nom_opt.values[0]:
                overlay_network.links.p_nom_opt[i] = linked.p_nom_opt.values[0]

    array_line = [['Line'] * len(overlay_network.lines),
                  overlay_network.lines.index]
    
    array_link = [['Link'] * len(overlay_network.links),
                  overlay_network.links.index]
    
    if method == 'rel':

        extension_lines = pd.Series((100 *
                                 (overlay_network.lines.s_nom_opt -
                                  overlay_network.lines.s_nom_min) /
                                overlay_network.lines.s_nom).data,
                                index=array_line)

        extension_links = pd.Series((100 *
                                 (overlay_network.links.p_nom_opt -
                                  overlay_network.links.p_nom_min)/
                                 (overlay_network.links.p_nom)).data,
                                index=array_link)
    if method == 'abs':
        extension_lines = pd.Series(
                                 (overlay_network.lines.s_nom_opt -
                                  overlay_network.lines.s_nom_min).data,
                                index=array_line)

        extension_links = pd.Series(
                                 (overlay_network.links.p_nom_opt -
                                  overlay_network.links.p_nom_min).data,
                                index=array_link)
        

    extension = extension_lines.append(extension_links)

    # Plot whole network in backgroud of plot
    network.plot(
            line_colors=pd.Series("grey", index = [['Line'] * len(
                    network.lines), network.lines.index]).append(
            pd.Series("grey", index = [['Link'] * len(network.links),
                  network.links.index])),
            bus_sizes=0,
            line_widths=pd.Series(0.5, index = [['Line'] * len(network.lines),
                  network.lines.index]).append(
            pd.Series(0.55, index = [['Link'] * len(network.links),
                  network.links.index])))

    if not ext_width:
        line_widths= pd.Series(0.8, index = array_line).append(
                pd.Series(0.8, index = array_link))
        
    else: 
        line_widths= 0.5 + (extension / ext_width)
        
    ll = overlay_network.plot(
        line_colors=extension,
        line_cmap=cmap,
        bus_sizes=0,
        title="Optimized AC- and DC-line expansion",
        line_widths=line_widths) 

    if not boundaries:
        v = np.linspace(min(extension), max(extension), 101)
        boundaries = [min(extension), max(extension)]
        
    else:
        v = np.linspace(boundaries[0], boundaries[1], 101)
        
    if not extension_links.empty:
        cb_Link = plt.colorbar(ll[2], boundaries=v,
                      ticks=v[0:101:10])
        cb_Link.set_clim(vmin=boundaries[0], vmax=boundaries[1])
        
        cb_Link.remove()
        
    cb = plt.colorbar(ll[1], boundaries=v,
                      ticks=v[0:101:10], fraction=0.046, pad=0.04)
    
    cb.set_clim(vmin=boundaries[0], vmax=boundaries[1])
    
    if method == 'rel':
        cb.set_label('line expansion relative to s_nom in %')
    if method == 'abs':
        cb.set_label('line expansion in MW')
    if filename is None:
        plt.show()
    else:
        plt.savefig(filename)
        plt.close()
def network_expansion ( network , method = 'rel' , ext_min = 0.1 , ext_width = False , filename = None , boundaries = [ ] ) : cmap = plt . cm . jet overlay_network = network . copy ( ) overlay_network . lines = overlay_network . lines [ overlay_network . lines . s_nom_extendable & ( ( overlay_network . lines . s_nom_opt - overlay_network . lines . s_nom_min ) / overlay_network . lines . s_nom >= ext_min ) ] overlay_network . links = overlay_network . links [ overlay_network . links . p_nom_extendable & ( ( overlay_network . links . p_nom_opt - overlay_network . links . p_nom_min ) / overlay_network . links . p_nom >= ext_min ) ] for i , row in overlay_network . links . iterrows ( ) : linked = overlay_network . links [ ( row [ 'bus1' ] == overlay_network . links . bus0 ) & ( row [ 'bus0' ] == overlay_network . links . bus1 ) ] if not linked . empty : if row [ 'p_nom_opt' ] < linked . p_nom_opt . values [ 0 ] : overlay_network . links . p_nom_opt [ i ] = linked . p_nom_opt . values [ 0 ] array_line = [ [ 'Line' ] * len ( overlay_network . lines ) , overlay_network . lines . index ] array_link = [ [ 'Link' ] * len ( overlay_network . links ) , overlay_network . links . index ] if method == 'rel' : extension_lines = pd . Series ( ( 100 * ( overlay_network . lines . s_nom_opt - overlay_network . lines . s_nom_min ) / overlay_network . lines . s_nom ) . data , index = array_line ) extension_links = pd . Series ( ( 100 * ( overlay_network . links . p_nom_opt - overlay_network . links . p_nom_min ) / ( overlay_network . links . p_nom ) ) . data , index = array_link ) if method == 'abs' : extension_lines = pd . Series ( ( overlay_network . lines . s_nom_opt - overlay_network . lines . s_nom_min ) . data , index = array_line ) extension_links = pd . Series ( ( overlay_network . links . p_nom_opt - overlay_network . links . p_nom_min ) . data , index = array_link ) extension = extension_lines . append ( extension_links ) network . plot ( line_colors = pd . Series ( "grey" , index = [ [ 'Line' ] * len ( network . lines ) , network . lines . index ] ) . append ( pd . Series ( "grey" , index = [ [ 'Link' ] * len ( network . links ) , network . links . index ] ) ) , bus_sizes = 0 , line_widths = pd . Series ( 0.5 , index = [ [ 'Line' ] * len ( network . lines ) , network . lines . index ] ) . append ( pd . Series ( 0.55 , index = [ [ 'Link' ] * len ( network . links ) , network . links . index ] ) ) ) if not ext_width : line_widths = pd . Series ( 0.8 , index = array_line ) . append ( pd . Series ( 0.8 , index = array_link ) ) else : line_widths = 0.5 + ( extension / ext_width ) ll = overlay_network . plot ( line_colors = extension , line_cmap = cmap , bus_sizes = 0 , title = "Optimized AC- and DC-line expansion" , line_widths = line_widths ) if not boundaries : v = np . linspace ( min ( extension ) , max ( extension ) , 101 ) boundaries = [ min ( extension ) , max ( extension ) ] else : v = np . linspace ( boundaries [ 0 ] , boundaries [ 1 ] , 101 ) if not extension_links . empty : cb_Link = plt . colorbar ( ll [ 2 ] , boundaries = v , ticks = v [ 0 : 101 : 10 ] ) cb_Link . set_clim ( vmin = boundaries [ 0 ] , vmax = boundaries [ 1 ] ) cb_Link . remove ( ) cb = plt . colorbar ( ll [ 1 ] , boundaries = v , ticks = v [ 0 : 101 : 10 ] , fraction = 0.046 , pad = 0.04 ) cb . set_clim ( vmin = boundaries [ 0 ] , vmax = boundaries [ 1 ] ) if method == 'rel' : cb . set_label ( 'line expansion relative to s_nom in %' ) if method == 'abs' : cb . set_label ( 'line expansion in MW' ) if filename is None : plt . show ( ) else : plt . savefig ( filename ) plt . close ( )
Plot relative or absolute network extension of AC- and DC-lines.
    
    Parameters
    ----------
    network: PyPSA network container
        Holds topology of grid including results from powerflow analysis
    method: str
        Choose 'rel' for extension relative to s_nom and 'abs' for 
        absolute extensions. 
    ext_min: float
        Choose minimum relative line extension shown in plot in p.u..
    ext_width: float or bool
        Choose if line_width respects line extension. Turn off with 'False' or
        set linear factor to decremise extension line_width.
    filename: str or None
        Save figure in this direction
    boundaries: array
       Set boundaries of heatmap axis