Repository: networkx/networkx

Issue:
`edgelist` is ignored when drawing selfloops
An issue with self-loop drawing was identified in the discussion in #4991. The `edgelist` parameter for `nx.draw_networkx_edges` is not properly handled for self-loop drawing.

### Steps to Reproduce

```python
>>> import matplotlib.pyplot as plt
>>> G = nx.path_graph(3)
>>> G.add_edge(0, 0)
>>> G.edges()
EdgeView([(0, 1), (0, 0), (1, 2)])
>>> nx.draw(G)  # Draws self-loop, as expected
>>> plt.figure();
>>> nx.draw(G, edgelist=[(0, 1), (1, 2)])  # Still draws self-loop (bug)
```

### Environment
<!--- Please provide details about your local environment -->
Python version: 3.9.6
NetworkX version: 2.6.2


### Additional context

At first glance, it seems the problem lies here:

https://github.com/networkx/networkx/blob/2eb274e39f712047cebf5666ee9caf2ba2e51ee4/networkx/drawing/nx_pylab.py#L853-L856

This code was added to ensure that self-loop edges are drawn even when edges are represented by a LineCollection object, but there should be some additional logic to check whether `edgelist` is `None` before setting it.


Discussion:

Patch:
diff --git a/networkx/drawing/nx_pylab.py b/networkx/drawing/nx_pylab.py
index e66d0c3a4..bbffab224 100644
--- a/networkx/drawing/nx_pylab.py
+++ b/networkx/drawing/nx_pylab.py
@@ -852,10 +852,10 @@ def draw_networkx_edges(
     # Draw the edges
     if use_linecollection:
         edge_viz_obj = _draw_networkx_edges_line_collection()
-        # Make sure selfloop edges are also drawn.
-        edgelist = list(nx.selfloop_edges(G))
-        if edgelist:
-            edge_pos = np.asarray([(pos[e[0]], pos[e[1]]) for e in edgelist])
+        # Make sure selfloop edges are also drawn
+        selfloops_to_draw = [loop for loop in nx.selfloop_edges(G) if loop in edgelist]
+        if selfloops_to_draw:
+            edge_pos = np.asarray([(pos[e[0]], pos[e[1]]) for e in selfloops_to_draw])
             arrowstyle = "-"
             _draw_networkx_edges_fancy_arrow_patch()
     else:


Test patch:
diff --git a/networkx/drawing/tests/test_pylab.py b/networkx/drawing/tests/test_pylab.py
index d533cebd1..cb79708b7 100644
--- a/networkx/drawing/tests/test_pylab.py
+++ b/networkx/drawing/tests/test_pylab.py
@@ -423,3 +423,13 @@ def test_draw_networkx_arrows_default_directed(drawing_func):
     )
     assert ax.patches
     plt.delaxes(ax)
+
+
+def test_edgelist_kwarg_not_ignored():
+    # See gh-4994
+    G = nx.path_graph(3)
+    G.add_edge(0, 0)
+    fig, ax = plt.subplots()
+    nx.draw(G, edgelist=[(0, 1), (1, 2)], ax=ax)  # Exclude self-loop from edgelist
+    assert not ax.patches
+    plt.delaxes(ax)