Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place xaxis grid over spectrogram in Python?

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.

enter image description here

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.

like image 659
Tom Kurushingal Avatar asked Jul 24 '15 14:07

Tom Kurushingal


People also ask

How do you set an axis plot in Python?

To set axes of the scatter plot, use xlim() and ylim() functions. To plot the scatter graph, use scatter() function. To set label at the x-axis, use xlabel() function. To set label at y-axis, use ylabel() function.

What is the use of Xticks () and Yticks () in plotting?

You can use the xticks() and yticks() functions and pass in an array denoting the actual ticks. On the X-axis, this array starts on 0 and ends at the length of the x array. On the Y-axis, it starts at 0 and ends at the max value of y . You can hard code the variables in as well.


1 Answers

As others have noted - it is very difficult to replicate your issue with the code you have provided.

In particular - I have tried on Windows 8.1, Ubuntu 14.04 (on Virtualbox VM), matplotlib versions 1.3.1 and 1.4.3, with and without text.usetex set and with Python 2.7.6 and Python 3. None of them reproduce your problem with the code you provide.

However

I can reproduce what you see if I replace the line

spectorgram_pressure_vs_frequency.xaxis.grid(True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)

with

pressure_vs_time.xaxis.grid(True, which='major', color='#FFFFFF', linestyle=':', linewidth = 0.3)

i.e. I try to set the xaxis.grid on the twinned axis rather than the original axis. I have to conclude that somehow in your real code you are setting the xaxis.grid on your twinned axis rather than your main axis.


This means I can answer your direct questions as follows:

How to place x-axis grids on top of the spectrogram like the y-axis grids in Python?

Your code does this - you call xaxis.grid() on the original (not twinned) axis.

I am using matplotlib version 1.3.1. Is this a version-specific problem?

No, the behaviour is the same in 1.3.1 and 1.4.3

like image 195
J Richard Snape Avatar answered Oct 12 '22 01:10

J Richard Snape