Linestyle for contour ignored when canvas saved

Hi,

I’m observing a stange issue when trying to plot a contour line, derived from a TH2D, using PyROOT and saving the canvas in PDF. The same issue arises when the canvas is saved to a .C file and executed in CINT, so I do not believe this to be a PyROOT-specific issue.

What I’m attempting to do is create a contour based on a TH2D for which the line has to be shown dashed (line style 7). However, in the resulting PDF (but also eps or png) the line is always shown solid. If the result is saved as .C and executed interactively, the dashes are shown. However, a c->Print() statement in CINT yields the same result.

The code is as follows:

[code]#!/usr/bin/env python

import ROOT

ROOT.gROOT.SetBatch(True)

def DrawContourLine95( leg, hist, text="", linecolor=1, linestyle=7, linewidth=2):
pval = 0.05
signif = ROOT.TMath.NormQuantile(1-pval)

#contour plot
h = ROOT.TH2D(hist) 
h.SetContour(1)
h.SetContourLevel(0, signif)

h.SetLineColor( linecolor )
h.SetLineWidth( linewidth )
h.SetLineStyle( linestyle )
ROOT.SetOwnership(h, 0) # see https://root-forum.cern.ch/t/legend-not-drawn-from-function-in-pyroot/2769/1
h.Draw( "samecont3" )

canvas = ROOT.TCanvas(“c1”, “c1”)

f = ROOT.TFile.Open(“data/mSUGRA.merged.Nominal.root”, “READ”)
h = f.Get(“sigp1expclsf”)
h.SetDirectory(0)
f.Close()

DrawContourLine95(None, h, “”, ROOT.kRed, 7, 2)

l = ROOT.TLine(0, 0, 6000, 1000)
l.SetLineStyle(7)
l.Draw(“same”)

canvas.Print(“test.pdf”)[/code]

(Our data is a [0, 6000]x[0, 1000] size TH2D with significance levels.)

The resulting TLine is nicely drawn dashed, but for the contour it doesn’t work. I also observe the same issue for any line style except 3. Is this a known issue?

Kind regards,
Geert-Jan
test.pdf (15.4 KB)

I do not see this issue:

root [3] hpxpy->SetLineStyle(7)
root [4] hpxpy->Draw("cont3");

c1.pdf (23.9 KB)

That is intriguing. Do you see the issue with the .C macro attached? (This is the output of the script saved in .C form.)
test.C (402 KB)

Oh I see the reason why you get this effect.

Your histogram has 100x100 bins therefore the bin size on the screen is equal or smaller to the length of a dash in the line style 7. Contours are drawn by little piece in each bin. They are not one single polyline. As you have very small bins, the dashing effect disappear. If you enlarge the window you see the dashing reappearing. In your case I would recommend dotted lines.

So an approach would be to rebin this histogram to a smaller bin size and then it should be fixed? Is there a simple method by which I could rebin the histogram using averages? (They should not be added, of course.)

Alternatively, I would also be happy with the contour in TGraph format - I assume the problem would then go away. Is there an easy way of getting the line in that format?

To get the graphs see:

root.cern.ch/root/htmldoc/THist … html#HP16a

I’ve attempted that in the past, but attempting this:

h.Draw("CONT LIST") print ROOT.gROOT.GetListOfSpecials().FindObject("contours")

gives a null pointer: <ROOT.TObject object at 0x0>.

Look a the example I pointed:

[...]
  // Draw contours as filled regions, and Save points
   HistStreamFn->Draw("CONT Z LIST");
   c->Update(); // Needed to force the plotting and retrieve the contours in TGraphs
[...]

I apologise, I found the issue: the gPad.Update() call was missing. Now it works as expected. Thanks a lot for the help!

(This reply crossed with your last post.)