Table in TLegend

Hi!

Run this:

const int histNumber = 8;	// total number of histograms


void hists () {
	TCanvas * c = new TCanvas("a", "b", 800, 380);
	c->SetMargin(0.1, 0.036, 0.1, 0.03);
	TH1D * hist;
	
	TLegend * legend = new TLegend(0.60, 0.80, 0.98, 0.96);
	legend->SetHeader("something");
	legend->SetNColumns(histNumber/2);
	
	gStyle->SetOptStat(false);
	
	for (int i = 0; i < histNumber; i++) {
		hist = new TH1D(Form("%d", i), "", 100, -3, 3);
		hist->FillRandom("gaus", 1E+4);
		hist->SetLineStyle(((i + 1) % 2) +1);
		hist->SetLineColor((i + 1) / 2 + 1);
		
		legend->AddEntry(hist, "a", "lep");
		hist->Draw("HIST E0 same");
	}
	
	legend->Draw();
	
	TText * text1 = new TText(0.10, 0.2, "Label1");
	text1->SetTextAlign(22);
	text1->Draw();
	
	TText * text2 = new TText(0.40, 0.1, "Label2");
	text2->SetTextAlign(22);
	text2->Draw();
	
	return;
}

It will produce a messy legend. What I want is something like this:


where the tiny coloured boxes should be + instead, each having the color of a histogram. Additionally, they should have the same “LineFillStyle”.

I think I’m not far from what I want, but I have some trouble with the Ttext coordinates. Furthermore, I’d like to have the same line-separators as shown in the picture.

May I get some help? :slight_smile:

The legend correspond to the attributes of your histogram. I
do not see why you think it is messy.
For the labels, the coordinates are not the Pad’s ones. May be you wanted to be in normalised coordinates ? in that case add text1->SetNDC() (and same for text2)

I meant that I set it so to be meaningless: a bunch of “a”, “something” as header, etc.

I’ll try with that text1->SetNDC() and maybe I should be able to get what I want by also using some TLine. :thinking:

That’s what your macro asks for…

I know. I made it in a couple of minutes just to provide an example. The macro in which I need the legend reads external files both for data and titles, legend_entries, etc.

I’m not blaming anyone for that. :muscle:

@couet, may I get some other help from you?

I’m almost done, but the TLegend only fills the first column:

const int histNumber = 12;	// total number of histograms

void hists () {
	TCanvas * c = new TCanvas("a", "b", 800, 380);
	c->SetMargin(0.1, 0.01, 0.1, 0.03);
	TH1D * hist;
	
	TPaveText * title1 = new TPaveText(0.68, 0.90, 0.73, 0.96, "NDC");
	TPaveText * title2 = new TPaveText(0.73, 0.90, 0.78, 0.96, "NDC");
	TPaveText * title3 = new TPaveText(0.78, 0.90, 0.83, 0.96, "NDC");
	TPaveText * title4 = new TPaveText(0.83, 0.90, 0.88, 0.96, "NDC");
	TPaveText * title5 = new TPaveText(0.88, 0.90, 0.93, 0.96, "NDC");
	TPaveText * title6 = new TPaveText(0.93, 0.90, 0.98, 0.96, "NDC");
	title1->SetShadowColor(0);
	title2->SetShadowColor(0);
	title3->SetShadowColor(0);
	title4->SetShadowColor(0);
	title5->SetShadowColor(0);
	title6->SetShadowColor(0);
	title1->AddText("25 m");
	title2->AddText("30 m");
	title3->AddText("35 m");
	title4->AddText("40 m");
	title5->AddText("-70 m");
	title6->AddText("+70 m");
	
	TPaveText * pave = new TPaveText(0.58, 0.80, 0.68, 0.90, "trNDC");
	pave->SetShadowColor(0);
	pave->SetTextAlign(12);
	pave->AddText("Primary");
	pave->AddText("Secondary");
	pave->AddLine(.0,.5,1.,.5);
	
	TLegend * legend = new TLegend(0.68, 0.80, 0.98, 0.90);
	legend->SetNColumns(6);
	
	gStyle->SetOptStat(false);
	
	for (int i = 0; i < histNumber; i++) {
		hist = new TH1D(Form("%d", i), "", 100, -3, 3);
		hist->FillRandom("gaus", 1E+4);
		hist->SetLineStyle((i / 6) + 1);
		hist->SetLineColor((i % 6) + 2);
		hist->SetLineWidth(2);
		
		legend->AddEntry(hist, "", "lep");
		hist->Draw("HIST E0 same");
	}

	pave->Draw();	

	title1->Draw();
	title2->Draw();
	title3->Draw();
	title4->Draw();
	title5->Draw();
	title6->Draw();
	
	legend->Draw();
	
	return;
}

Add a title to the histograms and it works:

  hist = new TH1D(Form("%d", i), "a", 100, -3, 3);

But you don’t want to see the name in the legend, only the marker. Here’s a workaround: set the legend margin to 0.9 or something like that and use a period as name.

  // before the loop
  legend->SetMargin(0.9);
  // ...
  // in the loop:
     hist = new TH1D(Form("%d", i), ".", 100, -3, 3);
  // ...
1 Like

Thank you!

This fixes the last issue. Thanks!

Hi,
seems TLegend::AddEntry needs some (none space) label
(Olivier correct me, if I am wrong)
So the following also seems to work with hist title empty

legend->SetMargin(1);  // center
....
legend->AddEntry(hist, "\t", "lep"); // white space tab

Otto

2 Likes

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