๐Ÿ“Š Plottingยถ

Concepts: Graph representations and plot views Network/Router API: ๐Ÿ“Š Plotting

This notebook demonstrates the plotting features of OptiWindNet.

[1]:
from optiwindnet.converting import G_from_S
from optiwindnet.heuristics import constructor
from optiwindnet.importer import load_repository
from optiwindnet.interarraylib import assign_cables
from optiwindnet.mesh import make_planar_embedding
from optiwindnet.pathfinding import PathFinder

Create a sample networkยถ

[2]:
locations = load_repository()
L = locations.london
P, A = make_planar_embedding(L)
S = constructor(A, capacity=7)
Gสน = G_from_S(S, A)
G = PathFinder(Gสน, planar=P, A=A).create_detours()

Using SVG-based svgplot()ยถ

The primary SVG-based plotting functions in OptiWindNet are svgplot() and svgpplot(). They produce vector figures suitable for web pages and Jupyter notebook display while keeping .ipynb files small and text-searchable.

[3]:
from optiwindnet.svg import svgplot
[4]:
svgplot(G)
[4]:
../_images/notebooks_lo14_plotting_8_0.svg

If G is assigned cable types, it is encoded in the thickness of the edges (higher capacities represented by thicker lines). The total network cost is also added to the infobox.

[5]:
# one 2-tuple for each cable type: (capacity, linear cost)
# capacities must be increasing
assign_cables(G, cables=[(2, 400), (4, 600), (7, 900)])
[6]:
svgplot(G)
[6]:
../_images/notebooks_lo14_plotting_11_0.svg

Node tags and figure legendsยถ

svgplot() supports a variety of node tagging options and automatic legend strip generation. The node_tag argument decides what is drawn inside the node symbols:

Value

Effect

True

The node number โ€” from order of appearance in the data, starting at 0 for terminals and negative for roots.

'label'

The nodeโ€™s label attribute, as defined in the .yaml or .osm.pbf file, or assigned programmatically.

'load'

The load exported by the node: for a terminal, the number of terminals upstream including itself; for a root, the total number of terminals connected to it.

'attribute_name'

The value of any other node attribute.

Default symbol sizes suit tags of up to three characters; longer tags may not fit.

[7]:
svgplot(G, node_tag='label', infobox=False, legend=True)
[7]:
../_images/notebooks_lo14_plotting_14_0.svg

Alternatively, the internal index of the nodes can be shown instead of their labels by setting node_tag=True:

[8]:
svgplot(G, node_tag=True, infobox=False)
[8]:
../_images/notebooks_lo14_plotting_16_0.svg

The nodes can also be tagged by the power flow (load) they export towards the substation:

[9]:
svgplot(G, node_tag='load')
[9]:
../_images/notebooks_lo14_plotting_18_0.svg

Displaying in loops and saving to filesยถ

The svgplot() function does not create an inline image immediately upon being called; the SVG is displayed when the object returned by the call is the cell output. To display figures created within code loops, use IPython.display.display.

[10]:
for cap in [9, 7, 5]:
    S_loop = constructor(A, capacity=cap)
    G_loopสน = G_from_S(S_loop, A)
    G_loop = PathFinder(G_loopสน, planar=P, A=A).create_detours()
    display(svgplot(G_loop))
../_images/notebooks_lo14_plotting_21_0.svg
../_images/notebooks_lo14_plotting_21_1.svg
../_images/notebooks_lo14_plotting_21_2.svg

To save an SVG representation to a file:

svgplot(G).save('myfig.svg')

svgplot() automatically tries to match the operating systemโ€™s theme, through the darkdetect package โ€” note that this detects the system theme, not JupyterLabโ€™s, so the two can disagree. To enforce a theme programmatically, set the dark parameter to True or False.

[11]:
svgplot(G, dark=False)
[11]:
../_images/notebooks_lo14_plotting_24_0.svg

Each location included in OptiWindNet has a graph attribute called landscape_angle. To orient the plot with north always up, set landscape=False.

[12]:
svgplot(G, landscape=False)
[12]:
../_images/notebooks_lo14_plotting_26_0.svg

Using Matplotlib-based gplot() & pplot()ยถ

OptiWindNet also provides Matplotlib-based plotting functions, gplot() and pplot(), under optiwindnet.plotting.

SVG-based plots are suitable for direct notebook display. Use the Matplotlib-based functions for Matplotlib-specific capabilities such as multiple axes and custom layout integration.

[13]:
import matplotlib.pyplot as plt

%config InlineBackend.figure_formats = ['svg']
plt.rcParams['svg.fonttype'] = 'none'
from optiwindnet.plotting import gplot, pplot

1. Custom Multi-Axis Subplotsยถ

Pass an existing plt.Axes object to place plots side by side or integrate them into a more complex subplot layout:

[14]:
fig, (ax1, ax2) = plt.subplots(1, 2, facecolor='none', figsize=(9, 3))
pplot(P, A, ax=ax1)
gplot(A, ax=ax2)
[14]:
<Axes: >
../_images/notebooks_lo14_plotting_32_1.svg

pplot(P, A) draws the navigation mesh P, using coordinates from A. The mesh includes an enclosing supertriangle; hide_ST=True, the default, adjusts the plot limits to hide its distant vertices.

To inspect candidate connections, plot A with gplot() or svgplot(). Direct Delaunay links are solid and diagonal alternatives are dashed; colors distinguish obstructed links. Possible root-to-terminal feeders are not edges of A: their obstacle-avoiding lengths are stored in A.graph['d2roots'], and they are omitted from the candidate-link plot.

To inspect selected connections before final routing, plot G_from_S(S, A). This includes boundary-following contours but not the crossing-avoidance detours produced by PathFinder.

2. Matplotlib Axes Customization and Savingยถ

gplot() returns a standard plt.Axes object. Use the returned axes to adjust the figure or export it through a Matplotlib backend, including PDF, PNG, and EPS:

[15]:
ax = gplot(G)
# ax.figure.savefig('myfig.pdf')
../_images/notebooks_lo14_plotting_36_0.svg