0

I seem to have some problems storing a plot created using matplotlib.pcolormesh(). As far i know is pcolormesh convert an input data matrix using a colormap. The colormap outputs a RGB value for each entry in the matrix and plots it.

Which in my head would be similar to

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm


fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data =  np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.axis('off')
plt.show()
raw_input("sadas")

convert = plt.get_cmap(cm.jet)
norm = matplotlib.colors.Normalize(vmin=0,vmax=1)
numpy_output_static = convert(norm(data.T))
plt.imshow(numpy_output_static,cmap = cm.jet, aspect = 'auto')
plt.show()
raw_input("asds")

enter image description here

enter image description here

Problem here is that the numpy array of the data being represented as a plot, is not similar to what the first plot shows. I need the numpy to have data that represents the plot, such that if I wanted to plot it, I would get an identical image as the first one, and the shape of the numpy array should be similar to the input data which was used in plot 1.

The numpy is being fed to a neural network, for detecting patterns which means that representation is important here.

So how do I make it store the actual plot, without all the red things..

And if this is not possible in matplotlib what other library would it be possible to do it in.

J.Down
  • 700
  • 1
  • 9
  • 32

2 Answers2

1

The data ranges from -1.828067 to 22.70058. However in the second plot, you cut it to the range between vmin=0 and vmax=1. Therefore all data that is larger than 1 will be red in the imshow plot.

If you use

norm = matplotlib.colors.Normalize(vmin=-1.828067,vmax=22.70058)

you should get the original array.

Mind that if you do not convert the data to a color array, the result should be the same, so that whole conversion might be unnecessary and you can simply do

plt.imshow(data.T,cmap = cm.jet, aspect = 'auto')
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
0

What I ended up doing is this:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from PIL import Image
import librosa
import librosa.display
from matplotlib import cm
from sklearn import preprocessing

min_max_scaler = preprocessing.MinMaxScaler(feature_range=(0, 1))

fig = plt.figure(figsize=(12,4))
min = -1.828067
max = 22.70058
data =  np.random.uniform(low=min, high=max, size=(474,40))
librosa.display.specshow(data.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
plt.axis('off')
plt.show()
#raw_input("sadas")

convert = plt.get_cmap(cm.jet)
data = min_max_scaler.fit_transform(data)
print data.min()
print data.max()
numpy_output_static = convert(data.T)
plt.imshow(numpy_output_static, aspect = 'auto')
plt.show()
#raw_input("asds")

I might have used to normalise function a bit incorrectly and thought that normalize would handle the input data, which didn't seem to be the case. So i used sklearnto normalise it before i made the plot.

And then make make use of the cmap.

And the result being:

enter image description here

J.Down
  • 700
  • 1
  • 9
  • 32