Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Noel Dawe committed Jul 26, 2012
1 parent d8d6b3e commit b69c2a0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 21 additions & 5 deletions examples/plotting/matplotlib_hist_example.py
@@ -1,9 +1,9 @@
#!/usr/bin/env python
import numpy as np
from rootpy.plotting import Hist, HistStack, Legend
from rootpy.plotting import Hist, HistStack, Legend, Canvas
import rootpy.plotting.root2matplotlib as rplt
import matplotlib.pyplot as plt

from matplotlib.ticker import AutoMinorLocator
import ROOT
# Setting this to True (default in rootpy)
# changes how the histograms look in ROOT...
Expand Down Expand Up @@ -43,20 +43,36 @@
stack.Add(h2)

# plot with ROOT
canvas = Canvas(width=700, height=500)
canvas.SetLeftMargin(0.15)
canvas.SetBottomMargin(0.15)
canvas.SetTopMargin(0.05)
canvas.SetRightMargin(0.05)
stack.Draw()
h3.Draw('E1 same')
h3.SetMaximum(max(h3) * 1.1)
stack.xaxis.SetTitle('Mass')
stack.yaxis.SetTitle('Events')
#stack.yaxis.SetLimits(0, max(h3) * 1.1)
#stack.yaxis.SetRangeUser(0, max(h3) * 1.1)
legend = Legend(2)
legend.AddEntry(h1, 'F')
legend.AddEntry(h2, 'F')
legend.AddEntry(h3, 'P')
legend.Draw()
canvas.Modified()
canvas.Update()

# plot with matplotlib
plt.figure(facecolor='white')
rplt.bar(stack, stacked=True)
rplt.errorbar(h3, emptybins=False)
fig = plt.figure(figsize=(7, 5), dpi=100, facecolor='white')
axes = plt.axes([0.15, 0.15, 0.8, 0.8])
axes.get_frame().set_linewidth(2)
axes.xaxis.set_minor_locator(AutoMinorLocator())
axes.yaxis.set_minor_locator(AutoMinorLocator())
axes.tick_params(which='major', labelsize=15, length=8)
axes.tick_params(which='minor', length=4)
rplt.bar(stack, stacked=True, axes=axes)
rplt.errorbar(h3, xerr=False, emptybins=False, axes=axes)
plt.xlabel('Mass', position=(1., 0.), ha='right')
plt.ylabel('Events', position=(0., 1.), va='top')
plt.legend(numpoints=1)
Expand Down
12 changes: 7 additions & 5 deletions rootpy/plotting/root2matplotlib.py
Expand Up @@ -31,7 +31,7 @@ def _set_defaults(h, kwargs, types=['common']):
defaults['fmt'] = h.GetMarkerStyle('mpl')
elif key == 'marker':
defaults['marker'] = h.GetMarkerStyle('mpl')
defaults['markersize'] = h.GetMarkerSize() * 6
defaults['markersize'] = h.GetMarkerSize() * 5
defaults['markeredgecolor'] = h.GetMarkerColor('mpl')
defaults['markerfacecolor'] = h.GetMarkerColor('mpl')
for key, value in defaults.items():
Expand All @@ -51,8 +51,8 @@ def _set_bounds(h,

xmin = h.xedgesl(0)
xmax = h.xedgesh(-1)
ymin = h.minimum()
ymax = h.maximum()
ymin = min(h)
ymax = max(h)

xwidth = xmax - xmin
if isinstance(xpadding, (tuple, list)):
Expand Down Expand Up @@ -313,8 +313,10 @@ def _errorbar(h, xerr, yerr, axes=None, emptybins=True, **kwargs):
y = np.array(list(h.y()))
if not emptybins:
nonempty = y != 0
xerr = xerr[:,nonempty]
yerr = yerr[:,nonempty]
x = x[nonempty]
y = y[nonempty]
if xerr is not False:
xerr = xerr[:,nonempty]
if yerr is not False:
yerr = yerr[:,nonempty]
return axes.errorbar(x, y, xerr=xerr, yerr=yerr, **kwargs)

0 comments on commit b69c2a0

Please sign in to comment.