grave icon indicating copy to clipboard operation
grave copied to clipboard

Backfill labeled node with color

Open CJ-Wright opened this issue 6 years ago • 7 comments

Is it possible to backfill a labeled node with a color? If so, how?

CJ-Wright avatar Jun 28 '18 22:06 CJ-Wright

What do you mean by backfill? You can fill a node and you can set textcolor and background color on text

dschult avatar Jun 30 '18 16:06 dschult

I mean background color. However that doesn't seem to be possible.

import networkx as nx
import matplotlib.pyplot as plt
import random
from grave import plot_network


graph = nx.barbell_graph(10, 14)

nx.set_node_attributes(graph, dict(graph.degree()), 'degree')
graph = nx.relabel_nodes(graph, {k: str(i) for i, k in enumerate(graph.node)})


def degree_colorer(node_attributes):
    deg = node_attributes['degree']
    shape = random.choice(['s', 'o', '^', 'v', '8'])
    if deg > 5:
        return {'color': 'r', 'size': 20*deg,
                # 'shape': shape,
                'label': 'hi'
                }
    return {'color': 'b', 'size': 20*deg,
            # 'shape': shape,
            'label': 'hi'
            }


def pathological_edge_style(edge_attrs):
    return {'color': random.choice(['r', (0, 1, 0, .5), 'xkcd:ocean'])}


fig, ax = plt.subplots()
plot_network(graph, ax=ax,
             node_style=dict(node_size=20, color='r', edgecolor='r'),
             # node_style=degree_colorer,
             edge_style=pathological_edge_style,
             node_label_style={'font_size': 20,
                               'font_color': 'r',
                               'backgroundcolor': 'b'
                               }
)
plt.show()

'''
We can calculate the execution order by walking the graph from a node and
summing it's edge labels (with zeros if there is none). This way all the nodes
on the first path will have a value of zero. Finally, since some nodes are
part of multiple trr
'''

Is the backgroundcolor key being passed in?

CJ-Wright avatar Jun 30 '18 18:06 CJ-Wright

You are correct -- while that property (and others) are available in matplotlib, the GraVE code doesn't pass through many text attributes to the label artists.

Is there a reason we limit the attributes using key_map here? Is there a nice way to have key_map mimic the matplotlib attributes directly?

dschult avatar Jul 02 '18 15:07 dschult

FYI I also hacked the Text object created (to make all the nodes red) and still no joy.

CJ-Wright avatar Jul 02 '18 16:07 CJ-Wright

Ahh -- in your example, the text is so large that it covers up the nodes. You have set the node_size to 20 but that doesn't seem to do anything. A little hunting shows t hat you need to set size=20 for nodes rather than node_size .

Looks like we have to make styles better at catching buggy input!

Anyway, if you use size=2000 you will see red disk nodes around your text labels.

dschult avatar Jul 02 '18 17:07 dschult

Yey human fuzzing!

CJ-Wright avatar Jul 02 '18 17:07 CJ-Wright

As a follow up on this I was able to change the node size and see the color but the text background color seems to be white, is it possible to change this?

figure_1-24

CJ-Wright avatar Aug 08 '18 14:08 CJ-Wright