# Plotting different segment in different color
import numpy as np
import matplotlib.pyplot as plt
# Create test data
section_one_x1 = np.linspace(0,2.5,100)
section_one_y1 = section_one_x1**3
section_two_x2 = np.linspace(2.5,5,100)
section_two_y2 = section_two_x2**3
# Plot segment one in blue color
plt.plot(section_one_x1, section_one_y1, 'b')
# Plot segment two in red color
plt.plot(section_two_x2, section_two_y2, 'r')
plt.grid(True)
plt.legend(["No Arc", "Arc"])
plt.show()
Fig. Multicolored plot
# Plotting sine wave with different segment in different color
import numpy as np
from numpy import genfromtxt
import matplotlib.pyplot as plt
import math
# Create test data
section_one_x1 = np.linspace(0,3.1416*2,500)
section_one_y1 = np.sin(section_one_x1)
section_two_x2 = np.linspace(3.1416*2,4*3.1416,500)
section_two_y2 = np.sin(section_two_x2)
plt.figure(dpi=200)
# Plot segment one in blue color
plt.plot(section_one_x1, section_one_y1, 'b')
# Plot segment two in red color
plt.plot(section_two_x2, section_two_y2, 'r')
plt.grid(True)
plt.legend(["Blue Section", "Red Section"], loc="best")
plt.xlabel("Value of x")
plt.ylabel("Sine (x)")
plt.show()
Python tutorials:
Multivariable Linear Regression using Gradient Descent Algorithm in Python,Step by Step from scratch
Merging Pandas DataFrames based on Multiple Columns, Indexing, Slicing using loc and iloc. Python
NumPy Array Complete Basic Tutorial: 1D, 2D, 3D array Manipulation, slicing, indexing, concatenation
Data Visualization, PYTHON MULTI COLOR PLOT using Matplotlib: add legends, title, labels
My requirements of the filters are:
Sample Period — 5 sec (t)
Sampling Freq — 30 samples / s , i.e 30 Hz (fs)
Total Samples — (fs x t) = 150
Signal Freq = 6 signal / 5 sec = 1.2 Hz
Nyquist Frequency = 0.5 * fs
order = Polynomial order of the signal
Please do the followings:
1. Create some sample data with noise
2. Filter implementation using scipy
3. Filter and plot the input output data.
## CODE SNIPPET IS GIVEN BELOW
import numpy as np
import plotly.graph_objects as go
from scipy.signal import butter, filtfilt
# Filter requirements
T = 5.0 # Sample Period
fs = 30.0 # sample rate, Hz
cutoff = 2.0 # desired cutoff frequency of the filter, Hz
nyq = 0.5 * fs # Nyquist Frequency
order = 2 # sin wave can be approx represented as quadratic
n = int(T * fs) # total number of samples
# sin wave
t = np.linspace(0, T, n, endpoint=False)
sig = np.sin(1.2 * 2 * np.pi * t)
# Add some noise
noise = 1.5 * np.cos(9 * 2 * np.pi * t) + 0.5 * np.sin(12.0 * 2 * np.pi * t)
data = sig + noise
def butter_lowpass_filter(data, cutoff, fs, order):
normal_cutoff = cutoff / nyq
# Get the filter coefficients
b, a = butter(order, normal_cutoff, btype='low', analog=False)
y = filtfilt(b, a, data)
return y
# Filter the data, and plot both the original and filtered signals
y = butter_lowpass_filter(data, cutoff, fs, order)
fig = go.Figure()
fig.update_layout(width=800)
fig.add_trace(go.Scatter(
x=t,
y=data,
mode='lines',
name='signal with noise'
))
fig.add_trace(go.Scatter(
x=t,
y=y,
mode='lines',
name='filtered signal'
))
fig.show()
Actual output will be interractive if plotted in Jupyter Notebook. A snapshot of the output is given here.