deflex.DeflexGraph.color_nodes_by_substring

DeflexGraph.color_nodes_by_substring(colors)[source]

Color all nodes in a specific color according to a given substring. A color can be assigned to every substring using a dictionary with the substrings as key an the color dictionary as value. The color dictionary needs to have the keys “fg” for the foreground color (font color) and “bg” for the background color (fill color). The color has to be in the hexadecimal style. Each substring key will be compared with the label of the node as string. If no substring match the default node color is used. If more than one substring is within a label the last match will overwrite the previous matches.

Parameters:colors (dict) – The dictionary needs to have the substring 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.

Examples

>>> from deflex import fetch_test_files
>>> from deflex import restore_results
>>> from deflex import DeflexGraph
>>> fn = fetch_test_files("de03_fictive.dflx")
>>> my_results = restore_results(fn)
>>> dflx_graph = DeflexGraph(my_results)
>>> my_colors = {
...     "H2": {"bg": "#00ff11", "fg": "#000000"},
...     "electricity": {"bg": "#efb507", "fg": "#000000"},
...     "bioenergy": {"bg": "#063313", "fg": "#ffffff"},
... }
>>> dflx_graph.color_nodes_by_substring(my_colors)
>>> mynode = [n for n in dflx_graph.nodes if "bioenergy" in n.label][0]
>>> getattr(mynode, "bgcolor")
'#063313'
>>> sorted(set([n.bgcolor for n in dflx_graph.nodes]))
['#00ff11', '#063313', '#6a6a72', '#efb507']
>>> sorted(set([n.fgcolor for n in dflx_graph.nodes]))
['#000000', '#ffffff']