2012-10-30 60 views
2

我想用rootpy和matplotlib繪製一個根二維直方圖。用rootpy和matplotlib繪製二維直方圖

我使用這個代碼是:

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。

我錯過了什麼嗎?有一個簡單的解決方法嗎? PS:我想用'rootpy'標籤來標記這個問題,但這是不可能的。所以我很抱歉,因爲這個問題很具體。

回答

3

rootpy的root2matplotlib接口現在提供hist2d,imshow和輪廓功能繪製2D ROOT直方圖。在這裏看到的例子:

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

+0

非常感謝您! – DragonTux

3

我剛剛在包含TH2D直方圖的ROOT文件上試過你的腳本。一切正常。

的/ opt/rootpy#貓version.txt 給我:0.7.0

如果我檢查我的

/usr/local/lib/python2.7/dist-packages/rootpy-dev -py2.7.egg/rootpy /繪圖/ root2matplotlib.py

,並將其與你的錯誤信息,那麼它看起來像我們使用不同版本的rootpy的。

嘗試rootpy的最新版本。