scipy-cluster 生成的树状图不显示

发布于 2024-09-04 06:26:46 字数 576 浏览 6 评论 0原文

我正在使用 scipy-cluster 对某些数据生成层次聚类。作为应用程序的最后一步,我将 dendrogram函数绘制聚类图。我使用内置的 Python 2.6.1 和 在 Mac OS X Snow Leopard 上运行这个 matplotlib 包。程序运行良好,但最后火箭飞船图标(据我所知,这是 python 中 GUI 应用程序的启动器)出现并立即消失,没有执行任何操作。什么也没显示。如果我在调用后添加“raw_input”,它就会永远在扩展坞中上下弹跳。如果我从终端运行 matplotlib 的简单示例应用程序,它运行良好。有人有这方面的经验吗?

I am using scipy-cluster to generate a hierarchical clustering on some data. As a final step of the application, I call the dendrogram function to plot the clustering. I am running on Mac OS X Snow Leopard using the built-in Python 2.6.1 and this matplotlib package. The program runs fine, but at the end the Rocket Ship icon (as I understand, this is the launcher for GUI applications in python) shows up and vanishes immediately without doing anything. Nothing is shown. If I add a 'raw_input' after the call, it just bounces up and down in the dock forever. If I run a simple sample application for matplotlib from the terminal it runs fine. Does anyone have any experiences on this?

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

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

发布评论

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

评论(3

感情旳空白 2024-09-11 06:26:46

我在 Ubuntu 10.04 上也遇到了同样的问题。
为了从 ipython 交互式控制台显示图形,请使用“-pylab”开关启动它,这可以交互式使用 matplotlib:

ipython -pylab

要在独立脚本执行期间显示图形,请使用 matplotlib.pyplot.show 调用。这是来自 hcluster 主页的示例,第一行和最后一行是此处的有效位:

from matplotlib.pyplot import show

from hcluster import pdist, linkage, dendrogram
import numpy
from numpy.random import rand

X = rand(10,100)
X[0:5,:] *= 2
Y = pdist(X)
Z = linkage(Y)
dendrogram(Z)

show()

I had the same issue on Ubuntu 10.04.
In order to get graphics to display from ipython interactive console, start it with "-pylab" switch, which enables the interactive use of matplotlib:

ipython -pylab

To get your graphics to display during the execution of a standalone script, use matplotlib.pyplot.show call. Here's an example from hcluster homepage, the first and last line are the significant bits here:

from matplotlib.pyplot import show

from hcluster import pdist, linkage, dendrogram
import numpy
from numpy.random import rand

X = rand(10,100)
X[0:5,:] *= 2
Y = pdist(X)
Z = linkage(Y)
dendrogram(Z)

show()
鸩远一方 2024-09-11 06:26:46

使用“-pylab”开关调用 ipython 对我来说没有什么区别。
(系统:Fedora 13)

虽然并不理想,但我的解决方案是将结果图显式写入文件。
例如:

...
dendrogram(Z)
pylab.savefig( "temp.png" )

希望这对遇到同样问题的人有所帮助。

修正:在 hcluster 包的简短教程中简单使用复制粘贴时要小心,特别是如果您在教程中显示的几种类型的树状图绘制之后调用 pylab.savefig() ,即,则

distMat = # whatever distance matrix you have
dendrogram( linkage( distMat ) )
pylab.savefig( "exampleDendrogram.png" )
dendrogram( linkage( distMat, method="complete" ) ) #instead of default "single"
pylab.savefig( "exampleDendrogram.png" )

exampleDendrogram.png 将包含同一张图中的单连锁树状图和全连锁树状图,它们很可能会交叉,看起来很乱。

如果你像我一样愚蠢,你会花 30-180 分钟对如何正确使用 hcluster 感到困惑,而实际上这只是在树状图调用之间重置 matplotlib 的问题:

distMat = # whatever distance matrix you have
dendrogram( linkage( distMat ) )
pylab.savefig( "exampleDendrogram1.png" )
pylab.cla()
dendrogram( linkage( distMat, method="complete" ) ) #instead of default "single"
pylab.savefig( "exampleDendrogram2.png" )

现在,生成的树状图图像文件将如下所示你期望它们看起来像。

Invoking ipython with "-pylab" switch didn't make a difference for me.
(System: Fedora 13)

Though not ideal, my solution was to explicitly write the resulting figure as a file.
For example:

...
dendrogram(Z)
pylab.savefig( "temp.png" )

Hope this helps anyone who is running into the same issue.

Amendment: Be careful about simply using copy-and-paste with the hcluster package's brief tutorial, notably in that if you call pylab.savefig() after several types of dendrogram drawing shown in the tutorial, i.e.

distMat = # whatever distance matrix you have
dendrogram( linkage( distMat ) )
pylab.savefig( "exampleDendrogram.png" )
dendrogram( linkage( distMat, method="complete" ) ) #instead of default "single"
pylab.savefig( "exampleDendrogram.png" )

Then exampleDendrogram.png will contain both the single-linkage dendrogram and the complete-linkage dendrogram in the same figure, and they will likely cross-cross and look like a mess.

If you're as stupid as me, you'll spend 30-180 minutes in confusion about how to properly use hcluster, when it's actually just a matter of resetting matplotlib between dendrogram calls:

distMat = # whatever distance matrix you have
dendrogram( linkage( distMat ) )
pylab.savefig( "exampleDendrogram1.png" )
pylab.cla()
dendrogram( linkage( distMat, method="complete" ) ) #instead of default "single"
pylab.savefig( "exampleDendrogram2.png" )

Now, the resulting dendrogram image files will look like what you expected them to look like.

Bonjour°[大白 2024-09-11 06:26:46

我也遇到过同样的问题。您可以使用以下方法之一

  1. 使用 plt.show() :
    在 dedogram 之后使用 plt.show() ,这将显示绘图
    使用 plt.show

  2. 在 jupyter 笔记本中首先使用 %matplotlib 内联。这将显示执行后的情节。
    使用 matplotlib 内联

I have been facing the same problem. You can use one of the below methods

  1. Use plt.show() :
    use plt.show() after dedogram, this will show the plot
    using plt.show

  2. Use %matplotlib inline intially in jupyter notebook. this will show the plot after execution.
    Using matplotlib inline

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