When I draw a bunch of TH2D histograms with “colz” option on different pads of a canvas and save the canvas as a pdf and a png file, plots on the pdf file look weird. Colors get blended between adjacent bins and empty bins are treated unequally. See the blue and white strips between X=[-40, -20] for example. They don’t have equal width. It is better on the png in this regard, but it has problems too. When I view the histograms with TBrowser, everything looks fine.
Attaching a reproducer macro.
What is the solution for improving image quality in pdf ?
I think you have it backwards; running your macro, the problem is not in the PDF (the top image in your post), but in the PNG file (bottom image). A PNG is a raster image, i.e. composed by pixels, and in your case there are just not enough pixels for the number of bins in all the plots in the canvas, so some rows/columns get squished, resulting in artifacts. To avoid that, you need a bigger canvas (or make each plot in a separate canvas and PNG file, also big enough, and then outside ROOT join the images -without resizing- into a bigger one), or use histograms with fewer bins.
Unfortunately none of these options worked for me when drawing multiple histograms on the same canvas and when saving to a pdf. Increasing the canvas size increases the image quality for png but not for pdf. I think there is no option other than drawing and saving one plot at a time and then merging them later, which will make life not easier.
I don’t see any uneven spaces on the PDF file. Maybe you mean the page size is too small? You can enlarge it as much as you need and it should look fine, as I think ROOT creates the plots with vector elements. Anyway, if you want a larger page size in the PDF, one way is with these steps:
Set a larger paper size, e.g.
gStyle->SetPaperSize(50,50); // in cm; defaults are 20,26
Save as EPS (not PDF)
c1->SaveAs("th2d_histogram.eps");
Convert (outside ROOT) the EPS to PDF with ps2pdf; since the EPS contains a bounding box, the page size can be preserved with the option -dEPSCrop:
$ ps2pdf -dEPSCrop th2d_histogram.eps
(without the option, the canvas is cropped to fit your system’s default page size, probably A4 or letter).
@dastudillo@couet
Can I kindly ask you to attach here or somewhere else the PDF output that you get when you run the macro I provided after putting in your modifications ? I want to make sure we are talking about the same thing.
Are you referring to the “stroke” of the color boxes that makes the height of the white gaps (strips) look smaller in height than the blue bins (boxes)?
Yes, that was one of my main points. White strips being thinner than colored strips.
And I didn’t know that was a known issue since 2012. Now I need to go through that topic with 50 messages long to have an understanding of what’s going on. I attached two screenshots I took when viewing @couet 's PDF output at 1600% magnification on Okular viewer.
Well, @dastudillo’s suggestion works up to a degree, but not perfect. I can still see what you call the strokes when zoomed in 1600% in Okular. @ferhue 's latex suggestion, pdftex or pdflatex conversion did not work in my setup despite the fact that the packages are installed. @couet All of these can be due to buggy pdf viewers but I switched to Windows just to see how Acrobat Reader shows the plots and I could still see the strokes.
In any way, I would prefer a direct pdf output, rather than two-step process with ps2pdf or pdflatex.
Upon suggestion of @ferhue about defining a color palette with 99% opacity, in my original macro, when I add the following lines,
// Number of colors in the palette
Int_t nColors = 50;
Int_t palette[nColors];
// Define a custom color palette
for (Int_t i = 0; i < nColors; i++) {
Double_t red = (Double_t)i / (nColors - 1); // Gradient in red
Double_t green = 1 - red; // Gradient in green
Double_t blue = 0.5; // Fixed blue value
Double_t alpha = 0.99; // Transparency at 99%
// Create a new TColor for each palette entry
TColor *color = new TColor(5000 + i, red, green, blue, "", alpha); // Indices start at 5000+
palette[i] = 5000 + i; // Assign the color index
}
// Set the palette to the histogram
gStyle->SetPalette(nColors, palette);
gStyle->SetLineScalePS(1);
I get an output I’d be ok if the color palette were still kBird. But it gets broken. (See attachment)
Do you know an easy workaround that would restore a kBird looking palette ?