Hello-
I’m very new to Root, so my apologies for asking such a basic question. However, I’ve spent a couple of hours trying to figure this out and I haven’t really gotten anywhere.
I’ve been having trouble getting axes labels and a title on a scatter plot. I have spent quite a bit of time with the tutorials and examples, but few seem to actually have titles and labels, and the ones that do seem to include them manually.
I’ve included a minimum example showing some of the things that I’ve tried. The first canvas appears as I expect it to, but the same technique doesn’t seem to work on the second one.
Any help/pointers/suggestions are welcome.
Thanks in advance,
–Chad
void minEx(){
TFile * f = new TFile("temp.root", "RECREATE");
TTree * dat = new TTree("data", "Tree containing x,y data");
Float_t x, y;
dat->Branch("x", &x, "x/F");
dat->Branch("y", &y, "y/F");
for (Int_t i=0; i < 10000; i++){
gRandom->Rannor(x, y);
dat->Fill();
}
TCanvas * c1 = new TCanvas("TheHistogramCanvas");
dat->Draw("y:x >> xyhist", "", "LEGO1");
TH2F * hist = f->Get("xyhist");
// This works
hist->SetTitle("This is the Histogram Title");
hist->GetXaxis()->SetTitle("This is the histogram x-axis");
hist->GetXaxis()->SetTitleOffset(2);
hist->GetYaxis()->SetTitle("This is the histogram y-axis");
hist->GetYaxis()->SetTitleOffset(2);
TCanvas * c2 = new TCanvas("TheScatterCanvas");
dat->Draw("y:x >> xyscatter");
TH2F * scatter = f->Get("xyscatter");
// This does not work
scatter->SetTitle("This Does Not Set the Scatter Title");
scatter->GetXaxis()->SetTitle("This does not set the scatter x-axis");
scatter->GetYaxis()->SetTitle("This does not set the scatter y-axis");
// This does not work
TGraph * g = gPad->FindObject("Graph");
g->SetTitle("This Does Not Set the Scatter Title");
g->GetXaxis()->SetTitle("This does not set the scatter x-axis");
g->GetYaxis()->SetTitle("This does not set the scatter y-axis");
// This will allow me to add lines but not to change or remove the
// original title line
TPaveText * tp = gPad->FindObject("title");
// shows the text that is currently shown as the title
TText * line = tp->GetLine(0);
line->Print();
line->SetText(0, 0, "This Does Not Set the Scatter Title");
// This adds a second line to the title
tp->AddText("Second Scatter Title Line");
}