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);
    TCanvas *can = new TCanvas("GRAPH",7000,400);
    gr->GetXaxis()->SetTitle("Time (s)");
   gr->GetYaxis()->SetTitle(" current"); 

    TLegend* legend= new TLegend(0.5,0.7,0.78,0.88);



    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);
axis ////
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 !

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.