Axes Lables and Title on a Scatter Plot

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");
}
root [0] ntuple->Draw("px:py");
root [1] htemp->SetTitle("Main Title ; X Title ; Y Title")

Thank you, using htemp in that way seems to work.

Can you explain, or point me to an explanation, why this works using htemp but does not work the way I had set it up? I don’t understand the difference between using the htemp pointer and getting the histogram pointer via the file structure. I’d like to understand this so that I might be able to better solve my own problems in the future.

Thank you very much,
–Chad

htemp is the frame histogram containing the result of your query. htemp is created automatically when you do not specify any histogram. You can also use you own histogram a follow. It works also:

root [0] TH2D *h2 = new TH2D("h2","h2",40,-10,10,40,-10,10)
root [1] ntuple->Draw("px:py >> h2");
root [2] h2->SetTitle("Main Title ; X Title ; Y Title")