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.

2 Answers 2

1

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 --

import matplotlib
matplotlib.use('AGG')
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.