Home | News | Documentation | Download

How to add tick marks and text right above plot

I would like to add tick marks and labels in between the tick marks on top of my plot. At the moment, I am trying to test out this as you see below.

# include <iostream>
# include <fstream>
# include <math.h>
# include <iomanip>
# include <cmath>
# include <stdlib.h>
# include <cstdlib>
//# include <fstream.h>
# include <string.h>
# include <string>
//# include <dos.h> //For Sleep() 

# include "TROOT.h"
# include "TFile.h"
# include "TTree.h"
# include "TBrowser.h"
# include "TH1.h"
# include "TH2.h"
# include "TH3.h"
# include "TGraph2D.h"
# include "TRandom.h"

int main(){

	//////Controls//////
	
	const char *inputFileName = "test_500.dat";
	
	/////////////////////////////////////////////////////////////////
	
	  // create the coordinate arrays
	vector<double> ages;
	vector<double> zones;
	vector<double> log_LHes;
	vector<double> dx;
    vector<double> dy;
	vector<double> dz;

	ifstream inFile(inputFileName);
	
	 if(inFile.is_open()){
    cout<<"Input File was opened successfully"<<endl;
  }
  
  //Main loop filling arrays from file//
  double lineage, linezone, lineHe;
  while (inFile>>lineage>>linezone>>lineHe) {
    ages.emplace_back(lineage);
    zones.emplace_back(linezone);
	log_LHes.emplace_back(lineHe);
	dx.emplace_back(0);
	dy.emplace_back(0);
	dz.emplace_back(0);
  }
  cout << "Read " << ages.size() << " lines\n";
  
  gStyle->SetPalette(62);
  
  TCanvas *c1 = new TCanvas("c1","log_LHe vs zone and age",200,10,1200,1000);
  
  TLatex latex;
   latex.SetTextSize(2);
   latex.SetTextAlign(13);  //align at top
   latex.DrawLatex(.2,1.4,"K_{S}");
  
  TGraph2DErrors *gr = new TGraph2DErrors(ages.size(),&ages[0],&zones[0],&log_LHes[0],&dx[0],&dy[0],&dz[0]);
	gr->SetTitle("log_LHe;star age (Gyr);zone number");
	gr->GetXaxis()->SetRangeUser(12.75403,12.88079);
	gPad->Modified();
	gr->Draw("colz");
	return 0;
	
}

However, when I go ahead and run my code, I see none of my sample latex text on my canvas. What am I missing? How can I put text and the tick marks above the plot?

test_500 file

ROOT Version: 6.11/02
Platform: Ubuntu 18.04
Compiler: Not Provided


Try, e.g.

latex.DrawLatexNDC(.2,.5,"K_{S}");

For ticks, you can add

gPad->SetTickx();  // or SetTicky() or SetTicks()

(see TPad).

{
   const char *inputFileName = "test_500.txt";

   // create the coordinate arrays
   vector<double> ages;
   vector<double> zones;
   vector<double> log_LHes;
   vector<double> dx;
   vector<double> dy;
   vector<double> dz;

   ifstream inFile(inputFileName);

   if (inFile.is_open())cout<<"Input File was opened successfully"<<endl;

   //Main loop filling arrays from file//
   double lineage, linezone, lineHe;
   while (inFile>>lineage>>linezone>>lineHe) {
      ages.emplace_back(lineage);
      zones.emplace_back(linezone);
      log_LHes.emplace_back(lineHe);
      dx.emplace_back(0);
      dy.emplace_back(0);
      dz.emplace_back(0);
   }
   cout << "Read " << ages.size() << " lines\n";

   gStyle->SetPalette(62);

   auto c1 = new TCanvas("c1","log_LHe vs zone and age",200,10,1200,1000);

   TGraph2DErrors *gr = new TGraph2DErrors(ages.size(),&ages[0],&zones[0],&log_LHes[0],&dx[0],&dy[0],&dz[0]);
   gr->SetTitle("log_LHe;star age (Gyr);zone number");
   gr->GetXaxis()->SetRangeUser(12.75403,12.88079);
   gPad->Modified();
   gr->Draw("colz");

   auto latex = new TLatex(.2,-20,"K_{S}");
   latex->SetTextSize(0.035);
   latex->Draw()
}

Perhaps I should clarify what I mean by tick marks. Let’s say I want to a tick mark on the top horizontal line of the plot at the x axis value (star age value) of 12.75403. Is there a way to specifically place tick marks on top of the plot at a point that correlates with an x value?

I attempted to do this with the modification, after reading here:

   c1->Update();
   c1->Range( 0., -500., 30., 500. );
   auto line = TLine(12.75403,0,12.75403,500);
   line.SetLineColor(kYellow);
   line.SetLineWidth(2);
   line.Draw();

But I am not getting a yellow line over my plot.

{
   const char *inputFileName = "test_500.txt";

   // create the coordinate arrays
   vector<double> ages;
   vector<double> zones;
   vector<double> log_LHes;
   vector<double> dx;
   vector<double> dy;
   vector<double> dz;

   ifstream inFile(inputFileName);

   if (inFile.is_open())cout<<"Input File was opened successfully"<<endl;

   //Main loop filling arrays from file//
   double lineage, linezone, lineHe;
   while (inFile>>lineage>>linezone>>lineHe) {
      ages.emplace_back(lineage);
      zones.emplace_back(linezone);
      log_LHes.emplace_back(lineHe);
      dx.emplace_back(0);
      dy.emplace_back(0);
      dz.emplace_back(0);
   }
   cout << "Read " << ages.size() << " lines\n";

   gStyle->SetPalette(62);

   auto c1 = new TCanvas("c1","log_LHe vs zone and age",200,10,1200,1000);

   TGraph2DErrors *gr = new TGraph2DErrors(ages.size(),&ages[0],&zones[0],&log_LHes[0],&dx[0],&dy[0],&dz[0]);
   gr->SetTitle("log_LHe;star age (Gyr);zone number");
   gr->GetXaxis()->SetRangeUser(12.75403,12.88079);
   gPad->Modified();
   gr->Draw("colz");


   auto line = new TLine(12.75403,0,12.75403,500);
   line->SetLineColor(kYellow);
   line->SetLineWidth(2);
   line->Draw();
}

@couet Thank you!

@couet Sorry, one more thing. Perhaps you already noticed, but the line

gr->GetXaxis()->SetRangeUser(12.75403,12.88079);

is not changing my x axis to only show that range. Does this line only work for histograms only? If so, how could I change the x axis range without histograms?

Put it after drawing the graph.

{
   const char *inputFileName = "test_500.txt";

   // create the coordinate arrays
   vector<double> ages;
   vector<double> zones;
   vector<double> log_LHes;
   vector<double> dx;
   vector<double> dy;
   vector<double> dz;

   ifstream inFile(inputFileName);

   if (inFile.is_open())cout<<"Input File was opened successfully"<<endl;

   //Main loop filling arrays from file//
   double lineage, linezone, lineHe;
   while (inFile>>lineage>>linezone>>lineHe) {
      ages.emplace_back(lineage);
      zones.emplace_back(linezone);
      log_LHes.emplace_back(lineHe);
      dx.emplace_back(0);
      dy.emplace_back(0);
      dz.emplace_back(0);
   }
   cout << "Read " << ages.size() << " lines\n";

   gStyle->SetPalette(62);

   auto c1 = new TCanvas("c1","log_LHe vs zone and age",200,10,1200,1000);

   TGraph2DErrors *gr = new TGraph2DErrors(ages.size(),&ages[0],&zones[0],&log_LHes[0],&dx[0],&dy[0],&dz[0]);
   gr->SetTitle("log_LHe;star age (Gyr);zone number");
   gPad->Modified();
   gr->Draw("colz");
   gPad->Update();
   gr->GetXaxis()->SetRangeUser(12.75403,12.88079);

   auto line = new TLine(12.75403,0,12.75403,500);
   line->SetLineColor(kYellow);
   line->SetLineWidth(2);
   line->Draw();
}