Skip to content

Plot overlayed 2D spectra

Jonathan J. Helmus edited this page Sep 30, 2013 · 1 revision

Create a plot of overlayed two-dimensional spectra

This example shows how nmrglue can be used to create a plot of two 2D spectra overlayed on each other.

Spectrum read using nmrglue can be plotted with matplotlib. Often it is useful to create a plot of two or more spectra overlayed on top of each other to visualize difference between spectra collected using different parameters or conditions. In this example a plot is created which overlay two simulated Sparky files with different spectral parameters.

Instructions

plot_overlay.py source code

#! /usr/bin/env python
# Create a plot of overlayed 2D spectra

import matplotlib.pyplot as plt
import nmrglue as ng
import numpy as np

# contour levels
contour_start = 1       # contour level start value
contour_num = 20        # number of contour levels
contour_factor = 1.25   # scaling factor between contour levels
cl = contour_start * contour_factor ** np.arange(contour_num)

# a list of Sparky filenames to plot
fnames = ['test_01.ucsf', 'test_02.ucsf']

# the contour colors to plot the above spectra with
colors_list = ['blue', 'red']

# create the figure
fig = plt.figure()
ax = fig.add_subplot(111)

# loop over the files and colors
for fname, colors in zip(fnames, colors_list):

    # read in the data from the Sparky (ucsf) file
    dic, data = ng.sparky.read(fname)

    # make ppm scales for both dimensions.
    uc_x = ng.sparky.make_uc(dic, data, dim=1)
    x0, x1 = uc_x.ppm_limits()
    uc_y = ng.sparky.make_uc(dic, data, dim=0)
    y0, y1 = uc_y.ppm_limits()


    print "Plotting spectrum in file:", fname
    print "1H Limits:", x0, x1
    print "15N Limits", y0, y1
    print "color:", colors
    # plot the contours
    ax.contour(data, cl, colors=colors, extent=(x0, x1, y0, y1))

# decorate the axes and set limits
ax.set_ylabel("15N (ppm)")
ax.set_xlabel("1H (ppm)")
ax.set_xlim(10.5, 6.5)
ax.set_ylim(130, 105)

# save the figure
fig.savefig("overlayed_2d.png")

Output:

Overlayed 2D spectra

Additional

The simulated spectra test_01.ucsf and test_02.ucsf were created using the Python scripts simulate_spectrum_01.py and simulate_spectrum_02.py, which require the peaks_01.txt and peaks_02.txt files.