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

Skip to content

Commit 6dcc1bb

Browse files
committed
added xcorr and acorr plot functions
svn path=/trunk/matplotlib/; revision=2978
1 parent 1fdadd0 commit 6dcc1bb

File tree

5 files changed

+114
-30
lines changed

5 files changed

+114
-30
lines changed

CHANGELOG

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
2007-01-11 Added Axes.xcorr to plot the cross correlation of x vs y -
2-
JDH
1+
2007-01-11 Added Axes.xcorr and Axes.acorr to plot the cross
2+
correlation of x vs y or the autocorrelation of x. pylab
3+
wrappers also provided. See examples/xcorr_demo.py - JDH
34

45
2007-01-10 Added "Subplot.label_outer" method. It will set the
56
visibility of the ticklabels so that yticklabels are only

boilerplate.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def %(func)s(*args, **kwargs):
4747
# these methods are all simple wrappers of Axes methods by the same
4848
# name.
4949
_plotcommands = (
50+
'acorr',
5051
'arrow',
5152
'axhline',
5253
'axhspan',
@@ -83,6 +84,7 @@ def %(func)s(*args, **kwargs):
8384
'quiver',
8485
'quiver2',
8586
'quiverkey',
87+
'xcorr',
8688
)
8789

8890
_misccommands = (

examples/xcorr_demo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from pylab import figure, show, nx
2+
3+
4+
x,y = nx.mlab.randn(2,100)
5+
fig = figure()
6+
ax1 = fig.add_subplot(211)
7+
ax1.xcorr(x, y)
8+
ax1.grid(True)
9+
ax1.axhline(0, color='black', lw=2)
10+
11+
ax2 = fig.add_subplot(212)
12+
ax2.acorr(x, normed=True)
13+
ax2.grid(True)
14+
ax2.axhline(0, color='black', lw=2)
15+
16+
show()

lib/matplotlib/axes.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2483,11 +2483,33 @@ def semilogy(self, *args, **kwargs):
24832483
return l
24842484
semilogy.__doc__ = dedent(semilogy.__doc__) % artist.kwdocd
24852485

2486+
def acorr(self, x, normed=False, detrend=detrend_none, **kwargs):
2487+
"""
2488+
Plot the autocorrelation of x. If normed=True, normalize the
2489+
data but the autocorrelation at 0-th lag. x is detrended by
2490+
the detrend callable (default no normalization.
2491+
2492+
data are plotted as plot(lags, c, **kwargs)
2493+
2494+
return value is lags, c, line where lags are a length
2495+
2*len(x)+1 lag vector, c is the 2*len(x)+1 auto correlation
2496+
vector, and line is a Line2D instance returned by plot. The
2497+
default linestyle is None and the default marker is 'o',
2498+
though these can be overridden with keyword args. The cross
2499+
correlation is performed with numerix cross_correlate with
2500+
mode=2.
2501+
2502+
The valid kwargs are Line2D properties:
2503+
%(Line2D)s
2504+
"""
2505+
return self.xcorr(x, x, normed, detrend, **kwargs)
2506+
acorr.__doc__ = dedent(acorr.__doc__) % artist.kwdocd
2507+
24862508
def xcorr(self, x, y, normed=False, detrend=detrend_none, **kwargs):
24872509
"""
24882510
Plot the cross correlation between x and y. If normed=True,
24892511
normalize the data but the cross correlation at 0-th lag. x
2490-
and y are deterned by the detrend callable (default no
2512+
and y are detrended by the detrend callable (default no
24912513
normalization. x and y must be equal length
24922514
24932515
data are plotted as plot(lags, c, **kwargs)

lib/matplotlib/pylab.py

Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
The majority of them, however, have matlab analogs
77
88
_Plotting commands
9+
acorr - plot the autocorrelation function
910
annotate - annotate something in the figure
1011
arrow - add an arrow to the axes
1112
axes - Create a new axes
@@ -79,8 +80,8 @@
7980
table - add a table to the plot
8081
text - add some text at location x,y to the current axes
8182
thetagrids - customize the radial theta grids and labels for polar
82-
8383
title - add a title to the current axes
84+
xcorr - plot the autocorrelation function of x and y
8485
xlim - set/get the xlimits
8586
ylim - set/get the ylimits
8687
xticks - set/get the xticks
@@ -1526,6 +1527,27 @@ def pcolor_classic(*args, **kwargs):
15261527
pcolor_classic.__doc__ = dedent(Axes.pcolor_classic.__doc__)
15271528

15281529
### Do not edit below this point
1530+
# This function was autogenerated by boilerplate.py. Do not edit as
1531+
# changes will be lost
1532+
def acorr(*args, **kwargs):
1533+
# allow callers to override the hold state by passing hold=True|False
1534+
b = ishold()
1535+
h = popd(kwargs, 'hold', None)
1536+
if h is not None:
1537+
hold(h)
1538+
try:
1539+
ret = gca().acorr(*args, **kwargs)
1540+
draw_if_interactive()
1541+
except:
1542+
hold(b)
1543+
raise
1544+
1545+
hold(b)
1546+
return ret
1547+
if Axes.acorr.__doc__ is not None:
1548+
acorr.__doc__ = dedent(Axes.acorr.__doc__) + """
1549+
Addition kwargs: hold = [True|False] overrides default hold state"""
1550+
15291551
# This function was autogenerated by boilerplate.py. Do not edit as
15301552
# changes will be lost
15311553
def arrow(*args, **kwargs):
@@ -1540,7 +1562,7 @@ def arrow(*args, **kwargs):
15401562
except:
15411563
hold(b)
15421564
raise
1543-
1565+
15441566
hold(b)
15451567
return ret
15461568
if Axes.arrow.__doc__ is not None:
@@ -1561,7 +1583,7 @@ def axhline(*args, **kwargs):
15611583
except:
15621584
hold(b)
15631585
raise
1564-
1586+
15651587
hold(b)
15661588
return ret
15671589
if Axes.axhline.__doc__ is not None:
@@ -1582,7 +1604,7 @@ def axhspan(*args, **kwargs):
15821604
except:
15831605
hold(b)
15841606
raise
1585-
1607+
15861608
hold(b)
15871609
return ret
15881610
if Axes.axhspan.__doc__ is not None:
@@ -1603,7 +1625,7 @@ def axvline(*args, **kwargs):
16031625
except:
16041626
hold(b)
16051627
raise
1606-
1628+
16071629
hold(b)
16081630
return ret
16091631
if Axes.axvline.__doc__ is not None:
@@ -1624,7 +1646,7 @@ def axvspan(*args, **kwargs):
16241646
except:
16251647
hold(b)
16261648
raise
1627-
1649+
16281650
hold(b)
16291651
return ret
16301652
if Axes.axvspan.__doc__ is not None:
@@ -1645,7 +1667,7 @@ def bar(*args, **kwargs):
16451667
except:
16461668
hold(b)
16471669
raise
1648-
1670+
16491671
hold(b)
16501672
return ret
16511673
if Axes.bar.__doc__ is not None:
@@ -1666,7 +1688,7 @@ def barh(*args, **kwargs):
16661688
except:
16671689
hold(b)
16681690
raise
1669-
1691+
16701692
hold(b)
16711693
return ret
16721694
if Axes.barh.__doc__ is not None:
@@ -1687,7 +1709,7 @@ def broken_barh(*args, **kwargs):
16871709
except:
16881710
hold(b)
16891711
raise
1690-
1712+
16911713
hold(b)
16921714
return ret
16931715
if Axes.broken_barh.__doc__ is not None:
@@ -1708,7 +1730,7 @@ def boxplot(*args, **kwargs):
17081730
except:
17091731
hold(b)
17101732
raise
1711-
1733+
17121734
hold(b)
17131735
return ret
17141736
if Axes.boxplot.__doc__ is not None:
@@ -1729,7 +1751,7 @@ def cohere(*args, **kwargs):
17291751
except:
17301752
hold(b)
17311753
raise
1732-
1754+
17331755
hold(b)
17341756
return ret
17351757
if Axes.cohere.__doc__ is not None:
@@ -1750,7 +1772,7 @@ def clabel(*args, **kwargs):
17501772
except:
17511773
hold(b)
17521774
raise
1753-
1775+
17541776
hold(b)
17551777
return ret
17561778
if Axes.clabel.__doc__ is not None:
@@ -1813,7 +1835,7 @@ def csd(*args, **kwargs):
18131835
except:
18141836
hold(b)
18151837
raise
1816-
1838+
18171839
hold(b)
18181840
return ret
18191841
if Axes.csd.__doc__ is not None:
@@ -1834,7 +1856,7 @@ def errorbar(*args, **kwargs):
18341856
except:
18351857
hold(b)
18361858
raise
1837-
1859+
18381860
hold(b)
18391861
return ret
18401862
if Axes.errorbar.__doc__ is not None:
@@ -1855,7 +1877,7 @@ def fill(*args, **kwargs):
18551877
except:
18561878
hold(b)
18571879
raise
1858-
1880+
18591881
hold(b)
18601882
return ret
18611883
if Axes.fill.__doc__ is not None:
@@ -1876,7 +1898,7 @@ def hist(*args, **kwargs):
18761898
except:
18771899
hold(b)
18781900
raise
1879-
1901+
18801902
hold(b)
18811903
return ret
18821904
if Axes.hist.__doc__ is not None:
@@ -1897,7 +1919,7 @@ def hlines(*args, **kwargs):
18971919
except:
18981920
hold(b)
18991921
raise
1900-
1922+
19011923
hold(b)
19021924
return ret
19031925
if Axes.hlines.__doc__ is not None:
@@ -1939,7 +1961,7 @@ def loglog(*args, **kwargs):
19391961
except:
19401962
hold(b)
19411963
raise
1942-
1964+
19431965
hold(b)
19441966
return ret
19451967
if Axes.loglog.__doc__ is not None:
@@ -2002,7 +2024,7 @@ def pie(*args, **kwargs):
20022024
except:
20032025
hold(b)
20042026
raise
2005-
2027+
20062028
hold(b)
20072029
return ret
20082030
if Axes.pie.__doc__ is not None:
@@ -2023,7 +2045,7 @@ def plot(*args, **kwargs):
20232045
except:
20242046
hold(b)
20252047
raise
2026-
2048+
20272049
hold(b)
20282050
return ret
20292051
if Axes.plot.__doc__ is not None:
@@ -2044,7 +2066,7 @@ def plot_date(*args, **kwargs):
20442066
except:
20452067
hold(b)
20462068
raise
2047-
2069+
20482070
hold(b)
20492071
return ret
20502072
if Axes.plot_date.__doc__ is not None:
@@ -2065,7 +2087,7 @@ def psd(*args, **kwargs):
20652087
except:
20662088
hold(b)
20672089
raise
2068-
2090+
20692091
hold(b)
20702092
return ret
20712093
if Axes.psd.__doc__ is not None:
@@ -2107,7 +2129,7 @@ def semilogx(*args, **kwargs):
21072129
except:
21082130
hold(b)
21092131
raise
2110-
2132+
21112133
hold(b)
21122134
return ret
21132135
if Axes.semilogx.__doc__ is not None:
@@ -2128,7 +2150,7 @@ def semilogy(*args, **kwargs):
21282150
except:
21292151
hold(b)
21302152
raise
2131-
2153+
21322154
hold(b)
21332155
return ret
21342156
if Axes.semilogy.__doc__ is not None:
@@ -2191,7 +2213,7 @@ def stem(*args, **kwargs):
21912213
except:
21922214
hold(b)
21932215
raise
2194-
2216+
21952217
hold(b)
21962218
return ret
21972219
if Axes.stem.__doc__ is not None:
@@ -2212,7 +2234,7 @@ def vlines(*args, **kwargs):
22122234
except:
22132235
hold(b)
22142236
raise
2215-
2237+
22162238
hold(b)
22172239
return ret
22182240
if Axes.vlines.__doc__ is not None:
@@ -2275,13 +2297,34 @@ def quiverkey(*args, **kwargs):
22752297
except:
22762298
hold(b)
22772299
raise
2278-
2300+
22792301
hold(b)
22802302
return ret
22812303
if Axes.quiverkey.__doc__ is not None:
22822304
quiverkey.__doc__ = dedent(Axes.quiverkey.__doc__) + """
22832305
Addition kwargs: hold = [True|False] overrides default hold state"""
22842306

2307+
# This function was autogenerated by boilerplate.py. Do not edit as
2308+
# changes will be lost
2309+
def xcorr(*args, **kwargs):
2310+
# allow callers to override the hold state by passing hold=True|False
2311+
b = ishold()
2312+
h = popd(kwargs, 'hold', None)
2313+
if h is not None:
2314+
hold(h)
2315+
try:
2316+
ret = gca().xcorr(*args, **kwargs)
2317+
draw_if_interactive()
2318+
except:
2319+
hold(b)
2320+
raise
2321+
2322+
hold(b)
2323+
return ret
2324+
if Axes.xcorr.__doc__ is not None:
2325+
xcorr.__doc__ = dedent(Axes.xcorr.__doc__) + """
2326+
Addition kwargs: hold = [True|False] overrides default hold state"""
2327+
22852328
# This function was autogenerated by boilerplate.py. Do not edit as
22862329
# changes will be lost
22872330
def cla(*args, **kwargs):

0 commit comments

Comments
 (0)