PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, June 27, 2022

[FIXED] How to select a list of edges to draw in networkx.draw

 June 27, 2022     graph, matplotlib, networkx, plot, python     No comments   

Issue

I have a networkx graph with many edges and for this reason I want to select a subset that I want to draw. But there is strange behaviour.

import networkx as nx
G = nx.Graph()
G.add_edge(0,1,color=.1,weight=2)
G.add_edge(1,2,color=.4,weight=4)
G.add_edge(2,3,color=1.4,weight=6)
G.add_edge(3,4,color=2.4,weight=3)
G.add_edge(4,0,color=5.7,weight=1)

colors = nx.get_edge_attributes(G,'color').values()
weights = nx.get_edge_attributes(G,'weight').values()

pos = nx.circular_layout(G)

# This works:
nx.draw(G, pos, 
        edge_color=colors, 
        width=list(weights),
        with_labels=True,
        node_color='lightgreen',
       )
# This works too:
nx.draw(G, pos, 
        edge_color=colors, 
        width=list(weights),
        with_labels=True,
        node_color='lightgreen',
        edgelist=[(0,1),(1,2),(2,3),(3,4),(4,0)],
       )

This is the result. (I will add a colorbar later, so the colors can be interpreted).

Image that is drawn

# This however gives an error:
# ValueError: Invalid RGBA argument: 0.1
nx.draw(G, pos, 
        edge_color=colors, 
        width=list(weights),
        with_labels=True,
        node_color='lightgreen',
        edgelist=[(0,1),(1,2),(2,3),],
       )

Is there a way to prevent this error? It seems to me that this is bug. But maybe there is something that I miss.


Solution

You set colors = nx.get_edge_attributes(G,'color').values()

This gives dict_values([0.1, 5.7, 0.4, 1.4, 2.4])

draw is trying to match 5 values to only 3 edges

So like you said, you have to resize the colors dict



Answered By - shullaw
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing