Question

I am trying to plot a ROOT 2D histogram with rootpy and matplotlib.

The code I use for this is:

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')

However, this leaves me with the error msg:

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'

Obviously I am using the wrong rootpy2matplotlib module, so I had a look: The module provides: hist, bar and errorbar functions - no specific for >= 2D.

Am I missing something? Is there an easy workaround?

PS: I would like to tag this question with a 'rootpy' tag, but it is not possible. So I apologise, since this question is quite specific.

Was it helpful?

Solution

rootpy's root2matplotlib interface now provides the hist2d, imshow and contour functions for plotting 2D ROOT histograms. See the example here:

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

OTHER TIPS

I just tried your script on a ROOT file containing a TH2D histogram. Everything worked.

/opt/rootpy # cat version.txt gives me: 0.7.0

if I check my

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

and compare it to the error messages you get, then it looks like we are using different versions of rootpy.

Try the newest version of rootpy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top