TypeError: Incompatible X, Y inputs to pcolormesh - Solved: Time-Frequency Domain Plot using Wavelet Transform
TypeError: Incompatible X, Y inputs to pcolormesh - Solved: Time-Frequency Domain Plot using Wavelet Transform
This article provides a corrected Python code snippet to resolve the 'TypeError: Incompatible X, Y inputs to pcolormesh' error encountered while plotting a time-frequency domain plot using the pcolormesh function in matplotlib. The code utilizes the pywt library for wavelet transform, including noise addition, thresholding, and signal extraction. This solution ensures correct dimensionality of the input data for accurate visualization of the time-frequency spectrum.
The original code contained an issue with the dimensionality of the X and Y inputs to the pcolormesh function. The updated code addresses this issue by ensuring proper alignment of the time and frequency dimensions.
Here's the corrected code:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import pywt
# Read Excel file and get data
data = pd.read_excel('your_file_path.xlsx')
time = data.iloc[:, 0].values
voltage = data.iloc[:, 1:5].values
# Plot the time domain signal
plt.figure(figsize=(10, 6))
plt.plot(time, voltage)
plt.xlabel('Time')
plt.ylabel('Voltage')
plt.title('Time Domain Plot')
plt.legend(['Voltage 1', 'Voltage 2', 'Voltage 3', 'Voltage 4'])
plt.grid(True)
plt.show()
# Haar wavelet transform
wavelet = pywt.Wavelet('haar')
coeffs = pywt.wavedec(voltage, wavelet)
# Add white noise signal
noise = np.random.normal(0, 0.1, voltage.shape)
coeffs_noise = [c + n for c, n in zip(coeffs, noise)]
# Thresholding to remove noise
threshold = 0.5
coeffs_filtered = [np.where(np.abs(c) < threshold, 0, c) for c in coeffs_noise]
# Inverse transform to extract the feature signal
voltage_filtered = pywt.waverec(coeffs_filtered, wavelet)
# Plot the time-frequency domain
plt.figure(figsize=(10, 6))
time_grid, freq_grid = np.meshgrid(time, np.arange(len(coeffs_filtered)+1))
plt.pcolormesh(time_grid, freq_grid[:-1], np.abs(coeffs_filtered), shading='auto')
plt.colorbar(label='Amplitude')
plt.xlabel('Time')
plt.ylabel('Scale')
plt.title('Time-Frequency Domain Plot')
plt.grid(True)
plt.show()
# Plot the extracted time domain signal
plt.figure(figsize=(10, 6))
plt.plot(time, voltage_filtered[0])
plt.xlabel('Time')
plt.ylabel('Voltage')
plt.title('Extracted Time Domain Signal')
plt.grid(True)
plt.show()
Please replace 'your_file_path.xlsx' with the actual path to your Excel file. The code will read the first column as sampling time and columns 2 to 5 as output voltage.
The code has been corrected to ensure the dimensions of the X and Y coordinates match for accurate plotting of the time-frequency domain. This modification addresses the 'Incompatible X, Y inputs' error.
We apologize for any inconvenience caused by the previous code. We hope this revised code runs smoothly. Feel free to ask if you have further questions.
原文地址: https://www.cveoy.top/t/topic/bOHg 著作权归作者所有。请勿转载和采集!