# Python | Plotting Coherence

Python | Plotting Coherence: Here, we will learn about the plotting coherence and its Python implementation.
Submitted by Anuj Singh, on July 14, 2020

With many applications, coherence is an important part of Physics and other subjects too. Therefore, Matplotlib allows us to plot the coherence between two signals using the pyplot package. In this example, we are going to generate two random signals using a NumPy random package and then plot them. For coherence between two signals, the plotting is done by vertical sub plotting for better visualization. ## Python code for plotting coherence

```import numpy as np
import matplotlib.pyplot as plt

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

plt.figure(figsize=(10,8))
plt.subplot(211)
plt.plot(t, s1, t, s2)
plt.xlim(0, 2)
plt.xlabel('time')
plt.ylabel('s1 and s2')
plt.grid(True)

plt.subplot(212)
plt.cohere(s1, s2, 256, 1. / dt)
plt.ylabel('coherence')
plt.show()
```

Output:

```Output is as figure
```

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates