pyROOT Interactivity and matplotlib

This question might be better suited for the matplotlib community, but I am more familiar with the ROOT community so I thought I would start here. I’ve run into an issue where the interactivity of the ROOT canvas after drawing something from a python script has stopped functioning. It has taken me some time, but I narrowed this down to including the pyplot package from matplotlib.

Using the following script the user can not click on any of the menus, or move items, etc. Commenting out the matplotlib resolves this issue, but the code base we are working on needs both packages. Has anyone run into this or know of a remedy?

import ROOT

#Importing matplotlib breaks interactivity!
import matplotlib.pyplot as plt

gLine = ROOT.TGraph()
gLine.SetPoint(0,0,1)
gLine.SetPoint(1,2,3)

gLine.Draw("ALP")
ROOT.gPad.Update()

raw_input("Press Enter to exit...")

Thanks!

Hi,

I can’t reproduce the issue. What ROOT version and platform did you observe this with?

If I log in to lxplus and do:

source /cvmfs/sft.cern.ch/lcg/views/LCG_91/x86_64-slc6-gcc62-opt/setup.sh
python yourcode.py

I can interact with the ROOT plot even if pyplot is imported. This uses ROOT 6.10.06, but it also works with the current master.

Cheers,
Enric

Ahh yes, sorry I left out the important version details.

macOS 10.13
OS: Darwin-17.0.0-x86_64-i386-64bit
python: 2.7.14 (default, Sep 27 2017, 12:15:00) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)]
matplotlib: 2.1.0
ROOT: 6.11/02

Generated with:

import platform
import sys

import matplotlib
import ROOT

if platform.system() == "Darwin":
    print "macOS", platform.mac_ver()[0]
print "OS:", platform.platform()
print "python:", sys.version
print "matplotlib:", matplotlib.__version__
print "ROOT:", ROOT.gROOT.GetVersion()

I believe I am using the Cocoa back-end, does anyone know how to check this from inside ROOT? I also, tried using ROOT 6.08.06, 6.10.08, and the master (6.13.01) with all of them giving the same result.

@couet any idea of what could be going on with the graphics here?

Actually there is internal check you can do to check which backend you are using . On all Unix like machines it is X11 … Except on Mac where it is Cocoa unless you explicitly install ROOT yourself using X11.

There is an internal check? Is it something accessible from gROOT or gSystem? In all the other issues we have discussed it always appears that I am using the Cocoa back-end on this machine, I’ve never explicitly gone after X11 and don’t have XQuartz running.

So the assumption is that the frozen canvas has something to do with a conflict between the Cocoa back-end and matplotlib?

Oops … mistyped … there is NO …

I assumed as much. It would be a nice addition.

An idea about the frozen canvas?

That would be useful on Mac only … and most of time it will now return “Cocoa”

To me il looks like more a python issue. Do you see the same problem with C++ ?

Sure, but I could definitively say which back-end I was using.

matplotlib is a python package. Not sure how I could reproduce the issue in C++.

:frowning: yes that way I hate using the python interface: it breaks the ROOT event loop … any way I found a script which does not froze the interface for me … don’t ask me why … sorry I am not an expert the ROOT python interface.

#!/usr/bin/env python
import ROOT

def main():
    from array import array
    ROOT.gStyle.SetTimeOffset(0);
    x = [1461168044L, 1461353937L, 1461583342L, 1461583828L, 1461590925L, 1461592425L, 1461595103L, 1461681830L, 1462790854L, 1463833176L]
    Hist = ROOT.TH1D("hist", "hist",len(x)-1, array("d",x))
    Hist.Draw("a")
    Hist.GetXaxis().SetTimeDisplay(1);
    Hist.GetXaxis().SetTimeFormat("%d-%m-%Y");
    Hist.SetFillColor(5)
    Hist.SetMarkerStyle(1)
    y = [50,60,100,75,80,65,90,70,80,67]
    for i in range(0,10):
        Hist.SetBinContent(i,y[i])
    Hist.Draw()
    raw_input()


if __name__ == "__main__":
    main()

The python interface does not “break the ROOT event loop” … there is no such thing. No matter what the interface, “something” must call the event loop. In root.exe, this is part of the same loop that waits for CLI input, but in Python, ROOT did not write the CLI input loop, so it has to be something else. Anyway, in plain English: “freezing” just means that the event loop isn’t called.

Mac is different in that it enforces the use of the inputhook rather than a GUI thread to feed events (Cocoa limitation). Most likely mathplotlib uses the Python inputhook as well and doesn’t play nice (PyROOT does, by calling the old hook).

Possibly, reinstalling the ROOT input hook after loading mathplotlib could work:

ROOT.InstallGUIEventInputHook()

The equivalent of:

ROOT.TVirtualX.Instance().InheritsFrom("TGCocoa")

is used throughout ROOT as an internal check.

This didn’t appear to have any effect.

I also, found this post after your suggestion about inputhooks. It did not resolve the issue either.

I found a “solution” on the forum, but it breaks the matplotlib plotting.

import matplotlib
#This line fixes the interactivity issue, but now matplotlib doesn't work (see below).
matplotlib.use('Agg')
import matplotlib.pyplot as plt

import ROOT

gLine = ROOT.TGraph()
gLine.SetPoint(0,0,1)
gLine.SetPoint(1,2,3)

gLine.Draw("ALP")
ROOT.gPad.Update()

#This plot never appears!
plt.plot((0,2), (1,3))
plt.show()

raw_input("Press Enter to exit...")

Well, you can’t use the GUI thread on Mac w/ Cocoa. Only other thing to try (although I see no reason why it should make a difference), is to trigger the ROOT “finalization” early:

import ROOT
ROOT.kRed              # force finalization
import matplotlib.pyplot as plt

Sorry, I’m not going to install ROOT on my Mac to try things out (I don’t support PyROOT anymore).

This didn’t work either.

I appreciate you providing some suggestions even though you aren’t supporting it.

Hi,

we could identify the cause of the issue. The problem arises when the _macosx library is loaded, namely:

from matplotlib.backends import _macosx

we are investigating.

Cheers,
Danilo

1 Like

Any progress on this? (Just bumping the thread to keep it from closing.)

Hi Karl,

thanks for the ping. Nothing conclusive yet.

Cheers,
Danilo