1

I am trying to visualize graphs generated from networkx, using d3py. I used the example provided (https://github.com/mikedewar/d3py/blob/master/examples/d3py_graph.py) but all I get is the graph without node names, how do I plot node names as well?

Also, how do I change edge and node colors?

4

3 回答 3

5

Here is a partial answer, but also read the caveat at the end.

The easiest thing is changing the node colour. If you run the example you pointed to with

with d3py.NetworkXFigure(G, width=500, height=500) as p:
    p += d3py.ForceLayout()
    p.css['.node'] = {'fill': 'blue', 'stroke': 'magenta'}
    p.show()

then you will have blue nodes with a magenta outline (you can use any html colour you like).

For edge colour, there is a hardcoded stroke: black; in the file d3py/geoms/graph.py. You can comment it out and reinstall d3py

line = {
    "stroke-width": "1px",
     "stroke": "black",
}
self.css[".link"] = line

Then you can specify edge colour and width as follows:

with d3py.NetworkXFigure(G, width=500, height=500) as p:
    p += d3py.ForceLayout()
    p.css['.node'] = {'fill': 'blue', 'stroke': 'magenta'}
    p.css['.link'] = {'stroke': 'red', 'stoke-width': '3px'}
    p.show()

There does not seem to be any way to add node labels easily with d3py. Using just d3.js, it can be done relatively easily (see this example). Which leads us to the major caveat...

Caveat

As @flup already mentioned, d3py does not seem really mature (the code from their Github repository does not even run the NetworkX example, there is a missing host parameter in networkx_figure.py). There has been no activity in the repo since a couple of months, so I guess the project is maybe abandoned. You could probably reach your goal much more easily using d3.js directly.

于 2013-07-12T18:53:48.303 回答
3

Just in case anyone ends up here looking around at d3py. I made d3py a couple of years ago now to see what it would feel like to plot d3 graphics from Python. It was fun! But I didn't have the time to do it properly, which was a shame.

However, Rob Story did! He made vincent, which is available at https://github.com/wrobstory/vincent. It's a much better implementation, and is much better thought out. Please do check it out!

于 2014-03-15T20:58:16.090 回答
1

Drawing 'static' graphs with networkx.

I would suggest trying mathplotlib. networkx itself recommends using matplotlib as a plotting supplement. It is easy to use and draws really nice graphs.

Say you have a networkx Graph "G". You can just plot it in 3 lines of code.

>>> import matplotlib.pyplot as plt
>>> nx.draw(G)
>>> plt.show()

Further, there is a flexibility of choosing between different layouts.

>>> nx.draw_random(G)
>>> nx.draw_circular(G)
>>> nx.draw_spectral(G)

Using an nx.draw*(G) command does not render the graph, until you use the plt.show(). It is as a "plot in the mind" or an abstract representation which you could customize before really rendering it.

Drawing interactive javascript / html5 graphs-

matplotlib is useful to draw static graphs. If you want to plot interactive Javascript graphs, then here are some interesting libraries (including D3.js)

This libraries are independent and their use with networkx is generally more useful in web applications where networkx code resides in the backend and the Javascript code lies in the front end.

于 2013-07-15T08:33:40.707 回答