changing arrowhead type in networkx

1,039 views
Skip to first unread message

Muhammad Yasir

unread,
Nov 12, 2017, 8:33:59 AM11/12/17
to networkx-discuss
Hi there,

I am plotting directed graph using networkx in python. However, I found that arrow head of edge is thick from one end instead of pointed arrow. I want to change the thick edge to pointed arrow. Here is my code, actual output, and desired output:


import networkx as nx
import matplotlib.pyplot as plt

G=nx.DiGraph()

item = [1,2]

G.add_edge(*item) #color = item[-1], weight = 2)

pos = nx.circular_layout(G)    
nx.draw(G, pos, with_labels = True, edge_color = 'b')   
plt.show()
Actual output:

Desired output



Any suggestion would be really appreciated. Thanks




dzinoviev

unread,
Nov 12, 2017, 12:25:20 PM11/12/17
to networkx...@googlegroups.com
There was some discussion about the arrowheads on stackoverflow recently: https://stackoverflow.com/questions/46356285/why-does-networkx-draw-directed-graph-edges-with-stubs



Dmitry Zinoviev, Professor, Suffolk University 
Author of Data Science Essentials in Python 
--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discu...@googlegroups.com.
To post to this group, send email to networkx...@googlegroups.com.
Visit this group at https://groups.google.com/group/networkx-discuss.
For more options, visit https://groups.google.com/d/optout.

satyam mukherjee

unread,
Nov 12, 2017, 12:56:43 PM11/12/17
to networkx...@googlegroups.com
I had made some changes in the original pylab code. Please find the attached file and check def draw_networkx_edges( 

The change is made here :

### Added by Satyam Mukherjee

        if G.is_directed() and arrows:

                edge_collection = LineCollection(edge_pos,

                                                                                 colors       = edge_colors,

                                                                                 linewidths   = lw,

                                                                                 antialiaseds = (1,),

                                                                                 linestyle    = style,

                                                                                 transOffset = ax.transData,

                                                                                 )

                edge_collection.set_alpha(alpha)

        #else:

                arrows=[]

                for src,dst in edge_pos:

                        x1,y1=src

                        x2,y2=dst

                        dx=x2-x1 # x offset

                        dy=y2-y1 # y offset

                        vu_x=dx/sqrt(dx*dx+dy*dy)#direction in arc sense

                        vu_y=dy/sqrt(dx*dx+dy*dy)

                        p=0.03 

                        arrows.append(FancyArrow(x1,y1,dx-p*vu_x,dy-p*vu_y,head_width=p,length_includes_head=True,).get_verts())

                        #arrows.append(Arrow(x1,y1,dx,dy, width=lw,length_includes_head=True,).get_verts())

                arrow_collection = PolyCollection(arrows,

                                                                                 facecolors = edge_colors,

                                                                                 antialiaseds = (1,),

                                                                                 transOffset = ax.transData,

                                                                                 linewidths   = lw,

                                                                                 linestyle    = style,


                                                                 )

        # update view        

        xx= [x for (x,y) in head+tail]        

        yy= [y for (x,y) in head+tail]        

        minx = amin(xx)

        maxx = amax(xx)

        miny = amin(yy)

        maxy = amax(yy)

        w = maxx-minx

        h = maxy-miny

        padx, pady = 0.05*w, 0.05*h

        corners = (minx-padx, miny-pady), (maxx+padx, maxy+pady)

        ax.update_datalim( corners)

        ax.autoscale_view()


        edge_collection.set_zorder(1) # edges go behind nodes            

        ax.add_collection(edge_collection)

        ## Added by Satyam Mukherjee

        if arrow_collection:

                arrow_collection.set_zorder(1) # edges go behind nodes            

                ax.add_collection(arrow_collection)


        return edge_collection



To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to networkx-discuss@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "networkx-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to networkx-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to networkx-discuss@googlegroups.com.



--
-------------------------------------------
WHEN LIFE GIVES U HUNDRED REASONS TO CRY,SHOW LIFE THAT U HAVE THOUSAND REASONS TO SMILE :-)

satyam mukherjee
224-436-3672 (Mob)
847-491-7238 (Off)
networkx_pylab_new.py
Reply all
Reply to author
Forward
0 new messages