Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fdadd0

Browse files
committed
added xcorr plot function
svn path=/trunk/matplotlib/; revision=2977
1 parent 6dd270a commit 1fdadd0

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2007-01-11 Added Axes.xcorr to plot the cross correlation of x vs y -
2+
JDH
3+
14
2007-01-10 Added "Subplot.label_outer" method. It will set the
25
visibility of the ticklabels so that yticklabels are only
36
visible in the first column and xticklabels are only

lib/matplotlib/axes.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
transpose, log, log10, Float, Float32, ravel, zeros,\
77
Int16, Int32, Int, Float64, ceil, indices, \
88
shape, which, where, sqrt, asum, compress, maximum, minimum, \
9-
typecode, concatenate, newaxis, reshape, resize, repeat
9+
typecode, concatenate, newaxis, reshape, resize, repeat, cross_correlate, nonzero
1010

1111
import numerix.ma as ma
1212

@@ -2208,7 +2208,7 @@ def vlines(self, x, ymin, ymax, fmt='k-', **kwargs):
22082208
return lines
22092209
vlines.__doc__ = dedent(vlines.__doc__) % artist.kwdocd
22102210

2211-
2211+
22122212
#### Basic plotting
22132213
def plot(self, *args, **kwargs):
22142214
"""
@@ -2366,6 +2366,7 @@ def plot_date(self, x, y, fmt='bo', tz=None, xdate=True, ydate=False,
23662366
return ret
23672367
plot_date.__doc__ = dedent(plot_date.__doc__) % artist.kwdocd
23682368

2369+
23692370
def loglog(self, *args, **kwargs):
23702371
"""
23712372
LOGLOG(*args, **kwargs)
@@ -2482,6 +2483,45 @@ def semilogy(self, *args, **kwargs):
24822483
return l
24832484
semilogy.__doc__ = dedent(semilogy.__doc__) % artist.kwdocd
24842485

2486+
def xcorr(self, x, y, normed=False, detrend=detrend_none, **kwargs):
2487+
"""
2488+
Plot the cross correlation between x and y. If normed=True,
2489+
normalize the data but the cross correlation at 0-th lag. x
2490+
and y are deterned by the detrend callable (default no
2491+
normalization. x and y must be equal length
2492+
2493+
data are plotted as plot(lags, c, **kwargs)
2494+
2495+
return value is lags, c, line where lags are a length
2496+
2*len(x)+1 lag vector, c is the 2*len(x)+1 cross correlation
2497+
vector, and line is a Line2D instance returned by plot. The
2498+
default linestyle is None and the default marker is 'o',
2499+
though these can be overridden with keyword args. The cross
2500+
correlation is performed with numerix cross_correlate with
2501+
mode=2.
2502+
2503+
The valid kwargs are Line2D properties:
2504+
%(Line2D)s
2505+
"""
2506+
kwargs = kwargs.copy()
2507+
kwargs.setdefault('marker', 'o')
2508+
kwargs.setdefault('linestyle', 'None')
2509+
2510+
Nx = len(x)
2511+
assert(Nx==len(y))
2512+
x = detrend(asarray(x))
2513+
y = detrend(asarray(y))
2514+
2515+
c = cross_correlate(x, y, mode=2)
2516+
2517+
if normed: c/=c[Nx-1]
2518+
2519+
2520+
lags = arange(-Nx+1,Nx)
2521+
line, = self.plot(lags, c, **kwargs)
2522+
return lags, c, line
2523+
xcorr.__doc__ = dedent(xcorr.__doc__) % artist.kwdocd
2524+
24852525
def legend(self, *args, **kwargs):
24862526
"""
24872527
LEGEND(*args, **kwargs)

0 commit comments

Comments
 (0)