Skip to main content

A simple plotting package for use with zfit

Project description

alexPlot

A simple plotting library for plotting zfit PDFs and datasets, this package contains functions useful for plotting in 1D. These plotting functions are built with matplotlib functions and make use of zfit.Space and zfit.pdf.SumPDF objects. By default asymmetric errors are applied and pulls are computed with PDF integrals. The libarary can be used with the only_canvas option to act like another normal matplotlib plotting function.

                                                     ,ggggggggggg,                          
                            ,dPYb,                  dP"""88""""""Y8,,dPYb,             I8   
                            IP'`Yb                  Yb,  88      `8bIP'`Yb             I8   
                            I8  8I                   `"  88      ,8PI8  8I          88888888
                            I8  8'                       88aaaad8P" I8  8'             I8   
                  ,gggg,gg  I8 dP   ,ggg,      ,gg,   ,gg88"""""    I8 dP    ,ggggg,   I8   
                 dP"  "Y8I  I8dP   i8" "8i    d8""8b,dP" 88         I8dP    dP"  "Y8gggI8   
                i8'    ,8I  I8P    I8, ,8I   dP   ,88"   88         I8P    i8'    ,8I ,I8,  
               ,d8,   ,d8b,,d8b,_  `YbadP' ,dP  ,dP"Y8,  88        ,d8b,_ ,d8,   ,d8',d88b, 
               P"Y8888P"`Y88P'"Y88888P"Y8888"  dP"   "Y8 88        8P'"Y88P"Y8888P"  8P""Y8 

Setting up

To install

pip install alexPlot

or

git clone ssh://git@gitlab.cern.ch:7999/amarshal/alexPlot.git
pip install --no-dependencies -e .
python -c 'import alexPlot'

Then

import alexPlot

# to ask for help 
alexPlot.help()
# to ask for examples
alexPlot.examples()
# to overwrite default options
alexPlot.options.estimate_pulls = False

Plotting data

import zfit
import numpy as np
import alexPlot

# plot using numpy array
data = np.random.normal(0,1,1000)
alexPlot.plot_data(data, figure_title='Numpy example')

# plot using a zfit dataset
obs = zfit.Space("x", limits=(-5, 5)) 
data = zfit.Data.from_numpy(obs=obs, array=data)
alexPlot.plot_data(data, also_plot_hist=True, color='tab:blue', figure_title='zfit example')

Plotting pdf

# Example with KDE
obs = zfit.Space("x", limits=(-5, 5)) 
data = np.random.normal(0,1,1000)
data = zfit.Data.from_numpy(obs=obs, array=data)
model_KDE = zfit.pdf.GaussianKDE1DimV1(obs=obs, data=data, bandwidth='silverman')
alexPlot.plot_pdf(model_KDE)

# Example with an exponential plus a Gaussian
obs = zfit.Space("x", limits=(0, 30))
mean = zfit.Parameter("mean", 17,)
sigma = zfit.Parameter("sigma", 2,)
model_Gauss = zfit.pdf.Gauss(mean, sigma, obs)
lam = zfit.Parameter("lam", -0.1)
model_Exp = zfit.pdf.Exponential(lam, obs)
frac = zfit.Parameter("frac", 0.2,)
total_model = zfit.pdf.SumPDF([model_Gauss,model_Exp], obs=obs, fracs=[frac])
alexPlot.plot_pdf(total_model)

Plotting data and pdf

# Example with KDE
alexPlot.plot_pdf_data(model_KDE, data, filename='examples/example_KDE_data.png', figure_title='KDE')

# Example with an exponential plus a Gaussian
alexPlot.plot_pdf_data(total_model, data)

Extra functionality

# Add weights
alexPlot.plot_pdf_data(total_model, data_np, 
        weights=np.abs(np.random.normal(0,1,np.shape(data_np))), stack=True)

# Highlight a signal peak and zoom in
alexPlot.plot_pdf_data(total_model, data_np, dash_signal=True, ymax=50)

# Add lables
alexPlot.plot_pdf_data(total_model, data, 
                       dash_signal=True, label='Total PDF',
                       component_labels=['Signal', 'Background'], 
                       xlabel=r'Some dimension (MeV/$c^2$)', units=r'MeV/$c^2$')

# Plot a log yscale
alexPlot.plot_pdf_data(total_model, data, log=True)

# Plot multiple datasets
data_A = np.random.normal(-1,1,1000)
data_B = np.random.normal(2,1,10000)
alexPlot.plot_data([data_A, data_B], color=['tab:blue','tab:red'], also_plot_hist=True, bins=35)

# Plot multiple datasets normalised
alexPlot.plot_data([data_A, data_B], label=['Dataset A', 'Dataset B'], 
                density=True, also_plot_hist=True, bins=35)

# Use custom pyplot commands
alexPlot.plot_pdf_data(total_model, data, log=True, 
                extra_pyplot_commands=["plt.axvline(x=15,c='k')"])

# Overlay custom pyplot objects
plt.figure(figsize=(13,10))
alexPlot.plot_pdf_data(total_model, data, only_canvas=True, stack=True, 
                component_colors=['tab:cyan','tab:grey'], color='r', pulls=False)
plt.axhline(y=10,c='r')
plt.savefig("examples/only_canvas.png")
plt.close("all")

# Use xlims
alexPlot.plot_pdf_data(total_model, data, stack=True, xmin=10, xmax=22,
                component_colors=['tab:cyan','tab:grey'], color='r')

# Plot multiple PDFs at once (note stack only stacks PDFs within same SumPDF)
obs = zfit.Space("x", limits=(-5, 5)) 
data_np = np.random.normal(0,1,2500)
data = zfit.Data.from_numpy(obs=obs, array=data_np)
model_KDE_A = zfit.pdf.GaussianKDE1DimV1(obs=obs, data=data, bandwidth='silverman')
data = zfit.Data.from_numpy(obs=obs, array=data_np[:1250])
model_KDE_B = zfit.pdf.GaussianKDE1DimV1(obs=obs, data=data, bandwidth='silverman')
yield_A = zfit.Parameter("yield_A", 2500)
model_KDE_A.set_yield(yield_A)
yield_B = zfit.Parameter("yield_B", 1250)
model_KDE_B.set_yield(yield_B)
alexPlot.plot_pdf_data([model_KDE_A, model_KDE_B], data_np, color=["#ffb366",'b'], component_colors=[["#ffb366"],['b']], alpha=[1.,0.25], label=['plot_A', 'plot_B'], stack=True)

                                                     ,ggggggggggg,                          
                            ,dPYb,                  dP"""88""""""Y8,,dPYb,             I8   
                            IP'`Yb                  Yb,  88      `8bIP'`Yb             I8   
                            I8  8I                   `"  88      ,8PI8  8I          88888888
                            I8  8'                       88aaaad8P" I8  8'             I8   
                  ,gggg,gg  I8 dP   ,ggg,      ,gg,   ,gg88"""""    I8 dP    ,ggggg,   I8   
                 dP"  "Y8I  I8dP   i8" "8i    d8""8b,dP" 88         I8dP    dP"  "Y8gggI8   
                i8'    ,8I  I8P    I8, ,8I   dP   ,88"   88         I8P    i8'    ,8I ,I8,  
               ,d8,   ,d8b,,d8b,_  `YbadP' ,dP  ,dP"Y8,  88        ,d8b,_ ,d8,   ,d8',d88b, 
               P"Y8888P"`Y88P'"Y88888P"Y8888"  dP"   "Y8 88        8P'"Y88P"Y8888P"  8P""Y8 

test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

alexPlot-1.0.1.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

alexPlot-1.0.1-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file alexPlot-1.0.1.tar.gz.

File metadata

  • Download URL: alexPlot-1.0.1.tar.gz
  • Upload date:
  • Size: 15.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for alexPlot-1.0.1.tar.gz
Algorithm Hash digest
SHA256 9bb482c6cb9d9eee0495da2f7c8fdc829c1e89a9b2e375d31a18e43d83461777
MD5 fc9ccc00596dca8ecc697303e386accc
BLAKE2b-256 263ebd26b363b0664c01dd7d382a3d585ddf46d568246b9d58e6dbbbad781bee

See more details on using hashes here.

File details

Details for the file alexPlot-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: alexPlot-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.12

File hashes

Hashes for alexPlot-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1dba1b304357e4b07702fdedde0c930c9ba8bdd7210f6d10cd9188e1e89fb1f1
MD5 d22b9fd2e0140b934c03dc3a068944a1
BLAKE2b-256 0ad0a154a9f7cc24e9b89746e0c9c9761456435ebd0650f73e40055bf9bbf528

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page