You can use colormaps to colour your network plots in order to get a quick and simple overview of line loadings and bus voltages. The plotting module provides functions to easily modify your bus and line collections. It also provides colourbars to match the colourcodes to the according numerical values.
First, we load the network and run a loadflow to retrieve results:
import pandapower as pp
import pandapower.networks as nw
import pandapower.plotting as plot
%matplotlib inline
net = nw.mv_oberrhein()
pp.runpp(net)
The pandapower plotting package contains convenience functions to create common colorbars and norms. We use the cmap_continous function to get a linear colormap with color centers green at 20%, yellow at 50% and red at 60% line loading:
cmap_list=[(20, "green"), (50, "yellow"), (60, "red")]
cmap, norm = plot.cmap_continous(cmap_list)
The colormap and norm are now passend to the create_bus_collection function and the collection is plotted with draw_collections:
lc = plot.create_line_collection(net, net.line.index, zorder=1, cmap=cmap, norm=norm, linewidths=2)
plot.draw_collections([lc], figsize=(8,6))
<matplotlib.axes._subplots.AxesSubplot at 0xec5c630>
cmap_list=[(0.975, "blue"), (1.0, "green"), (1.03, "red")]
cmap, norm = plot.cmap_continous(cmap_list)
bc = plot.create_bus_collection(net, net.bus.index, size=80, zorder=2, cmap=cmap, norm=norm)
plot.draw_collections([lc, bc], figsize=(8,6))
<matplotlib.axes._subplots.AxesSubplot at 0xed9da90>
Discrete colormaps can be used in the same way as continous colormaps using the cmap_voltage_discrete and cmap_loading_discrete functions. For discrete colormaps, each color has to be assigned a range instead of a center:
net = nw.mv_oberrhein()
pp.runpp(net)
cmap_list=[((0.975, 0.985), "blue"), ((0.985, 1.0), "green"), ((1.0, 1.03), "red")]
cmap, norm = plot.cmap_discrete(cmap_list)
bc = plot.create_bus_collection(net, net.bus.index, size=80, zorder=2, cmap=cmap, norm=norm)
cmap_list=[((10, 40), "green"), ((40, 55), "yellow"), ((55, 60), "red")]
cmap, norm = plot.cmap_discrete(cmap_list)
lc = plot.create_line_collection(net, net.line.index, zorder=1, cmap=cmap, norm=norm, linewidths=2)
plot.draw_collections([lc, bc], figsize=(8,6))
<matplotlib.axes._subplots.AxesSubplot at 0xdf63e48>
The functions to create colormaps and norms are merely convenience functions. You can individually create any colormap you like and pass it to the create_collection functions.
For example, for the colorbar "PuBu_r" from matplotlib:
from matplotlib.pyplot import get_cmap
from matplotlib.colors import Normalize
cmap = get_cmap('PuBu_r')
lc = plot.create_line_collection(net, net.line.index, zorder=1, color="grey", linewidths=2,
cmap=cmap)
bc = plot.create_bus_collection(net, net.bus.index, size=80, zorder=2)
plot.draw_collections([lc, bc], figsize=(8,6))
<matplotlib.axes._subplots.AxesSubplot at 0xee950b8>
Plotting without a norm maps the colorbar to the range of the data points (here: line loadings). Normalizing to values between 20 and 100 yields:
cmap = get_cmap('PuBu_r')
norm = Normalize(vmin=20, vmax=100)
lc = plot.create_line_collection(net, net.line.index, zorder=1, color="grey", linewidths=2,
cmap=cmap, norm=norm)
bc = plot.create_bus_collection(net, net.bus.index, size=80, zorder=2)
plot.draw_collections([lc, bc], figsize=(8,6))
<matplotlib.axes._subplots.AxesSubplot at 0xf233978>
The colorbar can be customized by disabling the automatic plotting of the colorbar in draw_collections and plotting the colorbar directly with the desired parameters:
from matplotlib.pyplot import colorbar
plot.draw_collections([lc, bc], figsize=(8,6), plot_colorbars=False)
cbar = colorbar(lc, extend="max")
cbar.set_ticks([50, 70, 100])
cbar.ax.set_ylabel("This is a individual colorbar title")
Text(0, 0.5, 'This is a individual colorbar title')