Home | News | Documentation | Download

Set transparent background of TCanvas in pyROOT

python

#1

Hi,

I am trying to generate a .png file with the transparent background histogram or any plot with pyROOT, but it doesn’t work for me as following setting:

import ROOT
c1 = ROOT.TCanvas("c1", "", 500, 500)
c1.SetFillStyle(4000) 
c1.SetFrameFillStyle(4000) 

h1 = ROOT.TH1F("h1", "", 10, 0, 1)
h1.Draw()

c1.Print("test.png")

Even I tried following setting, I get a white background .png instead of transparent.

c1.SetFillColorAlpha(1, 0.) # Use black to check if it goes transparent.
  • My environments :
    Python : 3.6
    ROOT : v6-14-02

Is it possible to have transparent background with pyROOT, or did I miss something?

Many thanks


#2

Hi @juifa-tsai

I can’t reproduce, with the code that you shared I get a canvas with a black background.

import ROOT
c1 = ROOT.TCanvas("c1", "", 500, 500)
c1.SetFillStyle(4000)
c1.SetFrameFillStyle(4000)

h1 = ROOT.TH1F("h1", "", 10, 0, 1)
rndmGenerator = ROOT.TRandom3()
for i in xrange(1000):
    rndm = rndmGenerator.Gaus()
    h1.Fill(rndm)
h1.Draw()

c1.SetFillColorAlpha(1, 0.)

c1.Print("test.png")

test

If you run the code above, your png image has white background?

Asking for the opinion of our graphics expert, @couet


#3

Hi @etejedor and @couet

Yes, I have tried your code, I got the white background as the attached file… Is the ROOT version issue or something about my environment?

test

Many thanks


#4

Hi @juifa-tsai

@couet will be back on Monday. Would it be an option for you to try with a newer ROOT version, in the meantime? What platform are you working on? Is it MacOS?

Enric