为什么图像存储的不同于所显示的图像?

时间:2017-04-27 00:50:04

标签: python numpy matplotlib librosa

我目前无法理解为什么在存储数据后无法重新创建图表。

import os
import sys
from os import listdir
from os.path import isfile, join
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import seaborn as sb
from matplotlib.colors import Normalize
import matplotlib
from matplotlib import cm
from PIL import Image
import librosa
import librosa.display
import ast

def make_plot_store_data(name,interweaved):
    librosa.display.specshow(interweaved.T,sr=16000,x_axis='frames',y_axis='mel',hop_length=160,cmap=cm.jet)
    plt.title("log mel power spectrum of interweaved " + name)
    plt.colorbar(format='%+02.0f dB')
    plt.savefig(plot+"/"+name+"_plot_interweaved_conv.png")
    plt.show()
    plt.close()


    convert = plt.get_cmap(cm.jet)
    numpy_output_interweawed = convert(interweaved.T)
    print interweaved.shape
    print numpy_output_interweawed.shape
    plt.imshow(numpy_output_interweawed, interpolation='nearest')
    plt.show()
    raw_input("Somethign")
    numpy_output_interweawed.dump(numpy_train+name+"_normalized_interweaved"+".dat")
    numpy_output_interweawed_or = convert(interweaved.T)*255
    numpy_output_interweawed_or.dump(numpy_train+name+"_interweaved"+".dat")

librosa显示的图表将数据视为

enter image description here

但是当我使用plt.cmap(cm.jet)转换原始数据时 并且它使用matplotlib.pyplot的情节与数据混淆,并且看起来不像原始的......

enter image description here

修改

这是

plt.imshow(interweaved.T,aspect = 'auto')
plt.show()
raw_input("Somethign")

enter image description here

看起来像图像的纵横比 - 它开始类似于原始图,当我使图更大 - 但为什么颜色太暗了......

enter image description here

1 个答案:

答案 0 :(得分:0)

没有什么是错的,就我所知,在没有数据或知道数组形状的情况下,一切似乎都按预期工作。

但是,如果你想让你的imshow情节看起来与librosa产生的情节类似,你可能会首先考虑使用不相等的方面

plt.imshow(...,  aspect="auto")

并可能使用imshow的extent参数正确设置绘图的数据范围。

另请注意,librosa生成的图是pcolormesh,因此要真正复制它,您可能希望保持数据的格式(不通过色图)并使用{{ 1}}绘制它。