def iplot(network, fig=None, bus_colors='blue',
          bus_colorscale=None, bus_colorbar=None, bus_sizes=10, bus_text=None,
          line_colors='green', line_widths=2, line_text=None, title="",
          branch_components=['Line', 'Link'], iplot=True, jitter=None):
    """
    Plot the network buses and lines interactively using plotly.

    Parameters
    ----------
    fig : dict, default None
        If not None, figure is built upon this fig.
    bus_colors : dict/pandas.Series
        Colors for the buses, defaults to "b"
    bus_colorscale : string
        Name of colorscale if bus_colors are floats, e.g. 'Jet', 'Viridis'
    bus_colorbar : dict
        Plotly colorbar, e.g. {'title' : 'my colorbar'}
    bus_sizes : dict/pandas.Series
        Sizes of bus points, defaults to 10
    bus_text : dict/pandas.Series
        Text for each bus, defaults to bus names
    line_colors : dict/pandas.Series
        Colors for the lines, defaults to "g" for Lines and "cyan" for
        Links. Colors for branches other than Lines can be
        specified using a pandas Series with a MultiIndex.
    line_widths : dict/pandas.Series
        Widths of lines, defaults to 2. Widths for branches other
        than Lines can be specified using a pandas Series with a
        MultiIndex.
    line_text : dict/pandas.Series
        Text for lines, defaults to line names. Text for branches other
        than Lines can be specified using a pandas Series with a
        MultiIndex.
    title : string
        Graph title
    branch_components : list of str
        Branch components to be plotted, defaults to Line and Link.
    iplot : bool, default True
        Automatically do an interactive plot of the figure.
    jitter : None|float
        Amount of random noise to add to bus positions to distinguish
        overlapping buses

    Returns
    -------
    fig: dictionary for plotly figure
    """

    defaults_for_branches = {
        'Link': dict(color="cyan", width=2),
        'Line': dict(color="blue", width=2),
        'Transformer': dict(color='green', width=2)
    }

    if fig is None:
        fig = dict(data=[],layout={})

    if bus_text is None:
        bus_text = 'Bus ' + network.buses.index

    x = network.buses.x
    y = network.buses.y

    if jitter is not None:
        x = x + np.random.uniform(low=-jitter, high=jitter, size=len(x))
        y = y + np.random.uniform(low=-jitter, high=jitter, size=len(y))

    bus_trace = dict(x=x, y=y,
                     text=bus_text,
                     type="scatter",
                     mode="markers",
                     hoverinfo="text",
                     marker=dict(color=bus_colors,
                                 size=bus_sizes),
                     )

    if bus_colorscale is not None:
        bus_trace['marker']['colorscale'] = bus_colorscale

    if bus_colorbar is not None:
        bus_trace['marker']['colorbar'] = bus_colorbar


    def as_branch_series(ser):
        if isinstance(ser, dict) and set(ser).issubset(branch_components):
            return pd.Series(ser)
        elif isinstance(ser, pd.Series):
            if isinstance(ser.index, pd.MultiIndex):
                return ser
            index = ser.index
            ser = ser.values
        else:
            index = network.lines.index
        return pd.Series(ser,
                         index=pd.MultiIndex(levels=(["Line"], index),
                                             labels=(np.zeros(len(index)),
                                                     np.arange(len(index)))))

    line_colors = as_branch_series(line_colors)
    line_widths = as_branch_series(line_widths)

    if line_text is not None:
        line_text = as_branch_series(line_text)

    shapes = []

    shape_traces = []

    for c in network.iterate_components(branch_components):
        l_defaults = defaults_for_branches[c.name]
        l_widths = line_widths.get(c.name, l_defaults['width'])
        l_colors = line_colors.get(c.name, l_defaults['color'])
        l_nums = None

        if line_text is None:
            l_text = c.name + ' ' + c.df.index
        else:
            l_text = line_text.get(c.name)

        if isinstance(l_colors, pd.Series):
            if issubclass(l_colors.dtype.type, np.number):
                l_nums = l_colors
                l_colors = None
            else:
                l_colors.fillna(l_defaults['color'], inplace=True)

        x0 = c.df.bus0.map(x)
        x1 = c.df.bus1.map(x)

        y0 = c.df.bus0.map(y)
        y1 = c.df.bus1.map(y)

        for line in c.df.index:
            color = l_colors if isinstance(l_colors, string_types) else l_colors[line]
            width = l_widths if isinstance(l_widths, (int, float)) else l_widths[line]

            shapes.append(dict(type='line',
                               x0=x0[line],
                               y0=y0[line],
                               x1=x1[line],
                               y1=y1[line],
                               opacity=0.7,
                               line=dict(color=color, width=width)))

        shape_traces.append(dict(x=0.5*(x0+x1),
                                 y=0.5*(y0+y1),
                                 text=l_text,
                                 type="scatter",
                                 mode="markers",
                                 hoverinfo="text",
                                 marker=dict(opacity=0.)))

    fig['data'].extend([bus_trace]+shape_traces)

    fig['layout'].update(dict(shapes=shapes,
                              title=title,
                              hovermode='closest',
                              showlegend=False))
                              #xaxis=dict(range=[6,14]),
                              #yaxis=dict(range=[47,55])


    if iplot:
        if not pltly_present:
            logger.warning("Plotly is not present, so interactive plotting won't work.")
        else:
            pltly.iplot(fig)

    return fig
def iplot ( network , fig = None , bus_colors = 'blue' , bus_colorscale = None , bus_colorbar = None , bus_sizes = 10 , bus_text = None , line_colors = 'green' , line_widths = 2 , line_text = None , title = "" , branch_components = [ 'Line' , 'Link' ] , iplot = True , jitter = None ) : defaults_for_branches = { 'Link' : dict ( color = "cyan" , width = 2 ) , 'Line' : dict ( color = "blue" , width = 2 ) , 'Transformer' : dict ( color = 'green' , width = 2 ) } if fig is None : fig = dict ( data = [ ] , layout = { } ) if bus_text is None : bus_text = 'Bus ' + network . buses . index x = network . buses . x y = network . buses . y if jitter is not None : x = x + np . random . uniform ( low = - jitter , high = jitter , size = len ( x ) ) y = y + np . random . uniform ( low = - jitter , high = jitter , size = len ( y ) ) bus_trace = dict ( x = x , y = y , text = bus_text , type = "scatter" , mode = "markers" , hoverinfo = "text" , marker = dict ( color = bus_colors , size = bus_sizes ) , ) if bus_colorscale is not None : bus_trace [ 'marker' ] [ 'colorscale' ] = bus_colorscale if bus_colorbar is not None : bus_trace [ 'marker' ] [ 'colorbar' ] = bus_colorbar def as_branch_series ( ser ) : if isinstance ( ser , dict ) and set ( ser ) . issubset ( branch_components ) : return pd . Series ( ser ) elif isinstance ( ser , pd . Series ) : if isinstance ( ser . index , pd . MultiIndex ) : return ser index = ser . index ser = ser . values else : index = network . lines . index return pd . Series ( ser , index = pd . MultiIndex ( levels = ( [ "Line" ] , index ) , labels = ( np . zeros ( len ( index ) ) , np . arange ( len ( index ) ) ) ) ) line_colors = as_branch_series ( line_colors ) line_widths = as_branch_series ( line_widths ) if line_text is not None : line_text = as_branch_series ( line_text ) shapes = [ ] shape_traces = [ ] for c in network . iterate_components ( branch_components ) : l_defaults = defaults_for_branches [ c . name ] l_widths = line_widths . get ( c . name , l_defaults [ 'width' ] ) l_colors = line_colors . get ( c . name , l_defaults [ 'color' ] ) l_nums = None if line_text is None : l_text = c . name + ' ' + c . df . index else : l_text = line_text . get ( c . name ) if isinstance ( l_colors , pd . Series ) : if issubclass ( l_colors . dtype . type , np . number ) : l_nums = l_colors l_colors = None else : l_colors . fillna ( l_defaults [ 'color' ] , inplace = True ) x0 = c . df . bus0 . map ( x ) x1 = c . df . bus1 . map ( x ) y0 = c . df . bus0 . map ( y ) y1 = c . df . bus1 . map ( y ) for line in c . df . index : color = l_colors if isinstance ( l_colors , string_types ) else l_colors [ line ] width = l_widths if isinstance ( l_widths , ( int , float ) ) else l_widths [ line ] shapes . append ( dict ( type = 'line' , x0 = x0 [ line ] , y0 = y0 [ line ] , x1 = x1 [ line ] , y1 = y1 [ line ] , opacity = 0.7 , line = dict ( color = color , width = width ) ) ) shape_traces . append ( dict ( x = 0.5 * ( x0 + x1 ) , y = 0.5 * ( y0 + y1 ) , text = l_text , type = "scatter" , mode = "markers" , hoverinfo = "text" , marker = dict ( opacity = 0. ) ) ) fig [ 'data' ] . extend ( [ bus_trace ] + shape_traces ) fig [ 'layout' ] . update ( dict ( shapes = shapes , title = title , hovermode = 'closest' , showlegend = False ) ) if iplot : if not pltly_present : logger . warning ( "Plotly is not present, so interactive plotting won't work." ) else : pltly . iplot ( fig ) return fig
Plot the network buses and lines interactively using plotly.

    Parameters
    ----------
    fig : dict, default None
        If not None, figure is built upon this fig.
    bus_colors : dict/pandas.Series
        Colors for the buses, defaults to "b"
    bus_colorscale : string
        Name of colorscale if bus_colors are floats, e.g. 'Jet', 'Viridis'
    bus_colorbar : dict
        Plotly colorbar, e.g. {'title' : 'my colorbar'}
    bus_sizes : dict/pandas.Series
        Sizes of bus points, defaults to 10
    bus_text : dict/pandas.Series
        Text for each bus, defaults to bus names
    line_colors : dict/pandas.Series
        Colors for the lines, defaults to "g" for Lines and "cyan" for
        Links. Colors for branches other than Lines can be
        specified using a pandas Series with a MultiIndex.
    line_widths : dict/pandas.Series
        Widths of lines, defaults to 2. Widths for branches other
        than Lines can be specified using a pandas Series with a
        MultiIndex.
    line_text : dict/pandas.Series
        Text for lines, defaults to line names. Text for branches other
        than Lines can be specified using a pandas Series with a
        MultiIndex.
    title : string
        Graph title
    branch_components : list of str
        Branch components to be plotted, defaults to Line and Link.
    iplot : bool, default True
        Automatically do an interactive plot of the figure.
    jitter : None|float
        Amount of random noise to add to bus positions to distinguish
        overlapping buses

    Returns
    -------
    fig: dictionary for plotly figure