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?

VividD
  • 10,456
  • 6
  • 64
  • 111
etzourid
  • 331
  • 4
  • 18
  • 2
    Looks like the library you're using is not yet mature enough to support advanced usage. You can adapt it to your own needs perhaps. – flup Jul 12 '13 at 18:22

3 Answers3

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.

Loïc Séguin-C.
  • 1,806
  • 2
  • 13
  • 14
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!

Mike Dewar
  • 10,945
  • 14
  • 49
  • 65
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.

Pranjal Mittal
  • 10,772
  • 18
  • 74
  • 99
  • I already tried matplotlib, but I was looking for something more fancy and also interactive – etzourid Jul 15 '13 at 18:46
  • 1
    @etzourid: Also have a look at the non-JavaScript manifestation of Cytoscape [here](http://cytoscape.org). It is interactive,well maintained, funded project. And if you have no issues with using Javascript or the Browser,then you should definitely try some JS libraries I mentioned in my answer. I have tried Arbor JS for some real network representation [experiment](http://webfuel.co.in/pranjal/cluster-mapping/), it is really interactive & cool to use, albeit a less maintained library. – Pranjal Mittal Jul 15 '13 at 21:28