I'm trying to plot a spectrogram. I am using the imshow in matplotlib.
The problem is this, I am reading the data from a text file generated in C++ (it essentially creates a 2D vector and stores this into a text file) I then read in the text file and plot.
Below is the output of the plot I've generated (from C++ and imported into python):

Here is a plot of a spectrogram, actually in Python (using matplotlib):

As you can see, the x and y axis are swapped, which is giving me the wrong shape for the actual spectrogram.
Why could this be? For example, could I be reading in the data wrong in the first place and this has happened? Or, could it be that I'm not setting the x, y axis itself?
Any help would be greatly appreciated
EDIT:
I read the array using this function:
def split_at_empty_lines(filename):
with open(filename) as f:
arr = []
for line in f:
if not line.strip() and arr:
yield arr
arr = []
elif line.strip(): arr.extend(float(x) for x in line.split())
if arr: yield arr;
The spectrogram is produced by doing the following:
a = list(split_at_empty_lines("file.txt");
hm = ax.imshow(a, interpolation='nearest',origin='lower',aspect='auto')
Here is the result:
[[ 26.9287 30.9089 34.9285 ..., 23.016 28.9027 36.4073]
[ 26.7964 26.8524 32.7296 ..., 22.9524 28.6145 33.7204]
[ 26.4222 27.0941 29.094 ..., 22.5309 27.6803 26.7073]
...,
[ 25.9362 25.8307 29.7039 ..., 22.0084 25.9855 28.9602]
[ 26.4222 27.0941 29.094 ..., 22.5309 27.6803 26.7073]
[ 26.7964 26.8524 32.7296 ..., 22.9524 28.6145 33.7204]]
Here is the results:

I don't know why I'm getting such different results.
If you are reading the data with numpy into an ndarray you can transpose the array before plotting.
import numpy as np
a = np.array(split_at_empty_lines("file.txt"))
print(a.T)
should produce
array([[1, 4],
[2, 5],
[3, 6]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With