我在我的应用程序中使用 scipy-cluster 。它提供了绘制层次聚类树的树状图的功能。查看源代码,我发现它最终通过调用draw_if_interactive来绘制树状图。正如人们所期望的那样,这在交互式会话中工作得很好,但是当我以非交互式方式运行脚本时,会弹出一个窗口并立即再次消失(我已将 matplotlib 配置为使用 macosx
-backend)。我需要一种方法来让我的应用程序等到用户关闭显示绘图的窗口,或者让它直接渲染到文件中(实际上我更喜欢)。同样,问题是,我无法修改生成绘图的代码,因此解决方案可能会涉及 matplotlib 的一些配置设置或类似的内容。
编辑:我添加了当前的解决方法作为答案,以便其他人可以使用它。由于它非常丑陋,我将保留这个问题,希望有人能提出更好的解决方案。
I am using scipy-cluster in my application. It provides a function to plot a dendrogram of the hierarchical cluster tree. Looking at the source I find that it eventually plots the dendrogram by calling draw_if_interactive
. As one would expect, this works fine in an interactive session, but when I run the script non-interactively, a window pops up and immediately vanishes again (I have configured matplotlib to use the macosx
-backend). I need a way to either make my application wait until the user closes the window showing the plot, or to make it render directly into a file (which actually I would prefer). Again, the problem is, that I cannot modify the code that generates the plot, so the solution will probably involve some configuration settings for matplotlib or something like that.
EDIT: I added my current workaround as an answer, so others may use it. Since it is very ugly, I will leave this question open hoping for someone to come up with a better solution.
发布评论
评论(2)
所以,这就是我现在想到的:
这必须在导入 hcluster 模块后执行。这在很多方面都很丑陋,但至少(由于 Python 的动态特性)我不必修改源代码。它让我为每个图设置文件名,这正是我所需要的。
So, here is what I came up with for now:
This must be executed after the
hcluster
-module is imported. This is ugly in many ways, but at least (thanks to the dynamic nature of Python) I do not have to modify the source-code. It let's me set the filename for each plot, and that is just what I need.macosx 后端不正确支持非交互模式(即设置交互关闭无效)。您可能更喜欢使用 AGG 后端 -
The macosx backend doesn't support non-interactive mode properly (ie setting interactive off has no effect). You might prefer to use the AGG backend for this --