๐ 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]:
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]:
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))
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]:
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]:
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: >
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')