Home | News | Documentation | Download

Colored 2D histograms in pdf output

Hello all,
When I print colored 2D histograms into a pdf file, the result is quite ugly:
Very thin white lines are drawn between the bins as well as in the color palette.
This makes to me very ugly the final output. Any idea about this issue and how to get rid of it?
Here I attached a simple macro to make a 2D histogram and print it to a pdf.

{
  TCanvas *C = new TCanvas("C","",850,500);
  C->cd();
  
  gStyle->SetPadLeftMargin(0.08);  // Margin left axis 
  gStyle->SetPadRightMargin(0.12); // Margin for palettes in 2D histos
  gStyle->SetPalette(1);
  gStyle->SetNumberContours(64);

  TH2F *h2D = new TH2F("h2D","",100,-1.,1.,100,-1.,1.);
  for(Int_t i=0;i<1e7;i++) {
    Float_t x,y;
    gRandom->Rannor(x,y);
    h2D->Fill(x,y);
  }
  
  
  h2D->Draw("colz");
  
  C->Print("h2D.pdf");
  
}

I need to finish several official plots for a publication and the only thing I need is to fix this issue.
Thanks a lot!
h2D.pdf (61.6 KB)

I think it depends on the pdf viewer. Here in acrobat reader it looks fine, in evince it does not.

acroread h2D.pdf
xpdf -aaVector no h2D.pdf
(I don’t know how to disable vector anti-aliasing in evince.)
display -density 288 -resize 25% h2D.pdf
display -density 288 -resample 72 h2D.pdf
(Note: the “display” utility comes from the ImageMagick, 288 = 4 * 72 where 72 is the nominal density / resolution of the h2D.pdf, so resize down by 1/4 = 25% -> this is called “supersampling” -> not very elegant, introduces a bit of blurriness, but helps)

Oh! I see. I am using mac os x Preview. Other mac programs handling pdf do the same (keynote, pages…).
I have installed acrobat reader and it is true, the plot looks OK without the ugly white lines.
Following some or you suggestions I have figured out that in the preferences panel of mac Preview there is an option under PDF section which says : “Smooth text and line art”. If one deselects it, there is no troubles with the white lines. However, the plot doesn’t look nice in general provided that the fonts and lines are not longer smoothed.
There is no way to fix that from ROOT?
Let me ask you another thing. It is about the palette displayed at the right.
Is it possible to change its size and position in the pad? and what about adding a title to the axis?
Thanks!

UPDATE: OK, sorry, palette position and properties can be edit using this: root.cern.ch/root/html/TPaletteAxis.html
But… Can we have more than one palette in the same pad?

UPDATE 2: Yes, imagine you have to 2D histograms named h1 and h2, and both are drawn in the same pad with “z” option. Then, we can make space for the two palettes in the same pad by doing something like:

  h1->Draw("colz");
  h2->Draw("colz same");
  Float_t y1 = gPad->GetBottomMargin();
  Float_t y2 = 1 - gPad->GetTopMargin();
  gPad->Update();

  TPaletteAxis *palette = (TPaletteAxis*)h1->GetListOfFunctions()->FindObject("palette");
  palette->SetY2NDC(y2);
  palette->SetY1NDC(0.5*(y1+y2));
  
  TPaletteAxis *palette2 = (TPaletteAxis*)h2->GetListOfFunctions()->FindObject("palette");
  palette2->SetY2NDC(0.5*(y1+y2));
  palette2->SetY1NDC(y1);

see attached file
movepal.C (2.42 KB)

Hello Couet,

This was helpful, but how to change the size of the ticks in the displayed palette?
I didn’t find a method for that in the TPaletteAxis class…
Regards.

h_myHisto->GetZaxis()->SetTickLength(0.35);

Root.pdf (335 KB)Hello rooters,
I am coming back with the same issue that I commented in the first post of this thread:
The ugly rendering of coloured 2D histograms in pdfs produced by ROOT when opening them with Mac OS Preview.
In respect to this, it was pointed out that the ugly effect of the white lines among the bins is not present not in Acrobat reader nor in Preview when deselecting the ‘smooth text and line art’.
Fine, that is true, but I still would like to make them properly visible in Preview (and Keynote).
Preview is a key component in graphics handling for Mac, and since we are more and more mac users every day, I think it will be worth to fix this.
One can argue that this ugly rendering is not a ROOT, but a Mac problem with the pdfs, right?
However, this doesn’t happen with the same colored 2D histograms created by Matlab (for instance).
Here an example:

ROOT:
Root.pdf (335 KB)
Matlab:
Matlab.pdf (158 KB)

One must have a Mac and Preview (or Keynote) to really note the difference in quality between this two graphs.
Basically, the effect is that Preview draw very thin white lines among the bins of the 2D histograms made by ROOT, which makes the plot erratic and blurry, and very UGLY, when there are many bins. Because of that, on lose the advantage of vector graphics (like eps or pdf) in making presentations, papers, etc.
This doesn’t happen with the MATLAB 2D histograms, so it should be a ROOT problem…

I’ll appreciate any progress on this, I need to make a big poster (and a paper) with several colored 2D histograms which simply don’t look good currently with root.
Thanks!

I’ll check

can you generate PS with matlab ? if yes I would like to see it …

In eps:
Matlab.eps (108 KB)

In ps:
Matlab.ps (208 KB)

I investigated a bit more. My first idea was that may be there was a rounding problem and that the boxes generated by ROOT were not exactly adjacent and that’s why some kind of white lines appear. To check that I generated a simpler color plot as PS and visualize it with Preview. I saw also the lines between the box. Then I changed the color of two adjacent boxes, having a white line between them, to black (PS files are ASCII and easy to edit for somebody who knows PostScript language). Going to black the white lines disappeared between the two boxes ! So it was then clear that it is not a problem with the boxes geometry.
I then examined the Preview window more closely using the Mac utility “DigitalColor Meter”. It showed that these “white” transitions are not white but some of colors close to the neighbouring colours. It looked like a kind of “smoothing”. Pushing this idea further I looked in the Preview Preferences and found out in the PDF tab the “Viewing Documents” option called “Smooth text and line art”. It is on by default. If you turn it off, then these white lines disappear ! So now it is clear that there is a bug in the smoothing algorithm of Preview.
But that does not explain why the palette on your Matlab plot does not show these lines (I do not look at the rest of the plot because it is not produced using boxes, therefore it cannot be compared with the ROOT plot). But the palette seems to be drawn with boxes so we can compare. First of all the two palettes are build with very different colours. It would be good to have similar color maps to make real comparison. Then, I renew my request of having a PS file generated by Matlab because it is ASCII and easier to read.
I will now investigate on the web to see if there is any issue regarding “buggy smoothing with Preview”.

Ok … Matlab is cheating … it is not real vector postscript but a binary image included in a PS document therefore it looks like Preview is not able to smooth the image… or may be it does but in any case the file is not readable.

Looks like a know problem:

idlcoyote.com/ps_tips/maccolors.php

Some infos from apple even:

support.apple.com/kb/PH5908

The simplest way to bypass this Preview bug is to save your images as PNG files instead of PDF or PS files as Preview is not able to smooth raster files. (the Matlab PS file is raster anyway).

Hello Couet,
Thanks for this investigation. I already knew about the option “smooth text and lines”, but the result is again quite unsatisfactory… It would be really nice if the plots could be done in root just in the way Matlab does, which seem to be much more “compatible” with the different viewers (acrobat, evince, mac Preview, etc.).
On the other hand, of course one can always make a png (that is what I have been doing so far to present these plots), but again they will look much better in pdf. Another reason is for publications: eps or pdf files fit much better (and look nicer) in latex documents…
I attach here the root version of the plot with the same color palette than the Matlab version:
Root.pdf (453 KB)

PS: I don’t understand how is Matlab cheating. Lines and fonts really look scalable. Perhaps is just the color image (the actual 2D histogram), what is done in ‘plain’ format. If this is the case, would be possible to cheat in ROOT just in the same way that Matlab does?

Cheers!

Looking at the PS generated by Matlab I can tell they are not doing vector PostScript they include a bitmap. No way we will do that in ROOT.

Yes, and in any case when you embed a MatLab plot it is like embedding a PNG as they generate a bitmap.

if they are real vector file… not pseudo bitmap …

Agreed and assuming you are not visualizing them with the buggy Preview it will be ok.

yes that is what is done.

No, that’s a bug in Preview. other previewer are fine (acrobat, ghostview etc …)

Should we conclude then that it is a bug with Preview and do nothing?
Too bad: For me it is like losing vector graphic capabilities for my work when using mac computers.
No more pdfs in Keynote’s presentations, no more papers with coloured pdfs visible using Preview…
I am still thinking that it will be a good idea to have these hybrid graphics: They will be properly rendered by any viewer, while keeping scalable capabilities for fonts and line arts.

The PDF and PS files generated by ROOT are purely vector. Matlab does not generated vector but some hybrid.
I do not know why matlab did that… But should we change the ROOT code for a bug in an other package which, by the way might be fixed one day ? Plus implementing what you suggest is not an easy change and may have many weird side effects.

You are not losing vector graphics as matlab is not doing vector graphics…

PNG files are very good in Keynote etc … generate them large enough and scale them in Keynote.

Finally the PDF quality is still available in all other viewer include Acrobat. I remind you that PDF is an Adobe (acrobat author) standard. The bug will have shown in Acrobat I would have guessed that ROOT PDF has a problem but that’s not the case.

Fair enough, we leave it like it is.
Many thanks for the help!
Cheers!

ROOT provides many output formats just to make sure you always have a solution. As I said, in this case, you can simply use the png output for you keynote. I do not see what is wrong with that. You will not lose in quality if you generate large enough picture you then scale in keynote. I do that all the time. I have even done very large A0 posters that way and the definition is really good.