Interactive NMR DemoΒΆ

There are many ways to create interactive plots in a Juypter notebook, and the visualization ecosystem is constantly changing. For example the Holoviz tool suite ( looks promising (especially the possibility of creating a web application using Panel). Another interesting option is nbinteract (

This notebook currently uses ipywidgets and bokeh to create some simple NMR demonstrations.

import os
import sys
module_path = os.path.abspath(os.path.join('..'))
if module_path not in sys.path:
from nmrsim.dnmr import dnmr_AB
Help on function dnmr_AB in module nmrsim.dnmr:

dnmr_AB(va, vb, J, k, w, limits=None, points=800)
    Simulate the DNMR lineshape for two coupled nuclei undergoing exchange
    (AB or AX pattern at the slow-exchange limit).

    va, vb : float
        frequencies of a and b nuclei (at the slow exchange limit,
        in the absence of coupling)
    J : float
        the coupling constant between the two nuclei.
    k : float
        rate constant for state A--> state B
    w : float
        peak widths at half height (at the slow-exchange limit).
    limits : (int or float, int or float), optional
        The minimum and maximum frequencies (in any order) for the simulation.
    points : int
        The length of the returned arrays (i.e. the number of points plotted).

    x, y : numpy.array, numpy.array
        Arrays for the x (frequency) and y (intensity) lineshape data points.

    See Also
    DnmrAB : A class representation for this simulation.

    See the documentation for the nmrsim.dnmr module.

args = (
    200,  # va
    100,  # vb
    10,   # J
    0.1,  # k
    0.5   # w

from ipywidgets import interact
from import push_notebook, show, output_notebook
from bokeh.plotting import figure
Loading BokehJS ...
# get initial xy data
x, y = dnmr_AB(*args)
p = figure(title = 'DNMR AB Interactive Plot',
r = p.line(x, y)
def interactive_ab(va=110, vb=100, J=10, k=0.1, w=0.5):
    args = (va, vb, J, k, w)
    x, y = dnmr_AB(*args)['y'] = y['x'] = x

show(p, notebook_handle=True)
interact(interactive_ab, k=(0.1, 100))
<function __main__.interactive_ab(va=110, vb=100, J=10, k=0.1, w=0.5)>