I have the following plot, which provides the spectrogram of a pressure signal along with the signal placed on it for comparison. I was able to draw the y-axis grids on the spectrogram, but could not place the x-axis grid on it.
The data used to generate the spectrogram is available here.
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('pressure.dat', skiprows = 1, delimiter = '\t')
pressure = data[:, 1]
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 = 90000, 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()
# plt.gca
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.plot(((theta + 360) / (6 * 6600)), pressure, linewidth = 0.75, linestyle = '-', color = '#FFFFFF', zorder = 2)
pressure_vs_time.grid(b = True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)
spectorgram_pressure_vs_frequency.xaxis.grid(True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.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')
plt.close()
How to place x-axis grids on top of the spectrogram like the y-axis grids in Python? I am using matplotlib version 1.3.1. Is this a version-specific problem?
Update
I updated matplotlib from version 1.3.1 to 1.4.3, even then I cannot set the x-axis grids on.