Despite the many past posts on how to achieve a transparent background in ROOT, I couldn’t find anything that works on my Fedora 25 / ROOT 6.10.02 installation.
Basically, I want to draw a graph, within a TFrame I specify (currently using canvas.DrawFrame(…)) and a TGaxis on a transparent background. I’m running into two difficulties:
making a pad transparent works as in the old instructions (FillStyle 4000). Not sure how to do it with a DrawFrame.
none of the examples of how to make the canvas transparent work for me. Neither on screen, nor in a png (I haven’t tested pdf thoroughly, but nothing worked there so far). For example:
$ root -bn
------------------------------------------------------------
| Welcome to ROOT 6.10/02 http://root.cern.ch |
| (c) 1995-2017, The ROOT Team |
| Built for linuxx8664gcc |
| From tag v6-10-02, 6 July 2017 |
| Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
------------------------------------------------------------
root [0] TCanvas c1
(TCanvas &) Name: c1 Title: c1
root [1] c1.SetFillColorAlpha(kRed,0.2)
root [2] c1.SaveAs("test.png")
Info in <TCanvas::Print>: file test.png has been created
Creates a white png, rather than a semi-transparent reddish png
I have all sort of partial workarounds with imagemagick scripts. But a transparent background would make things so much easier…
Hi @harelamnon. You can use an SVG format instead. I got the reddish transparency that you ask for. I’m using 6.13/01 though. EDIT: This format works great if you are working with PDF output (Like from LaTeX), as you will have vectorial images instead of raster images.
Thanks for your answer. Manually examining the SVG file, I see it has the correct “fill-opacity”!
Unfortunately, that doesn’t help me as I need to use these images in tools that fail to handle this semi-transparent SVG correctly. Namely PowerPoint, which can’t even read SVG files, and ImageMagick, which apparently ignores the “fill-opacity” field.
When I analyze the image you uploaded with ImageMagick’s display, it can’t even find an alpha channel. When I use it in powerpoint, it appears opaque. Running your code myself, I also get opaque images.
Maybe the image on your screen is semi-transparent, but the fact it isn’t on my screen is off-topic. I’m trying to figure out how to use ROOT to save an image file, such as a .png, with a semi-transparent image.
If you need a quick solution you can try to export SVG and then use something like: http://svgtopng.com/ All of us have different outputs because we are using different root versions.
Thanks, that’s a viable work-around!
I’m not used to other tools out-performing ImageMagick
(BTW: oddly, the alpha isn’t quite constant in this .png, but it’s
acceptable).
Meanwhile I used a workaround of plotting with matplotlib.
Their axes aren’t quite as nice as ROOT axes, but they’re acceptable.
Just in case someone googles this thread, the key lines I used for plotting
filled TGraphs in matplotlib are are something like:
We’re getting less and less ROOT-centric here, as this is a minor comment
on the output of http://svgtopng.com/
In the last image that Andres sent, the alpha values range from 32 to 51 -
they are not constant.
I see this by opening it with ImageMagick’s “display”; right clicking the
image, choosing “Image Info”,
and scrolling down to the “Alpha:” channel info.
This is merely an oddity - the mean is 50.9, so almost all the alpha values
are 51.