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...")
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.
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?
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:
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).