用python绘制关系矩阵

发布于 2024-10-26 09:58:41 字数 900 浏览 1 评论 0原文

我有一个使用 wxpython 的 python 界面,它允许用户填写一个矩阵(0/1),然后为他们绘制图表。该程序创建一个 numpy 矩阵,然后根据该矩阵创建一个 networkx 图,然后使用 matplotlib.pylab 显示该图。

numpy 是必须的,因为该程序还可以执行其他操作,例如获取传递、自反和对称闭包...至于 networkx,如果您推荐其他更好的用于绘制矩阵的东西,我可以使用其他东西,至于 matplotlib,我讨厌如果您知道显示图表的任何其他方式,请提出建议。

matplotlib 是我的问题的根源,当用户单击图形按钮时,我的程序读取矩阵,制作图形,然后 matplotlib 将其显示在新窗口中(默认情况下)。现在,如果用户在没有先关闭 matplotlib 窗口的情况下返回到原始窗口并绘制不同的矩阵,程序就会崩溃。

在我看来,绘制关系“箭头”的方式也没有吸引力。

我需要一种更好的方法来绘制矩阵,或者至少作为强制关闭 myplotlib 窗口的一种方法,我尝试了 plt.close() 但这不起作用,窗口将保持打开状态,并且两个窗口都会说(不是响应),我必须结束进程。

这是有问题的代码的一部分:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-11-02 09:58:41

在我看来,您的主要抱怨是 wx 处理 matplotlib 窗口的方式。可以将 Matplotlib 图形嵌入到您的 wx 窗口中。这是一个例子:

http://wiki.scipy.org/Matplotlib_figure_in_a_wx_panel (已更新)

它变得有点复杂。基本上,您应该复制代码并替换“DemoPlotPanel.draw()”方法。您需要修改代码以指定要绘制的轴。它隐藏在此处的networkx文档中:

http://networkx.lanl.gov/reference/drawing.html

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

2024-11-02 09:58:41

我只是使用 Networkx 文档的一个示例:

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

所以你一开始的 plt.close() 语句没有意义,你应该删除它。你还应该计算节点的坐标,句子 pos=nx.spring_layout( G) 这样做。您调用特定的布局算法并提供图 G,您将获得一个字典作为回报,其中包含每个节点的 x 和 y 坐标。

仔细查看以下示例:http://networkx.lanl.gov/gallery.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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文