Making the x and y axis the same axis ratio (1:1)


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’m trying to make the x and y-axis of my contour plot having the same axis ratio (1:1). I have
created a contour plot, but the x and y-axis don’t have the same axis ration. How do I make both
axis the same ratio (1:1) ?

#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)");
   g->Draw("COLZ");
   g->GetYaxis()->SetRange(-0.05,0.05);
   g->GetXaxis()->SetRange(-0.05,0.05);
   gPad->Update();
}

Try:

  TCanvas *c = new TCanvas("c", "c");
  g->Draw("COLZ");
  g->GetHistogram()->GetYaxis()->SetRangeUser(-0.05, 0.05);
  g->GetHistogram()->GetXaxis()->SetRangeUser(-0.05, 0.05);
  c->SetRealAspectRatio();
  c->Modified(); c->Update();

See also: Root Forum → Search → aspect ratio

That worked. Thanks for the help