paulbrodersen / netgraph

Publication-quality network visualisations in python
GNU General Public License v3.0
660 stars 39 forks source link

Not specifying node_edge_width when node_size is small causes node colours to not show up #77

Closed darryl34 closed 8 months ago

darryl34 commented 8 months ago

Basically when node_size is set to a small value, but node_edge_width is not specified, the node edge fills the entire node, giving the false impression that there is an issue with setting the node colours.

Using the example given for community node layout/bundled edges found here, by simply removing the node_edge_width param, and setting node_size to 0.1.

# create a modular graph
partition_sizes = [5, 10, 15, 20]
g = nx.random_partition_graph(partition_sizes, 0.5, 0.1)

# create a dictionary that maps nodes to the community they belong to
node_to_community = dict()
node = 0
for community_id, size in enumerate(partition_sizes):
    for _ in range(size):
        node_to_community[node] = community_id
        node += 1

community_to_color = {
    0 : 'tab:blue',
    1 : 'tab:orange',
    2 : 'tab:green',
    3 : 'tab:red',
}
node_color = {node: community_to_color[community_id] for node, community_id in node_to_community.items()}

Graph(g,
      node_color=node_color, node_size=0.5, edge_width=0.1,
      node_layout='community', node_layout_kwargs=dict(node_to_community=node_to_community),
      edge_layout='bundled', edge_layout_kwargs=dict(k=100),
)

plt.show()

1236c68a-008d-4f5d-95db-d1edbae36cf9

Increasing the node sizes correctly shows the different colours as expected. a662e7b5-d868-4977-83d0-f283349da14e

Don't think this is a bug but raising this issue here because I was working on a graph with a large number of nodes and for the longest time I thought the issue was with node_color. I was thinking maybe a warning could be issued somehow if one sets the node_size to a small value without setting node_edge_width to zero.

paulbrodersen commented 8 months ago

I was thinking maybe a warning could be issued somehow if one sets the node_size to a small value without setting node_edge_width to zero.

Great suggestion. There are additional complications (the node_edge_color might be the same as the node_color in which case Netgraph probably should not raise a warning) but I like idea. I will think about where and when to raise the warning, and incorporate it in the next release.

paulbrodersen commented 8 months ago

Added the following check that results in a warning if the node radius is smaller than the node edge width and the color of the node face is distinct from the the color of the node edge:

        for node in self.nodes:
            if (node_size[node] < node_edge_width[node]) & (node_color[node] != node_edge_color[node]):
                msg  = f"The border around the node {node} is broader than its radius."
                msg += f" The node will mostly have the color of the border ({node_edge_color[node]}), even though a different face color was specified ({node_color[node]})."
                msg += f" To address this issue, reduce the value given for `node_edge_width`."
                warnings.warn(msg)