Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

python - how do i store pcolormesh plot as a numpy array - while preserving data shape?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...