Hi,
I have a TH2D whose y-axis shows the base 10 logarithm of a certain physical quantity; thus, the y-axis values are -3, -2,…,1,2; I simply want to turn these numbers into 0.001, 0.02,…,10, 100, respectively. However, I do not want this modification to affect the plot: I already have a log-scale, I only want to modify the y-axis values.
How could I do?
Thank you!
Hi,
try the att. macro
Otto
logaxis.C (523 Bytes)
couet
January 14, 2016, 1:01pm
3
I guess you can try:
h2->GetYaxis()->SetLimits(0.001,100);
It does not work.
However, this is my macro:
#include "TCanvas.h"
#include "TStyle.h"
#include "TH2.h"
#include <fstream>
#include "TLatex.h"
#include "TAttLine.h"
#include "TGraph.h"
void mymacro() {
TCanvas *mymacro = new TCanvas("plot","plot", 1100, 1100);
TH2D *r1 = new TH2D("g1","g1", 100, 150, 1000, 100, -14, 6);
gStyle->SetOptStat(0);
gStyle->SetOptTitle(0);
ifstream red1 ("path/RED");
double x_bin;
double y_bin;
double ylog;
int i=0;
while(red1>>x_bin>>y_bin){
r1->Fill(x_bin, y_bin);
i++;
}
r1->SetFillColor(kRed);
gStyle->SetPalette(1);
r1->Draw("box");
r1->GetYaxis()->SetRangeUser(-11,4);
gPad->RedrawAxis();
mymacro->Print("myMacro.pdf");
}
#ifndef __CINT__
int main() {
mymacro();
return 0 ; }
#endif
The RED file can be found here: dropbox.com/s/ohd7jos6mpe65z5/RED?dl=0 .
As you can see, the origial y-axis range is (-14,6); then, I restrict it to (-11,4).
Tiziano
January 15, 2016, 12:33am
5
I tried it; this is the result: dropbox.com/s/7kmd7g9flli88 … o.pdf?dl=0
This is the macro that produced it:
#include "TCanvas.h"
#include "TStyle.h"
#include "TH2.h"
#include <fstream>
#include "TLatex.h"
#include "TAttLine.h"
#include "TGraph.h"
#include "TGaxis.h"
#include "TMath.h"
void mymacro() {
TCanvas *mymacro = new TCanvas("plot","plot", 1100, 1100);
TH2D *r1 = new TH2D("r1","y-axis title", 100, 150, 1000, 100, -14, 6);
gStyle->SetOptStat(0);
ifstream red1 ("/home/d/Scrivania/myMacro/log10_ggF_A_hZ_tautaull_TH_vs_mA_RED");
double x_bin;
double y_bin;
double ylog;
int i=0;
while(red1>>x_bin>>y_bin){
r1->Fill(x_bin, y_bin);
i++;
}
r1->SetFillColor(kRed);
r1->Draw("box");
TGaxis *nya = new TGaxis(150, -14, 150, 6, TMath::Power(10,-14), TMath::Power(10,6), 510, "G");
r1->GetYaxis()->SetLabelOffset(-100);
nya->Draw();
r1->GetXaxis()->SetTitle("x-axis title");
mymacro->Print("myMacro.pdf");
}
#ifndef __CINT__
int main() {
mymacro();
return 0 ; }
#endif
However, I do not know where those intermediate small black marks on the y-axis come from; I do not know what they mean. How could I delete them?
couet
January 15, 2016, 8:57am
6
void mymacro1() {
TCanvas *mymacro = new TCanvas("plot","plot", 1100, 1100);
TH2D *r1 = new TH2D("g1","g1", 100, 150, 1000, 100, 0.001, 100.);
gStyle->SetOptStat(0);
gStyle->SetOptTitle(0);
ifstream red1 ("RED.txt");
double x_bin;
double y_bin;
double y_bin2;
double ylog;
int i=0;
while(red1>>x_bin>>y_bin){
y_bin2 = ((y_bin+14)/20)*(100-0.001)+0.001;
r1->Fill(x_bin, y_bin2);
i++;
}
r1->SetFillColor(kRed);
r1->Draw("box");
}
Tiziano
January 15, 2016, 10:08am
7
If y_bin were equal to 0 y_bin2 should be equal to 1; however, this does not happen by using your code. Moreover, the y-axis I obtain is not the right one.
The result I previously obtained (dropbox.com/s/7kmd7g9flli88 … o.pdf?dl=0 ) was fine except for those strange marks on the y-axis.
Hi
“those strange marks on the y-axis” are the secondary tickmarks.
Its very hard (question to Olivier: impossible???) to get rid off.
In principle setting ndiv (normally 510) = 10 should do it,
but log scales seem tricky.
Otto
couet
January 15, 2016, 1:08pm
9
That’s right, the secondary ticks can not be removed in log scale.
Tiziano
January 15, 2016, 6:17pm
10
It does not work.
Are there any other methods to modify the y-axis?
Hi,
the following should do the trick:
void logaxis()
{
TCanvas *c = new TCanvas("c1", "c1", 10, 10, 400, 800);
Double_t fromy = -14, toy = 6;
TH2D* h2 = new TH2D("h2", "h2", 50,0,50, 50, fromy, toy);
h2->Draw();
Double_t x1 = h2->GetXaxis()->GetXmin();
Double_t y1 = h2->GetYaxis()->GetXmin();
Double_t y2 = h2->GetYaxis()->GetXmax();
h2->GetYaxis()->SetNdivisions(20); // make sure no 2nd ticks
TGaxis *nya = new TGaxis(x1, y1, x1, y2,
TMath::Power(10,fromy), TMath::Power(10,toy), 20, "SG");
h2->GetYaxis()->SetLabelOffset(-100); // hide orig labels
nya->SetTickLength(0); // hide ticks, see option "S" above
nya->Draw();
}
Caveat: This only works with integer limits when ticks of the linear and log
scale coincide
Otto