Python Forum
Defining x and y axes in Spectrogram Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Defining x and y axes in Spectrogram Function
#1
As part of my Astronomy Homework, I've to produce a Time vs Frequency Spectrogram of some data. The data I have is 2 columns - the first of which is the time (seconds) and the second column is the gravitational wave strain. I also have a single value for the Frequency. This is the example code I have been provided with:


from scipy.signal import spectrogram  
spec_f, spec_t, spec = spectrogram(ht, fs)  
plt.pcolormesh(spec_t, spec_f, spec, shading='gouraud')  
plt.ylabel('Frequency [Hz]')  
plt.xlabel('Time [sec]')  
plt.show()
No matter what I have done, I can't seem to get it to work - I mainly get that 'ht' is not defined. I have tried to define it but I'm not sure what the proper syntax would be for it. (ht is the timeseries and fs is the sampling Frequency).

If someone could guide me in the right direction I'd greatly appreciate it.
Reply
#2
You say you have the data in 2 columns. Would need to see more of your code (please post using Python tags to preserve formatting) to see what that structure is (?numpy array ?pandas dataframe ?list). Basically assign the columns to variables and call your function on those, or call directly on the columns.
Reply
#3
(Nov-28-2020, 01:51 PM)jefsummers Wrote: You say you have the data in 2 columns. Would need to see more of your code (please post using Python tags to preserve formatting) to see what that structure is (?numpy array ?pandas dataframe ?list). Basically assign the columns to variables and call your function on those, or call directly on the columns.

I've updated the post so the code is now in the correct form. This is the whole code I have been given, I've not added taken away anything. would I be able to see an example of assigning the variables.

Many Thanks.
Reply
#4
I'm not familiar with spectrogram so the below DOES NOT WORK, but it shows how you may assign. The problem I appear to have here is not having the data.

import numpy as np
ht = np.array([1,2,3,4,5,6,7,8,9])
fs = np.array([3,1,5,2,6])
from scipy.signal import spectrogram  
spec_f, spec_t, spec = spectrogram(ht,fs)  
plt.pcolormesh(spec_t, spec_f, spec, shading='gouraud')  
plt.ylabel('Frequency [Hz]')  
plt.xlabel('Time [sec]')  
plt.show()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I set the figure title and axes labels font size in Matplotlib? anouar2002 1 1,649 Nov-03-2020, 05:31 PM
Last Post: ndc85430
  Plotting axes help StillAnotherDave 11 3,623 Jan-24-2020, 04:29 PM
Last Post: StillAnotherDave
  defining a function to see if a list is sorted Siylo 14 6,984 Nov-29-2018, 05:25 PM
Last Post: wavic
  python 3.4.3 Question a defining function student8 1 4,328 Oct-02-2017, 07:59 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020