How to set custom x-axis labels using a TGraph?


I’ve got a problem with setting the labels of a TGraph X-axis to custom names (strings).
So instead of let’s say ‘1,2,3,4,5,…’ I’d like to have ‘string1, string2, string3, …’:
I’ve got 60 datapoints and to every datapoint I want one custom string as a label in the X-axis.

Unfortunately I didn’t find a solution with google (3h) and the ROOT user manual so that I tried the following (like you can do it with histograms):

[code]Graph1 = ROOT.TGraph(60, DataArrayX, DataArrayY)

iterate over i from 1 to 60

Graph1.GetXaxis().SetBinLabel(i, ‘Test’)

However my labels won’t align with my datapoints because I don’t know how many bins my TGraph has got (does it even have histogram-like bins?).

Some might say now ‘use a histogram’; well that’s what I did before I tried TGraph (example for first bin):

[code]hist = ROOT.TH2F(‘test’, ‘test’,60, 1, 60, 200, -5, 1)
Xaxis = hist.GetXaxis()

set bin label

hist.GetXaxis().SetBinLabel(1, str(test))
hist.Fill(1, testdata)[/code]

Using the above code I still can’t get my datapoints to align with my labels so it looks like e.g. datapoint 1 is at x=1.2 and not x=1 as filled.
I guess for this kind of problem TGraph should be the way to go because I think that I’d need like 5000 bins considering how binning works.
I’ve attached a picture of my try with the histogram class.

So my question is the following: How can I label my TGraph X-axis with 60 strings instead of a 1-60 range?
With .SetBinLabel it just doesn’t work.

Thanks for your help!
RunToDeltaEDividedByE.pdf (15.8 KB)

I think possibly the problem is that the “bins” of the TAxis (yes, it’s dumb that it has bins) don’t necessarily correspond to the X-coordinate at the bin location. In fact, in my simple code below, xax.GetNbins() actually was 100, despite only having 20 data points.

So what you could do is search the TAxis bins for your X-coordinate in order to figure out which label (if any) to put there. Here is some toy code that might be useful for you:

import ROOT,array
x = array.array("d",range(20))
y = array.array("d",(i*i for i in x))
g = ROOT.TGraph(len(x),x,y)
xax = g.GetXaxis()
pi = ROOT.TMath.Pi()
i = 0
while i*pi <= xax.GetXmax():
    bin_index = xax.FindBin(i*pi)
    xax.SetBinLabel(bin_index,"%d pi" % i)


Thank you very much that solved my problem!

However I’ve got another one now: The last thing that I want to do is to draw a grid on the X-axis.
WIth histograms it works perfectly with ‘canvas.SetGridx(1)’ but if I try it with TGraph it doesn’t appear in the Plot. This is how I’ve done it:

ca1 = ROOT.TCanvas()
Graph1 = ROOT.TGraph(60, DataArrayX, DataArrayY)
xax = Graph1.GetXaxis()

for i in range(60):
if i == 0:
binIndex = xax.FindBin(i)
print binIndex
xax.SetBinLabel(binIndex, str(RunList[i]))




I also tried to set the grid after the bin loop but that didn’t help.
I guess that it doesn’t work because there are no ticks in my graph and I can’t find an option in the TAxis Function Members to set my own ticks.

I’ve attached my plot (somehow the orientation of my labels is a bit messed too).
RunToDeltaEDividedByE.pdf (14.7 KB)

I couldn’t find anything useful about ticks, grids, and alphanumeric labels. The standard examples in the tutorial folder have grids enabled, so I don’t know why it isn’t working here. Maybe it’s some weird subtlety between TGraphs and TH1…

For your axis angle, you can use to try the four different hardcoded angles. Too bad there’s no SetLabelAngle method, I think if you want fancy 45 degree labels you might have to draw separate text labels/boxes yourself.

Maybe one of the other experts can answer, and you can also try re-asking your question about grids in the regular ROOT forum. It gets more traffic and attention than the PyROOT one.


Since ROOT version 6.07/07 see also: