Save Axis title


is it possible to save axis title together with a TGraph object in a TFile?
I successfully save the graph title, but not axis title…



I just try to:

  1. save a graph having a title along the X axis,
  2. exit root,
  3. enter root again,
  4. attached the new file,
  5. plot the graph in it… and the x axis is there…

Cat you provide a smal macro reproducing the problem ?
The macro I used to create graph is:

void graph() {
   TCanvas *c1 = new TCanvas("c1","A Simple Graph Example",200,10,700,500);

   const Int_t n = 20;
   Double_t x[n], y[n];
   for (Int_t i=0;i<n;i++) {
     x[i] = i*0.1;
     y[i] = 10*sin(x[i]+0.2)-5;
   gr = new TGraph(n,x,y);


I am running a compiled C++ program which uses root. In the program I create a TFile which is filled with a TGraph. Before Writing the graph to the TFile I set the graph title and also the axis title.
gr->GetXaxis()->SetTitle(“Segment Length [cm]”);

Then, once the program finishes, I open root and use a TBrowser to plot graphs. The graph title is correctly
saved and displayed while the axis title is not.

Any idea?

Try (as compiled or as interpreted):

[code]#include “TFile.h”
#include “TGraph.h”
#include “TAxis.h”

void WriteGraph(void)
TFile *f = new TFile(“Graph.root”, “RECREATE”);

Double_t X[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Double_t Y[10] = {0, 11, 22, 33, 44, 55, 66, 77, 88, 99};

TGraph *g = new TGraph(10, X, Y);
g->SetNameTitle(“MyGraphName”, “My Graph Title”);
g->GetXaxis()->SetTitle(“My X axis”);
g->GetYaxis()->SetTitle(“My Y axis”);


delete f;


void ReadGraph(void)
TFile *f = new TFile(“Graph.root”, “READ”);

TGraph *g = ((TGraph *)0);

f->GetObject(“MyGraphName”, g);

if (g) g->Draw(“AL*”);

// delete f;


void Graph(void)
Warning: If I use a TBrowser to display “MyGraphName” then I get a proper picture in ROOT 5.30 but a completely wrong picture in ROOT 5.28.


Sorry to resurrect such an old post, but I’ve been having similar problems with saving axis titles. I found after much trial and error, that if one modifies the graph points then the TAxis attributes are all reset. So in the example given by pepe, if I modify WriteGraph() as follows:

TGraph *g = new TGraph(10, X, Y); g->SetNameTitle("MyGraphName", "My Graph Title"); g->GetXaxis()->SetTitle("My X axis"); g->GetYaxis()->SetTitle("My Y axis"); g->SetPoint(1,1,10); // ADDED

Then the axis titles are reset to null strings. Is this expected behavior? As a user, it seems very odd to me. If so, could it be better documented please?

Yes, when you change a point, TGraph consider that it is a new graph and the underlaying histogram is deletd: … tml#Aex2JB
Set the titles after doing SetPoint