# Flow Plot Example

## Contents

Note

You can download this example as a Jupyter notebook or start it in interactive mode.

# Flow Plot Example#

Here, we are going to import a network and plot the electricity flow

[1]:

import warnings

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
import pandas as pd
from shapely.errors import ShapelyDeprecationWarning

import pypsa

warnings.filterwarnings("ignore", category=ShapelyDeprecationWarning)
plt.rc("figure", figsize=(10, 8))

ERROR 1: PROJ: proj_create_from_database: Open of /home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/share/proj failed


## Import and optimize a network#

[2]:

n = pypsa.examples.ac_dc_meshed(from_master=True)
n.optimize()

WARNING:pypsa.io:Importing network from PyPSA version v0.17.1 while current version is v0.28.1. Read the release notes at https://pypsa.readthedocs.io/en/latest/release_notes.html to prepare your network for import.
WARNING:pypsa.consistency:The following buses have carriers which are not defined:
Index(['London', 'Norwich', 'Norwich DC', 'Manchester', 'Bremen', 'Bremen DC',
'Frankfurt', 'Norway', 'Norway DC'],
dtype='object', name='Bus')
WARNING:pypsa.consistency:The following links have carriers which are not defined:
WARNING:pypsa.consistency:The following lines have carriers which are not defined:
Index(['0', '1', '2', '3', '4', '5', '6'], dtype='object', name='Line')
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
WARNING:pypsa.consistency:The following buses have carriers which are not defined:
Index(['London', 'Norwich', 'Norwich DC', 'Manchester', 'Bremen', 'Bremen DC',
'Frankfurt', 'Norway', 'Norway DC'],
dtype='object', name='Bus')
WARNING:pypsa.consistency:The following links have carriers which are not defined:
WARNING:pypsa.consistency:The following lines have carriers which are not defined:
Index(['0', '1', '2', '3', '4', '5', '6'], dtype='object', name='Line')
WARNING:pypsa.consistency:The following lines have zero x, which could break the linear load flow:
Index(['2', '3', '4'], dtype='object', name='Line')
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['0', '1', '5', '6'], dtype='object', name='Line')
INFO:linopy.model: Solve problem using Highs solver
INFO:linopy.io: Writing time: 0.07s
INFO:linopy.solvers:Log file at /tmp/highs.log
INFO:linopy.constants: Optimization successful:
Status: ok
Termination condition: optimal
Solution: 188 primals, 468 duals
Objective: -3.47e+06
Solver model: available
Solver message: optimal

INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-ext-p-lower, Generator-ext-p-upper, Line-ext-s-lower, Line-ext-s-upper, Link-ext-p-lower, Link-ext-p-upper, Kirchhoff-Voltage-Law were not assigned to the network.

Running HiGHS 1.7.1 (git hash: 0c240d8): Copyright (c) 2024 HiGHS under MIT licence terms
Coefficient ranges:
Matrix [1e-02, 1e+00]
Cost   [9e-03, 3e+03]
Bound  [2e+07, 2e+07]
RHS    [9e-01, 1e+03]
Presolving model
391 rows, 187 cols, 930 nonzeros  0s
305 rows, 101 cols, 1042 nonzeros  0s
303 rows, 99 cols, 1058 nonzeros  0s
Presolve : Reductions: rows 303(-165); columns 99(-89); elements 1058(+51)
Solving the presolved LP
Using EKK dual simplex solver - serial
Iteration        Objective     Infeasibilities num(sum)
0    -2.1204510016e+07 Pr: 102(98953); Du: 0(4.73182e-11) 0s
126    -3.4742560406e+06 Pr: 0(0) 0s
Solving the original LP from the solution after postsolve
Model   status      : Optimal
Simplex   iterations: 126
Objective value     : -3.4742560406e+06
HiGHS run time      :          0.00

[2]:

('ok', 'optimal')


Get mean generator power by bus and carrier:

[3]:

gen = n.generators.assign(g=n.generators_t.p.mean()).groupby(["bus", "carrier"]).g.sum()


Plot the electricity flows:

[4]:

# links are not displayed for prettier output ('link_widths=0')
n.plot(
bus_sizes=gen / 5e3,
bus_colors={"gas": "indianred", "wind": "midnightblue"},
margin=0.5,
flow="mean",
line_widths=0.1,
)
plt.show()

/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/IPython/core/pylabtools.py:77: DeprecationWarning: backend2gui is deprecated since IPython 8.24, backends are managed in matplotlib and can be externally registered.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/IPython/core/pylabtools.py:77: DeprecationWarning: backend2gui is deprecated since IPython 8.24, backends are managed in matplotlib and can be externally registered.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/IPython/core/pylabtools.py:77: DeprecationWarning: backend2gui is deprecated since IPython 8.24, backends are managed in matplotlib and can be externally registered.
warnings.warn(
/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/cartopy/mpl/feature_artist.py:144: UserWarning: facecolor will have no effect as it has been defined as "never".
warnings.warn('facecolor will have no effect as it has been '


Plot the electricity flows with a different projection and a colored map:

[5]:

# links are not displayed for prettier output ('link_widths=0')
n.plot(
bus_sizes=gen / 5e3,
bus_colors={"gas": "indianred", "wind": "midnightblue"},
margin=0.5,
flow="mean",
line_widths=0.1,
projection=ccrs.EqualEarth(),
color_geomap=True,
)
plt.show()

/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/cartopy/mpl/feature_artist.py:144: UserWarning: facecolor will have no effect as it has been defined as "never".
warnings.warn('facecolor will have no effect as it has been '


Set arbitrary values as flow argument using the MultiIndex of n.branches():

[6]:

flow = pd.Series(10, index=n.branches().index)

[7]:

flow

[7]:

component  name
Norway Converter     10
Bremen Converter     10
Line       0                    10
1                    10
2                    10
3                    10
4                    10
5                    10
6                    10
dtype: int64

[8]:

# links are not displayed for prettier output ('link_widths=0')
n.plot(
bus_sizes=gen / 5e3,
bus_colors={"gas": "indianred", "wind": "midnightblue"},
margin=0.5,
flow=flow,
line_widths=2.7,
projection=ccrs.EqualEarth(),
color_geomap=True,
)
plt.show()

/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/cartopy/mpl/feature_artist.py:144: UserWarning: facecolor will have no effect as it has been defined as "never".
warnings.warn('facecolor will have no effect as it has been '


[9]:

# Pandas series with MultiIndex
collection = n.plot(
bus_sizes=gen / 5e3,
bus_colors={"gas": "indianred", "wind": "midnightblue"},
margin=0.5,
flow=flow,
line_widths=2.7,

/home/docs/checkouts/readthedocs.org/user_builds/pypsa/conda/latest/lib/python3.12/site-packages/cartopy/mpl/feature_artist.py:144: UserWarning: facecolor will have no effect as it has been defined as "never".