Adding contour lines into my contour plot


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/00
Platform: Ubuntu 20.04.1 LTS (WSL2)
Compiler: GNU or GCC


Hello,
I have already created a contour plot with the magnetic flux density and now I’m trying to add contour lines into the contour plot. How can I add contour lines into my plot? My code is shown below.

#include "TFile.h"
#include "TTree.h"
#include "TCanvas.h"
#include "TH1F.h"
#include <iostream>

using namespace std;


void Contour_Plot(){

const Int_t NRGBs = 5;
  const Int_t NCont = 99;

  Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
  Double_t red[NRGBs]   = { 0.00, 0.00, 0.87, 1.00, 0.51 };
  Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
  Double_t blue[NRGBs]  = { 0.51, 1.00, 0.12, 0.00, 0.00 }; 
  TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);   
  gStyle->SetNumberContours(NCont);
  
  TGraph2D *g=new TGraph2D("test_file.txt","%lg %lg %lg");
  	
   g->SetNpx(200);
   g->SetNpy(200);
   g->SetTitle("Contour plot of the magnetic flux density (T) in the x-y plane");
   g->GetHistogram()->GetXaxis()->SetTitle("x (m)");
   g->GetHistogram()->GetYaxis()->SetTitle("y (m)");
    TCanvas *c = new TCanvas("c", "c");
   g->Draw("COLZ");
   g->GetHistogram()->GetYaxis()->SetRangeUser(-5.0, 5.0);
  g->GetHistogram()->GetXaxis()->SetRangeUser(-5.0, 5.0);
  c->SetRealAspectRatio();
  c->Modified(); c->Update();
   
   gPad->Update();
}


g->Draw("COLZ CONT3"); // e.g., CONT2 or CONT3 or CONT5

1 Like

It worked. Thanks for the help