<p>This might help you</p>
<pre><code>for edge in G_nx.edges(data=True):
    color = &quot;black&quot;
    weight = edge[2][&quot;weight&quot;]
    if weight &gt; 0:
        color = &quot;red&quot;
    elif weight &lt; 0:
        color = &quot;blue&quot;
    edge[2][&quot;color&quot;] = color
    edge[2][&quot;penwidth&quot;] = abs(weight)
</code></pre>
<p>This gives you the following graph:</p>
<p><a href="https://i.sstatic.net/xa2yg.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xa2yg.png" alt="enter image description here" /></a></p>
<p>Depending on your network size this solution might take some time. &quot;penwidth&quot; is the graphviz information for the edge width.</p>

CC BY-SA 4.0
2021-10-29T23:32:05.350Z
Thank you veru much !! It works !!
Apppii092
CC BY-SA 4.0
2021-10-31T20:47:28.773Z
@Apppii092 if my answer solves your question could you please mark it as accepted?
Mr_Z
CC BY-SA 4.0
2021-10-28T20:02:34.657Z
2021-10-28T20:02:34.657Z
Mr_Z
<p>I am now drawing a weighted directed graph using Pygraphviz.</p>
<p>The adjacency matrix A of the weighted directed graph is</p>
<pre><code>A = pd.DataFrame({'X': [0.0,0.1,-0.8], 'Y': [-0.7,0.0,-0.1], 'Z': 
    [0.4,0,-0.1]} ,index=[&quot;X&quot;, &quot;Y&quot;, &quot;Z&quot;])
</code></pre>
<p><a href="https://i.sstatic.net/tpEZ4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/tpEZ4.png" alt="A" /></a></p>
<p>where A_{i,j} indicates the edge weight from the node i to the node j.</p>
<p>I was able to draw the graph without edge color.</p>
<pre><code>import networkx as nx
from networkx.drawing.nx_agraph import to_agraph
from IPython.display import SVG, display

G_nx = nx.from_pandas_adjacency(A,create_using=nx.DiGraph())
G_nx.graph['edge'] = {'arrowsize': '1.0', 'splines': 'curved'}
G_nx.graph['graph'] = {'scale': '3'}


Agraph_eg = to_agraph(G_nx)                                                              
Agraph_eg .node_attr[&quot;height&quot;] = 0.3
Agraph_eg .node_attr[&quot;width&quot;] = 0.3
Agraph_eg .node_attr[&quot;shape&quot;] = &quot;circle&quot;
Agraph_eg .node_attr[&quot;fixedsize&quot;] = &quot;true&quot;
Agraph_eg .node_attr[&quot;fontsize&quot;] = 8
Agraph_eg.layout(prog=&quot;neato&quot;)                                                                     
Agraph_eg.draw('graph_eg1.png')
</code></pre>
<p><a href="https://i.sstatic.net/k7jpY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/k7jpY.png" alt="graph_eg1.png" /></a></p>
<p>But I have no idea how to assign edge colors and widths according to the following two rules.</p>
<p>#1 If weights are more than 0, edge color should be red. If weight are less than 0, edge color should be blue.</p>
<p>#2 Edge widths should be proportional to the absolute values of weights.</p>
<p>Do you have any idea how to do this??</p>

<python><networks><graphs>
In Pygraphviz, How can I assign edge colors according to edge weights?
# In Pygraphviz, How can I assign edge colors according to edge weights?

(Asked by: username_0 on 2021-10-28)

I am now drawing a weighted directed graph using Pygraphviz.
The adjacency matrix A of the weighted directed graph is
A = pd.DataFrame({'X': [0.0,0.1,-0.8], 'Y': [-0.7,0.0,-0.1], 'Z': 
    [0.4,0,-0.1]} ,index=["X", "Y", "Z"])


where A_{i,j} indicates the edge weight from the node i to the node j.
I was able to draw the graph without edge color.
import networkx as nx
from networkx.drawing.nx_agraph import to_agraph
from IPython.display import SVG, display

G_nx = nx.from_pandas_adjacency(A,create_using=nx.DiGraph())
G_nx.graph['edge'] = {'arrowsize': '1.0', 'splines': 'curved'}
G_nx.graph['graph'] = {'scale': '3'}


Agraph_eg = to_agraph(G_nx)                                                              
Agraph_eg .node_attr["height"] = 0.3
Agraph_eg .node_attr["width"] = 0.3
Agraph_eg .node_attr["shape"] = "circle"
Agraph_eg .node_attr["fixedsize"] = "true"
Agraph_eg .node_attr["fontsize"] = 8
Agraph_eg.layout(prog="neato")                                                                     
Agraph_eg.draw('graph_eg1.png')


But I have no idea how to assign edge colors and widths according to the following two rules.
#1 If weights are more than 0, edge color should be red. If weight are less than 0, edge color should be blue.
#2 Edge widths should be proportional to the absolute values of weights.
Do you have any idea how to do this??

**Tags:** python, networks, graphs

---

## 1 answers

### Answer by username_1 on 2021-10-28. Score: 2 | Accepted answer

This might help you
for edge in G_nx.edges(data=True):
    color = "black"
    weight = edge[2]["weight"]
    if weight > 0:
        color = "red"
    elif weight < 0:
        color = "blue"
    edge[2]["color"] = color
    edge[2]["penwidth"] = abs(weight)

This gives you the following graph:

Depending on your network size this solution might take some time. "penwidth" is the graphviz information for the edge width.

> **Comments:**
> **username_0**: Thank you veru much !! It works !!  
> **username_1**: @Apppii092 if my answer solves your question could you please mark it as accepted?  

---

