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

标签 python matplotlib root-framework rootpy

我正在尝试使用 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。

我错过了什么吗?有简单的解决方法吗?

PS:我想用“rootpy”标签来标记这个问题,但这是不可能的。所以我很抱歉,因为这个问题非常具体。

最佳答案

rootpy 的 root2matplotlib 接口(interface)现在提供用于绘制 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

关于python - 使用 rootpy 和 matplotlib 绘制二维直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13143911/

相关文章:

python - 求点与线之间的距离,得到该点在一条线上的投影距离

python - curve_fit函数与原始数据不完美拟合

c++ - 分段拟合 ROOT Cern,范围不受尊重

c++ - 从文本文件创建和填充 ROOT 文件

c++ - 编译几个源文件(主文件和头文件)并在 ROOT CINT 中链接它们?

python - 如何从长格式获取 Anscombe 数据宽格式

python - Tkinter 显示初始屏幕并隐藏主屏幕,直到 __init__ 完成

python - Pandas : how to merge 2 dataframes on key1. str.endswith(key2)

python - 在 Travis CI 中测试基于 matplotlib 的绘图

python - pyplot.scatter(dataframe) 与 dataframe.plot(kind ='scatter' )