diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2022-10-21 17:59:58 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-21 20:59:58 -0400 |
commit | 73afc6768021f46279d05e6b8a8e722bffac1d79 (patch) | |
tree | 22dd528d1fd8580e9f1d440aa5c7d9d3d92e4cbe /networkx/drawing | |
parent | e644cc34705cc014c9419a2924822eb0c2c3fb0d (diff) | |
download | networkx-73afc6768021f46279d05e6b8a8e722bffac1d79.tar.gz |
Warn on unused visualization kwargs that only apply to FancyArrowPatch edges (#6098)
* Add test for UserWarning with ignored vizopts.
* Implement UserWarning for bad vizopts combos.
* Fix instance in test suite caught by new warnings.
* Update test to check warnings are *not* raised when using FAPs.
Diffstat (limited to 'networkx/drawing')
-rw-r--r-- | networkx/drawing/nx_pylab.py | 40 | ||||
-rw-r--r-- | networkx/drawing/tests/test_pylab.py | 37 |
2 files changed, 73 insertions, 4 deletions
diff --git a/networkx/drawing/nx_pylab.py b/networkx/drawing/nx_pylab.py index 09e5ed3a..674c28a4 100644 --- a/networkx/drawing/nx_pylab.py +++ b/networkx/drawing/nx_pylab.py @@ -650,6 +650,42 @@ def draw_networkx_edges( # undirected graphs (for performance reasons) and use FancyArrowPatches # for directed graphs. # The `arrows` keyword can be used to override the default behavior + use_linecollection = not G.is_directed() + if arrows in (True, False): + use_linecollection = not arrows + + # Some kwargs only apply to FancyArrowPatches. Warn users when they use + # non-default values for these kwargs when LineCollection is being used + # instead of silently ignoring the specified option + if use_linecollection and any( + [ + arrowstyle is not None, + arrowsize != 10, + connectionstyle != "arc3", + min_source_margin != 0, + min_target_margin != 0, + ] + ): + import warnings + + msg = ( + "\n\nThe {0} keyword argument is not applicable when drawing edges\n" + "with LineCollection.\n\n" + "To make this warning go away, either specify `arrows=True` to\n" + "force FancyArrowPatches or use the default value for {0}.\n" + "Note that using FancyArrowPatches may be slow for large graphs.\n" + ) + if arrowstyle is not None: + msg = msg.format("arrowstyle") + if arrowsize != 10: + msg = msg.format("arrowsize") + if connectionstyle != "arc3": + msg = msg.format("connectionstyle") + if min_source_margin != 0: + msg = msg.format("min_source_margin") + if min_target_margin != 0: + msg = msg.format("min_target_margin") + warnings.warn(msg, category=UserWarning, stacklevel=2) if arrowstyle == None: if G.is_directed(): @@ -657,10 +693,6 @@ def draw_networkx_edges( else: arrowstyle = "-" - use_linecollection = not G.is_directed() - if arrows in (True, False): - use_linecollection = not arrows - if ax is None: ax = plt.gca() diff --git a/networkx/drawing/tests/test_pylab.py b/networkx/drawing/tests/test_pylab.py index f642dcc4..cef2702d 100644 --- a/networkx/drawing/tests/test_pylab.py +++ b/networkx/drawing/tests/test_pylab.py @@ -1,6 +1,7 @@ """Unit tests for matplotlib drawing functions.""" import itertools import os +import warnings import pytest @@ -396,6 +397,7 @@ def test_labels_and_colors(): G, pos, edgelist=[(4, 5), (5, 6), (6, 7), (7, 4)], + arrows=True, min_source_margin=0.5, min_target_margin=0.75, width=8, @@ -752,3 +754,38 @@ def test_draw_networkx_edges_undirected_selfloop_colors(): for fap, clr, slp in zip(ax.patches, edge_colors[-3:], sl_points): assert fap.get_path().contains_point(slp) assert mpl.colors.same_color(fap.get_edgecolor(), clr) + plt.delaxes(ax) + + +@pytest.mark.parametrize( + "fap_only_kwarg", # Non-default values for kwargs that only apply to FAPs + ( + {"arrowstyle": "-"}, + {"arrowsize": 20}, + {"connectionstyle": "arc3,rad=0.2"}, + {"min_source_margin": 10}, + {"min_target_margin": 10}, + ), +) +def test_user_warnings_for_unused_edge_drawing_kwargs(fap_only_kwarg): + """Users should get a warning when they specify a non-default value for + one of the kwargs that applies only to edges drawn with FancyArrowPatches, + but FancyArrowPatches aren't being used under the hood.""" + G = nx.path_graph(3) + pos = {n: (n, n) for n in G} + fig, ax = plt.subplots() + # By default, an undirected graph will use LineCollection to represent + # the edges + kwarg_name = list(fap_only_kwarg.keys())[0] + with pytest.warns( + UserWarning, match=f"\n\nThe {kwarg_name} keyword argument is not applicable" + ): + nx.draw_networkx_edges(G, pos, ax=ax, **fap_only_kwarg) + # FancyArrowPatches are always used when `arrows=True` is specified. + # Check that warnings are *not* raised in this case + with warnings.catch_warnings(): + # Escalate warnings -> errors so tests fail if warnings are raised + warnings.simplefilter("error") + nx.draw_networkx_edges(G, pos, ax=ax, arrows=True, **fap_only_kwarg) + + plt.delaxes(ax) |