Skip to content

Fix draw_networkx_nodes return type - #7685

Merged
dschult merged 6 commits into
networkx:mainfrom
kbonney:plot-return-type
Oct 18, 2024
Merged

dschult merged 6 commits into
networkx:mainfrom
kbonney:plot-return-type

Conversation

@kbonney

@kbonney kbonney commented Oct 17, 2024

Copy link
Copy Markdown
Contributor

This PR addresses #7684 by returning the collection object rather than axis object in draw_networkx_nodes. Additional tests are added to check that the return types of draw_networkx_nodes and draw_networkx_edges return expected types.

@kbonney kbonney changed the title Plot return type Fix draw_networkx_nodes return type Oct 17, 2024
@MridulS

MridulS commented Oct 17, 2024

Copy link
Copy Markdown
Member

This behavior was introduced in #7570, maybe @mjschwenne can review this?

@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.

It looks like the original return was commented out with the axis returned instead.
There might be a reason, or it might have been for debugging something else and not replaced.

Meanwhile the new tests are failing, but I hope it is just a missing input arg.

Comment thread networkx/drawing/tests/test_pylab.py Outdated
Comment on lines +371 to +383
# nodes
nodes = nx.draw_networkx_nodes(G)
assert isinstance(nodes, PathCollection)
# edges
edges = nx.draw_networkx_edges(dG, arrows=True)
assert isinstance(edges, list)
if len(edges) > 0:
assert isinstance(edges[0], FancyArrowPatch)
edges = nx.draw_networkx_edges(dG, arrows=False)
assert isinstance(edges, LineCollection)
edges = nx.draw_networkx_edges(dG, arrows=None)
assert isinstance(edges, LineCollection)
edges = nx.draw_networkx_edges(G, arrows=True)

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.

Suggested change
# nodes
nodes = nx.draw_networkx_nodes(G)
assert isinstance(nodes, PathCollection)
# edges
edges = nx.draw_networkx_edges(dG, arrows=True)
assert isinstance(edges, list)
if len(edges) > 0:
assert isinstance(edges[0], FancyArrowPatch)
edges = nx.draw_networkx_edges(dG, arrows=False)
assert isinstance(edges, LineCollection)
edges = nx.draw_networkx_edges(dG, arrows=None)
assert isinstance(edges, LineCollection)
edges = nx.draw_networkx_edges(G, arrows=True)
pos = nx.spring_layout(G)
# nodes
nodes = nx.draw_networkx_nodes(G, pos)
assert isinstance(nodes, PathCollection)
# edges
edges = nx.draw_networkx_edges(dG, pos, arrows=True)
assert isinstance(edges, list)
if len(edges) > 0:
assert isinstance(edges[0], FancyArrowPatch)
edges = nx.draw_networkx_edges(dG, pos, arrows=False)
assert isinstance(edges, LineCollection)
edges = nx.draw_networkx_edges(dG, pos, arrows=None)
assert isinstance(edges, LineCollection)
edges = nx.draw_networkx_edges(G, pos, arrows=True)

@mjschwenne

Copy link
Copy Markdown
Contributor

I don't remember there being any specific reason I changed the return type on this function. I suspect that it was to help with generating images in a jupyter notebook but I'm not sure. Reverting the return type to what's promised in the doc string and what has been used historically by this function is a good idea. Technically if we wanted to keep the change (which we don't) we'd need a deprecation cycle I think that since that didn't happen the change was never met to be permanent.

@kbonney

kbonney commented Oct 17, 2024 •

Copy link
Copy Markdown
Contributor Author

It looks like the original return was commented out with the axis returned instead. There might be a reason, or it might have been for debugging something else and not replaced.

Meanwhile the new tests are failing, but I hope it is just a missing input arg.

Updated the tests so that they don't fail anymore.

I don't remember there being any specific reason I changed the return type on this function. I suspect that it was to help with generating images in a jupyter notebook but I'm not sure. Reverting the return type to what's promised in the doc string and what has been used historically by this function is a good idea. Technically if we wanted to keep the change (which we don't) we'd need a deprecation cycle I think that since that didn't happen the change was never met to be permanent.

For more context, this came to my attention because some plotting functions in a software I maintain are built using draw_networkx_nodes and we use the collection objects to create colormaps, which breaks when the return type changes to axis.

@mjschwenne mjschwenne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the catch and test to make sure it doesn't happen again

@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.

Thanks @kbonney!!
And thanks @mjschwenne

@dschult dschult added this to the 3.4.2 milestone Oct 18, 2024
@dschult
dschult merged commit fb17e15 into networkx:main Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

4 participants