# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (Belle2)
#     language: python
#     name: python3
# ---

# # Introduction to plotting 

import sciplot as sp
import matplotlib.pyplot as plt
# %matplotlib inline

plt.style.use('sciplot')

# %load_ext autoreload
# %autoreload 2

# ### Creating some pseudo data

# +
import pandas as pd
import numpy as np

ns =30000
nb =80000

df = {'mass': np.append(np.random.random_sample(nb)*7 - 3.5, np.random.normal(0, 0.5, ns))}
df['sig'] = np.append(np.zeros(nb),np.ones(ns), )
df['exp'] = np.random.randint(0,8, ns+nb)
df['gauss'] = np.append(np.random.random_sample(nb)*7 - 3.5, np.random.normal(-1, 0.5, ns))

df = pd.DataFrame(df)

df1 = {'mass': np.append(np.random.random_sample(nb)*7 - 3.5, np.random.random_sample(ns)*7 - 3.5 )}
df1['sig'] = np.append(np.zeros(nb),np.ones(ns), )
df1 = pd.DataFrame(df1)
# -

# # Simple Plot

sp.hist(df.mass, lw=2, scale=0.5, weights=np.random.normal(1,0.1,len(df)))
sp.xlim()
sp.labels("Mass", "Events", root_style=1)

sp.figure()
sp.hist(df.mass, color=1, style=0)
sp.xlim()
sp.labels("Mass", "Events", root_style=1)

xx = df.sample(50).mass
sp.figure()
sp.errorhist(xx, color='black')
sp.xlim()
plt.ylim(0)
sp.labels("Mass", "Events", 'GeV', root_style=1)

sp.errorhist(xx, box=True)
sp.xlim()
plt.ylim(0)
sp.labels("Mass", "Events", 'GeV',root_style=1)

# # Several Distrtibutions

# +
sp.figure()

sp.hist(df.mass,  style=0,)

sp.hist(df1.mass,  lw=2)

sp.xlim()
sp.labels("Mass", "Events",'' )
# -

sp.sig_bkg_plot(df, "mass", 'sig')

sp.figure()
sp.hist(df[df.sig==1].mass,  style=0, color=0, range=(-3,3), label='Signal')
sp.hist(df1.mass,  style=1, color=1, label='MC1')
sp.hist(np.random.normal(-3,1, 10000), style=2, color=3, label='NP')
sp.xlim()
plt.legend()
sp.labels("Mass", "Events",'GeV' ,1)

with plt.style.context("sciplot_ticks"):
    sp.stacked(df, "mass", 'exp', bins=50,)
    # sp.errorbar(df1.mass, color='black')
    plt.legend()
    sp.labels('$\Delta M$', "Events", "GeV", )

    sp.xlim()

sp.stacked([df[df.exp==2].mass, 
                df[df.exp==3].mass], 
               bins=50, lw=.25)
# sp.errorbar(df1.mass, color='black')
sp.xlim()

sp.stacked(df, "mass", 'exp', bins=50, color=sp.b2helix(8), label=range(8))
sp.errorhist(df.mass.values, color='black', weights=np.random.normal(1.01,0.9, len(df)), label="Data")
sp.xlim()
plt.legend()
sp.labels("$M$", "Events", 'GeV', 1)

from sciplot.analysis import plot_flatness


with plt.style.context(('sciplot_ticks')):
    sp.figure()
    plot_flatness(df.mass, df.gauss, xrange= (-4,3))
    sp.xlim()

y,x,h = sp.stacked([df[df.exp==2].mass, 
                df[df.exp==3].mass], 
               bins=50, lw=.25)
sp.errorbar((x[:-1]+ x[1:])/2, y, np.sqrt(y) )



StarcoderdataJupyterScriptsDedupFiltered