I am trying to plot the spectrogram of a signal along with the signal on the same plot as shown in figure:
The raw data is available here. But the signal and its spectrogram is not aligned. Why does this happen and how to align both of them in matplotlib?
Reproducible code
from __future__ import division
from matplotlib import ticker as mtick
from matplotlib.backends.backend_pdf import PdfPages
import matplotlib.pyplot as plt
import numpy as np
data = np.genfromtxt('spectrogram.dat', skiprows = 2, delimiter = ',')
pressure = data[:, 1] * 0.065
theta = data[:, 0]
with PdfPages('Spectorgram of cylinder pressure.pdf') as spectorgram_pressure:
_spectorgram_pressure_vs_frequency_ = plt.figure(figsize=(5.15, 5.15))
_spectorgram_pressure_vs_frequency_.clf()
spectorgram_pressure_vs_frequency = plt.subplot(111)
cax = plt.specgram(pressure * 100000, NFFT = 256, Fs = 50000, cmap=plt.cm.gist_heat, zorder = 1)
spectorgram_pressure_vs_frequency.grid(False, which="major")
spectorgram_pressure_vs_frequency.set_xlabel('Time (s)', labelpad=6)
spectorgram_pressure_vs_frequency.set_ylabel('Frequency (Hz)', labelpad=6)
y_min, y_max = spectorgram_pressure_vs_frequency.get_ylim()
cbar = plt.colorbar(orientation='vertical', ax = spectorgram_pressure_vs_frequency, fraction = 0.046, pad = 0.2)
cbar.set_label('Power spectral density (dB)', rotation=90)
primary_ticks = len(spectorgram_pressure_vs_frequency.yaxis.get_major_ticks())
pressure_vs_time = spectorgram_pressure_vs_frequency.twinx()
pressure_vs_time.grid(False)
pressure_vs_time.plot(theta / 1000, pressure, linewidth = 0.75, linestyle = '-', color = 'k', alpha = 0.7, zorder = 3)
pressure_vs_time.set_ylabel('Cylinder pressure (bar)', labelpad=6)
pressure_vs_time.yaxis.set_major_locator(mtick.LinearLocator(primary_ticks))
# spectorgram_pressure_vs_frequency.set_xlim([0, max(cax[2])])
spectorgram_pressure.savefig(bbox_inches='tight')