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=0.05, ax=None, geomap=True, projection=None, bus_colors='b', line_colors={'Line': 'g', 'Link': 'cyan'}, bus_sizes=10, line_widths={'Line': 2, 'Link': 2}, flow=None, title='', line_cmap=None, bus_cmap=None, boundaries=None, geometry=False, branch_components=['Line', 'Link'], jitter=None, basemap=None, basemap_parameters=None, color_geomap=None)

Plot the network buses and lines using matplotlib and Basemap.

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 Basemap or Cartopy (depends on what is installed). If string is passed, it will be used as a resolution argument. For Basemap users ‘c’ (crude), ‘l’ (low), ‘i’ (intermediate), ‘h’ (high), ‘f’ (full) are valid resolutions options. For Cartopy users ‘10m’, ‘50m’, ‘110m’ are valid resolutions options.

  • 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 “b”

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

  • 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.

  • flow (snapshot/pandas.Series/function/string) – Flow to be displayed in the plot, defaults to None. If an element of network.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.

  • title (string) – Graph title

  • line_cmap (plt.cm.ColorMap/str|dict) – If line_colors are floats, this color map will assign the colors. Use a dict to specify colormaps for more than one branch type.

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

  • 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

  • basemap_parameters (dict) – Specify a dict with additional constructor parameters for the Basemap. Will disable Cartopy. Use this feature to set a custom projection. (e.g. {‘projection’: ‘tmerc’, ‘lon_0’:10.0, ‘lat_0’:50.0})

  • 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='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

Return type

dictionary for plotly figure

See also the SciGRID plotly example.