embedding matplotlib in C++ and closing the matplotlib window

Hi,

I'm using matplotlib in a C++ app (with a qt4 gui), by embedding python with
boost::python. The C++ app calls Py_Initialize(), init_myplottingmodule(), and
boost::python::import("matplotlib.pylab") once on startup and certain GUI
events then fire up a matplotlib window via calls like this one:

// simplified version
void plotCurve(){
    clf();
    title("Diagonal line plot");
    double O1[] = {0,1};
    plot(std::vector<double>(O1,O1+2), std::vector<double>(O1,O1+2), "b:");
    show();
}

Although this does sort of work, it doesn't work quite right. Specifically on
clicking on the close button of the window, bad things frequently happen. For
example, the first close seems to work, but subsequent attempts to close newly
opened plots just zombify the window (it no longer repaints automatically but
stays open; replots happen to that same window, i.e. the one that was opened
second).

Now I'm somewhat aware of all the nastiness of different event loops, the
heroic efforts of ipython to make matplotlib work nicely interactively without
blocking the REPL, but somehow I would have (naively) thought that all that
work was just needed to avoid blocking. For the code above, blocking till the
window would be closed is perfectly fine. Indeed that's what happens if I try
something equivalent in the plain python shell (outside the C++ app).

Any idea about the likely cause of the problem and what might be the simplest
way to get it to work as expected (similar symptoms appear w/ matplotlib
0.87.7 and 0.91 under linux and osx)?

cheers,

'as

Eeeeew, that looks really dangerous. For starters, I would never try
to do this using pylab, but rather follow the lead of
examples/embedding_in_qt.py. Then you won't be competing for the
mainloop....

JDH

···

On 10/11/07, Alexander Schmolck <a.schmolck@...361...> wrote:

Hi,

I'm using matplotlib in a C++ app (with a qt4 gui), by embedding python with
boost::python. The C++ app calls Py_Initialize(), init_myplottingmodule(), and
boost::python::import("matplotlib.pylab") once on startup and certain GUI
events then fire up a matplotlib window via calls like this one:

// simplified version
void plotCurve(){
    clf();
    title("Diagonal line plot");
    double O1 = {0,1};
    plot(std::vector<double>(O1,O1+2), std::vector<double>(O1,O1+2), "b:");
    show();