2 Pad multigraph with text on x-axis

Hello, I wrote this macro to get a 2 pad multigraph. It runs but with some problems

  1. Even if I setted the vertical grid…I don’t get it
  2. I don’t get the years (2016/17 , etc) on the two x-axis in the two plots

mulgr.cpp (6.4 KB)

data.txt (145 Bytes)

Thank you


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hello @faca87; I am sure @moneta can help you with your TMultiGraph issues.

Cheers,
J.

Thank you @jalopezg . Yes, I think that @moneta or @couet know how to fix it.
Thank you

Your macro is a bit messy… It has a lot of useless code, many things are conflicting … It looks a bit like “random coding”… Therefore the list of problems is too log to be detailed here. I just rewrote/cleaned it for the top plot. You can take it as example for the 2nd one. Here it is:

void mulgr() {
   const char *datain = "data.txt";
   TCanvas *c01 = new TCanvas("c01","multigraph",1280,1024);

   TPad *pad1 = new TPad("pad1", "pad1", 0.0, 0.3, 1.0, 1.0);
   pad1->SetGridx();         // Vertical grid
   pad1->Draw();             // Draw the upper pad: pad1
   pad1->cd();

   gStyle->SetTitleFontSize(0.08);
   TMultiGraph *mg = new TMultiGraph("mg","");

   float offx  = 1.3;
   float offy  = 1.3;
   float margr = 0.08;
   float w     = 3;
   float margl = 0.12;
   float line  = 2;
   float xl1   = 0.5, xl2 = 5.5;

   gPad->SetLeftMargin(margl);
   gPad->SetRightMargin(margr);

   TGraphErrors *gr1 = new TGraphErrors(datain,"%lg %lg %*lg %*lg %*lg");
   gr1->SetMarkerColor(kBlue);
   gr1->SetLineColor(kBlue);
   gr1->SetMarkerStyle(20);
   gr1->SetMarkerSize(1.3);
   gr1->SetLineWidth(line);
   mg->Add(gr1);

   TGraphErrors *gr2 = new TGraphErrors(datain,"%lg %*lg %lg %*lg %*lg");
   gr2->SetMarkerColor(kRed);
   gr2->SetLineColor(kRed);
   gr2->SetMarkerStyle(21);
   gr2->SetMarkerSize(1.3);
   gr2->SetLineWidth(line);
   mg->Add(gr2);
   mg->Draw("ALP");

   mg->GetXaxis()->SetTitle("Year");
   mg->GetXaxis()->SetTitleOffset(offx);

   mg->GetYaxis()->SetTitle("#Schools");
   mg->GetYaxis()->SetTitleOffset(offy);

   mg->GetXaxis()->SetTitleFont(43);
   mg->GetXaxis()->SetTitleSize(20);
   mg->GetYaxis()->SetTitleFont(43);
   mg->GetYaxis()->SetTitleSize(20);
   mg->GetXaxis()->SetNdivisions(5);

   mg->GetXaxis()->ChangeLabel(1, 45, 0.02, 31, -1, -1,"2016/17");
   mg->GetXaxis()->ChangeLabel(2, 45, 0.02, 31, -1, -1,"2017/18");
   mg->GetXaxis()->ChangeLabel(3, 45, 0.02, 31, -1, -1,"2018/19");
   mg->GetXaxis()->ChangeLabel(4, 45, 0.02, 31, -1, -1,"2019/20");
   mg->GetXaxis()->ChangeLabel(5, 45, 0.02, 31, -1, -1,"2020/21");

   TLegend* leg = new TLegend(0.15, 0.75, .25, .85);
   leg->SetHeader("Legend");
   leg->AddEntry(gr1, "Physics", "AP");
   leg->AddEntry(gr2, "Total", "P");
   leg->Draw();
}

It gives:

Thank you @couet it works!

1 Like

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