0

I need to change the default arrow style for digraphs on networkx. I've read something about matplotlib.patches.ArrowStyle, but I am too new coding and even newer in python, so I did not understand at all. My current code is:

G=nx.DiGraph()
n=len(C)
print(n)    
for i in range(n):
    G.add_node(i)

for i in nodos_usuarios:
    for j in nodos_cri:
        if Y[i,j].x==1:
            G.add_edge(i,j)

pos = nx.circular_layout(G)
x=G.node()
y=G.edges()
elist = edges
print(x)
print("")
print(y)
print("")
nx.draw_networkx_nodes(G, pos, label="ciudades",size=0.1)
nx.draw_networkx_labels(G, pos)
nx.draw_networkx_edges(G, pos, edgelist=elist, edge_color='green', width=3, label="S") 
plt.legend(fontsize = 'medium')
plt.axes().set_aspect('equal')
plt.savefig("Red.png")
plt.show()

Y is a matrix related to the graph G. My current output is: Ugly graph - Ugly arrow

So I need help to change this arrow style to the classical arrow that we all know. Is there someone could help me to do this more easily?

Bill Armstrong
  • 1,615
  • 3
  • 23
  • 47
Héctor Alonso
  • 181
  • 1
  • 2
  • 12
  • are you using a recent version of networkx? [https://networkx.github.io/documentation/latest/release/release_2.1.html](v2.1 release notes) indicate when it was overhauled – Bonlenfum Jul 19 '18 at 16:02
  • I guess not, how can I update my actual version of networkx using pip? – Héctor Alonso Jul 19 '18 at 20:15
  • I updated the version, now how can I use the arrow style? – Héctor Alonso Jul 19 '18 at 20:29
  • I think that any version before 2.1 doesn't have the new arrow style. Which one are you using? You can check by typing this on command line: `python -c "import networkx; print(networkx.__version__)"` – Bonlenfum Jul 28 '18 at 12:10
  • To update with pip, you need to explicitly use the `--upgrade` flag (with pip install), otherwise it keeps whatever version you had before, if there was a version installed – Bonlenfum Jul 28 '18 at 12:12

1 Answers1

0

Networkx has several attributes that you can use to modify your draw_networkx_edges. It looks like you've set the width to be disproportionate with the size of the plot. Without you're data, I can't reproduce, but if you reduce width=3 to something smaller or the overall size to something larger (in proportion to the width of the edges you should get a nicer looking graph.

Docs on networkx.drawing.nx_pylab.draw_networkx_edges. See width where the default is set to 1.0 as opposed to your override of 3.0.

Using a simple example from THIS source:

#draw with `width=1.0`

enter image description here

#draw with `width=3.0`

enter image description here

If you also consider your node size in this code nx.draw_networkx_nodes(G, pos, label="ciudades",size=0.1) (the 0.1) it is very small.

Changing Arrow Heads:

draw_networkx_edges() also has attributes for arrows which is default set to True and arrowstyle which provides for optional arrow head styles as set by matplotlib.patches.ArrowStyle. You can do something like this:

nx.draw_networkx_edges(
    G, pos, 
    edgelist=elist, 
    edge_color='green', 
    width=3, 
    label="S",
    arrowstyle='-|>')  #<-- This is the "CurvedFilledB" class

In the docs for ArrowStyle you can simply pass the attribute characteristics you'd like to modify into the draw_networkx_edges code. Without your data used to generate your image, it's difficult to tell what combination of these will provide the best result - easy enough to plug in different variables and see what happens.

Bill Armstrong
  • 1,615
  • 3
  • 23
  • 47
  • Thanks, that advice makes my plot better, but what about the arrows?, I still have the ugly arrows in my graph, do you know how to fix it? greetings. – Héctor Alonso Jul 19 '18 at 05:25
  • The correct link for the first URL posted is https://networkx.github.io/documentation/latest/reference/generated/networkx.drawing.nx_pylab.draw_networkx_edges.html?highlight=draw%20network%20edges#networkx.drawing.nx_pylab.draw_networkx_edges (there was an extraneous period in the URL). – Victoria Stuart Jun 04 '19 at 22:12