Skip to content

Individualize drawing attributes - #7570

Merged
dschult merged 10 commits into
networkx:mainfrom
mjschwenne:individualize-drawing-attrs
Aug 28, 2024
Merged

dschult merged 10 commits into
networkx:mainfrom
mjschwenne:individualize-drawing-attrs

Conversation

@mjschwenne

Copy link
Copy Markdown
Contributor

A set of improvements to the existing visualization suite aimed to align the current functionality with what will be implemented after the refactor.

Some attributes can only have one value per visualization, such as node font color or arrow style for directed graphs, but if attributes are read from the graph, we should allow individual values for all of these.

While I haven't broken any of the existing tests, I'm not sure that I fully understand the testing infrastructure for the visualization module. Tests will need to be added, but I'm still working on it.

Below are some examples on the karate club graph.

nx.draw_networkx(G, pos=pos, ax=ax, node_shape='o', 
    font_size={n: int(n / (34/15) + 5) for n in G.nodes()}, 
    font_color={n: 'k' if n % 2 == 0 else 'r' for n in G.nodes()})

image

nx.draw_networkx(G, ax=plt.axes(), 
    node_shape=np.array(['o' if n % 2 == 0 else '^' for n in G.nodes()]))

image

arrowstyles = ["-|>" if (u + v) % 2 == 0 else "-[" for u, v in G.edges()]
arrowsizes = [(u+v)/2 for u, v in G.edges()]
nx.draw_networkx(G, ax=plt.axes(), arrows=True, arrowstyle=arrowstyles, arrowsize=arrowsizes)

image

min_source_margin = [u for u, _ in G.edges()]
min_target_margin = [v for _, v in G.edges()]
nx.draw_networkx(G, ax=plt.axes(), arrows=True, min_source_margin=min_source_margin, min_target_margin=min_target_margin)

image

@mjschwenne mjschwenne added type: Enhancements Visualization Related to graph visualization or layout labels Jul 22, 2024
@mjschwenne

Copy link
Copy Markdown
Contributor Author

So I've added some basic tests, but some of the tests in test_pylab don't seem to have any assertions (like the test_labels_and_colors test. Since there is not decorator, I think this is just checking that no exceptions are raised in the calls to draw_networkx_*. It is also the only test that references any font-specific attribute (in this case font size).

Do we need to add more tests for things like font weight, font color, etc? I've added a call using multiple font colors.

@dschult

dschult commented Jul 24, 2024

Copy link
Copy Markdown
Member

The testing is intentionally weak for the drawing tools -- intentional in that we figured it is just a temporary stopgap tool and we'd get to better testing when we made it a real thing. Fast forward 18 years and we're at the point of needing/thinking about tests. The tests with no asserts were our "smoke-tests" to run the code and see if it started smoking or blowing up or something (syntax error).

I would suggest that you add tests as much as you feel makes sense. It will help you keep track of which features are set up which way and how they might change. But don't worry about complete testing unless you think that helps your design work. The interface will change so the tests will need to change too.

@mjschwenne

Copy link
Copy Markdown
Contributor Author

I've added a few basic tests, mostly form the examples I've included in the PR. I think these are enough to catch any breakage at least until #7589 lands, which has a much more robust set of tests.

@mjschwenne
mjschwenne marked this pull request as ready for review August 14, 2024 14:52

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Nice tests parametrization. Centralized handling of parameter presence. Thanks @mjschwenne

@dschult dschult left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to merge this PR soon. We should exercise the code and see what breaks. The previous tests were not very complete.

Comment thread networkx/drawing/nx_pylab.py Outdated
)
# Convert to one element np.array if needed
if not isinstance(node_shape, np.ndarray) and not isinstance(node_shape, list):
node_shape = np.asarray([node_shape for _ in range(len(nodelist))])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that np.array converts the input to an array with a copy and np.asarray converts without a copy if it can. Here we input a list so they both create a new array. So the preferred form is np.array.

Suggested change
node_shape = np.asarray([node_shape for _ in range(len(nodelist))])
node_shape = np.asarray([node_shape for _ in range(len(nodelist))])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, interesting. I did not know this bit of numpy-fu.

@dschult
dschult merged commit 9c3972e into networkx:main Aug 28, 2024
@dschult dschult added this to the 3.4 milestone Aug 28, 2024
rossbar pushed a commit to Peiffap/networkx that referenced this pull request Sep 6, 2024
* individualize parameters for draw_networkx_label

* individualize parameters for draw_networkx_edge_labels

* individualize parameters for draw_networkx_nodes

* fix node drawing issues

* individualize attributes for draw_networkx_edges

* update documentation

* add some basic tests

* add font_color call

* basic tests

* switch asarray to array
@mjschwenne
mjschwenne deleted the individualize-drawing-attrs branch October 17, 2024 22:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs review type: Enhancements Visualization Related to graph visualization or layout

Development

Successfully merging this pull request may close these issues.

2 participants