views:

47

answers:

1

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.

A: 

So, here is what I came up with for now:

class myplot(object):
    def __init__(self, filename):
        self._filename = filename

    def resetFileName(self, fileName):
        self._filename = fileName

    def __call__(self):
        matplotlib.pylab.savefig(self._filename)

plotfunction = myplot("foo.png")
matplotlib.pylab.draw_if_interactive = plotfunction

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.

Space_C0wb0y