Plotting Networks

See the module pypsa.plot.

PyPSA has several functions available for plotting networks with different colors/widths/labels on buses and branches.

Static plotting with matplotlib

Static plots of networks can be created that use the library matplotlib. This is meant for use with Jupyter notebooks, but can also be used to generate image files. To plot a network with matplotlib run network.plot().

Network.plot(margin=None, ax=None, geomap=True, projection=None, bus_colors='cadetblue', bus_alpha=1, bus_sizes=0.02, bus_cmap=None, line_colors='rosybrown', link_colors='darkseagreen', transformer_colors='orange', line_widths=1.5, link_widths=1.5, transformer_widths=1.5, line_cmap=None, link_cmap=None, transformer_cmap=None, flow=None, branch_components=None, layouter=None, title='', boundaries=None, geometry=False, jitter=None, color_geomap=None)

Plot the network buses and lines using matplotlib and cartopy.

Parameters
  • margin (float) – Margin at the sides as proportion of distance between max/min x,y

  • ax (matplotlib ax, defaults to plt.gca()) – Axis to which to plot the network

  • geomap (bool/str, default True) – Switch to use Cartopy and draw geographical features. If string is passed, it will be used as a resolution argument, valid options are ‘10m’, ‘50m’ and ‘110m’.

  • projection (cartopy.crs.Projection, defaults to None) – Define the projection of your geomap, only valid if cartopy is installed. If None (default) is passed the projection for cartopy is set to cartopy.crs.PlateCarree

  • bus_colors (dict/pandas.Series) – Colors for the buses, defaults to “cadetblue”. If bus_sizes is a pandas.Series with a Multiindex, bus_colors defaults to the n.carriers[‘color’] column.

  • bus_alpha (float) – Adds alpha channel to buses, defaults to 1.

  • bus_sizes (dict/pandas.Series) – Sizes of bus points, defaults to 1e-2. If a multiindexed Series is passed, the function will draw pies for each bus (first index level) with segments of different color (second index level). Such a Series is ob- tained by e.g. n.generators.groupby([‘bus’, ‘carrier’]).p_nom.sum()

  • bus_cmap (plt.cm.ColorMap/str) – If bus_colors are floats, this color map will assign the colors

  • line_colors (str/pandas.Series) – Colors for the lines, defaults to ‘rosybrown’.

  • link_colors (str/pandas.Series) – Colors for the links, defaults to ‘darkseagreen’.

  • transfomer_colors (str/pandas.Series) – Colors for the transfomer, defaults to ‘orange’.

  • line_widths (dict/pandas.Series) – Widths of lines, defaults to 1.5

  • link_widths (dict/pandas.Series) – Widths of links, defaults to 1.5

  • transformer_widths (dict/pandas.Series) – Widths of transformer, defaults to 1.5

  • line_cmap (plt.cm.ColorMap/str|dict) – If line_colors are floats, this color map will assign the colors.

  • link_cmap (plt.cm.ColorMap/str|dict) – If link_colors are floats, this color map will assign the colors.

  • transformer_cmap (plt.cm.ColorMap/str|dict) – If transformer_colors are floats, this color map will assign the colors.

  • flow (snapshot/pandas.Series/function/string) – Flow to be displayed in the plot, defaults to None. If an element of n.snapshots is given, the flow at this timestamp will be displayed. If an aggregation function is given, is will be applied to the total network flow via pandas.DataFrame.agg (accepts also function names). Otherwise flows can be specified by passing a pandas Series with MultiIndex including all necessary branch components. Use the line_widths argument to additionally adjust the size of the flow arrows.

  • layouter (networkx.drawing.layout function, default None) – Layouting function from networkx which overrules coordinates given in n.buses[['x','y']]. See list of available options.

  • title (string) – Graph title

  • boundaries (list of four floats) – Boundaries of the plot in format [x1,x2,y1,y2]

  • branch_components (list of str) – Branch components to be plotted, defaults to Line and Link.

  • jitter (None|float) – Amount of random noise to add to bus positions to distinguish overlapping buses

  • color_geomap (dict or bool) – Specify colors to paint land and sea areas in. If True, it defaults to {‘ocean’: ‘lightblue’, ‘land’: ‘whitesmoke’}. If no dictionary is provided, colors are white.

Returns

bus_collection, branch_collection1, … – Collections for buses and branches.

Return type

tuple of Collections

See also the SciGRID matplotlib example.

Interactive plotting with plotly

Interactive plots of networks can be created that use the d3js-based library plotly (this uses JavaScript and SVGs). This is meant for use with Jupyter notebooks. To plot a network with plotly run network.iplot().

Network.iplot(fig=None, bus_colors='cadetblue', bus_alpha=1, bus_sizes=10, bus_cmap=None, bus_colorbar=None, bus_text=None, line_colors='rosybrown', link_colors='darkseagreen', transformer_colors='orange', line_widths=3, link_widths=3, transformer_widths=3, line_text=None, link_text=None, transformer_text=None, layouter=None, title='', size=None, branch_components=None, iplot=True, jitter=None, mapbox=False, mapbox_style='open-street-map', mapbox_token='', mapbox_parameters={})

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 “cadetblue”. If bus_sizes is a pandas.Series with a Multiindex, bus_colors defaults to the n.carriers[‘color’] column.

  • bus_alpha (float) – Adds alpha channel to buses, defaults to 1.

  • bus_sizes (float/pandas.Series) – Sizes of bus points, defaults to 10.

  • bus_cmap (plt.cm.ColorMap/str) – If bus_colors are floats, this color map will assign the colors

  • bus_colorbar (dict) – Plotly colorbar, e.g. {‘title’ : ‘my colorbar’}

  • bus_text (pandas.Series) – Text for each bus, defaults to bus names

  • line_colors (str/pandas.Series) – Colors for the lines, defaults to ‘rosybrown’.

  • link_colors (str/pandas.Series) – Colors for the links, defaults to ‘darkseagreen’.

  • transfomer_colors (str/pandas.Series) – Colors for the transfomer, defaults to ‘orange’.

  • line_widths (dict/pandas.Series) – Widths of lines, defaults to 1.5

  • link_widths (dict/pandas.Series) – Widths of links, defaults to 1.5

  • transformer_widths (dict/pandas.Series) – Widths of transformer, defaults to 1.5

  • line_text (pandas.Series) – Text for lines, defaults to line names.

  • link_text (pandas.Series) – Text for links, defaults to link names.

  • tranformer_text (pandas.Series) – Text for transformers, defaults to transformer names.

  • layouter (networkx.drawing.layout function, default None) –

    Layouting function from networkx which overrules coordinates given in n.buses[['x','y']]. See list of available options.

  • title (string) – Graph title

  • size (None|tuple) – Tuple specifying width and height of figure; e.g. (width, heigh).

  • 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

  • mapbox (bool, default False) – Switch to use Mapbox.

  • mapbox_style (str, default 'open-street-map') –

    Define the mapbox layout style of the interactive plot. If this is set to a mapbox layout, the argument mapbox_token must be a valid Mapbox API access token.

    Valid open layouts are:

    open-street-map, white-bg, carto-positron, carto-darkmatter, stamen-terrain, stamen-toner, stamen-watercolor

    Valid mapbox layouts are:

    basic, streets, outdoors, light, dark, satellite, satellite-streets

  • mapbox_token (string) – Mapbox API access token. Obtain from https://www.mapbox.com. Can also be included in mapbox_parameters as accesstoken=mapbox_token.

  • mapbox_parameters (dict) – Configuration parameters of the Mapbox layout. E.g. {“bearing”: 5, “pitch”: 10, “zoom”: 1, “style”: ‘dark’}.

Returns

fig

Return type

dictionary for plotly figure

See also the SciGRID plotly example.