deflex.DeflexGraph.color_nodes_by_type

DeflexGraph.color_nodes_by_type(colors, use_name=True)[source]

Color all nodes in a specific color according to their class.

Use the group_nodes_by_type() method to get all existing types. Now a color can be assigned to every type using a color dictionary. If no color is defined for an existing class the default color is used. By default the name of each class is used.

Parameters:colors (dict) – The dictionary needs to have the class (name of class) as keys and a color dictionary as value. The color dictionary has two keys, “fg” for the foreground color (font color) and “bg” for the background color (fill color) and the color as value. The color has to be in the hexadecimal style.
use_name : bool
Use the name of the class instead of the class as key. If the class is used, the classes also have to be used for the colors as key.

Examples

>>> from deflex import fetch_test_files
>>> from deflex import restore_results
>>> from deflex import DeflexGraph
>>> from oemof import solph
>>> fn = fetch_test_files("de03_fictive.dflx")
>>> my_results = restore_results(fn)
>>> dflx_graph = DeflexGraph(my_results)
>>> my_colors = {
...     "Bus": {"bg": "#00ff11", "fg": "#000000"},
...     "GenericStorage": {"bg": "#efb507", "fg": "#000000"},
...     "Transformer": {"bg": "#94221d", "fg": "#000000"},
...     "Source": {"bg": "#996967", "fg": "#000000"},
...     "Sink": {"bg": "#31306e", "fg": "#ffffff"},
... }
>>> dflx_graph.color_nodes_by_type(my_colors)
>>> bus = [a for a in dflx_graph.nodes if isinstance(a, solph.Bus)][0]
>>> getattr(bus, "bgcolor")
'#00ff11'
>>> sorted(set([a.bgcolor for a in dflx_graph.nodes]))
['#00ff11', '#31306e', '#94221d', '#996967', '#efb507']
>>> sorted(set([a.fgcolor for a in dflx_graph.nodes]))
['#000000', '#ffffff']
>>> my_colors = {
...     solph.Bus: {"bg": "#00ff11", "fg": "#000000"},
...     "GenericStorage": {"bg": "#efb507", "fg": "#000000"},
...     "Transformer": {"bg": "#94221d", "fg": "#000000"},
...     solph.Source: {"bg": "#996967", "fg": "#000000"},
...     solph.Sink: {"bg": "#31306e", "fg": "#ffffff"},
... }
>>> dflx_graph.color_nodes_by_type(my_colors, use_name=False)
>>> bus = [a for a in dflx_graph.nodes if isinstance(a, solph.Bus)][0]
>>> getattr(bus, "bgcolor")
'#00ff11'
>>> sorted(set([a.bgcolor for a in dflx_graph.nodes]))
['#00ff11', '#31306e', '#6a6a72', '#996967']
>>> sorted(set([a.fgcolor for a in dflx_graph.nodes]))
['#000000', '#ffffff']