diff --git a/examples/lines_bars_and_markers/cohere_demo.py b/examples/lines_bars_and_markers/cohere_demo.py new file mode 100644 index 000000000000..370149695398 --- /dev/null +++ b/examples/lines_bars_and_markers/cohere_demo.py @@ -0,0 +1,34 @@ +""" +===================================== +Plotting the coherence of two signals +===================================== + +An example showing how to plot the coherence of two signals. +""" +import numpy as np +import matplotlib.pyplot as plt + +# Fixing random state for reproducibility +np.random.seed(19680801) + +dt = 0.01 +t = np.arange(0, 30, dt) +nse1 = np.random.randn(len(t)) # white noise 1 +nse2 = np.random.randn(len(t)) # white noise 2 + +# Two signals with a coherent part at 10Hz and a random part +s1 = np.sin(2 * np.pi * 10 * t) + nse1 +s2 = np.sin(2 * np.pi * 10 * t) + nse2 + +fig, axs = plt.subplots(2, 1) +axs[0].plot(t, s1, t, s2) +axs[0].set_xlim(0, 2) +axs[0].set_xlabel('time') +axs[0].set_ylabel('s1 and s2') +axs[0].grid(True) + +cxy, f = axs[1].cohere(s1, s2, 256, 1. / dt) +axs[1].set_ylabel('coherence') + +fig.tight_layout() +plt.show() diff --git a/examples/pylab_examples/cohere_demo.py b/examples/pylab_examples/cohere_demo.py deleted file mode 100644 index 96149c5d789b..000000000000 --- a/examples/pylab_examples/cohere_demo.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -Compute the coherence of two signals -""" -import numpy as np -import matplotlib.pyplot as plt - -# Fixing random state for reproducibility -np.random.seed(19680801) - - -# make a little extra space between the subplots -plt.subplots_adjust(wspace=0.5) - -dt = 0.01 -t = np.arange(0, 30, dt) -nse1 = np.random.randn(len(t)) # white noise 1 -nse2 = np.random.randn(len(t)) # white noise 2 -r = np.exp(-t/0.05) - -cnse1 = np.convolve(nse1, r, mode='same')*dt # colored noise 1 -cnse2 = np.convolve(nse2, r, mode='same')*dt # colored noise 2 - -# two signals with a coherent part and a random part -s1 = 0.01*np.sin(2*np.pi*10*t) + cnse1 -s2 = 0.01*np.sin(2*np.pi*10*t) + cnse2 - -plt.subplot(211) -plt.plot(t, s1, t, s2) -plt.xlim(0, 5) -plt.xlabel('time') -plt.ylabel('s1 and s2') -plt.grid(True) - -plt.subplot(212) -cxy, f = plt.cohere(s1, s2, 256, 1./dt) -plt.ylabel('coherence') -plt.show() diff --git a/examples/tests/backend_driver.py b/examples/tests/backend_driver.py index cf5a79b10036..370c794fe47e 100755 --- a/examples/tests/backend_driver.py +++ b/examples/tests/backend_driver.py @@ -52,6 +52,7 @@ files['lines'] = [ 'barh_demo.py', + 'cohere_demo.py', 'fill_demo.py', 'fill_demo_features.py', 'line_demo_dash_control.py', @@ -127,7 +128,6 @@ 'barcode_demo.py', 'boxplot_demo.py', 'broken_barh.py', - 'cohere_demo.py', 'color_by_yvalue.py', 'color_demo.py', 'colorbar_tick_labelling_demo.py', diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 56c99d9b3ec1..fe284b7b030c 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -7082,7 +7082,7 @@ def cohere(self, x, y, NFFT=256, Fs=2, Fc=0, detrend=mlab.detrend_none, Examples -------- - .. plot:: mpl_examples/pylab_examples/cohere_demo.py + .. plot:: mpl_examples/lines_bars_and_markers/cohere_demo.py """ if not self._hold: self.cla()