I need to prepare a plot similar to figure below using coefficients of details of a wavelet transform.I am trying for more than 2 weeks to find how I can do it.
This plot represents coefficients of details of a wavelet transformation at different levels (1, 2, 3, and 4).The coefficients of details (cA4,cD4,cD3,cD2,cD1=coeffs) are a 1D array and each has different size.
<
def wavelet(data):
waveletname = 'sym5'
coeffs = wavedec(data, 'sym5', level=5)
cA5,cD5,cD4,cD3,cD2,cD1=coeffs
>
A possible approach is to draw each array as a 1d image, each at a different y position.
plt.imshow
needs a 2D array, so reshaping the array to have 1 as first dimension and the original size as second dimension gives a horizontal image. (If it isn't a numpy array yet, it needs to be converted via np.array(ci).reshape(1, -1)
). Via the extent
parameter, bounding x and y values can be set. interpolation='nearest'
shows hard boundaries between each of the pixels. aspect='auto'
is needed to prevent imshow to set a fixed aspect ratio.
from matplotlib import pyplot as plt
import numpy as np
# generate six random 1d arrays of different sizes
coeffs = [np.random.uniform(0, 1, np.random.randint(30, 100)) for i in range(6)]
# cA5, cD5, cD4, cD3, cD2, cD1 = coeffs
for i, ci in enumerate(coeffs):
plt.imshow(ci.reshape(1, -1), extent=[0, 1000, i + 0.5, i + 1.5], cmap='inferno', aspect='auto', interpolation='nearest')
plt.ylim(0.5, len(coeffs) + 0.5) # set the y-lim to include the six horizontal images
# optionally relabel the y-axis (the given labeling is 1,2,3,...)
plt.yticks(range(1, len(coeffs) + 1), ['cA5', 'cD5', 'cD4', 'cD3', 'cD2', 'cD1'])
plt.show()