graphing a relationship matrix in python
i have a python interface using wxpython which allows the user to fill in a matrix (0/1) and then graphs it for them. The program creates a numpy matrix, then makes a networkx graph out of that matrix, and then uses matplotlib.pylab to display the graph.
numpy is a must, because the program also does other things like get the transitive, reflexive, and symmetric closures... as for networkx i can use something else if u recommend something else better for graphing matrices, and as for matplotlib, i hate it, please if u know of any other way to display a graph please advice.
matplotlib is the source of my problem, when the users clicks the graph button, my programs reads the matrix, makes a graphs and matplotlib displays it in a new window (by default). Now if the users goes back to 开发者_如何转开发the original window and graphs a different matrix without first closing the matplotlib window, the program crashes.
also the way the relationship "arrows" are drawn is, in my opinion, unattractive.
i need a better way to graph my matrix, or at the very least as a way to force close the myplotlib window, i tried plt.close() but that didnt work, the window would remain open, and both windows will say (Not Responding) and i have to end process.
this is the part of the code in question:
import numpy as np
import networkx as nx
import matplotlib.pylab as plt
... ... ...
def graph(values)
plt.close() #with or without this it does not work
matrix = np.matrix(values)
graph = nx.DiGraph(matrix)
nx.draw(graph)
plt.show()
return
It seems to me like your main complaint is in the way that wx handles the matplotlib windows. It is possible to embed the Matplotlib figure in your wx window. Here's an example:
http://wiki.scipy.org/Matplotlib_figure_in_a_wx_panel (updated)
It gets a bit complicated. Basically, you should copy the code and replace the "DemoPlotPanel.draw()" method. You'll need to modify your code to specify the axis to draw on. It's buried in the networkx documentation here:
http://networkx.lanl.gov/reference/drawing.html
I am just using an example of the Networkx documentation:
try:
import matplotlib.pyplot as plt
except:
raise
import networkx as nx
G=nx.star_graph(20)
pos=nx.spring_layout(G)
nx.draw(G,pos)
plt.show() # display
So your plt.close() statement at the start does not make sense, you should remove it.You should also calculate the coordinates for your nodes, the sentence pos=nx.spring_layout(G) does this. You call a specific layout algorithm and supply your graph G, you get a dictionary in return with for each node the x and y coordinates.
Have a close look at the examples at: http://networkx.lanl.gov/gallery.html
精彩评论