Edit Legend Marker Size

Hi,
I am plotting some 2-d histograms with quite a lot of points, and their marker is very small. I am trying to create a legend for these histograms, but the legend is very hard to read, because the marker size is the same as the histogram. It is suggested in another post that the TLegendEntry returned by AddEntry() might be directly modified so that the legend marker is larger, but I have not found this to be possible. (Post is here: [url]Marker size in TLegend Could someone show more detail about how this is done? I’ve posted two examples of my attempts below, both of which return the same small marker as when no marker size is set.
Thanks for any help.

– Simon

void test()
{

  TH2F* gpm2 = new TH2F("gpm2","",100, 0, 3000, 100, 0, 3000);
  TH2F* gpm1 = new TH2F("gpm1","",100, 0, 3000, 100, 0, 3000);

  TF1 * mygaus = new TF1("mygaus", "gaus",0,3000);
  mygaus->SetParameters(1,1500,50);
  gpm2->FillRandom("mygaus", 10000);
  mygaus->SetParameters(1,2000,150);
  gpm1->FillRandom("mygaus", 10000);

  gpm2->SetTitle("MyTitle");
  gpm1->SetMarkerColor(2);

  gpm2->Draw();
  gpm1->Draw("same");
  TLegend * leg = new TLegend(0.1,0.2,0.3,0.4);
  TLegendEntry * legee = new TLegendEntry(gpm1,"gpm1","p");                                                                            
  legee->SetMarkerSize(20);
  leg->GetListOfPrimitives()->Add(legee);                                                                                               
  leg->AddEntry(gpm2,"gpm2","p");
  leg->Draw();

}

The marker you are using is the dot and is not scalable. As explained here:
root.cern.ch/root/html/TAttMarker.html#M3

Try:

gpm1->SetMarkerStyle(20);

But your plot will be changed. As you seem to be interested in the color only you can try to display in the legend the fill area attributes (assuming you set the color the same as the marker one). It will be more readable.

Indeed, gpm1->SetMarkerStyle(20); makes the marker in the legend visible, but as you say, it changes the plot as well.

Your suggestion of using the “fill” option in the legend conveys exactly the desired information without altering the plot, so I think I will use that. For example,

gpm1->SetFillColor(2); leg->AddEntry(gpm1,"gpm1","f");

Many thanks for the suggestions.

– Simon

Hi, consider these options:

  1. Define two TMarker objects -as large as you want- to be drawed superimposed to the 2 legend’s markers.
  2. Build your own legend by hand (using e.g. TLatex instead using TLegend); it’s only two entries: little work, nice results.

Yes they are other possibilities…

Hi,
I also have the same problem and if I use “f”, then different colors in for different plots are somehow not visible in legend. I am using scatter plot in TGraph.Can you please show an example of using Latex for the same.

Thanks.

Can you post some example macro showing your problem ?

Hi,

Thanks for your reply :). It works now. I am posting the code here. If someone finds it helpful, I would be happy. Earlier I was not using “SetFillColor”, so the legends were black. Now the current version of the code looks like the following, which works perfectly for me:

//Code
    TLegend *leg=new TLegend(0.8,0.8,0.9,0.9);     //x1,x2,y1,y2 respectively
    leg->AddEntry(h1," name of the histogram","f");      //f:fill,p:point,l:line
    h1->SetFillColor(kBlue);
    leg->SetBorderSize(1);
    leg->Draw();

But I would still be interested to know how we can use Latex here. Any short example would be very helpful. Thanks :slight_smile:

See the first example example in the TLegend documentation.

1 Like

thanks :slight_smile: