Can I get an example showing how to plot the coherence of two signals using matplotlib?

May 27, 2019 in Python
edited May 27, 2019 1,731 views

## 1 answer to this question.

This should work well:

```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()```

Will give the following output:

answered May 27, 2019 by anonymous

edited May 27, 2019 by Kalgi

## How to plot a histogram when you have two variables on the same side? two graphes in the same chart?

You don't have to use two charts. ...READ MORE

## I want to download a file from the website by web scraping. Can anyone explain how to do this in jupyter lab (python) with an example?

Hey, Web scraping is a technique to automatically ...READ MORE

## How to check the memory usage of an object in python

Hi, @Roshni, You can use this code below: import ...READ MORE

## How to override the default value of a Model Field from an Abstract Base Class

Hello @kartik, You can do this as follows: class ...READ MORE

## Example code for creating broken barh graph using matplotlib

Try this, it should work well. import matplotlib.pyplot ...READ MORE

## How to use categorical variables in Matplotlib?

Many times you want to create a ...READ MORE