Yc @sdZddlZddlmZddlmZmZmZm Z m Z m Z ddddd d d d d dddg Z dddZdeedZdddddddddddd Zdddddddddedd ZdddddddddZddd dddddded! Zd"Zd#Zd$Zd%Zd&Zd'Zdddd(Zd)ZdS(*s ********** Matplotlib ********** Draw networks with matplotlib. See Also -------- matplotlib: http://matplotlib.org/ pygraphviz: http://pygraphviz.github.io/ iN(tis_string_like(t shell_layouttcircular_layouttkamada_kawai_layouttspectral_layoutt spring_layoutt random_layouttdrawt draw_networkxtdraw_networkx_nodestdraw_networkx_edgestdraw_networkx_labelstdraw_networkx_edge_labelst draw_circulartdraw_kamada_kawait draw_randomt draw_spectralt draw_springt draw_shellcKsyddlj}Wn5tk r2tdntk rJdGHnX|dkrf|j}n |j}|jd|dkr|jdkr|j d }q|j }nd|krd |k|d>> G = nx.dodecahedral_graph() >>> nx.draw(G) >>> nx.draw(G, pos=nx.spring_layout(G)) # use spring layout See Also -------- draw_networkx() draw_networkx_nodes() draw_networkx_edges() draw_networkx_labels() draw_networkx_edge_labels() Notes ----- This function has the same name as pylab.draw and pyplot.draw so beware when using >>> from networkx import * since you might overwrite the pylab.draw function. With pyplot use >>> import matplotlib.pyplot as plt >>> import networkx as nx >>> G = nx.dodecahedral_graph() >>> nx.draw(G) # networkx draw() >>> plt.draw() # pyplot draw() Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html iNsMatplotlib required for draw()s!Matplotlib unable to open displaytwiit with_labelstlabelstpostax(iiii(tmatplotlib.pyplottpyplott ImportErrort RuntimeErrortNonetgcft get_figuret set_facecolort_axstacktadd_axestgcaRt set_axis_offtdraw_if_interactive(tGRRtkwdstplttcf((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyR,s0;        cKsyddlj}Wn5tk r2tdntk rJdGHnX|dkrltjj|}nt|||}t ||d||}|rt |||n|j dS(sDraw the graph G using Matplotlib. Draw the graph with Matplotlib with options for node positions, labeling, titles, and many other drawing features. See draw() for simple drawing without labels or axes. Parameters ---------- G : graph A networkx graph pos : dictionary, optional A dictionary with nodes as keys and positions as values. If not specified a spring layout positioning will be computed. See :py:mod:`networkx.drawing.layout` for functions that compute node positions. arrows : bool, optional (default=True) For directed graphs, if True draw arrowheads. with_labels : bool, optional (default=True) Set to True to draw labels on the nodes. ax : Matplotlib Axes object, optional Draw the graph in the specified Matplotlib axes. nodelist : list, optional (default G.nodes()) Draw only specified nodes edgelist : list, optional (default=G.edges()) Draw only specified edges node_size : scalar or array, optional (default=300) Size of nodes. If an array is specified it must be the same length as nodelist. node_color : color string, or array of floats, (default='r') Node color. Can be a single color format string, or a sequence of colors with the same length as nodelist. If numeric values are specified they will be mapped to colors using the cmap and vmin,vmax parameters. See matplotlib.scatter for more details. node_shape : string, optional (default='o') The shape of the node. Specification is as matplotlib.scatter marker, one of 'so^>v>> G = nx.dodecahedral_graph() >>> nx.draw(G) >>> nx.draw(G, pos=nx.spring_layout(G)) # use spring layout >>> import matplotlib.pyplot as plt >>> limits = plt.axis('off') # turn of axis Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html See Also -------- draw() draw_networkx_nodes() draw_networkx_edges() draw_networkx_labels() draw_networkx_edge_labels() iNsMatplotlib required for draw()s!Matplotlib unable to open displaytarrows( RRRRRtnxtdrawingRR R R R$(R%RR)RR&R'tnode_collectiontedge_collection((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyRs{   i,trtog?c Ksddl}yddlj}ddl}Wn5tk rJtdntk rbdGHnX| dkr~|j} n|dkrt|}n| st |dkrdSy*|j g|D]}||^q}WnEt k r}t j d|n tk r't j dnXt||jr^t|||||| }d}n| j|dddf|dddfd |d |d |d |d |d| d|d| d|  }|jd|S(sDraw the nodes of the graph G. This draws only the nodes of the graph G. Parameters ---------- G : graph A networkx graph pos : dictionary A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2. ax : Matplotlib Axes object, optional Draw the graph in the specified Matplotlib axes. nodelist : list, optional Draw only specified nodes (default G.nodes()) node_size : scalar or array Size of nodes (default=300). If an array is specified it must be the same length as nodelist. node_color : color string, or array of floats Node color. Can be a single color format string (default='r'), or a sequence of colors with the same length as nodelist. If numeric values are specified they will be mapped to colors using the cmap and vmin,vmax parameters. See matplotlib.scatter for more details. node_shape : string The shape of the node. Specification is as matplotlib.scatter marker, one of 'so^>v>> G = nx.dodecahedral_graph() >>> nodes = nx.draw_networkx_nodes(G, pos=nx.spring_layout(G)) Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html See Also -------- draw() draw_networkx() draw_networkx_edges() draw_networkx_labels() draw_networkx_edge_labels() iNsMatplotlib required for draw()s!Matplotlib unable to open displayisNode %s has no position.sBad value in node positions.itstctmarkertcmaptvmintvmaxtalphat linewidthstlabeli(t collectionsRRtnumpyRRRR"tlisttlentasarraytKeyErrorR*t NetworkXErrort ValueErrort isinstancetIterablet apply_alphatscattert set_zorder(R%Rtnodelistt node_sizet node_colort node_shapeR6R3R4R5RR7R8R&R9R'tnptvtxyteR,((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyR sHV     *  /  tktsolidc 5 Ksy`ddl}ddlj}ddlj}ddlm}m}ddlm }ddl }Wn5t k rt dnt k rdGHnX| dkr|j} n|dkrt|j}n| st|dkrdS|jg|D]"}||d||df^q}|j|sD|f}n|}t| ra|j|rat|t|kra|jg|D]}t|^qrtg|D]}|j||^q}q|jg|D]}t| ^qrR|jg|D]'}|j|o.t|dk^q rIt|}q^d}qtd nBt|st|dkr|j||f}n td ||d |d|ddd|d| j}|jd|j| | j||j|r|j|n|dkr|dk rFt||sFt n|j!|j||j"||dk s| dk r|j#|| q|j$nd}|j%rV| rV|}g}d}x |D]\}} |\}!}"| \}#}$|#|!}%|$|"}&|j&t'|%d|&d}'|'dkr=qn|%dkr`|#}(|&||"})n|&dkr|$})|%||!}(nH|j(|&|%}*||'|j)|*|!}(||'|j*|*|"})|j+|(|)f|#|$ffqW||d |dg|D]}+d |+^qddd| j}|jd|j| | j|n|j,|j-|dddddf},|j.|j-|dddddf}-|j,|j-|dddddf}.|j.|j-|dddddf}/|-|,}0|/|.}1d|0d|1}2}3|,|2|.|3f|-|2|/|3ff}4| j/|4| j0|S(s\Draw the edges of the graph G. This draws only the edges of the graph G. Parameters ---------- G : graph A networkx graph pos : dictionary A dictionary with nodes as keys and positions as values. Positions should be sequences of length 2. edgelist : collection of edge tuples Draw only specified edges(default=G.edges()) width : float, or array of floats Line width of edges (default=1.0) edge_color : color string, or array of floats Edge color. Can be a single color format string (default='r'), or a sequence of colors with the same length as edgelist. If numeric values are specified they will be mapped to colors using the edge_cmap and edge_vmin,edge_vmax parameters. style : string Edge line style (default='solid') (solid|dashed|dotted,dashdot) alpha : float The edge transparency (default=1.0) edge_ cmap : Matplotlib colormap Colormap for mapping intensities of edges (default=None) edge_vmin,edge_vmax : floats Minimum and maximum for edge colormap scaling (default=None) ax : Matplotlib Axes object, optional Draw the graph in the specified Matplotlib axes. arrows : bool, optional (default=True) For directed graphs, if True draw arrowheads. label : [None| string] Label for legend Returns ------- matplotlib.collection.LineCollection `LineCollection` of the edges Notes ----- For directed graphs, "arrows" (actually just thicker stubs) are drawn at the head end. Arrows can be turned off with keyword arrows=False. Yes, it is ugly but drawing proper arrows with Matplotlib this way is tricky. Examples -------- >>> G = nx.dodecahedral_graph() >>> edges = nx.draw_networkx_edges(G, pos=nx.spring_layout(G)) Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html See Also -------- draw() draw_networkx() draw_networkx_nodes() draw_networkx_labels() draw_networkx_edge_labels() iN(tcolorConvertertColormap(tLineCollectionsMatplotlib required for draw()s!Matplotlib unable to open displayiiiis8edge_color must consist of either color names or numberss\edge_color must be a single color or list of exactly m colors where m is the number or edgestcolorsR7t antialiasedst linestylet transOffsetg?g?ig?(ii(ig?(i(1t matplotlibRRtmatplotlib.cbooktcbooktmatplotlib.colorsRPRQtmatplotlib.collectionsRRR:RRRR"R;tedgesR<R=titerableRtalltruettupletto_rgbaR@t transDataREt set_labeltadd_collectiont is_numliket set_alphaRAtAssertionErrort set_arraytset_cmaptset_climt autoscalet is_directedtsqrttfloattarctan2tcostsintappendtamintraveltamaxtupdate_datalimtautoscale_view(5R%Rtedgelisttwidtht edge_colortstyleR6t edge_cmapt edge_vmint edge_vmaxRR)R8R&RWR'tcbRPRQRRRJRMtedge_postlwR1t edge_colorsR-tarrow_collectiont arrow_colorsta_postptsrctdsttx1ty1tx2ty2tdxtdytdtxatyatthetatwwtminxtmaxxtminytmaxyRthtpadxtpadytcorners((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyR sW     8  (() 4               !   #    1111  (  i s sans-seriftnormalc Ksty"ddlj} ddlj} Wn5tk rAtdntk rYdGHnX| dkru| j} n|dkrtd|j D}n| j dd} | j dd}i}x|j D]\}}||\}}t |st |}n| j|||d |d |d |d |d |d| d|d| jd|dt }|||>> G = nx.dodecahedral_graph() >>> labels = nx.draw_networkx_labels(G, pos=nx.spring_layout(G)) Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html See Also -------- draw() draw_networkx() draw_networkx_nodes() draw_networkx_edges() draw_networkx_edge_labels() iNsMatplotlib required for draw()s!Matplotlib unable to open displaycss|]}||fVqdS(N((t.0tn((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pys sthorizontalalignmenttcentertverticalalignmenttsizetcolortfamilytweightR6t transformtbboxtclip_on(RRRXRYRRRR"tdicttnodestgettitemsRtstrttextRatTrue(R%RRt font_sizet font_colort font_familyt font_weightR6RRR&R'R~RRt text_itemsRR8txtytt((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyR ~sB?        g?i c !Ksy.ddlj} ddlj}ddl}Wn5tk rMtdntk redGHnX| dkr| j} n|dkrt d|j dt D}n|}i}x|j D]\\}}}||\}}||\}}|||d||||d|}}| r|j ||||d|jd }|d krr|d 8}n|d kr|d 7}n|j||f}| jj|j|f|jd"d}nd}| dkrt dddd#dd$} nt|st|}n| jdd}| jdd}| j|||d|d|d|d|d|d|d|d|d| jd| d d d!t  } | |||f>> G = nx.dodecahedral_graph() >>> edge_labels = nx.draw_networkx_edge_labels(G, pos=nx.spring_layout(G)) Also see the NetworkX drawing examples at https://networkx.github.io/documentation/latest/auto_examples/index.html See Also -------- draw() draw_networkx() draw_networkx_nodes() draw_networkx_edges() draw_networkx_labels() iNsMatplotlib required for draw()s!Matplotlib unable to open displaycss*|] \}}}||f|fVqdS(N((RtuRKR((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pys @stdatag?g@ihiZiiiiigtboxstyletroundtectfcRRRRRRRR6trotationRRtzorderR(ii(g?g?g?(g?g?g?(RRRXRYR:RRRR"RR\RRRntpitarrayRattransform_anglestreshapeRRRR(!R%Rt edge_labelst label_posRRRRR6RRtrotateR&R'R~RJRRtn1tn2R8RRRRRRtangleRLt trans_angleRRR((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyR slL    %)           cKst|t||dS(s8Draw the graph G with a circular layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. N(RR(R%tkwargs((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyR xs cKst|t||dS(sKDraw the graph G with a Kamada-Kawai force-directed layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. N(RR(R%R((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyRs cKst|t||dS(s6Draw the graph G with a random layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. N(RR(R%R((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyRs cKst|t||dS(s8Draw the graph G with a spectral layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. N(RR(R%R((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyRs cKst|t||dS(s6Draw the graph G with a spring layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. N(RR(R%R((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyRs cKsH|jdd}|dk r(|d=nt|t|d||dS(s6Draw networkx graph with shell layout. Parameters ---------- G : graph A networkx graph kwargs : optional keywords See networkx.draw_networkx() for a description of optional keywords, with the exception of the pos parameter which is not used by this function. tnlistN(RRRR(R%RR((so/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/drawing/nx_pylab.pyRs   cCs'ddl}ddl}y/ddl}ddlm} ddlj} Wntk rftdnXt|t|krt |d|j r| j d|} | j ||| j |} n^y|j| j |g} Wn<tk r&|jg|D]} | j | ^q} nXyt|t| ksW| jt|kr| jt|df| d | dddf<| d | dddf<| d | ddd fs . Z y  b      M