TGraph2D how to set axis ranges?

I have 3 data sets which I draw with TGraph2D::Draw(“cont4z”). X and Y in all data sets cover about 0 to 1 range. However, they do not cover necessarily exactly this range, so when I plot the graph, I often miss the starting 0 or/and ending 1 on one or both of the axis. I’d like axis to display 0 and 1, and to have ticks every 0.1, which is not always the case (not sure, when TGraph2D decides to draw ticks every 0.1 and when it decides to draw every 0.2).

I am not sure how to do that. GetXaxis()->SetRangeUser() allows me only to shorten the axis, not to enlongate it. gPad->SetMargin() is of some help, but it changes the margin on all sides of the TGraph2D, so it happens that to show 1 on Y axis, i get more than 1 on X axis… I guess this question may have an obvious answer, but I have problems finding it…

Hi,

you could simply draw an empty histogram with tuned axes. Then you draw your TGraph2D on top.

Cheers, Axel.

use TPad::DrawFrame. see examples in tutorials, eg tutorials/graphs/gerrs2.C

Rene

I am not sure if it works properly with TGraph2D.

When I use DrawFrame, the axis are properly drown in requested range. But than I invoke TGraph2D::Draw(“cont4z”) and axes are adjusted to what TGraph2D wants… Maybe TGraph2D::Draw by default invokes redrawing axes?

Cont4 uses special a coordinates’ system. The SAME option does not work in a simple way with CONT4. I tried to simulated what you are doing using an other option. I used the histogram hpxpy produced by the macro $ROOTSYS/tutorials/hsimple.C

(When you use the CONT option on a TGaph2D it is in fact the TGraph2D underlying histogram which is plotted. So an example using a TH2D is valid for your case…)

gPad->DrawFrame(-10,-10,10,10)
hpxpy.Draw(“cont same z”)

The hpxpy range is: -4 -4 4 4

These command give the following plot.


The problem is, that what I’m trying to plot… looks ugly when using cont (not cont4). Is there a way, even a more complicated one, to plot in requested ranges cont4?

I guess one way is to create TH2 from TGraph2D using Interpolate, but I have no idea how to compute proper interpolation points to make created TH2 look same as TGraph2D after drawing with cont4.

Have you tried to do SetHistogram() on your TGraph2D ?
You create a histogram with the correct range and you define it using (SetHistogram() ) as the underlying TGraph2D histogram.
If it works it might be the simplest way to proceed.

Yes, it works great. Thank! That’s exactly what I need.

Perhaps this trick could be documented somehow or some kind of workaround for cont4 option could be done to perform it automatically?

It is not a trick. SetHistogram() is something which is implemented in many classes. But I agree it would be good to add an example in the TGraph2D help.

SetHistogram() really helps a lot! Please emphasise it in document.

Hi LeWhoo,

Can you give me a simple example, how you did it. Its not working for me.

Please give me a simple example how one can do that.

Thank you.

Can you post what you have ?