本文介绍了如何在保存数据形状的同时将pcolormesh图存储为numpy数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在存储使用matplotlib.pcolormesh()创建的绘图时遇到一些问题.据我所知,pcolormesh使用色图转换输入数据矩阵.色彩图为矩阵中的每个条目输出一个RGB值并将其绘制.

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.

在我脑海中与

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

这里的问题是被表示为绘图的数据的numpy数组与第一幅绘图所显示的不相似.我需要numpy具有表示该图的数据,以便如果要对其进行绘制,则将获得与第一个图相同的图像,并且numpy数组的形状应类似于在其中使用的输入数据.情节1.

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.

numpy被馈送到神经网络,用于检测模式,这意味着表示在这里很重要.

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..

如果在matplotlib中这是不可能的,那么可以在其他哪个库中进行.

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

推荐答案

数据范围从-1.82806722.70058.但是,在第二个图中,将其切为vmin=0vmax=1之间的范围.因此,所有大于1的数据在imshow图中将显示为红色.

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.

如果您使用

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

您应该获得原始数组.

请注意,如果不将数据转换为颜色数组,结果应该是相同的,因此可能不需要整个转换,而您只需执行

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

这篇关于如何在保存数据形状的同时将pcolormesh图存储为numpy数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:54