Superimposing 2 TGraphs with diffrent Scales

Hello my name is Johannes Stingl,

I have a little problem, which is certainly easy to solve I just cannot find the answer.
I can easily superimpose 2 TGraphs, the problem is I need to scale one of the Graphs since its Y values are about 1/20 of the other Graph’s Y values. The idea is to scale the smaler graph to the size of the first one and paint a second axis on the right side of the pad. I tried painting the Graphs with the ‘A’ Option and it seemded to work but the first Graph is only visible for a split second until the other Graph is painted ontop of it. It also seems there is no scale method for the TGraph object thus the example using histogramms doesn’t apply in this case. There is one more thing that might be important. The X values don’t have the same distance to ea. other which I think poses a problem when tring to access the underlying histogramm.

Thank you very much for any suggestion.

The example “twoscales.C” in the turotial directory, may help you to draw a 2nd axis on your plot. I put it in attachment.To help you further (if needed) can you send a little example showing your prodblem ? that way we could modify it to answer your question.
twoscales.C (1.31 KB)

Thank you for your suggestion.
Of course I already know this example. The problem is that I’m using TGraph and not a histogramm. And adding a second axis doesn’t seemd to complicated. As it seems there is no method like GetMaximum() or Scale() for TGraphs. Here is an example of my atempt:

The result is that I do get both Graphs in the same pad (as long as I don’t try calling ->Scale() ) which is fine. But the second graph is almost not visible since it’s values are too low. That’s why I thought of scaling in the first place. The reason why I don’t use histogramms is that the data isn’t random but has a exact correlation between x and y values.
If there is no easy workaround for this problem I will do it with histogramms like in the example.
Thanks again for your help

I recenlty posted a reply to root-talk which may help you. The example was:

{
   gROOT->Reset();

   ifstream data;
   data.open("led1.data");
   
   const Int_t n = 100;
   Int_t in = 0;
   Float_t x[n], y1[n], y2[n], x1;

   while (data >> x1)
   {
      x[in] = x1;
      data >> y1[in];
      data >> y2[in];
      in++;
   }
   
   TGraph *inputSpec = new TGraph(in, x, y1);
   TGraph *inputSpec2 = new TGraph(in, x, y2);

   inputSpec->GetHistogram()->SetXTitle("X title");
   inputSpec->GetHistogram()->SetYTitle("Y title");
   
   TCanvas *c1 = new TCanvas("c1", "Intensity of LED 1",0,  0, 800, 600);  
   c1->SetLogy();
   
   inputSpec->Draw("AC");
   gPad->Update();
   inputSpec2->Draw("CP");

   
   //draw an axis on the right side
   TGaxis *axis = new TGaxis(gPad->GetUxmax(),
                             gPad->GetUymin(),
                             gPad->GetUxmax(),
                             gPad->GetUymax(),
                             0,10,510,"+L");

   axis->Draw();
}

Note also that the TGraph class has a GetHistogram method. So you can then use the histograms methods on the histogram returned by this method.

The data fille used in this example is:

1 1 5
2 2 4
3 3 3
4 4 2
5 5 1
6 4 2
7 3 3
8 2 4
9 1 5

I think this will work.
Thank you for your help.

I am doing something similar, however the scales of the data are vastly different.
I took Oliver’s example code, and modified the led1.data to be 10 times smaller. The data/axes then dont scale correctly. It appears that the world coordinate system takes on the attributes of the first TGraph plotted.

What is desireable is to with the 2 different scales, actually have the second plot tied to the second scale.

This obviously means 2 world coordinate systems.

I tried modifying the code by adding the AY+ option to the second draw function. This changes the world coordinate system to the new scale of the TGraph, but does not preserve the original.

Pretty sure what I am looking for is a way to overlay 2 plots, and maintain 2 different world scales. Any options?
Chris

I’ve attached some sample code that illustrates 2 problems. The first of which is that the new Axis scale doesn’t appear to meet the x axis when a set range call has been made. The second of which is that the secondary axis, of a different scale, doesn’t match up with the values of the TGraph outlined in red.

It seems that a scale method for TGraph might be helpful. In this way the data in the second TGraph could be scaled to match the difference between the left and right. This doesn’t take into account any offset.

I also tried making 2 pads. I drew the first TGraph in the first pad and made the second pad transparent and drew the second TGraph in the second pad. This didn’t work since the bounding boxes didn’t line up, and the graph itself was not transparent.

Any other ideas?
Chris
twoscale3.C (2.04 KB)

I think you need: $ROOTSYS//tutorials/hist/transpad.C

This almost does the trick. I have attached 2 sample files. example2 is very very close. If I could make the second TGraph transparent, then I’d be done.

The other example, in which I choose to draw the axis seperately, example3.C, doesn’t seem to fix the world coordniates of the second pad.
Chris
example2.C (1.35 KB)

sorry didn’t attach example 3
example3.C (2.22 KB)

It is a little rough around the edges, and zooming probably will be a bit of work.
Chris
example4.C (3.26 KB)

If it is possible to have the TGraph be transparent. example 2 would make it a cinch.
Chris

This collection of posts makes your point a bit unclear. What are you asking now ? seems to me the last example (example4.C) looks correct.

PS: you can remove intermediate posts if you think they are not relevant anymore.

Hi,
Yes, I agree that I made it a bit unclear. Example 4 does do the trick.

I was asking if it were possible to change the transparency of a TGraph. If you look at the code in example 2, overlaid 2 TGraphs. The second uses the “AY+” option. So it scales correctly. However, the first TGraph is covered. You can find this out by using the pointer to slide the second graph to one side.

If the second TGraph were transparent, AND, I could use “AY+” such that it would not obscure the first TGraph, I could have accomplished the same feat without using a second TPad.

This may be closer to a design issue though. As I said, example4 works. I was trying to show that it was possible to gain an interesting feature in root.

As always, it is very hard to communicate without a whiteboard and a face to face. :smiley:

Thanks for your continued help.
Chris

Yes you are right, example2.C can be improved. The attached version does the job.
example2.C (1.08 KB)

I wish I had your understanding of root!
Thanks for the help.
Chris

One issue with example2.C is that one can not zoom via the empty histogram method. I tried to modify example2.C (see attachment) by adding the histogram hDummy with no luck.

When I add hDummy to pad2 I can access the hDummy object in the editor when the script is run but the “Axis Range” bars in the Binning tab do not zoom the graphs. If I add hDummy to pad1 I can only access pad2 via the editor.

Is their another solution?

Thanks,
Sanjeev
example2_1.C (1.24 KB)

The macro example2.C draws two curves in two different pads. It is done this way because the Y scales are different for the two data sets. So, as there is 2 pads, you should change the range on both (along X) to make sure the X scale remains the same for the 2 curves.

I guess I am not sure how to graphically select pad1 to change its range. It appears that one can only select pad2.

Would the solution than be to write a macro which adjusts the pad’s ranges?

Thanks,
Sanjeev

Yes you should change the ranges of pad1 and pad2 to make sure they match.