Pyroot : SaveAs() riddle - cannot save canvas in png format

Please provide the following information:


ROOT Version : 6.12/06
Platform, compiler : CentOS 7.5, gcc4.8.5, python 3.6.5


Hi, in a pyroot macro I’m saving a Canvas in pdf, png and jpg format.

Here an excerpt of the code:

from ROOT import TF1, TF2, TH1, TH2, TH2F, TAxis, TMath, TEllipse, TStyle, TFile, TColor, TSpectrum, TCanvas, TPad, TVirtualFitter, gStyle
import numpy as np 
import sys
import time 

[...] 

pad1.cd()
histo.Draw("colz") 
pad1.Update()
pad2.cd()
histo_zoom.Draw("colz")
pad2.Update()

can.SaveAs("test.pdf")
can.SaveAs("test.png")
can.SaveAs("test.jpg")

It works for pdf and jpg, but not for png. A file test.png is created, but it does not contain the image and does not seem to be a png file. What might be the reason?

If I save manually through the saveas option in the canvas menu it works just fine.

Thanks for looking into the riddle!

I just tried with a macro I have and I have no problem to generate the png. Can you provide a small example (we can run) reproducing the problem ?

Hey @couet,
this is the example which on my machine reproduces the error:



from ROOT import TF1, TF2, TH1, TH2, TH2D, TAxis, TMath, TEllipse, TStyle, TFile, TColor, TSpectrum, TCanvas, TPad, TVirtualFitter
import numpy as np 
import sys
import time 

# Upper and lower bounds of the 2D histogram and the 2D function: 
x0 = 0. 
x1 = 100. 
y0 = 0. 
y1 = 100. 

# Simple 2D function to fill the histogram : 
xy = TF2( 'xy', '(x+3*y)', x0, x1, y0, y1)

# Create and fill histogram : 
bin_no = 11 
h3 = TH2D("h3", "fill the boxes", bin_no, x0, x1, bin_no, y0, y1) 
n_entries = 1000
h3.FillRandom("xy", n_entries)

# Set up canvas : 
w = 1400 
h =  700
can  = TCanvas("can", "histograms   ", w, h)
pad1 = TPad( 'pad1', 'Random fill of (x+3*y). ', 0.05, 0.05, 0.45, 0.95, -2)
pad2 = TPad( 'pad2', 'Random fill of (x+3*y).', 0.55, 0.05, 0.95, 0.95, -2 )
#TPad( name, title, xlow, ylow, xup, yup, color = -1, bordersize = -1, bordermode = -2 ) 	

# Draw the pads : 

pad2.Draw()
pad1.Draw()

# Draw the histograms  
pad1.cd()                               # Here the 'directory' is changed to pad1 defined above. 
h3.Draw('colz')
pad2.cd()
h3.Draw('box')
can.Update()                            # Shows the histogram in the Canvas without having to click on it. 

can.SaveAs("test.pdf")
can.SaveAs("test.png")
can.SaveAs("test.jpg")

It works for me. I get:

May be @etejedor as an idea what could be wrong in your case.

Okay, so it is probably my environment which has a quirk.

@etejedor: Could you have a look at the history of this post? I use a linux (centos) machine and for some reason I cannot SaveAs("*.png") with pyroot.

ROOT Version : 6.12/06
Platform, compiler : CentOS 7.5, gcc4.8.5, python 3.6.5

Hi @iMi,

I do not see how the fact that you use Python could make the difference here, since SaveAs ends up invoking C++ code and you say it works for other formats.

Can you check what happens when you invoke can.SaveAs("test.png") from C++ in your environment?

Cheers,
Enric

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.