How to fix the Tcanvas in TMultiGraph for several files

Good day

I am using TMultiGraph to add several plots before drawing them in the same TCanvas and title.
but the problem I got the very different axis limit and overlapped instead of having the axis limit of Canvas and other structures. I will appreciate any help to avoid overlapping of axis and having fixed Canvas for all contain all the plots in it.
I attached the structure of the code :slight_smile:

int main()
{
    
    auto  mg  = new TMultiGraph();
    string st = "Pulse";
    string ext = ".txt";
    string filename;
    for (int i=1; i<= 4; i++){
        
	stringstream ss;
        ss << i;
        filename = st + ss.str() + ext;
        cout << filename << "\n";
	
	ifstream myfile(filename.c_str()); 
        for (int j=0; j<60; j++){
            ////////////////
            ///////////////
	    
	TGraph *gr = new TGraphErrors(int,x,y);
	mg->Add(gr,"L");
	}
        
    }
    TCanvas *can = new TCanvas("GRAPH",7000,400);
   gr->SetTitle("Pulse");
    gr->GetXaxis()->SetTitle("Time (s)");
   gr->GetYaxis()->SetTitle(" current"); 
   gr->GetXaxis()->CenterTitle();
    gr->GetXaxis()->SetLimits(1.,1.e8);
    gr->SetMinimum(0.1);
    gr->SetMaximum(1.0);  
        /////////
        ////////
	     
    gStyle->SetTitleFontSize(0.033);   	     	


    mg->Draw("SAME");
    TLegend* legend= new TLegend(0.5,0.7,0.78,0.88);

    legend->AddEntry(gr,"current","l");

    legend->Draw();

    return 0;
    
}

Thank in advance

TMultiGraph automatically adjusts axes limits according to its added graphs.

You need to: “mg->Draw(); /* creates axes */ gr->Draw(); /* adds another gr */

BTW. There is no “SAME” drawing option for (multi)graphs.

yes no, “SAME” was a mistake .
when I use order of :
TGraph *gr = new TGraphErrors(int,x,y);
mg->Add(gr,“L”);
mg->Draw()
axis ////
/////
gr->Draw()
I have got on plot just empty Canvas with no errors comments

1 Like

Means what?

Make sure that the “gr” is not completely empty, e.g. try: gr->Print();
I don’t think the variable name can be “int” in “TGraphErrors(int,x,y);

BTW, You added “gr” to “mg” so you do not need to draw “gr” again.

Yes , on the light of what you said I created the axis then draw gr added all gr then mg->Draw() . also I tried to added gPad->Modified() then axis gPad->Update() but in all cases the axis automatically adjust and gave me the very same chaotic axis . I do not why !
Thanks

You need to first create the “mg” then add all your “gr” graphs to it and finally draw “mg” (which will then create axes taking into account all its added graphs).

Exactly that is what I have done then gPad->Update()! but it does not fix the problem

Check / draw every “gr” graph separately in order to find which one is “bad”.


it gives me this graph when I’ve checked separately one gr. which the same problem presents

For a single graph, try: gr->Draw("A*");
For the multigraph, try: mg->Draw("APL");

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.