使用rootpy和matplotlib绘制二维直方图

4

我想使用rootpy和matplotlib绘制ROOT 2D直方图。

我用的代码是:

from rootpy.io import File
from rootpy.plotting import Hist
import rootpy.plotting.root2matplotlib as rplt
import matplotlib.pyplot as plt
inputFile = File('mydata.root', 'read')
h_response = inputFile.myfolder.response

plt.figure(figsize=(16, 10), dpi=100)
rplt.hist(h_response, label='response matrix')
h_response.Draw()
plt.xlabel('reconstructed $E_{\mathrm{T}}^{miss}$')
plt.ylabel('Generated $E_{\mathrm{T}}^{miss}$')
plt.title('Response Matrix')
plt.savefig('ResponseMatrix.png')

然而,这导致了错误消息:
Traceback (most recent call last):
  File "/storage/Dropbox/Workspace/Analysis/DailyPythonScripts/src/unfolding.py", line 66, in <module>
    rplt.hist(h_response, label='response matrix')
  File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.0_a0-py2.7-linux-x86_64.egg/rootpy/plotting/root2matplotlib.py", line 140, in hist
    snap_zero=snap_zero)
  File "/usr/local/lib/python2.7/dist-packages/rootpy-0.7.0_a0-py2.7-linux-x86_64.egg/rootpy/plotting/root2matplotlib.py", line 82, in _set_bounds
    ywidth = ymax - ymin
TypeError: unsupported operand type(s) for -: 'list' and 'list'

显然我使用了错误的rootpy2matplotlib模块,因此我查看了一下:

该模块提供:hist,bar和errorbar函数 - 没有针对>= 2D的特定函数。

我是否遗漏了什么?是否有简单的解决方法?

附注:我想给这个问题打上“rootpy”标签,但这是不可能的。所以我道歉,因为这个问题非常具体。

2个回答

4

现在,rootpy的root2matplotlib接口为绘制2D ROOT直方图提供了hist2d、imshow和contour函数。请参见此处的示例:

https://github.com/rootpy/rootpy/blob/master/examples/plotting/plot_matplotlib_hist2d.py

from matplotlib import pyplot as plt
from rootpy.plotting import root2matplotlib as rplt
from rootpy.plotting import Hist2D
import numpy as np

a = Hist2D(100, -3, 3, 100, 0, 6)
a.fill_array(np.random.multivariate_normal(
    mean=(0, 3),
    cov=np.arange(4).reshape(2, 2),
    size=(1E6,)))

fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(15, 5))

ax1.set_title('hist2d')
rplt.hist2d(a, axes=ax1)

ax2.set_title('imshow')
im = rplt.imshow(a, axes=ax2)

ax3.set_title('contour')
rplt.contour(a, axes=ax3)

fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)

plt.show()

enter image description here


3

我刚刚在一个包含 TH2D 直方图的 ROOT 文件上尝试了你的脚本,一切都正常。

/opt/rootpy # cat version.txt 给出的版本是: 0.7.0

如果我检查我的 /usr/local/lib/python2.7/dist-packages/rootpy-dev-py2.7.egg/rootpy/plotting/root2matplotlib.py 并将其与你收到的错误消息进行比较,那么看起来我们正在使用不同版本的 rootpy。

请尝试最新版本的 rootpy。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接