我想在 ROOT TGraph 中绘制一些 numpy 数组(使用这个构造函数https://root.cern.ch/root/html/TGraph.html#TGraph:TGraph@4),但我得到了意想不到的结果。鉴于此代码:
import numpy as np
from ROOT import TGraph
import math
ac = np.empty([4,4], np.dtype('float64'))
# just filling with some float values
for i in range(0,len(ac[:,1])):
ac[i,1]= (1/9.*float(i+1)+10)
print type(ac[:,1])
print len(ac[:,1])
# calling the TGraph constructor
rplot2 =TGraph(len(ac[:,1]),ac[:,1],ac[:,1])
输出是:
<type 'numpy.ndarray'>
4
Traceback (most recent call last):
File "test.py", line 13, in <module>
rplot2 =TGraph(len(ac[:,1]),ac[:,1],ac[:,1])
TypeError: none of the 11 overloaded methods succeeded. Full details:
TGraph::TGraph() =>
takes at most 0
[I suppress here some non-relevant output]
TGraph::TGraph(Int_t n, const Int_t* x, const Int_t* y) =>
could not convert argument 2
TGraph::TGraph(Int_t n, const Float_t* x, const Float_t* y) =>
could not convert argument 2
TGraph::TGraph(Int_t n, const Double_t* x, const Double_t* y) =>
could not convert argument 2
而如果我声明一个单维数组:
import numpy as np
from ROOT import TGraph
import math
ac = np.empty(4, np.dtype('float64'))
# just filling with some float values
for i in range(0,len(ac)):
ac[i]= (1/9.*float(i+1)+10)
print type(ac[:])
print len(ac[:])
# calling the TGraph constructor
rplot2 =TGraph(len(ac[:]),ac[:],ac[:])
输出是:
<type 'numpy.ndarray'>
4
那没问题。我可以解决第一个行为:
rplot2 =TGraph(len(ac[:,1]),np.array(ac[:,1]),np.array(ac[:,1]))
但我不明白为什么。另外,您有其他解决方法的想法吗?