Python interactive graphs library - python

I'm writing a software in Python for graphs creation and visualization. I can't find a graphic library to use (in addition to PyQt) to display a graph (different types of nodes with edges) and let the user modify it by adding and deleting nodes and edges. The idea is to manage graphs in a similar way to Dia, for example.

Related

How to draw readable, preferably interactive, network graphs with python?

I'm trying to draw a graph of any network running my script. I use scapy to collect packets and would like to have a node per computer communicating, and an edge per connection.
The issue is I can't find a way to visualize the graph well enough on my screen. So far combining networkx with matlib.pyplot managed to bring the best results, but it still seems pretty random and chaotic, the tags are hard to read, nodes are on top of each other, etc'. It is also preferable to have the ability to interact with the graph - move nodes around, hover over nodes/edges to get extra info, perhaps zoom in or even cluster together nodes so that when you click on the cluster you can see which nodes compose the cluster.
Since analyzing the network data and adding nodes&edges to the graph will be tedious for you to read, I'm adding only the relevant part here (the part that actually shows the graph I built):
pos = nx.spring_layout(Graph, scale=2)
edge_labels = nx.get_edge_attributes(Graph, "Protocol")
nx.draw(Graph,pos, with_labels=True, node_size=600, font_size=8, font_weight='bold')
nx.draw_networkx_edge_labels(Graph, pos, edge_labels=edge_labels, font_size=8)
plt.show()
(I imported networks as nx and matplotlib.pyplot as plt)
I also tried graphviz, ploty and bokeh but couldn't really make them work and after troubleshooting on Google got the impression that anyway they won't fix my problem, and I also tried adjustText - but I could not manage to fit it in my code in any way (can't find how to get the text attribute of my graph) and Holoviews - but it refuses to show an image no matter what I try (even if I copy and paste examples from their site - either python says that '%opts...' is invalid syntax, or if I try changing options any other way the code just runs until it ends and doesn't show anything on the screen.
This is what the graph looks like:
I'm finding a lot of partial solutions online so none of them work, does anybody has a comprehensive solution?
Drawing heavy graphs with plt can be a bit problematic, the problem here is not only with the data, it is also a problem for a human eye to get a lot of information in one look.
My suggestion is to use a more advanced graph visualization library, for example, ipycytoscape. you can define also styles and more features with it that will match your demands
from ipycytoscape import CytoscapeWidget
graph_draw = ipycytoscape.CytoscapeWidget()
graph_draw.graph.add_graph_from_networkx(nx_graph, directed=True)
In addition, if you will use CytoscapeWidget you can interact with the graph and match the focus of the view to the part in the graph that interests you the most.
You can tune the hyper-parameters (k and iterations) of the nx.spring_layout to arrange the nodes. Once you tune the parameters, the connected nodes will be close to each other, and not-contacted nodes will maintain a maximum possible distance.
pos = nx.spring_layout(G,k=0.1, iterations=20)

How to efficiently animate a Graphviz graph?

I would like to animate a Graphviz generated graph, changing the node labels and the node and edge colors.
I was creating the graphs using this Python interface for Graphviz. The only way to do that seems to be to create every image independently (although the changes between frames would be very small) and join all the images into an animation, as explained here.
Is there a more efficient way to create the animation, avoiding the creation of all these images independently?
There is also d3-graphviz, which takes a full dot graph description per animation step, converts it into an SVG, and then uses JavaScript to convert one graph into the other, and to inject animation.
You can see an example here.
There is a python package (GraphvizAnim) to do so.
Being in alpha only, but non the less looking quite decent.

Dyamic rendering of graph API for python

I wish to find a library which is able to render the evolution of a graph during the processing phase. Therefore you would be able to see the graph visually growing and/or pruned according to the current results processed by the classes. Is there any library for python which has the following requirements?
Assuming you are talking about a changing dataset (as opposed to the graph-theoretic type) matplotlib can do this. The documentation for the animation functionality is lengthy, but there are many examples at:
http://matplotlib.org/examples/animation/
If you are talking about the other type of a "graph", you can use networkx to manipulate and render the graph with fixed positions. Underneath it uses matplotlib as its drawing backend.

Drawing clustered graphs in Python

I already have a way of clustering my graph, so the process of clustering isn't the issue here. What I want to do is, once we have all the nodes clustered - to draw the clustered graph in Python, something like this:
I looked into networkx, igraph and graph-tool, but they seem to do the clustering, but not the drawing. Any ideas and propositions of what library should I use for drawing the already clustered graph, which will minimize the number of crossing links?
Take a look at GraphViz
http://www.graphviz.org/Gallery/directed/cluster.html
There's a Python binding for that, but I have to say I always create the text files directly as they're easy enough to write. Don't be fooled by the plain-looking examples, every aspect of your graph is highly customizable and you can make some pretty nifty graph visualizations with it. Not sure about nested clusters though, never tried that out.

Best python UI package for simple graph simulations (TSP simulation, etc...)

I've never done any UI programming in python before. What is the best (read most intuitive, easy to use, functional) UI package for python for doing simulations?
I'll be doing a simulation of TSP right now. So I'll have a graph (nodes and edges) where the edges are rapidly changing, along with some selection boxes to choose different styles of the algorithm, choose the number of nodes, etc..
I've already written this code with a command line interface and I'm hoping for something pretty seamless to port in a gui :)
I am not sure what you mean by "simulations" since the type of UI you want to do depends on what you simulate. But if you want to visualize graphs, networkx is pretty cool.
Such a simulation could be easily coded using:
networkx - for the graph data structures and algorithms
matplotlib - which is used by networkx to visualize graphs
Some GUI framework - PyQt, for instance, for the display and interaction with the user
What's cool is that these can be learned and tried separately. networkx is very powerful and can provide anything you need graph-vise. It works well with matplotlib, and you can show the steps of TSP by different colorings of edges/nodes. matplotlib can also be easily integrated with PyQt to put it all into a single interactive program.
Colleagues of mine are working on a similar-sounding setup to you - they use http://matplotlib.sourceforge.net/ and PyQt - PyQt can add a matplotlib object easily as a widget so the two integrate very well. A tutorial for PyQt is available here: http://www.zetcode.com/tutorials/pyqt4/
If you have graphs, you should definitely check out PyGraphviz (the interface is pretty similar to the aforementioned networkx)

Categories