I have two TF1s (one is the modified version of the other), therefore they should be defined on the same x/y range. However, when I plotted them on the same axes (see the codes), I got something unexpected (picture). How to workaround this ?
Yes, that’s the point, you should not impose the range on the second function as it is already set on the first one and you draw the 2nd with the option SAME. The range of the 1st function imposes itself on the 2nd.
void Ng() {
auto f1 = new TF1("f1","1 + 0.5*cos(39.8*x + 0)",2.5,4);
f1->SetNpx(1000);
f1->GetHistogram()->GetXaxis()->SetRangeUser(3,3.5);
f1->SetLineColor(4);
f1->SetLineWidth(4);
f1->SetMarkerStyle(20);
f1->SetMarkerColor(2);
auto f2 = new TF1("f2","1 + 0.5*cos(39.8*x + 0)",2.5,4);
f2->SetNpx(1000);
// f2->GetHistogram()->GetXaxis()->SetRangeUser(3,3.5); /// << do not set the range on the 2nd funtion
f2->SetLineColor(4);
f2->SetLineWidth(4);
f2->SetMarkerStyle(20);
f2->SetMarkerColor(2);
auto c = new TCanvas();
f1->Draw(" p");
f2->Draw("same L");
}
@couet Assume you have several functions coming from different macros, each of which has some range set. Now you say they cannot be drawn together on the same canvas.
The problem is not there is you use TF1::SetRange instead of TF1::GetHistogram()->GetXaxis()->SetRangeUser. So it is bug in the second case. Can you maybe open a GitHub issue?
void Ng() {
auto f1 = new TF1("f1","1 + 0.5*cos(39.8*x + 0)",2.5,4);
f1->SetNpx(1000);
f1->SetLineColor(4);
f1->SetLineWidth(4);
f1->SetMarkerStyle(20);
f1->SetMarkerColor(2);
auto c = new TCanvas();
f1->Draw(" p");
f1->Draw("same L");
}