Edit this code to make the plots share the same x-axis (to preserver space):
python
# Generate a 1D signal with Gaussian noise
signal_length = 1000
noise = np.random.randn(signal_length) * 0.1
signal = np.sin(2*np.linspace(0,3,signal_length)) + noise
# Create a blurring kernel (e.g., a Gaussian kernel)
kernel_size = 5
kernel = np.exp(-np.linspace(-kernel_size, kernel_size, 2*kernel_size)**2 / (2*kernel_size**2)) / np.sqrt(2*np.pi*kernel_size**2)
# Convolve the signal with the kernel to simulate blurring
blurred_signal = convolve(signal, kernel, mode='same')
# Deconvolve the blurred signal using deconvvolve
deconvolved_signal, remainder = deconvolve(blurred_signal, kernel)
deconvolved_signal = np.hstack((deconvolved_signal, blurred_signal[len(deconvolved_signal):])) ## Or you can padd with zeros
signals = {"origina":signal, "blurred_signal":blurred_signal, "deconvolved_signal":deconvolved_signal}
fig, axs = plt.subplots(3, 1, figsize=(12, 8))
for ax, sig in zip(axs.flatten(), signals):
ax.plot(signals[sig], label=sig)
ax.legend()
Assistant
Sure! To have all three plots share the same x-axis and preserve space, you can use the sharex=True parameter in plt.subplots(). This ensures that all subplots share the same x-axis, which not only aligns them but also reduces the amount of space used by hiding redundant x-axis labels. Additionally, it's a good practice to add a common x-axis label for clarity.
Here's the edited code:
python
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import convolve, deconvolve
# Generate a 1D signal with Gaussian noise
signal_length = 1000
noise = np.random.randn(signal_length) * 0.1
signal = np.sin(2 * np.linspace(0, 3, signal_length)) + noise
# Create a blurring kernel (e.g., a Gaussian kernel)
kernel_size = 5
kernel = np.exp(-np.linspace(-kernel_size, kernel_size, 2 * kernel_size)**2 / (2 * kernel_size**2))
kernel /= np.sqrt(2 * np.pi * kernel_size**2) # Normalize the kernel
# Convolve the signal with the kernel to simulate blurring
blurred_signal = convolve(signal, kernel, mode='same')
# Deconvolve the blurred signal using deconvolve
deconvolved_signal, remainder = deconvolve(blurred_signal, kernel)
# Pad the deconvolved signal to match the original signal length
deconvolved_signal = np.hstack((deconvolved_signal, blurred_signal[len(deconvolved_signal):])) # Or you can pad with zeros
# Prepare the signals for plotting
signals = {
"Original": signal,
"Blurred Signal": blurred_signal,
"Deconvolved Signal": deconvolved_signal
}
# Create subplots with shared x-axis
fig, axs = plt.subplots(3, 1, figsize=(12, 8), sharex=True)
# Plot each signal in its respective subplot
for ax, (label, sig) in zip(axs, signals.items()):
ax.plot(sig, label=label)
ax.legend()
ax.set_ylabel('Amplitude') # Optional: Add y-axis label for clarity
# Add a common x-axis label
axs[-1].set_xlabel('Sample Index')
# Adjust layout to prevent overlap
plt.tight_layout()
plt.show()
Changes Made:
Shared X-axis:
Added sharex=True in plt.subplots() to ensure all subplots share the same x-axis.
Fixed Label Typo:
Changed the key "origina" to "Original" for clarity.
Common X-axis Label:
Added axs[-1].set_xlabel('Sample Index') to label the shared x-axis on the bottom subplot.
Y-axis Labels (Optional):
Added ax.set_ylabel('Amplitude') to each subplot for clarity.
Layout Adjustment:
Used plt.tight_layout() to automatically adjust subplot parameters for a neat layout.
Imports and Kernel Normalization:
Ensured all necessary imports are included.
Normalized the Gaussian kernel for proper convolution.
Visualization:
This setup will stack the three plots vertically, aligning them by their x-axis. The x-axis labels will appear only at the bottom subplot, conserving space and providing a cleaner look.
[image: Shared X-Axis Plot] open(Replace with actual plot image if needed)
Feel free to adjust the figure size or other styling parameters to better suit your specific needs!