Re: Graph with "text" label

Rene Brun (Rene.Brun@cern.ch)
Thu, 16 Apr 1998 09:14:42 +0200


Thomas Hadig wrote:
>
> Hi,
>
> i would like to create a graph whose y-axis labels are text.
>
> Maybe i should draw a sketch :
>
> Set 1 |-O-|
>
> Set 2 |---O---|
>
> Set 3 |O|
>
> +----+----+----+----+
> 0 1 2 3 4
> x
>
> Is there a special command out there in root land ?
>
> I tried to use TGraphErrors, but i would have to remove the y-axis
> ticks and labels and add the text and have to match the individual
> y-heights.

Hi Thomas,
You will find below a small example illustrating how to add labels
on an axis.
The TAxis class foresees alphanumeric labels instead of numeric
labels in its data structure. This functionality is not yet used
by higher level classes such as TGraph or TH1.

Rene Brun

{
gROOT->Reset();
c1 = new TCanvas("c1","gerrors2",200,10,700,500);
c1->SetFillColor(42);
c1->SetGridy();

// create a 2-d histogram to define the range
TH2F *hr =
new TH2F("hr","Special labels on the axis",2,-0.4,1.2,2,0,12);
hr->GetYaxis()->SetLabelOffset(1);
hr->GetYaxis()->SetNdivisions(10);
hr->SetXTitle("X title");
hr->Draw();
c1->GetFrame()->SetFillColor(21);
c1->GetFrame()->SetBorderSize(12);

// create a graph with errors
Int_t n = 6;
Float_t x[] = {-0.22, 0.029, 0.225, 0.426, 0.67, 1.09};
Float_t y[] = {1,3,5,7,9,11};
Float_t ex[] = {.05,.1,.07,.07,.04,.05};
Float_t ey[] = {.8,.7,.6,.5,.4,.4};
gr1 = new TGraphErrors(n,x,y,ex,ey);
gr1->SetMarkerColor(kBlue);
gr1->SetMarkerStyle(21);
gr1->Draw("P");

// Draw labels on the y axis
TText *t = new TText();
t->SetTextAlign(32);
t->SetTextSize(0.035);
t->SetTextFont(72);
char *labels[6] = {"Jan98","Feb98","Mar98","Apr98","May98","Jun98"};
for (Int_t i=0;i<6;i++) {
t->DrawText(-0.42,y[i],labels[i]);
}
}