Receiving key error on Networkx color_map












0














I'm having trouble getting the color_map to work with my networkx graph. It's fairly simply code but won't seem to work. I've looked through other similar threads but no the solutions don't seem to work.



I have data that look like this:



edgelist_manual = [{'source': 'ABE', 'target': 'ATL', 'value': 851},
{'source': 'ABE', 'target': 'BHM', 'value': 1},
{'source': 'ABE', 'target': 'CLE', 'value': 805}]

edgelist = pd.DataFrame(edgelist_manual)

nodelist_manual = [{'source': 'ABE', 'value': '4807', 'group': 0},
{'source': 'ABI', 'value': '2660', 'group': 4},
{'source': 'ABQ', 'value': '41146', 'group': 2}]

nodelist = pd.DataFrame(nodelist_manual)


I run the code below, but my color_map keep screwing up. I just get a key error on the 'group' reference.



import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt

nodelist = pd.read_csv('final_nodes.csv')
edgelist = pd.read_csv('final_edges.csv')

g = nx.Graph()

for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
for i, nlrow in nodelist.iterrows():
g.node[nlrow['source']].update(nlrow[1:].to_dict())

color_map = {0: 'r', 1:'b', 2:'r', 3:'b', 4:'r', 5:'b'}
colors = [color_map[g.node[node]['group']] for node in g]

nx.draw(g, node_color=colors)
ax = plt.gca()
ax.collections[0].set_edgecolor("#555555")
plt.show()


The only difference from this and my code is that rather than creating the data manually I'm loading it from .csv. I've checked for trailing whitespaces on the feature labels but nothing. I don't understand indices well so I wonder if those are messing it up. Any ideas?



Thanks!










share|improve this question


















  • 1




    Please provide the error message. Does this code you've provided also have the same error as your original code?
    – Joel
    Nov 20 at 21:00






  • 1




    Based on what you've said, I think the error is in the line colors = [...], and it's giving an error for g.node[node]['group']. An obvious step to take in debugging would be to see why it's giving you an error. So why not print g.node[node] for a few nodes? Given the error, this appears to not be what you expect it to be, perhaps once you see what it is, you'll figure out the error.
    – Joel
    Nov 21 at 0:35










  • Yeah I get the same problem with the above code. (I did change ABI and ABQ to ABE just so it's sensible, but same error). When I print g.node[node] I get NameError: name 'node' is not defined
    – Kees
    Nov 21 at 14:40






  • 1




    So when you're printing g.node[node], what is node defined to be (hint - I don't think you've defined it before that test)? It's defined in your list comprehension, but it won't be defined outside that. You could for example do for node in G: print(g.node[node])
    – Joel
    Nov 21 at 21:38












  • It would still be helpful if you provided the full error message. Please edit your question to include it.
    – Joel
    Nov 21 at 21:39
















0














I'm having trouble getting the color_map to work with my networkx graph. It's fairly simply code but won't seem to work. I've looked through other similar threads but no the solutions don't seem to work.



I have data that look like this:



edgelist_manual = [{'source': 'ABE', 'target': 'ATL', 'value': 851},
{'source': 'ABE', 'target': 'BHM', 'value': 1},
{'source': 'ABE', 'target': 'CLE', 'value': 805}]

edgelist = pd.DataFrame(edgelist_manual)

nodelist_manual = [{'source': 'ABE', 'value': '4807', 'group': 0},
{'source': 'ABI', 'value': '2660', 'group': 4},
{'source': 'ABQ', 'value': '41146', 'group': 2}]

nodelist = pd.DataFrame(nodelist_manual)


I run the code below, but my color_map keep screwing up. I just get a key error on the 'group' reference.



import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt

nodelist = pd.read_csv('final_nodes.csv')
edgelist = pd.read_csv('final_edges.csv')

g = nx.Graph()

for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
for i, nlrow in nodelist.iterrows():
g.node[nlrow['source']].update(nlrow[1:].to_dict())

color_map = {0: 'r', 1:'b', 2:'r', 3:'b', 4:'r', 5:'b'}
colors = [color_map[g.node[node]['group']] for node in g]

nx.draw(g, node_color=colors)
ax = plt.gca()
ax.collections[0].set_edgecolor("#555555")
plt.show()


The only difference from this and my code is that rather than creating the data manually I'm loading it from .csv. I've checked for trailing whitespaces on the feature labels but nothing. I don't understand indices well so I wonder if those are messing it up. Any ideas?



Thanks!










share|improve this question


















  • 1




    Please provide the error message. Does this code you've provided also have the same error as your original code?
    – Joel
    Nov 20 at 21:00






  • 1




    Based on what you've said, I think the error is in the line colors = [...], and it's giving an error for g.node[node]['group']. An obvious step to take in debugging would be to see why it's giving you an error. So why not print g.node[node] for a few nodes? Given the error, this appears to not be what you expect it to be, perhaps once you see what it is, you'll figure out the error.
    – Joel
    Nov 21 at 0:35










  • Yeah I get the same problem with the above code. (I did change ABI and ABQ to ABE just so it's sensible, but same error). When I print g.node[node] I get NameError: name 'node' is not defined
    – Kees
    Nov 21 at 14:40






  • 1




    So when you're printing g.node[node], what is node defined to be (hint - I don't think you've defined it before that test)? It's defined in your list comprehension, but it won't be defined outside that. You could for example do for node in G: print(g.node[node])
    – Joel
    Nov 21 at 21:38












  • It would still be helpful if you provided the full error message. Please edit your question to include it.
    – Joel
    Nov 21 at 21:39














0












0








0







I'm having trouble getting the color_map to work with my networkx graph. It's fairly simply code but won't seem to work. I've looked through other similar threads but no the solutions don't seem to work.



I have data that look like this:



edgelist_manual = [{'source': 'ABE', 'target': 'ATL', 'value': 851},
{'source': 'ABE', 'target': 'BHM', 'value': 1},
{'source': 'ABE', 'target': 'CLE', 'value': 805}]

edgelist = pd.DataFrame(edgelist_manual)

nodelist_manual = [{'source': 'ABE', 'value': '4807', 'group': 0},
{'source': 'ABI', 'value': '2660', 'group': 4},
{'source': 'ABQ', 'value': '41146', 'group': 2}]

nodelist = pd.DataFrame(nodelist_manual)


I run the code below, but my color_map keep screwing up. I just get a key error on the 'group' reference.



import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt

nodelist = pd.read_csv('final_nodes.csv')
edgelist = pd.read_csv('final_edges.csv')

g = nx.Graph()

for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
for i, nlrow in nodelist.iterrows():
g.node[nlrow['source']].update(nlrow[1:].to_dict())

color_map = {0: 'r', 1:'b', 2:'r', 3:'b', 4:'r', 5:'b'}
colors = [color_map[g.node[node]['group']] for node in g]

nx.draw(g, node_color=colors)
ax = plt.gca()
ax.collections[0].set_edgecolor("#555555")
plt.show()


The only difference from this and my code is that rather than creating the data manually I'm loading it from .csv. I've checked for trailing whitespaces on the feature labels but nothing. I don't understand indices well so I wonder if those are messing it up. Any ideas?



Thanks!










share|improve this question













I'm having trouble getting the color_map to work with my networkx graph. It's fairly simply code but won't seem to work. I've looked through other similar threads but no the solutions don't seem to work.



I have data that look like this:



edgelist_manual = [{'source': 'ABE', 'target': 'ATL', 'value': 851},
{'source': 'ABE', 'target': 'BHM', 'value': 1},
{'source': 'ABE', 'target': 'CLE', 'value': 805}]

edgelist = pd.DataFrame(edgelist_manual)

nodelist_manual = [{'source': 'ABE', 'value': '4807', 'group': 0},
{'source': 'ABI', 'value': '2660', 'group': 4},
{'source': 'ABQ', 'value': '41146', 'group': 2}]

nodelist = pd.DataFrame(nodelist_manual)


I run the code below, but my color_map keep screwing up. I just get a key error on the 'group' reference.



import itertools
import copy
import networkx as nx
import pandas as pd
import matplotlib.pyplot as plt

nodelist = pd.read_csv('final_nodes.csv')
edgelist = pd.read_csv('final_edges.csv')

g = nx.Graph()

for i, elrow in edgelist.iterrows():
g.add_edge(elrow[0], elrow[1], attr_dict=elrow[2:].to_dict())
for i, nlrow in nodelist.iterrows():
g.node[nlrow['source']].update(nlrow[1:].to_dict())

color_map = {0: 'r', 1:'b', 2:'r', 3:'b', 4:'r', 5:'b'}
colors = [color_map[g.node[node]['group']] for node in g]

nx.draw(g, node_color=colors)
ax = plt.gca()
ax.collections[0].set_edgecolor("#555555")
plt.show()


The only difference from this and my code is that rather than creating the data manually I'm loading it from .csv. I've checked for trailing whitespaces on the feature labels but nothing. I don't understand indices well so I wonder if those are messing it up. Any ideas?



Thanks!







networkx colormap






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 17:01









Kees

4717




4717








  • 1




    Please provide the error message. Does this code you've provided also have the same error as your original code?
    – Joel
    Nov 20 at 21:00






  • 1




    Based on what you've said, I think the error is in the line colors = [...], and it's giving an error for g.node[node]['group']. An obvious step to take in debugging would be to see why it's giving you an error. So why not print g.node[node] for a few nodes? Given the error, this appears to not be what you expect it to be, perhaps once you see what it is, you'll figure out the error.
    – Joel
    Nov 21 at 0:35










  • Yeah I get the same problem with the above code. (I did change ABI and ABQ to ABE just so it's sensible, but same error). When I print g.node[node] I get NameError: name 'node' is not defined
    – Kees
    Nov 21 at 14:40






  • 1




    So when you're printing g.node[node], what is node defined to be (hint - I don't think you've defined it before that test)? It's defined in your list comprehension, but it won't be defined outside that. You could for example do for node in G: print(g.node[node])
    – Joel
    Nov 21 at 21:38












  • It would still be helpful if you provided the full error message. Please edit your question to include it.
    – Joel
    Nov 21 at 21:39














  • 1




    Please provide the error message. Does this code you've provided also have the same error as your original code?
    – Joel
    Nov 20 at 21:00






  • 1




    Based on what you've said, I think the error is in the line colors = [...], and it's giving an error for g.node[node]['group']. An obvious step to take in debugging would be to see why it's giving you an error. So why not print g.node[node] for a few nodes? Given the error, this appears to not be what you expect it to be, perhaps once you see what it is, you'll figure out the error.
    – Joel
    Nov 21 at 0:35










  • Yeah I get the same problem with the above code. (I did change ABI and ABQ to ABE just so it's sensible, but same error). When I print g.node[node] I get NameError: name 'node' is not defined
    – Kees
    Nov 21 at 14:40






  • 1




    So when you're printing g.node[node], what is node defined to be (hint - I don't think you've defined it before that test)? It's defined in your list comprehension, but it won't be defined outside that. You could for example do for node in G: print(g.node[node])
    – Joel
    Nov 21 at 21:38












  • It would still be helpful if you provided the full error message. Please edit your question to include it.
    – Joel
    Nov 21 at 21:39








1




1




Please provide the error message. Does this code you've provided also have the same error as your original code?
– Joel
Nov 20 at 21:00




Please provide the error message. Does this code you've provided also have the same error as your original code?
– Joel
Nov 20 at 21:00




1




1




Based on what you've said, I think the error is in the line colors = [...], and it's giving an error for g.node[node]['group']. An obvious step to take in debugging would be to see why it's giving you an error. So why not print g.node[node] for a few nodes? Given the error, this appears to not be what you expect it to be, perhaps once you see what it is, you'll figure out the error.
– Joel
Nov 21 at 0:35




Based on what you've said, I think the error is in the line colors = [...], and it's giving an error for g.node[node]['group']. An obvious step to take in debugging would be to see why it's giving you an error. So why not print g.node[node] for a few nodes? Given the error, this appears to not be what you expect it to be, perhaps once you see what it is, you'll figure out the error.
– Joel
Nov 21 at 0:35












Yeah I get the same problem with the above code. (I did change ABI and ABQ to ABE just so it's sensible, but same error). When I print g.node[node] I get NameError: name 'node' is not defined
– Kees
Nov 21 at 14:40




Yeah I get the same problem with the above code. (I did change ABI and ABQ to ABE just so it's sensible, but same error). When I print g.node[node] I get NameError: name 'node' is not defined
– Kees
Nov 21 at 14:40




1




1




So when you're printing g.node[node], what is node defined to be (hint - I don't think you've defined it before that test)? It's defined in your list comprehension, but it won't be defined outside that. You could for example do for node in G: print(g.node[node])
– Joel
Nov 21 at 21:38






So when you're printing g.node[node], what is node defined to be (hint - I don't think you've defined it before that test)? It's defined in your list comprehension, but it won't be defined outside that. You could for example do for node in G: print(g.node[node])
– Joel
Nov 21 at 21:38














It would still be helpful if you provided the full error message. Please edit your question to include it.
– Joel
Nov 21 at 21:39




It would still be helpful if you provided the full error message. Please edit your question to include it.
– Joel
Nov 21 at 21:39

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397955%2freceiving-key-error-on-networkx-color-map%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397955%2freceiving-key-error-on-networkx-color-map%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

To store a contact into the json file from server.js file using a class in NodeJS

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen