# Pyroot script crashes when adding second axis

**URL:** <https://root-forum.cern.ch/t/pyroot-script-crashes-when-adding-second-axis/47316>\
**Category:** Newbie\
**Tags:** graphics, python, root\
**Created:** [October 19, 2021, 5:56pm UTC](https://root-forum.cern.ch/t/pyroot-script-crashes-when-adding-second-axis/47316 "2021-10-19T17:56:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [October 19, 2021, 5:56pm UTC](https://root-forum.cern.ch/t/pyroot-script-crashes-when-adding-second-axis/47316/1 "2021-10-19T17:56:00Z")

</div>

Hello!  
I have this simple Python script, however it crashes when I try to add a secondary x axis. The canvas gets somehow deleted after the TGaxis::Draw command.

```auto
TypeError: void TPad::SaveAs(const char* filename = "", const char* option = "") =>
    TypeError: callable was deleted

```

Any idea what I might be doing wrong?

```auto
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import sys
import numpy as np
from ROOT import TFile, TTree, TCanvas, TH1D, TLegend, TF1, TGaxis, TGraph, TLine, gStyle, gPad, gROOT, kBlack, kRed, kBlue, kGreen, kOrange, kMagenta, kViolet, kCyan, kRed, kBlue, kYellow

hustart = [-1024, -975, -925, -875, -825,
           -775, -300, -250, -200, -150,
           -100, -75, -50, -25, 0,
           15, 25, 40, 60, 75,
           90, 130, 170, 200, 230,
           260, 295, 330, 360, 390,
           420, 450, 480, 510, 540,
           570, 595, 625, 650, 680,
           3072]

humed = [(hustart[i-1] + hustart[i])/2. for i in range(1,len(hustart))]
matids = [i-1 for i in range(1,len(hustart))]

mat2hu = TGraph(len(matids), np.asarray(matids,'d'), np.asarray(humed,'d'))

c = TCanvas('Attenuation')
c.SetGridx()
c.SetGridy()
h = TH1D('h','',40,-0.5, 39.5)
h.SetBinContent(2, 4)
h.Draw()

c.Modified()
c.Update()

def myfunc(x):
    val = mat2hu.Eval(x[0])
    return val

myfunc = TF1("myfunc",myfunc,0,39)
A1 = TGaxis(0,gPad.GetUymax(),39,gPad.GetUymax(),"myfunc",515,"-")
A1.Draw()

c.SaveAs('/tmp/'+c.GetName()+'.png')

```

using:

```auto
   ------------------------------------------------------------------
  | Welcome to ROOT 6.25/01 https://root.cern |
  | (c) 1995-2021, The ROOT Team; conception: R. Brun, F. Rademakers |
  | Built for linuxx8664gcc on Sep 01 2021, 23:19:42 |
  | From heads/master@v6-25-01-1861-g6b33e04cac |
  | With c++ (Ubuntu 8.4.0-1ubuntu1~18.04) 8.4.0 |
  | Try '.help', '.demo', '.license', '.credits', '.quit'/'.q' |
   ------------------------------------------------------------------

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [October 20, 2021, 6:38am UTC](https://root-forum.cern.ch/t/pyroot-script-crashes-when-adding-second-axis/47316/2 "2021-10-20T06:38:08Z")

</div>

it must be a python issue because a C reduced version of your script works fine:

```auto
{
   auto c = new TCanvas("Attenuation");
   c->SetGridx();
   c->SetGridy();
   auto h = new TH1D("h","",40,-0.5, 39.5);
   h->SetBinContent(2, 4);
   h->Draw();

   auto A1 = new TGaxis(0,gPad->GetUymax(),39,gPad->GetUymax(),"myfunc",515,"-");
   A1->Draw();

   c->Print("a.png")
}

```

 ![Screenshot 2021-10-20 at 08.35.43](https://root-forum.cern.ch/uploads/default/original/3X/4/e/4e751a481bd6cc0d5a3b6746de8c389dbc3b1a92.jpeg)

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [October 20, 2021, 7:19am UTC](https://root-forum.cern.ch/t/pyroot-script-crashes-when-adding-second-axis/47316/3 "2021-10-20T07:19:41Z")

</div>

Yes, maybe I am not defining well the `def myfunc` function or `mat2hu` has to be made a global variable? I normally use C++, so if anyone with more experience with Python can help with this, I would appreciate it 🙂

---

<div class="post-metadata">

**Author:** ![ferhue](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ferhue/32/29462_2.png) [@ferhue](https://root-forum.cern.ch/u/ferhue)\
**Post date:** [October 20, 2021, 10:40am UTC](https://root-forum.cern.ch/t/pyroot-script-crashes-when-adding-second-axis/47316/4 "2021-10-20T10:40:18Z")

</div>

Ok, I managed to fix it, by changing the following:

`myfunc2 = TF1("myfunc",myfunc,0,39)`

and:  
`def myfunc(x,p):`
