Re: [ROOT] TGraph & SetMinimum

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Feb 20 2003 - 19:15:29 MET


Hi Jeff,

Thanks for reporting this interesting sequence. I have fixed this problem
in the CVS version. Meanwhile, you can solve this problem by updating the
canvas after having drawn the 3rd plot
  //third plot
  c->cd(3);
  g3->Draw("AP");
  c->Update();  //add this line

The problem is that TGraph::GetAxis creates automatically the histogram
object for plotting and ignores the existing min/max settings.

Rene Brun

Jeff Hartnell wrote:
> 
> Hi Rene,
> A couple of months ago I discovered that a TGraph's SetMinimum/Maximum got
> overwritten when you set the title of the axis after plotting it. Thank you
> for fixing this.
> 
> I have discovered another similar feature (which has been bugging me (excuse
> pun) for a long time and took hours to track down!).
> 
> If you do the following:
> 1.) Create the graph
> 2.) Set it's min/max
> 3.) Fill it with points
> 4.) Plot it
> 5.) Set the axis title
> Then the min/max you set gets overwritten.
> 
> If you do the following (swap 2 and 3 around):
> 1.) Create the graph
> 2.) Fill it with points
> 3.) Set it's min/max
> 4.) Plot it
> 5.) Set the axis title
> Then you are ok, so this indicates that it is something to do with the order
> in which you fill with points and set min/max.
> 
> However, if you do my first example but don't include step 5 then you are
> also ok, as in:
> 1.) Create the graph
> 2.) Set it's min/max
> 3.) Fill it with points
> 4.) Plot it
> Setting min/max first is ok as long as you don't want to set the title on
> the axis!!!!!!
> 
> I have included a little code snippet below illustrating the problem.
> Many thanks,
> Jeff.
> 
> P.S. Using latest root.
> 
>   Int_t height=2000;
>   Int_t numPoints=10;
> 
>   TGraph* g=new TGraph(numPoints);
>   TGraph* g2=new TGraph(numPoints);
>   TGraph* g3=new TGraph(numPoints);
> 
>   //set g2 and g3 min/max before SetPoint
>   g2->SetMinimum(0);
>   g2->SetMaximum(5000);
>   g3->SetMinimum(0);
>   g3->SetMaximum(5000);
> 
>   for (Int_t i=0;i<numPoints;i++){
>     height+=100;
>     g->SetPoint(i,i,height);
>     g2->SetPoint(i,i,height);
>     g3->SetPoint(i,i,height);
>   }
> 
>   //set g min/max after SetPoint
>   g->SetMinimum(0);
>   g->SetMaximum(5000);
> 
>   TCanvas *c=new TCanvas("c","c",0,0,1200,800);
>   c->SetFillColor(0);
>   c->Divide(3,1);
> 
>   //first plot
>   c->cd(1);
>   g->Draw("AP");
>   g->SetTitle("Plot Ok - Min/max set after SetPoint()");
>   g->SetMarkerStyle(3);
>   g->SetMarkerColor(2);
>   g->SetMarkerSize(0.4);
> 
>   g->GetXaxis()->SetTitle("x Value");
>   g->GetYaxis()->SetTitle("Height");
>   g->GetXaxis()->CenterTitle();
>   g->GetYaxis()->CenterTitle();
> 
>   //second plot
>   c->cd(2);
>   g2->Draw("AP");
>   g2->SetTitle("Plot Ok - Min/max set before SetPoint() (no axis
> modification)");
>   g2->SetMarkerStyle(3);
>   g2->SetMarkerColor(2);
>   g2->SetMarkerSize(0.4);
> 
>   //third plot
>   c->cd(3);
>   g3->Draw("AP");
>   g3->SetTitle("Plot NOT Ok - Min/max set before SetPoint() and axis
> modification");
>   g3->SetMarkerStyle(3);
>   g3->SetMarkerColor(2);
>   g3->SetMarkerSize(0.4);
> 
>   g3->GetXaxis()->SetTitle("x Value");
>   g3->GetYaxis()->SetTitle("Height");
>   g3->GetXaxis()->CenterTitle();
>   g3->GetYaxis()->CenterTitle();
> 
> ----- Original Message -----
> From: "Rene Brun" <brun@pcbrun.cern.ch>
> To: "Jeff Hartnell" <jeffrey.hartnell@physics.ox.ac.uk>
> Cc: <roottalk@cern.ch>
> Sent: Friday, December 06, 2002 10:09 PM
> Subject: Re: [ROOT] TGraph & SetMinimum
> 
> > Hi Jeff,
> >
> > Add the statement
> >    c->Update();
> > after your Draw calls, eg:
> >
> >   //second plot
> >   c->cd(2);
> >   g2->Draw("AP");
> >   c->Update();
> >   g2->SetMinimum(0);
> >
> >
> >   //third plot
> >   c->cd(3);
> >   g3->Draw("AP");
> >   c->Update();
> >   g3->SetMinimum(0);
> >
> > This is not necessary anymore if you use the development version in CVS.
> >
> > Rene Brun
> >
> > On Fri, 6 Dec 2002, Jeff Hartnell wrote:
> >
> > > Hi Rene,
> > > I've tracked down the problem. Doing what you suggest works BUT if you
> > > then set the title on the axis it somehow resets the scale!!!
> > >
> > > g->Draw("AP");
> > > g->SetMinimum(0);
> > >
> > > Works just fine but
> > >
> > > g->Draw("AP");
> > > g->SetMinimum(0);
> > >
> > > g2->GetXaxis()->SetTitle("x value");
> > >
> > > doesn't work and the scale gets reset.
> > >
> > > See my macro below demonstrating the problem.
> > >
> > > Cheers,
> > > Jeff.
> > >
> > >   Int_t height=2000;
> > >   Int_t numPoints=10;
> > >
> > >   TGraph* g=new TGraph(numPoints);
> > >   TGraph* g2=new TGraph(numPoints);
> > >   TGraph* g3=new TGraph(numPoints);
> > >   TGraph* g4=new TGraph(numPoints);
> > >
> > >   for (Int_t i=0;i<numPoints;i++){
> > >     height+=100;
> > >     g->SetPoint(i,i,height);
> > >     g2->SetPoint(i,i,height);
> > >     g3->SetPoint(i,i,height);
> > >     g4->SetPoint(i,i,height);
> > >   }
> > >
> > >   TCanvas *c=new TCanvas("c","c",0,0,1200,800);
> > >   c->SetFillColor(0);
> > >   c->Divide(2,2);
> > >
> > >   //first plot
> > >   c->cd(1);
> > >   g->Draw("AP");
> > >   g->SetMinimum(0);
> > >   g->SetTitle("Test plot - I work");
> > >   g->SetMarkerStyle(3);
> > >   g->SetMarkerColor(2);
> > >   g->SetMarkerSize(0.2);
> > >
> > >   //second plot
> > >   c->cd(2);
> > >   g2->Draw("AP");
> > >   g2->SetMinimum(0);
> > >   g2->SetTitle("Test plot - I don't work (x-axis matters)");
> > >   g2->SetMarkerStyle(3);
> > >   g2->SetMarkerColor(2);
> > >   g2->SetMarkerSize(0.2);
> > >
> > >   g2->GetXaxis()->SetTitle("x value");
> > >   //g2->GetXaxis()->CenterTitle();
> > >
> > >   //third plot
> > >   c->cd(3);
> > >   g3->Draw("AP");
> > >   g3->SetMinimum(0);
> > >   g3->SetTitle("Test plot - I don't work (y-axis matters)");
> > >   g3->SetMarkerStyle(3);
> > >   g3->SetMarkerColor(2);
> > >   g3->SetMarkerSize(0.2);
> > >
> > >   g3->GetXaxis()->SetTitle("x value");
> > >   g3->GetXaxis()->CenterTitle();
> > >   g3->GetYaxis()->SetTitle("height");
> > >   g3->GetYaxis()->CenterTitle();
> > >
> > >   //fourth plot
> > >   c->cd(4);
> > >   g4->Draw("AP");
> > >   g4->SetMinimum(0);
> > >   g4->SetTitle("Test plot - I work (SetMin last)");
> > >   g4->SetMarkerStyle(3);
> > >   g4->SetMarkerColor(2);
> > >   g4->SetMarkerSize(0.2);
> > >
> > >   g4->GetXaxis()->SetTitle("x value");
> > >   g4->GetXaxis()->CenterTitle();
> > >   g4->GetYaxis()->SetTitle("height");
> > >   g4->GetYaxis()->CenterTitle();
> > >   //call again right at the end to sort it out!!!!
> > >   g4->SetMinimum(0);
> > >
> > >
> > >
> > > ********************************************
> > > Jeffrey Hartnell, Oxford/RAL DPhil student.
> > >
> > > Email: jeffrey.hartnell@physics.ox.ac.uk
> > > Phone numbers, addresses etc on website:
> > > http://www-pnp.physics.ox.ac.uk/~hartnell/
> > > ********************************************
> > >
> > > ----- Original Message -----
> > > From: "Rene Brun" <brun@pcbrun.cern.ch>
> > > To: "Jeff Hartnell" <jeffrey.hartnell@physics.ox.ac.uk>
> > > Cc: <roottalk@cern.ch>
> > > Sent: Friday, December 06, 2002 2:44 PM
> > > Subject: Re: [ROOT] TGraph & SetMinimum
> > >
> > >
> > > > Hi Jeff,
> > > >
> > > > What you do should work
> > > >     g->Draw("ap");
> > > >     g->SetMinimum(0);
> > > >
> > > > if not, please send me a short but running script showing the problem.
> > > >
> > > > Rene Brun
> > > >
> > > > On Fri, 6 Dec 2002, Jeff Hartnell wrote:
> > > >
> > > > > Hi,
> > > > > It seems to be impossible to override the automatic axis scaling in
> > > > > TGraph when you draw it with the "a" option.
> > > > >
> > > > > I found this in the digest:
> > > > >
> > > > > From: Rene Brun (Rene.Brun@cern.ch)
> > > > > Date: Wed Mar 20 2002 - 10:33:17 MET
> > > > >
> > > > >   a.. Next message: Rene Brun: "Re: [ROOT] TGaxis"
> > > > >   b.. Previous message: Rene Brun: "Re: [ROOT] TGeant3, TGeant4 from
> > > > > AliSoft"
> > > > >   c.. In reply to: Steffen Grohmann: "[ROOT] SetRangeUser Problem on
> > > > > Y-axis"
> > > > >   d.. Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> > > > >
> > > >
> > > > ----------------------------------------------------------------------
> > > --
> > > > > --------
> > > > >
> > > > >
> > > > > Hi Steffen,
> > > > >
> > > > > When you use the option "a" in TAxis::Draw, the axis limits are
> > > > > automatically recomputed from the range of the actual Y values.
> > > > > You can force a max/min of the y axis via
> > > TAxis::SetMaximum/SetMinimum.
> > > > > An alternative solution is to use TPad::DrawFrame.
> > > > >
> > > > > Rene Brun
> > > > >
> > > > > but TAxis doesn't have SetMaximum or minimum does it?
> > > > >
> > > > > I do this:
> > > > >
> > > > > gAdcPhNear[l]->Draw("AP");
> > > > > gAdcPhNear[l]->SetMinimum(0);
> > > > >
> > > > > but it still gets drawn with the minimum above zero. Why doesn't
> > > this
> > > > > work? Can it be made to work?
> > > > >
> > > > > Has anyone found a solution to this problem? I could always draw a
> > > > > histogram first with the appropriate scale, but that's ugly!
> > > > > Thanks,
> > > > > Jeff.
> > > > >
> > > > > p.s. using latest root on redhat 7.3.1
> > > > >
> > > > >
> > > >
> > >
> >



This archive was generated by hypermail 2b29 : Thu Jan 01 2004 - 17:50:09 MET