Skip to content

Read and Export data

The functions in the previous tutorial make plotting pretty automatic, using matplotlib's standard style. But sometimes, you might just want to read the data without immediately plotting it—maybe to style it your own way or to convert it into a different format.

from spinplots.plot import bruker1d
from spinplots.utils import nmr_df
import matplotlib.pyplot as plt

Customize spinplots

In the previous tutorial, we used bruker1d(['../../data/1D/8/pdata/1'], xlim=(250, -20)) to create a simple 13C NMR plot. By adding the option return_fig=True, you can return the figure object, which allows you to further customize the appearance of the spectrum as you like.

fig, ax = bruker1d(['../../data/1D/8/pdata/1'], xlim=(250, -20), return_fig=True)
ax.set_title('Customized plot')
ax.set_xlabel('$^{31}$P / ppm') 
ax.lines[0].set_color('purple')
ax.lines[0].set_linewidth(2)
ax.lines[0].set_linestyle('-.')
fig.savefig('../../data/1D/customized_plot.png')
No description has been provided for this image

Get Pandas Dataframe

A different option is to use the nmr_df function from spinplots.utils to get a Pandas DataFrame. This way, you can manipulate the data however you like before plotting.

df = nmr_df('../../data/1D/8/pdata/1')
print(df)
                hz         ppm     intensity  norm_intensity nuclei
0     45312.603793  257.397948  3.428250e+06        0.138226    13C
1     45306.580587  257.363734  3.387071e+06        0.136565    13C
2     45300.557381  257.329519  3.342889e+06        0.134784    13C
3     45294.534174  257.295304  3.294650e+06        0.132839    13C
4     45288.510968  257.261089  3.243548e+06        0.130778    13C
...            ...         ...           ...             ...    ...
8187  -3999.385176  -22.718481  2.074913e+06        0.083660    13C
8188  -4005.408382  -22.752696  2.093999e+06        0.084429    13C
8189  -4011.431588  -22.786911  2.114482e+06        0.085255    13C
8190  -4017.454795  -22.821126  2.133052e+06        0.086004    13C
8191  -4023.478001  -22.855340  2.149332e+06        0.086660    13C

[8192 rows x 5 columns]

plt.plot(df['ppm'], df['intensity'], color='green')
plt.xlim(250, -20)
plt.xlabel('$^{31}$C (ppm)')
plt.ylabel('Intensity (a.u.)')
plt.show()
No description has been provided for this image

Pandas DataFrames can be easily exported to other formats such as .csv. You can do this directly in your code with df.to_csv('exported_data.csv'), or use the bruker2csv function from the terminal for a quick export.

!bruker2csv ../../data/1D/8/pdata/1 ../../data/1D/exported_data.csv
Data written to ../../data/1D/exported_data.csv