我正在尝试绘制频谱图。我使用matplotlib中的imshow。
问题是,我从C++生成的文本文件中读取数据(它实际上创建了一个二维向量并将其存储到文本文件中),然后读入文本文件并进行绘图。
下面是我生成的绘图输出(来自C++并导入到python):
这是一张Python中的频谱图(使用matplotlib):
如您所见,x和y轴被交换了,这给了我实际频谱图的错误形状。
这可能是为什么呢?例如,我可能在第一次读取数据时读取错误,导致出现了这种情况吗?还是说我没有设置x、y轴本身?
非常感谢您提供任何帮助
编辑:
我使用以下函数读取数组:
这里是结果:
我不知道为什么会得到如此不同的结果。
问题是,我从C++生成的文本文件中读取数据(它实际上创建了一个二维向量并将其存储到文本文件中),然后读入文本文件并进行绘图。
下面是我生成的绘图输出(来自C++并导入到python):
这是一张Python中的频谱图(使用matplotlib):
如您所见,x和y轴被交换了,这给了我实际频谱图的错误形状。
这可能是为什么呢?例如,我可能在第一次读取数据时读取错误,导致出现了这种情况吗?还是说我没有设置x、y轴本身?
非常感谢您提供任何帮助
编辑:
我使用以下函数读取数组:
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;
制作频谱图的步骤如下:
a = list(split_at_empty_lines("file.txt");
hm = ax.imshow(a, interpolation='nearest',origin='lower',aspect='auto')
这里是结果:
[[ 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]]
这里是结果:
![enter image description here](https://istack.dev59.com/bw99o.webp)
a
的第一行相同?而且你确定你的 C++ 文件是按行主序(row-major)写入数据,而不是列主序(column-major)吗? - bogatron