Turning ripples into waves

XKC(T)D plot fun

Jakecob Vanderplas Sketch Path Filter got into the core of matplotlib 1.3. Now it is easy to produce plots with the XKCD style with just the following:

In [2]:
import matplotlib.pyplot as plt
plt.xkcd()  #<- That's it!
<matplotlib.rc_context at 0x7f1491b06510>

You will need the latest development version of matplotlib. (If you use OpenSUSE 12.3 just add my custom repository, here is a list of the packages available).

And here is my XKC(T)D profile ;-)

In [3]:
import gsw
import numpy as np
from oceans.sw_extras import gamma_GP_from_SP_pt
from ctd import DataFrame, Series, movingaverage, rosette_summary, lp_filter

cast = DataFrame.from_cnv('./data/CTD_001.cnv.gz', compression='gzip')
hours = cast['timeS'].max() / 60. / 60.
lon, lat = cast.longitude.mean(), cast.latitude.mean()

cast = cast[cast['pumps']]  # True for good values.
cast = cast[~cast['flag']]  # True for bad values blame SBE!

keep = set(['t090C', 'c0S/m', 'sbeox0Mm/Kg', 'dz/dtM'])
cast = cast.drop(keep.symmetric_difference(cast.columns), axis=1)  # Remove unwanted columns.

cast['dz/dtM'] = movingaverage(cast['dz/dtM'], window_size=48)
cast['sbeox0Mm/Kg'] = movingaverage(cast['sbeox0Mm/Kg'], window_size=48)

kw = dict(sample_rate=24.0, time_constant=0.15)
cast.index = lp_filter(cast.index, **kw)

downcast, upcast = cast.split()

downcast = downcast.press_check()  # Remove pressure reversals.
downcast = downcast[downcast['dz/dtM'] >= 0.25]  # Threshold velocity.

kw = dict(n1=2, n2=20, block=150)
downcast = downcast.apply(Series.despike, **kw)

downcast = downcast.apply(Series.bindata, **dict(delta=1.))
downcast = downcast.apply(Series.interpolate)

pmax = max(cast.index)
if pmax >= 500:
    window_len = 21
elif pmax >= 100:
    window_len = 11
    window_len = 5
kw = dict(window_len=window_len, window='hanning')
downcast = downcast.apply(Series.smooth, **kw)
In [4]:
fig, ax = downcast['t090C'].plot()
ax.set_ylabel("Pressure [dbar]")
fig.set_size_inches((5, 7))
In [5]:

This post was written as an IPython notebook. It is available for download or as a static html.

Creative Commons License
python4oceanographers by Filipe Fernandes is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at